-
Notifications
You must be signed in to change notification settings - Fork 1
/
triples.py
63 lines (54 loc) · 1.56 KB
/
triples.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
#!/usr/bin/env python3
import sys
import re
exp = re.compile('^([^|]*)[|](.*)$')
nouns="nouns"
adj_pair="adj_pair"
det_pair="det_pair"
adj = {}
noun = set()
det = {}
### TODO: nouns should also be map if in spelling...
def main():
with open(adj_pair,'r') as f:
for line in f:
[old,new] = line.rstrip().split()
adj[old] = new
with open(det_pair,'r') as f:
for line in f:
[old,new] = line.rstrip().split()
det[old] = new
with open(nouns,'r') as f:
for line in f:
noun.add(line.rstrip())
for line in sys.stdin:
m=exp.match(line)
if m:
key=m.group(1)
words = m.group(2).split()
else:
key=""
words = line.split()
i=0
nwords = []
while i < len(words)-2:
if words[i] in det and words[i+1] in adj and words[i+2] in noun:
if det[words[i]] == words[i]:
ndetlist = [ words[i] ]
else:
ndetlist = ["[","@alt",det[words[i]],words[i],"]" ]
nwords = nwords + ndetlist + [ "[","@alt",adj[words[i+1]],words[i+1],"]",words[i+2]]
i=i+3
else:
nwords.append(words[i])
i=i+1
if i< len(words):
nwords.append(words[i])
if i+1 < len(words):
nwords.append(words[i+1])
if key:
print("|".join((key," ".join(nwords))))
else:
print(" ".join(nwords))
if __name__ == "__main__":
main()