Newer
Older
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import array
import logging
import re
import struct
from io import open
from unitex import UnitexException, UnitexConstants
from unitex.utils.fsa import FSAConstants, Automaton
from unitex.utils.types import BRACKETED_ENTRY, Tag, Entry
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
_LOGGER = logging.getLogger(__name__)
class CompressedEntry(Entry):
SEPARATORS = (" ", "-")
SPLITTER = re.compile("([-\s])")
def __init__(self):
super(CompressedEntry, self).__init__()
def compute(self, lemma, form):
n, i = "", 0
while i < len(lemma) and lemma[i].isdigit():
n = n + lemma[i]
i = i + 1
if i > 0:
prefix = form[:len(form)-int(n)]
else:
prefix = form
suffix = lemma[i:]
return "%s%s" % (prefix, suffix)
def uncompress(self, lemma):
form = self.get_form()
if not lemma:
return form
# If two words don't have de same number of elements
# the compressed lemma is preceded by '_'
if lemma[0] == '_':
return self.compute(lemma[1:], form)
wtab = self.SPLITTER.split(form)
ltab = self.SPLITTER.split(lemma)
l = []
for i in range(len(ltab)):
if not ltab[i]:
continue
elif ltab[i] in self.SEPARATORS:
l.append(ltab[i])
else:
l.append(self.compute(ltab[i], wtab[i]))
return "".join(l)
def load(self, form, data, lemmatize=True):
data = data.rstrip()
self.set_form(form)
lemma = ""
i = 0
lemma, escaped = "", False
try:
while True:
if data[i] == "." and escaped is False:
break
elif data[i] == "\\":
if escaped is True:
lemma += data[i]
escaped = False
else:
lemma += data[i]
escaped = True
else:
lemma += data[i]
escaped = False
i += 1
except IndexError:
raise UnitexException("Wrong lemma for entry '%s' ..." % data)
if lemmatize is True:
self.set_lemma(self.uncompress(lemma))
Tag.load(self, data[i+1:])
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
INITIAL_STATE_OFFSET=4
INF_SEPARATOR=re.compile(r"(?<![\\]),")
def __init__(self):
self.__bin = None
self.__inf = None
self.__buffer = None
def lookup(self, token, i=None, pos=None):
if i is None:
i = 0
if pos is None:
pos = self.INITIAL_STATE_OFFSET
tnbr = self.__bin[pos] * 256 + self.__bin[pos+1]
pos = pos + 2
_LOGGER.debug("Lookup Start: token[%s|%s] -- pos(%s) -- tnbr(%s)\n" % (token[:i], token[i:], pos, tnbr))
if i == len(token):
data = []
_LOGGER.debug(" Check Final State: pos(%s) -- tnbr(%s)\n" % (pos, tnbr))
if not (tnbr & 32768):
_LOGGER.debug(" -> Final\n")
index = self.__bin[pos] * 256 * 256 + self.__bin[pos+1] * 256 + self.__bin[pos+2]
for inf in self.INF_SEPARATOR.split(self.__inf[index]):
E = CompressedEntry()
E.load(token, inf)
data.append(E)
else:
_LOGGER.debug(" -> Not final\n")
return data, pos-2
elif tnbr & 32768:
tnbr = tnbr - 32768
else:
pos = pos + 3
for j in range(tnbr):
char = chr(self.__bin[pos] * 256 + self.__bin[pos+1])
_LOGGER.debug(" Matching char[%s] -- pos(%s) -> current[%s]\n" % (token[i], pos, char))
pos = pos + 2
offset = self.__bin[pos] * 256 * 256 + self.__bin[pos+1] * 256 + self.__bin[pos+2]
pos = pos + 3
if char == token[i]:
_LOGGER.debug(" -> Char found\n")
return self.lookup(token, i+1, offset)
# WEIRD... Objective: handle whitespaces in MWU dictionaries for the match function
# -> ["Conseil", "d'", "administration"] == "Conseil d'administration"
elif char == u" " and i == 0:
_LOGGER.debug(" -> Char is whitespace [pass]\n")
return self.lookup(token, i, offset)
return None, pos
def find(self, token):
entries, pos = self.lookup(token)
return entries
def match(self, sequence, i=None, mode=None, separator=None):
if i is None:
i = 0
if mode is None:
mode = UnitexConstants.MATCH_MODE_LONGEST
elif mode not in [UnitexConstants.MATCH_MODE_LONGEST,\
UnitexConstants.MATCH_MODE_SHORTEST,\
UnitexConstants.MATCH_MODE_ALL]:
raise UnitexException("Wrong match mode: %s ..." % mode)
matches = []
buffer, pos, tnbr = [], None, None
for j in range(i, len(sequence)):
_LOGGER.debug("Match Token: '%s'\n" % sequence[j])
entries, pos = self.lookup(sequence[j], pos=pos)
if entries is None:
_LOGGER.debug(" -> No entry found ...\n")
break
_LOGGER.debug(" -> Entries found: pos[%s] -- tnbr[%s]\n" % (pos, tnbr))
buffer.append(j)
if entries:
matches.append((entries, buffer[:]))
if mode == UnitexConstants.MATCH_MODE_SHORTEST:
return matches
if separator is not None:
_LOGGER.debug("Match Separator: '%s'\n" % separator)
entries, pos = self.lookup(separator, pos=pos)
if entries is None:
_LOGGER.debug(" -> No separator found ...\n")
break
_LOGGER.debug(" -> Separator found\n")
if not matches:
return None
elif mode == UnitexConstants.MATCH_MODE_LONGEST:
return [matches[-1]]
elif mode == UnitexConstants.MATCH_MODE_ALL:
return matches
def dump(self, pos=None):
if pos is None:
pos = self.INITIAL_STATE_OFFSET
self.__buffer = []
tnbr = self.__bin[pos] * 256 + self.__bin[pos+1]
pos = pos + 2
if not (tnbr & 32768):
index = self.__bin[pos] * 256 * 256 + self.__bin[pos+1] * 256 + self.__bin[pos+2]
form = "".join(self.__buffer)
for inf in self.INF_SEPARATOR.split(self.__inf[index]):
E = CompressedEntry()
E.load(form, inf)
yield E
pos = pos + 3
else:
tnbr = tnbr - 32768
for j in range(tnbr):
self.__buffer.append(chr(self.__bin[pos] * 256 + self.__bin[pos+1]))
pos = pos + 2
offset = self.__bin[pos] * 256 * 256 + self.__bin[pos+1] * 256 + self.__bin[pos+2]
pos = pos + 3
for E in self.dump(offset):
yield E
if self.__buffer:
self.__buffer.pop()
def load(self, bin, inf, encoding=None):
if encoding is None:
encoding = UnitexConstants.DEFAULT_ENCODING
INF = open(inf, "r", encoding=encoding)
self.__inf = INF.readlines()
self.__inf.pop(0) # Remove number information
INF.close()
BIN = open(bin, "r+b")
a = struct.unpack('B', BIN.read(1))[0]
b = struct.unpack('B', BIN.read(1))[0]
c = struct.unpack('B', BIN.read(1))[0]
d = struct.unpack('B', BIN.read(1))[0]
size = d + (256*c) + (256*256*b) + (256*256*256*a)
BIN.close()
BIN = open(bin, "rb")
self.__bin = array.array('B')
byte = BIN.read(1)
while byte:
tmp = struct.unpack('B', byte)[0]
self.__bin.append(tmp)
byte = BIN.read(1)
BIN.close()
class CompressedDictionary(OldCompressedDictionary):
def __init__(self):
super(CompressedDictionary, self).__init__()
raise NotImplementedError
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
class GRF(Automaton):
def __init__(self, name="GRF"):
super(GRF, self).__init__(name)
def load(self, file, encoding=None):
if encoding is None:
encoding = UnitexConstants.DEFAULT_ENCODING
raise NotImplementedError
def save(self, file, encoding=None):
if encoding is None:
encoding = UnitexConstants.DEFAULT_ENCODING
X = 1000
Y = 1000
GAP = 20
transitions = []
transitions.append({"label": FSAConstants.EPSILON, "targets": set([])})
transitions.append({"label": "", "targets": set([])})
nmap = {}
root = []
for edge, sid, tid in self.iter("dfs"):
source = self[sid]
target = self[tid]
index = 0
key = (str(edge), tid)
if key in nmap:
index = nmap[key]
else:
index = len(transitions)
nmap[key] = index
transitions.append({"label": str(edge), "targets": set([])})
if sid == self.get_initial():
transitions[0]["targets"].add(str(index))
if target.is_final() is True:
transitions[index]["targets"].add("1")
for _edge in target:
for _target in target[_edge]:
_index = 0
_key = (str(_edge), _target.get_id())
if _key in nmap:
_index = nmap[_key]
else:
_index = len(transitions)
nmap[_key] = _index
transitions.append({"label": str(_edge), "targets": set([])})
transitions[index]["targets"].add(str(_index))
with open(file, "w", encoding=encoding) as output:
output.write("#Unigraph\r\n")
output.write("SIZE %s %s\r\n" % (X+GAP, Y+GAP))
output.write("FONT Times New Roman:B 10\r\n")
output.write("OFONT Monospaced:B 8\r\n")
output.write("BCOLOR 16777215\r\n")
output.write("FCOLOR 0\r\n")
output.write("ACOLOR 13487565\r\n")
output.write("SCOLOR 16711680\r\n")
output.write("CCOLOR 255\r\n")
output.write("DBOXES y\r\n")
output.write("DFRAME y\r\n")
output.write("DDATE y\r\n")
output.write("DFILE y\r\n")
output.write("DDIR n\r\n")
output.write("DRIG n\r\n")
output.write("DRST n\r\n")
output.write("FITS 100\r\n")
output.write("PORIENT L\r\n")
output.write("#\r\n")
output.write("%s\r\n" % len(transitions))
for transition in transitions:
label = transition["label"]
size = len(transition["targets"])
targets = " ".join(list(transition["targets"]))
if size == 0:
output.write('"%s" %s %s %s \r\n' % (label, GAP, GAP, size))
else:
output.write('"%s" %s %s %s %s \r\n' % (label, GAP, GAP, size, targets))
class SentenceFST(Automaton):
def __init__(self, name="SentenceFST"):
super(SentenceFST, self).__init__(name)
self.__sentence = None
self.__tokens = None
def get_sentence(self):
return self.__sentence
def get_tokens(self):
return self.__tokens
def get_token(self, i):
try:
return self.__tokens[i]
except IndexError:
raise UnitexException("SentenceFST token index out of range (size: %s)." %\
len(self.__tokens))
def get_entries(self):
return self.__entries
def get_label(self, key):
try:
return self.__entries[key]
except KeyError:
raise UnitexException("SentenceFST label key error.")
self.__sentence = sentence
self.__tokens = []
start = 0
for index, length in tokens:
end = start + length
self.__tokens.append(self.__sentence[start:end])
start = end
transitions = []
for i in range(len(states)):
initial = False
if i == 0:
initial = True
final = False
if states[i] == "t":
final = True
sid = self.add_node(initial=initial, final=final)
if final is True:
break
for lid, tid in states[i]:
p1 = entries[lid][1][0][0]
p2 = entries[lid][1][1][0]
if p1 not in self.__entries:
self.__entries[p1] = []
self.__entries[p1].append((entry, p2))
transitions.append((sid, lid, tid))
for sid, lid, tid in transitions:
self.add_edge(lid, sid, tid)
class TextFST:
def __init__(self):
def __del__(self):
self.__tfst.close()
return len(self.__tind)
def __getitem__(self, i):
try:
position = self.__tind[i]
except IndexError:
raise UnitexException("TextFST index out of range (size: %s)." % len(self))
self.__tfst.seek(position)
line = self.__tfst.readline()
while line:
line = line.rstrip()
if line[0] != "$":
raise UnitexException("File '%s' is corrupted ..." % self.__tfst.name)
# The sentence number (format '$n')
number = int(line[1:])
line = self.__tfst.readline()
line = line.rstrip()
# The text of the sentence
text = line
line = self.__tfst.readline()
line = line.rstrip()
# The tokens of the text
# -> [(x1, y), (x2, y2), ..., (xi, yi)]
# where,
# - x: token index in file 'tokens.txt'
# - y: length of the token (in characters)
tokens = [tuple(int(t) for t in token.split("/")) for token in line.split(" ")]
line = self.__tfst.readline()
line = line.rstrip()
# The offset of the sentence (from the begining of the text)
# -> X_Y
# where,
# - X: the offset in tokens
# - Y: the offset in characters
offset = tuple(int(o) for o in line.split("_"))
line = self.__tfst.readline()
line = line.rstrip()
states = []
while line != "t":
if line[0] != ":":
raise UnitexException("File '%s' is corrupted ..." % self.__tfst.name)
line = line[1:].strip()
line = line.split()
state = []
for i in range(0, len(line), 2):
state.append((int(line[i]), int(line[i+1])))
states.append(state)
line = self.__tfst.readline()
line = line.rstrip()
if not line:
raise UnitexException("File '%s' is corrupted ..." % self.__tfst.name)
line = self.__tfst.readline()
line = line.rstrip()
if line[0] != "f":
raise UnitexException("File '%s' is corrupted ..." % self.__tfst.name)
line = self.__tfst.readline()
line = line.rstrip()
tags = []
while line != "f":
if line == "@<E>":
tags.append(("<E>", None))
elif line == "@STD":
line = self.__tfst.readline()
line = line.rstrip()
content = line[1:]
entry = Entry()
if BRACKETED_ENTRY.match(content):
content = BRACKETED_ENTRY.sub(r"\1", content)
entry.load(content)
else:
entry.set_form(content)
line = self.__tfst.readline()
line = line.rstrip()
if line[0] != "@":
raise UnitexException("File '%s' is corrupted ..." % self.__tfst.name)
position = [tuple(int(i) for i in p.split(".")) for p in line[1:].split("-")]
tags.append((entry, position))
else:
raise UnitexException("File '%s' is corrupted ..." % self.__tfst.name)
line = self.__tfst.readline()
line = line.rstrip()
if line[0] != ".":
raise UnitexException("File '%s' is corrupted ..." % self.__tfst.name)
line = self.__tfst.readline()
line = line.rstrip()
_LOGGER.debug("SENTENCE[%s]\n" % number)
_LOGGER.debug(" - offset: (%s)\n" % ", ".join([str(i) for i in offset]))
_LOGGER.debug(" - text: %s\n" % text)
_LOGGER.debug(" - tokens: [%s]\n" % ", ".join([str(t) for t in tokens]))
_LOGGER.debug(" - states:\n")
for state in states:
_LOGGER.debug(" - s: %s\n" % state)
_LOGGER.debug(" - tags:\n")
for tag in tags:
S = SentenceFST("SENTENCE[%d]" % number)
S.load(text, tokens, states, tags)
return S
def __iter__(self):
for i in range(len(self)):
yield self[i]
def load(self, fst, index, encoding=None):
if encoding is None:
encoding = UnitexConstants.DEFAULT_ENCODING
self.__tfst = open(fst, "r", encoding=encoding)
self.__tind = []
with open(index, "rb") as fin:
i = fin.read(4)
while i:
position = struct.unpack("<L", i)
self.__tind.append(position[0])
i = fin.read(4)