-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwmorph.py
executable file
·173 lines (155 loc) · 5.71 KB
/
wmorph.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
#!/usr/bin/env python3
# Copyright (C) 2016.
# Author: Jesús Manuel Mager Hois
# e-mail: <[email protected]>
# Project website: http://turing.iimas.unam.mx/wix/
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import re
import codecs
#prefixes and affixes of wixarika verbs
from .wix.wixaffixes import pre, post
class Verb:
def __init__(self, verb, debug=0):
self.verb = verb.lower()
#print(self.verb)
self.paths = []
self.roots = []
self.rootslarge = []
self.debug=debug
#Fl = codecs.open("data/steam.large", mode="r", encoding="utf-8")
F = codecs.open("data/steam", mode="r", encoding="utf-8")
line = F.readline()
while 1:
line=line.replace("\n", "")
self.roots.append(line)
line=F.readline()
if not line:
break
if self.debug:
print("**************************")
print(self.roots)
self.start()
def start(self, prev="", pos=0, path=[]):
if pos > len(pre)-1:
return
if self.debug:
print("New branch: ", str(pos), str(prev), str(path))
gotone = False
for s in pre[pos]:
s_reg = s.replace("+", "\+")
prev_reg=prev.replace("+", "\+")
if self.debug:
print("Searching ^"+prev_reg+s_reg+"+")
reg = re.compile("^"+prev_reg+s_reg+"+")
m = reg.match(self.verb)
if m:
gotone= True
if self.debug:
print("Found:" + str(pos) + m.group())
nprev = m.group()
npath = list(path)
npath.append((""+str(pos)+"", s))
self.start(nprev, pos+1, npath)
nprev = nprev.replace("+","\+")
for root in self.roots:
root2 = root.replace("+","\+")
rootmatch=re.compile("^"+nprev+root2+"+")
rm = rootmatch.match(self.verb)
if rm:
if self.debug:
print("Found:" + "[root]" + rm.group())
print("Found:" + "[root]" + root)
nrprev = rm.group()
nrpath = list(npath)
nrpath.append(("", root))#id of steam TODO
if self.debug:
print(nrprev)
if len(self.verb) == len(nrprev):
self.paths.append(nrpath)
self.end(prev=nrprev,path=nrpath)
continue
if not gotone:
if pos > 17:
return
self.start(prev, pos+1, path)
return
def end(self, prev="", pos=1, path=[]):
if pos <= 0 or pos >= len(post):
return
if self.debug:
print("Actual path", prev)
if len(prev) == len(self.verb):
if self.debug:
print(path)
return
gotone=False
if self.debug:
print(str(-pos), str(post[-pos]))
for s in post[-pos]:
if self.debug:
print("Actual suffix:", s, "at pos", str(pos))
s_reg = s.replace("+", "\+")
prev_reg= prev.replace("+", "\+")
if self.debug:
print("Searching ^"+prev_reg+s_reg+"+")
reg = re.compile("^"+prev_reg+s_reg+"+")
m = reg.match(self.verb)
if m:
gotone= True
if self.debug:
print("Found:" + str(pos) + m.group())
nprev = m.group()
if self.debug:
print("Next search:", nprev)
npath = list(path)
npath.append(("-"+str(pos)+"", s))
if len(self.verb) == len(nprev):
self.paths.append(npath)
self.end(nprev, pos+1, npath)
#if not gotone:
self.end(prev, pos+1, path)
class Word:
def __init__(self, model_file):
F = codecs.open("dic", mode="r", encoding="utf-8")
self.dic = {}
self.symbols = '!¡"¿?,.'
self.model = io.read_binary_model_file(model_file)
line = F.readline()
while line:
line = line.split()
if line:
self.dic[line[0]] = line
line = F.readline()
def checkdic(self,word):
if word in self.dic.keys():
try:
pos = self.dic[word][1]
except:
pos =""
print(word, end=" ")
else:
if word in self.symbols:
print(word, end=" ")
else:
#print(word, "[Nid]", end=" ")
seg = self.model.viterbi_segment(word)
for affix in seg[0]:
print(affix, end=" ")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Formato:")
print(" wmorph.py word")
sys.exit()
v = Verb(sys.argv[1], debug=1)
print("Found paths")
print(v.paths)