forked from sarves/thamizhilip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtamil.py
302 lines (270 loc) · 12.8 KB
/
tamil.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
##Copyright [2021] K.Sarveswaran ([email protected])
##
##Licensed under the Apache License, Version 2.0 (the "License");
##you may not use this file except in compliance with the License.
##You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
##Unless required by applicable law or agreed to in writing, software
##distributed under the License is distributed on an "AS IS" BASIS,
##WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
##See the License for the specific language governing permissions and
##limitations under the License.
##
##
import stanza
import sys
import subprocess
import os
import json
import datetime
from urllib.request import urlopen
from zipfile import ZipFile
from pathlib import Path
import tqdm
#dir_path = os.path.dirname(os.path.realpath(__file__))
dir_path = str(Path.home())+"/thamizhi-models"
def downloadModels():
path = str(Path.home())+"/thamizhi-models"
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed, may be you have already downloaded Thamizhi Models" % path)
my_file = Path(path+"/tempfile.zip")
if my_file.is_file():
print("Models exist")
else:
zipurl = 'http://nlp-tools.uom.lk/thamizhi-models/thamizhi_models.zip'
zipresp = urlopen(zipurl)
tempzip = open(path+"/tempfile.zip", "wb")
tempzip.write(zipresp.read())
tempzip.close()
zf = ZipFile(path+"/tempfile.zip")
zf.extractall(path)
zf.close()
def printUDLabel(find_ud_labels):
label_list=[]
lfile=open(dir_path+"/fsts/label-list","r")
for line in lfile:
label_list.append(line.strip())
lfile.close()
ud_labels=[]
counter=0
for x in range(len(find_ud_labels)):
for y in range(len(label_list)):
if label_list[y].split(":")[0][1:]== find_ud_labels[x]:
if label_list[y].split(":")[1] != "_" :
for z in label_list[y].split(":")[1].split("|"):
ud_labels.append(z)
ud_labels.sort()
return "|".join(ud_labels)
def isEnglish(text):
try:
text.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return 0
else:
return 1
def loadModels(*arg):
if len(arg)==0 or (len(arg)==1 and (arg[0]=="parsing" or arg[0]=="dependency")):
config = {
'processors': 'tokenize,pos,mwt,lemma,depparse',
'lang': 'ta',
'tokenize_model_path': dir_path+'/thamizhi_tokenizer.pt',
'pos_model_path': dir_path+'/thamizhi_pos.pt',
'mwt_model_path': dir_path+'/thamizhi_mwt.pt',
'pos_pretrain_path': dir_path+'/ta_pretrain.pt',
'lemma_model_path': dir_path+'/thamizhi_lemma.pt',
'depparse_model_path': dir_path+'/thamizhi_depparse.pt'
}
elif len(arg)==1:
if arg[0]=="pos" or arg[0]=="POS":
config = {
'processors': 'tokenize,pos',
'lang': 'ta',
'tokenize_model_path': dir_path+'/thamizhi_tokenizer.pt',
'pos_pretrain_path': dir_path+'/ta_pretrain.pt',
'pos_model_path': dir_path+'/thamizhi_pos.pt'
}
elif len(arg)==2:
if (arg[0]=="pos" or arg[0]=="POS") and (arg[1]=="amrita" or arg[1]=="AMRITA"):
config = {
'processors': 'tokenize,pos',
'lang': 'ta',
'tokenize_model_path': dir_path+'/thamizhi_tokenizer.pt',
'pos_pretrain_path': dir_path+'/ta_pretrain.pt',
'pos_model_path': dir_path+'/thamizhi_pos_amrita.pt'
}
return stanza.Pipeline(**config)
def posTag(text,model):
doc = model(text+ " .")
postagged=""
for sent in doc.sentences :
for word in sent.words :
postagged=postagged+word.text+"|"+word.upos+"\n"
return postagged
def find_morphemes(word):
#reading fsts, fsts in fst_list has to be placed in a priority order in which look up should happen
#this needs to be passed to the function using which morphemes are extracted
fsts=[]
f1=open(dir_path+"/fsts/fst-list","r")
for line in f1:
fsts.append(line.strip())
f1.close()
analyses=[]
for fst in fsts:
p1 = subprocess.Popen(["echo", word], stdout=subprocess.PIPE)
file_name=dir_path+"/fsts/"+fst
#print(file_name)
p2 = subprocess.Popen(['flookup',file_name], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output,err = p2.communicate()
#print(output.decode("utf-8"))
#1st analysis is broken by new line to tackle cases with multiple analysis
#then analysis with one output is handled
#1st each line is broken by tab to find lemma and analysis
#then those are store in a list and returned back to main
lines=output.decode("utf-8").strip().split("\n")
if len(lines) > 1:
#print(line)
for line in lines:
analysis=line.split()
if len(analysis) > 1:
if "?" in output.decode("utf-8"):
results=0
else:
#print(analysis[1].strip().split("+"))
analyses.append(analysis[1].strip().split("+"))
else:
return 0
#this is to handle cases with one output, 1st each line is broken by tab to
#find lemma and analysis
#then those are store in a list and returned back to main
else:
analysis=output.decode("utf-8").split()
if len(analysis) > 1:
if "?" in output.decode("utf-8"):
results=0
else:
#print(analysis[1].strip().split("+"))
analyses.append(analysis[1].strip().split("+"))
#print(analyses)
else:
return 0
#print(analyses)
if analyses :
return analyses
else:
return 0
def guess_morphemes(word):
gussers=[]
f1=open(dir_path+"/fsts/guesser-list","r")
for line in f1:
gussers.append(line.strip())
f1.close()
analyses=[]
for fst in gussers:
p1 = subprocess.Popen(["echo", word], stdout=subprocess.PIPE)
file_name=dir_path+"/fsts/"+fst
p2 = subprocess.Popen(["flookup", file_name], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
output,err = p2.communicate()
#1st analysis is broken by new line to tackle cases with multiple analysis
#then analysis with one output is handled
#1st each line is broken by tab to find lemma and analysis
#then those are store in a list and returned back to main
lines=output.decode("utf-8").strip().split("\n")
if len(lines) > 1:
for line in lines:
analysis=line.split(" ")
if len(analysis) > 1:
if "?" in output.decode("utf-8"):
results=0
else:
#print(analysis[1].strip().split("+"))
analyses.append(analysis[1].strip().split("+"))
else:
return 0
#this is to handle cases with one output, 1st each line is broken by tab to
#find lemma and analysis
#then those are store in a list and returned back to main
analysis=output.decode("utf-8").split(" ")
if len(analysis) > 1:
if "?" in output.decode("utf-8"):
results=0
else:
#print(analysis[1].strip().split("+"))
analyses.append(analysis[1].strip().split("+"))
else:
return 0
if analyses :
return analyses
else:
return 0
def morphTag(*arg):
punct_dict = """{".":"period",",":"comma",";":"semi-colon",":":"colon","-":"hyphen","(":"open-bracket",")":"close-bracket"}"""
punct_json = json.loads(punct_dict)
annotype=""
word=arg[0].strip()
if len(arg)==2:
annotype=arg[1]
analysis=[]
if word in punct_json:
analysis.append(word+":"+punct_json[word])
elif word.isnumeric() :
analysis.append(word+":NUM")
elif isEnglish(word) == 1 :
analysis.append(word+":English Text")
else:
interim_analyses = find_morphemes(word)
#print(interim_analyses)
if interim_analyses != 0 :
for each_analysis in interim_analyses:
lemma=""
lables=""
pos=""
counter=0
for analysis_output in each_analysis:
if counter==0:
lemma=analysis_output
elif counter==1:
pos=analysis_output
else:
lables=lables+","+analysis_output
counter=counter+1
if annotype=="ud":
analysis.append(word+"|"+printUDLabel(list(set(lables.split(",")))))
else:
analysis.append(word+'|'.join(list(set(lables.split(",")))))
else:
gusses = guess_morphemes(word)
if gusses != 0 :
for each_guess in gusses:
counter=0
lemma=""
lables=""
pos=""
for analysis_output in each_guess:
if counter==0:
lemma=analysis_output
elif counter==1:
pos=analysis_output
else:
lables=lables+","+analysis_output
counter=counter+1
analysis.append(word+"/"+printUDLabel(list(set(lables.split(",")))))
else:
analysis.append(word+":Unknown")
return "\n".join(analysis)
def depTag(data_input,nlp):
doc = nlp(data_input+ " .")
taggeddata=data_input+"\n"
for sent in doc.sentences :
for word in sent.words :
taggeddata=taggeddata+str(word.id)+ "|" + word.upos + "|" + str(word.deprel)+ "|" + str(word.head) + "\n"
return taggeddata
#downloadModels()
#print(posTag("தமிழ்",loadModels("pos","amrita")))
#print(depTag("அவன் புத்தகத்தை வாங்கினான்",loadModels()))
#print(morphTag("வருகிறான்","ud"))