-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_segments_inflections.py
46 lines (36 loc) · 1.16 KB
/
combine_segments_inflections.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
import sys
def replaceUmlauts(string):
return string.replace("ä", "ae").replace("ö", "oe").replace("ü", "ue")
segments = sys.argv[1]
inflections = sys.argv[2]
state = 0
word2segm = {}
with open(segments) as f:
for line in f:
line = line.strip()
if state > 2 and not line:
if word:
if word not in word2segm:
word2segm[word] = set()
word2segm[word].add(segm)
state = 0
continue
elif state == 0:
word = line
elif state == 2:
segm = line
state += 1
with open(inflections) as f:
for line in f:
line = line.strip()
form, analyses = line.split("\t")
form = replaceUmlauts("".join(form.split()))
if form not in word2segm:
print(f"Warning: did't find segmentation for {form}", file=sys.stderr)
continue
segmented = word2segm[form]
if len(segmented) > 1:
print(f"Warning: found multiple segmentations for {form}", file=sys.stderr)
continue
segmented = list(segmented)[0]
print(f"{segmented}\t{analyses}")