-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-azemorph.py
executable file
·132 lines (113 loc) · 3.51 KB
/
update-azemorph.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
#!/usr/bin/python
# coding=utf-8
# -*- encoding: utf-8 -*-
###############################################################################
## Dictionary trimming script for TRmorph style transducers
###############################################################################
import sys, codecs, copy, commands, os;
sys.stdin = codecs.getreader('utf-8')(sys.stdin);
sys.stdout = codecs.getwriter('utf-8')(sys.stdout);
sys.stderr = codecs.getwriter('utf-8')(sys.stderr);
tag_files = {
'adjectives.dix': ['<adj>'],
'adverbs.dix': ['<adv>'],
'cnjadv.dix': ['<cnjadv>'],
'cnjcoo.dix': ['<cnjcoo>'],
'cnjsub.dix': ['<cnjsub>'],
'det.dix': ['<det>'],
'interjections.dix': ['<ij>'],
'nouns.dix': ['<n>'],
'pn_acr.dix': ['<np><acr>'],
'pn_ant.dix': ['<np><ant>'],
'pn_cog.dix': ['<np><cog>'],
'pn_org.dix': ['<np><org>'],
'postpositions.dix': ['<postp>'],
'postpositions_infl.dix': ['<postp>'],
'pronouns.dix': ['<prn>', '<prn><locp>', '<prn><dem>', '<prn><pers>', '<prn><qst>', '<prn><ind>'],
'proper_nouns.dix': ['<np>'],
'toponyms.dix': ['<np><top>'],
'verbs.dix': ['<v>'],
'verbs_iv.dix': ['<v><iv>'],
'verbs_tv.dix': ['<v><tv>']
};
tags_stems = {};
extra_files_common = ['lexicon/misc']
LTEXPAND = 'lt-expand';
if len(sys.argv) < 3: #{
print 'update.py <bidix> <azmorph dir>';
sys.exit(-1);
#}
bidix = sys.argv[1]; # An apertium bilingual dictionary
azmorph = sys.argv[2] + '/'; # Directory of TRmorph
azout = './morph-az/'; # Output directory
retval = commands.getstatusoutput(LTEXPAND + ' ' + bidix);
for line in retval[1].split('\n'): #{
if line.count(':>:') > 0: #{
row = line.split(':>:');
elif line.count(':<:') > 0: #{
row = line.split(':<:');
else:
row = line.split(':');
#}
left = row[0];
right = row[1];
tags = '<' + '<'.join(row[1].split('<')[1:]);
lema = row[1].split('<')[0];
if tags not in tags_stems: #{
tags_stems[tags] = [];
#}
tags_stems[tags].append(lema);
print '~' , lema.decode('utf-8') ;
#}
for tags in tags_stems.keys(): #{
print tags.decode('utf-8') , len(tags_stems[tags]);
#}
for f in tag_files.keys(): #{
print '\n*' , f.decode('utf-8') + ':';
# Read lexicon file and check to see if there is an entry for each line in the bidix
if f.count('.dix') > 0: #{
outfile = file(azout + '/lexicon/' + f.replace('.dix', ''), 'w+');
else: #{
outfile = file(azout + '/lexicon/' + f, 'w+');
#}
outfile.close();
for tag in tag_files[f]: #{
if tag not in tags_stems: #{
continue;
#}
if not os.path.exists(azmorph + '/lexicon/' + f): #{
continue
#}
if f.count('.dix') > 0: #{
outfile = file(azout + '/lexicon/' + f.replace('.dix', ''), 'a+');
else: #{
outfile = file(azout + '/lexicon/' + f, 'a+');
#}
for line in file(azmorph + '/lexicon/' + f).read().split('\n'): #{
if line == '': #{
continue;
#}
lema = '';
if line.count('#') > 0: #{
lema = line.split('#')[1].strip();
elif line.count('<e lm="') > 0: #{
lema = line.split('lm="')[1].split('"')[0].strip();
else: #{
lema = line.strip();
#}
if lema in tags_stems[tag]: #{
print '+' , f.decode('utf-8') , lema.decode('utf-8') , line.decode('utf-8');
if line.count('\t') > 0: #{
outfile.write(line.split('\t')[0] + '\n');
elif line.count('<i>') > 0: #{
outfile.write(line.split('<i>')[1].split('</i>')[0].replace('<s n="', '<').replace('"/>', '>').replace('<b/>', ' ') + '\n');
else: #{
outfile.write(line + '\n');
#}
else: #{
print '-' , f.decode('utf-8') , lema.decode('utf-8') , line.decode('utf-8');
#}
#}
outfile.close();
#}
#}