-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
140 lines (112 loc) · 5.16 KB
/
utils.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
138
139
140
# coding=utf-8
from collections import Counter
import re
class ConllEntry:
def __init__(self, id, form, lemma, pos, xpos, feats=None, parent_id=None, relation=None, deps=None, misc=None):
self.id = id
self.form = form
self.norm = normalize(form)
self.xpos = xpos.upper()
self.pos = pos.upper()
self.parent_id = parent_id
self.relation = relation
self.lemma = lemma
self.feats = feats
self.deps = deps
self.misc = misc
self.pred_parent_id = None
self.pred_relation = None
self.pred_pos = None
self.pred_feats = None
self.idChars = []
def __str__(self):
values = [str(self.id), self.form, self.lemma, self.pred_pos, self.xpos, self.feats, str(self.pred_parent_id) if self.pred_parent_id is not None else None, self.pred_relation, self.deps, self.misc]
return '\t'.join(['_' if v is None else v for v in values])
def vocab(conll_path):
wordsCount = Counter()
posCount = Counter()
relCount = Counter()
morphCount = Counter()
#Character vocabulary
c2i = {}
c2i["_UNK"] = 0 # unk char
c2i["<w>"] = 1 # word start
c2i["</w>"] = 2 # word end index
c2i["NUM"] = 3
root = ConllEntry(0, '*root*', '*root*', 'ROOT-POS', 'ROOT-CPOS', '_', -1, 'rroot', '_', '_')
root.idChars = [1,2]
tokens = [root]
for line in open(conll_path, 'r'):
tok = line.strip().split('\t')
if not tok or line.strip() == '':
if len(tokens)>1:
wordsCount.update([node.norm for node in tokens if isinstance(node, ConllEntry)])
posCount.update([node.pos for node in tokens if isinstance(node, ConllEntry)])
relCount.update([node.relation for node in tokens if isinstance(node, ConllEntry)])
morphCount.update([node.feats for node in tokens if isinstance(node, ConllEntry)])
# for node in tokens:
# if isinstance(node, ConllEntry):
# morphCount.update(node.feats.split('|'))
tokens = [root]
else:
if line[0] == '#' or '-' in tok[0] or '.' in tok[0]:
tokens.append(line.strip())
else:
entry = ConllEntry(int(tok[0]), tok[1], tok[2], tok[3], tok[4], tok[5], int(tok[6]) if tok[6] != '_' else -1, tok[7], tok[8], tok[9])
if entry.norm == 'NUM':
entry.idChars = [1,3,2]
else:
chars_of_word = [1]
for char in tok[1]:
if char not in c2i:
c2i[char] = len(c2i)
chars_of_word.append(c2i[char])
chars_of_word.append(2)
entry.idChars = chars_of_word
tokens.append(entry)
if len(tokens) > 1:
wordsCount.update([node.norm for node in tokens if isinstance(node, ConllEntry)])
posCount.update([node.pos for node in tokens if isinstance(node, ConllEntry)])
relCount.update([node.relation for node in tokens if isinstance(node, ConllEntry)])
for node in tokens:
if isinstance(node, ConllEntry):
morphCount.update(node.feats)
return (wordsCount, {w: i for i, w in enumerate(wordsCount.keys())}, c2i, posCount.keys(), relCount.keys(), morphCount.keys())
def read_conll(fh,c2i):
#Character vocabulary
root = ConllEntry(0, '*root*', '*root*', 'ROOT-POS', 'ROOT-CPOS', '_', -1, 'rroot', '_', '_')
root.idChars = [1,2]
tokens = [root]
for line in fh:
tok = line.strip().split('\t')
if not tok or line.strip() == '':
if len(tokens)>1: yield tokens
tokens = [root]
else:
if line[0] == '#' or '-' in tok[0] or '.' in tok[0]:
tokens.append(line.strip())
else:
entry = ConllEntry(int(tok[0]), tok[1], tok[2], tok[3], tok[4], tok[5], int(tok[6]) if tok[6] != '_' else -1, tok[7], tok[8], tok[9])
if entry.norm == 'NUM':
entry.idChars = [1,3,2]
else:
chars_of_word = [1]
for char in tok[1]:
if char in c2i:
chars_of_word.append(c2i[char])
else:
chars_of_word.append(0)
chars_of_word.append(2)
entry.idChars = chars_of_word
tokens.append(entry)
if len(tokens) > 1:
yield tokens
def write_conll(fn, conll_gen):
with open(fn, 'w') as fh:
for sentence in conll_gen:
for entry in sentence[1:]:
fh.write(str(entry) + '\n')
fh.write('\n')
numberRegex = re.compile("[0-9]+|[0-9]+\\.[0-9]+|[0-9]+[0-9,]+");
def normalize(word):
return 'NUM' if numberRegex.match(word) else word.lower()