-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordforms
executable file
·295 lines (269 loc) · 11.3 KB
/
wordforms
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
#!/bin/sh
print_help_and_exit () {
printf "Usage: wordforms [-s | -p] DICTIONARY.aff DICTIONARY.dic STEM
wordforms -g DICTIONARY.aff DICTIONARY.dic OUTPUT_FILE
Generate all variations of a word stem STEM in the dictionary file DICTIONARY.dic by
affixing all prefixes and suffixes listed as flags of the stem, based on rules stored
in DICTIONARY.aff, and print the results. If -g is given, generate every variation of
every stem in the dictionary file DICTIONARY.dic by affixing all relevant flags, and
write the results to file OUTPUT_FILE. Note that typically this will generate tens of
thousands of words.
-s print only the stem, and any suffixed forms of the stem
-p print only the stem, and any prefixed forms of the stem
-g generate all words in the entire dictionary and save them to a file
"
exit 1
}
generate () {
number_of_entries=$(( $( cat "$2" | wc -l ) - 1 ))
awk -v number_of_entries="$number_of_entries" -v fx="$fx" '
BEGIN {
delete referenced_affix_names
flag_type = "unicode"
complex_prefixes = ""
percent_complete = 0
entry_step_per_percent = number_of_entries / 100
next_update = entry_step_per_percent
}
function debug(text) {
#print text > "/dev/tty"
return
}
function print_to_terminal(text) {
printf text > "/dev/tty"
}
function separate_flags(flags) {
# temp array, grab its contents as soon as you run
# this function (cant return arrays with awk).
# array has numerical indices and char / string / number data
delete separated_flags_array
if (flag_type == "num") split(flags, separated_flags_array, ",");
else if (flag_type == "long") {
for (i=0; i*2<length(flags); i++) {
separated_flags_array[i] = substr(flags, i*2, 2)
}
}
else split(flags, separated_flags_array, "");
}
function affix_word(word, input_flags, location, affix_level) {
debug("\n\nSTART AFFIX_WORD:\t" word "\t" input_flags "\t" location "\t" affix_level)
delete to_be_processed[word "/" input_flags "/" location "/" affix_level]
return_value = 0
# separate the flags ("separated_flags_array" holds the data)
separate_flags(input_flags)
# local affixes are added as string to "to_be_processed" array
# TODO turn this array into a more elegant database array, reduce string splitting / processing?
local_prefixes = ""
local_suffixes = ""
for (i in separated_flags_array) {
if (separated_flags_array[i] " 1 delete" in suffix_db) local_suffixes = local_suffixes separated_flags_array[i] (flag_type=="num" ? "," : "" );
else if (separated_flags_array[i] " 1 delete" in prefix_db) local_prefixes = local_prefixes separated_flags_array[i] (flag_type=="num" ? "," : "" );
}
# group the words suffixes and prefixes together in arrays
for (i in separated_flags_array) {
# check if flag exists in prefix / suffix database array by searching for an
# element that must exist if the flag rule entry exists in the db i.e. "delete"
if (separated_flags_array[i] " 1 delete" in suffix_db && ((location !~ "5$" && !complex_prefixes) || (location !~ "4$" && complex_prefixes)) ) {
for (j=1; separated_flags_array[i] " " j " delete" in suffix_db; j++) {
if (word ~ suffix_db[separated_flags_array[i] " " j " requires"]) {
if (apply_suffix(word, affix_level, location, separated_flags_array[i] " " j)) return_value = 1;
debug("stem \"" word "\" matches flag " separated_flags_array[i] "(" j ") with regex " suffix_db[separated_flags_array[i] " " j " requires"] " made word " affixed_word)
if (local_prefixes) {
return_value = 1
sandwich_location = location (location ~ "3$" ? 4 : 5)
to_be_processed[affixed_word "/" local_prefixes "/" sandwich_location "/" affix_level] = 1
debug("\t(from suffix) sandwich prefixing: " affixed_word "/" local_prefixes "/" sandwich_location "/" affix_level)
}
}
}
}
else if (separated_flags_array[i] " 1 delete" in prefix_db && ((location !~ "^1" && complex_prefixes) || (location !~ "^2" && !complex_prefixes)) ) {
for (j=1; separated_flags_array[i] " " j " delete" in prefix_db; j++) {
if (word ~ prefix_db[separated_flags_array[i] " " j " requires"]) {
if (apply_prefix(word, affix_level, location, separated_flags_array[i] " " j)) return_value = 1;
debug("stem \"" word "\" matches flag " separated_flags_array[i] "(" j ") with regex " prefix_db[separated_flags_array[i] " " j " requires"] " made word " affixed_word)
if (local_suffixes) {
return_value = 1
sandwich_location = (location ~ "^3" ? 2 : 1) location
to_be_processed[affixed_word "/" local_suffixes "/" sandwich_location "/" affix_level] = 1
debug("\t(from prefix) sandwich suffixing: " affixed_word "/" local_suffixes "/" sandwich_location "/" affix_level)
}
}
}
}
}
# cut off the last "," added to a string of flags of type "num"
if (flag_type == "num") {
local_suffixes = substr(local_suffixes, 1, length(local_suffixes) - 1)
local_prefixes = substr(local_prefixes, 1, length(local_prefixes) - 1)
}
debug("END AFFIX_WORD")
return return_value
}
function apply_suffix(word, affix_level, local_location, rule, return_value) {
cut_chars = (suffix_db[rule " delete"] == "0" ? 0 : length(suffix_db[rule " delete"]))
affixed_word = substr(word, 1, length(word) - cut_chars) (suffix_db[rule " add"] == "0" ? "": suffix_db[rule " add"])
all_variations[affixed_word] = affixed_word
if (rule " flags" in suffix_db) {
return_value = 1
if (local_location ~ "4$") local_location = local_location "5";
else if (local_location ~ "3$") local_location = local_location "4";
to_be_processed[affixed_word "/" suffix_db[rule " flags"] "/" local_location "/" affix_level + 1] = 1
}
return return_value
}
function apply_prefix(word, affix_level, local_location, rule, return_value) {
cut_chars = (prefix_db[rule " delete"] == "0" ? 0 : length(prefix_db[rule " delete"]))
affixed_word = (prefix_db[rule " add"] == "0" ? "": prefix_db[rule " add"]) substr(word, cut_chars+1)
all_variations[affixed_word] = affixed_word
if (rule " flags" in prefix_db) {
return_value = 1
if (local_location ~ "^2") local_location = "1" local_location;
else if (local_location ~ "^3") local_location = "2" local_location;
to_be_processed[affixed_word "/" prefix_db[rule " flags"] "/" local_location "/" affix_level + 1] = 1
}
return return_value
}
# get some .aff settings to help configure the affixed word generation
NR==FNR && $1=="COMPLEXPREFIXES" { complex_prefixes = "yes"; next }
NR==FNR && $1=="FLAG" { flag_type = $2; next }
# save all prefixes to the database
NR==FNR && $1=="PFX" && fx!=1 && NF>4 && $4!~"[1-9][0-9]*" {
if ($2 in prefix_name_to_rule_length) prefix_name_to_rule_length[$2] = prefix_name_to_rule_length[$2] + 1;
else prefix_name_to_rule_length[$2] = 1;
i = prefix_name_to_rule_length[$2]
split($4, a_f, "/")
prefix_db[$2 " " i " delete"] = $3
prefix_db[$2 " " i " add"] = a_f[1]
if (2 in a_f) {
prefix_db[$2 " " i " flags"] = a_f[2]
separate_flags(a_f[2])
for (flag in separated_flags_array) {
referenced_affix_names[separated_flags_array[flag]] = separated_flags_array[flag]
}
}
prefix_db[$2 " " i " requires"] = "^"$5
next
}
# save all suffixes to the database
NR==FNR && $1=="SFX" && fx!=2 && NF>4 && $4!~"[1-9][0-9]*" {
if ($2 in suffix_name_to_rule_length) suffix_name_to_rule_length[$2] = suffix_name_to_rule_length[$2] + 1;
else suffix_name_to_rule_length[$2] = 1;
i = suffix_name_to_rule_length[$2]
split($4, a_f, "/")
suffix_db[$2 " " i " delete"] = $3
suffix_db[$2 " " i " add"] = a_f[1]
if (2 in a_f) {
suffix_db[$2 " " i " flags"] = a_f[2]
separate_flags(a_f[2])
for (flag in separated_flags_array) {
referenced_affix_names[separated_flags_array[flag]] = separated_flags_array[flag]
}
}
suffix_db[$2 " " i " requires"] = $5"$"
next
}
NR!=FNR && FNR==1 {
# do anything required at the end of the aff file / beginning of dic file
if (number_of_entries >= 100) print_to_terminal("Generating all words in the dictionary... 0%");
}
NR!=FNR && (FNR-1)>=int(next_update) && number_of_entries>=100 {
percent_complete += 1
next_update = entry_step_per_percent*(percent_complete+1)
print_to_terminal("\r\033[0KGenerating all words in the dictionary... " percent_complete "%")
if (percent_complete == 100) print_to_terminal("\n");
}
NR!=FNR && FNR>1 {
# strips out anything on the line that follows a tab
# tries to strip comments out too i.e. starting with " #"
split($0, e_, "\t")
split(e_[1], e, " #")
split(e[1], entry_and_flags, "/")
all_variations[entry_and_flags[1]] = entry_and_flags[1]
# check if dic entry has flags
if (2 in entry_and_flags) {
to_be_processed[e[1] "/3/1"] = 1
process = 1
while (process) {
process = 0
for (entry in to_be_processed) {
split(entry, entry_data, "/")
# if data includes flags (#2) and no. flags applied (#4) is below four
if (length(entry_data) > 3 && entry_data[4] < 4) {
if (affix_word(entry_data[1], entry_data[2], entry_data[3], entry_data[4])) process = 1;
}
else {
process = 1
all_variations[entry_data[1]] = entry_data[1]
delete to_be_processed[entry]
}
}
}
}
next
}
END {
if (number_of_entries >= 100) print_to_terminal("Sending entries to Hunspell...\n");
for (variation in all_variations) {
print(all_variations[variation])
}
if (number_of_entries >= 100) print_to_terminal("Spellchecking entries...\n");
}
' $1 $2
}
# this switches whether to print only suffixed (fx = 1),
# prefixed (fx = 2) or all forms (fx = 0) of a stem.
# -g ignores the -s and -p and generates all forms
fx=0
generate_all_words=false
case $1 in
-s) fx=1 ; shift;;
-p) fx=2 ; shift;;
-g) generate_all_words=true ; shift;;
*) [ ! -e "$PWD/$1" ] && print_help_and_exit;;
esac
# check if there are the correct remaining arguments
[ $# -eq 3 ] || print_help_and_exit
# check that the relevant files exist
if [ ! -e "$PWD/$1" ] ; then echo "wordforms: Affix file $PWD/$1 does not exist." ; exit 1 ; fi
if [ ! -e "$PWD/$2" ] ; then echo "wordforms: Dictionary file $PWD/$2 does not exist." ; exit 1 ; fi
# clear any previous temporary dictionary
test -h /tmp/wordforms.aff && rm /tmp/wordforms.aff
rm -f /tmp/wordforms.dic
# make a symbolic link of the aff file
ln -s "$PWD/$1" /tmp/wordforms.aff
if [ "$generate_all_words" = true ] ; then
# find all words in the dictionary that can be
# generated by affixing the stems in the dic file
# with the rules in the aff file
# make a symbolic link of the dic file
ln -s "$PWD/$2" /tmp/wordforms.dic
generate /tmp/wordforms.aff /tmp/wordforms.dic | hunspell -d /tmp/wordforms -G -l | sort -u > $3
else
# find all lines exactly matching the search
# stem given in the command line arguments,
# plus optional forward slash and flags.
# does not filter for comments or other data
# in lines - these are copied into the temp
# dic and filtered out in the main awk script
# in the function generate()
awk -v stem="$3" '
BEGIN { stems_found = "" }
$0~"^"stem"[/$]" || $0==stem { stems_found = "yes" ; stems[$0] = $0 }
END {
if (stems_found) {
print(length(stems))
for (s in stems) {
print(stems[s])
}
}
else {
exit 1
}
}
' "$PWD/$2" > /tmp/wordforms.dic
# if no matching stems have been found then exit;
# theres no need to run the main word generation code
if [ $? -ne 0 ] ; then exit 1 ; fi
generate /tmp/wordforms.aff /tmp/wordforms.dic | sort -u
fi