-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorpguesser2.py
137 lines (124 loc) · 4.44 KB
/
corpguesser2.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
# entryharvester.py
copyright = """Copyright © 2017, Kimmo Koskenniemi
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 hfst, sys, argparse
argparser = argparse.ArgumentParser("python3 corpguesser.py",
description="Produces lexicon entries using also corpus data")
argparser.add_argument("-g", "--guesser",
default="fin-guess.fst",
help="a guesser fst")
argparser.add_argument("-c", "--corp-entries",
default="../guessing/r-guesser.fst",
help="fst composed out of a word list fst and a guesser fst")
argparser.add_argument("-u", "--unique", type=int, default=0,
help="if > 0 then accept an entry which has a sufficient set of word forms in corpus")
args = argparser.parse_args()
guesser_fil = hfst.HfstInputStream(args.guesser)
guesser_fst = guesser_fil.read()
guesser_fil.close()
guesser_fst.invert()
guesser_fst.minimize()
guesser_fst.lookup_optimize()
def unique_entry(word_forms):
remaining = {0}
first = True
for word_form in word_forms:
entries_and_weights = guesser_fst.lookup(word_form, output="tuple")
entries = set()
for e,w in entries_and_weights:
entries.add(e)
if remaining == {0}:
remaining = entries
else:
remaining = remaining & entries
if not remaining:
break
return remaining
corp_fil = hfst.HfstInputStream(args.corp_entries)
corp_fst = corp_fil.read()
corp_fil.close()
corp_fst.minimize()
corp_fst.lookup_optimize()
def check_corp(entry, word_form):
result = corp_fst.lookup(entry, output="tuple")
corp_words = [wd for wd,wg in result]
word_form_set = set(corp_words)
word_form_set.add(word_form)
word_forms = list(word_form_set)
#entries = unique_entry(word_forms)
return word_forms
def nextline():
linenl = sys.stdin.readline()
if not linenl:
exit()
return linenl.strip()
print("\nENTER FORMS OF A WORD:\n")
while True:
word_form = nextline()
res = guesser_fst.lookup(word_form, output="tuple")
remaining = set()
weight = {}
for r,w in res:
remaining.add(r)
weight[r] = w
dic = {}
for entry in remaining:
words = check_corp(entry, word_form)
if args.unique > 1:
print(' '*8, entry, words)
dic[entry] = set(words)
di = list(dic.items())
ce_list = [(e, ws) for (e, ws) in di if ws]
max_len = max([len(ws) for e,ws in ce_list])
#print("celist:", ce_list)###
best_len = -1
i = 0
entry_list = []
for e, ws in ce_list:
ents = unique_entry(list(ws))
if len(ents) == 1:
if len(ws) > best_len:
best_len = len(ws)
best_ent = e
i = i + 1
entry_list.append((e,ws))
print(" "*4+"(", i, ") <<", e, ">>", ", ".join(list(ws)))
if args.unique > 0 and best_len >= max_len:
remaining = set([best_ent])
else:
print(" "*8, [(e, weight[e]) for e in remaining])
while len(remaining) > 1:
line = nextline()
if line == "":
print("GIVING UP THIS WORD\n\n")
break
if line[0] == '-':
res = guesser_fst.lookup(line[1:], output="tuple")
else:
res = guesser_fst.lookup(line, output="tuple")
entries = set([r for r,w in res])
saved = remaining
if line[0] == '-':
remaining = remaining - entries
else:
remaining = remaining & entries
if not remaining:
print("DOES NOT FIT! IGNORED.")
remaining = saved
elif len(remaining) > 1:
print(" "*8, [(e, weight[e]) for e in remaining], "\n")
if len(remaining) == 1:
e = list(remaining)[0]
print("\n" + "="*18)
print(e, ";", weight[e])
print("="*18 + "\n")