-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.py
393 lines (361 loc) · 13.2 KB
/
tokenizer.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
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
287
288
289
290
291
292
293
294
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
#prova
#punti: come punteggiatura, come abbreviazione (cercare lista abbreviazioni italiano),
#se parola che segue è minuscolo allora abbreviazione. Eccezione: abbreviazione come ultima parola della frase.
#complicazione data dai discorsi diretti che appaiono dentro frase madre:
#es. “Why not bite the bullet?” said a spokesman <- senza frase citata manca il soggetto -> se minuscola dopo virgolette è una frase unica (non sempre vero es. “You still do that?” Abbey said.)
#punti esclamativi/interrogativi: punteggiatura quasi sempre, con rare eccezioni (yahoo!)
#due punti, punto e virgola: spesso separano due frasi, ma spesso è punteggiatura interna alla frase
#idem, a volte usati per enumerazioni -> non considerare in italiano questi casi
#espressioni multiparola: "et cetera", "ad hoc", "en vogue", "3.30 pm", "Dic. 23 1990", proper names "Daimler Chrisler AG"(<- non curartene è Named Entity Recognition)
#clitics: opposto delle mutliparola, da una sola parola, più token "amarlo", "l'essere"
#dehypenation: togliere il trattino messo per spezzare una parola. 1) preprocessing-> pre- processing -> preprocessing
#2) pre-processing -> pre- processing -> pre-processing 2) pre- and postprocessing-> pre-\n and postprocessing-> pre- and postprocessing
#spazio mancante: es. "ore.Le" oppure "comunque,quella"
#Emoticon: :)
#Emoji: ':smiley:' or '\U0001f604' '🔥'
#URL
#Indirizzo email
from emoji import UNICODE_EMOJI
import re
def is_emoji(s):
return s in UNICODE_EMOJI
def is_emoticon(s):
#if (re.search(r"^[(>:#;=*[8|\-B/\\@<~^%$LXO0}3Vb][a-zA-Z0-9',v(>:#;=*+[8|\-B/\\@<~^%$LXoO0}3Vb)]*$",s)):
if s in [":D", ":(", "XD", ":-D", ":-(", ";)", ":-D", ";P", ":P"]:
return 1
else:
return 0
'''if (is_emoji("🔥")):
print("è una emoji")
else:
print("non è una emoji")
is_emoticon("XD")'''
time_re = re.compile(r'^(([01]?\d|2[0-3])(:|.)([0-5]\d)|24:00)$')
def is_time_format(s):
return bool(time_re.match(s))
decimalNumRe = re.compile('[0-9]+\.[0-9]+')
def is_decimal(s):
return bool(decimalNumRe.match(s))
listaParole = []
listaAbbreviazioni = []
listaMultiparole = []
listaMesi = []
listaParoleTrattino = []
listaNonClitici = []
def createList(file, lista):
with open(file) as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip('\n') for x in content]
for x in content:
words = x.split()
for y in words:
lista.append(y)
def createList2(file, lista):
with open(file) as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip('\n') for x in content]
for x in content:
words = x.split()
for y in words:
lista.append(y.lower())
lista.append(y)
def createListWhitespaces(file, lista):
with open(file) as f:
content = f.readlines()
# you may also want to remove whitespace characters like `\n` at the end of each line
content = [x.strip('\n') for x in content]
for x in content:
lista.append(x)
createList("testo0.txt", listaParole)
createList("abbreviazioniITA.txt", listaAbbreviazioni)
createListWhitespaces("multiWordExprITA.txt", listaMultiparole)
createList("mesi.txt", listaMesi)
createList("paroleConTrattino.txt", listaParoleTrattino)
createList2("nonclitici.txt", listaNonClitici)
#prova = "Nel.mezzo.del.cammin"
def splitKeep(c, s):
result = []
if c in s:
i = s.index(c)
result.append(s[0:i])
result.append(s[i:i+1])
if (c in s[i+1:len(s)]):
resto = splitKeep(c, s[i+1:len(s)])
for y in resto:
result.append(y)
else:
if len(s[i+1:len(s)])>0:
result.append(s[i+1:len(s)])
else:
result = s
return result
def dotIsPunctuation(s, l):
if "www" in s or "@" in s:
return 0
if is_time_format(s):
return 0
if is_decimal(s):
return 0
if s in listaAbbreviazioni:
return 0
for i in listaAbbreviazioni:
if i in s:
return 0
#se ha un punto non alle estremità
else:
if s.find('.')!= len(s)-1:
return 1
else:
index = l.index(s)
#se non è l'ultimo elemento della lista, guardo la parola seguente
if index < len(l)-1:
#Se la prima lettera della parola seguente è maiuscola
if l[index+1][0].isupper() or l[index+1][0].isdigit() or is_emoji(l[index+1][0]) or is_emoticon(l[index+1][0]):
return 1
else:
return 0
#se è l'ultimo punto del documento, sicuramente è di punteggiatura
else:
return 1
def isMultiword(s, l):
index = l.index(s)
#se non è l'ultimo elemento della lista, guardo la parola seguente
if index < len(l)-1:
multiword = s + ' '+l[index+1]
if multiword in listaMultiparole:
return 1
if 'am' == l[index+1] or 'pm' == l[index+1]:
return 1
return 0
def isDate(s,l):
index = l.index(s)
#se ha due parole che la seguono
if index < len(l)-2:
#Formati ammessi: 23 Dic 1990 oppure Dic. 23 1990
if (s.isdigit() and l[index+2].isdigit() and l[index+1] in listaMesi) or (s in listaMesi and l[index+2].isdigit() and l[index+1].isdigit()):
return 1
return 0
def isClitic(s):
#"gli" è un caso a parte
clitic = ["mi", "ti", "ci", "vi", "lo", "la", "li", "le", "ne", "si"]
for x in clitic:
if s.endswith(x) and (s[len(s)-3] == 'r'):
return 1
if s.endswith('gli') and (s[len(s)-4] == 'r'):
return 2
return 0
def isCliticCumulo(s):
clitic = ["lo", "la", "li", "le", "ne"]
for x in clitic:
if s.endswith(x) and s not in listaNonClitici:
radice = s[0:len(s)-2]
if radice.endswith("me") or radice.endswith("te") or radice.endswith("ce"):
return 1
if radice.endswith("glie"):
return 2
return 0
def createToken(lista):
for x in lista:
#vado a spezzare la parola col punto se contiene un punto e se questo è un punto di punteggiatura
if '.' in x and (dotIsPunctuation(x, lista)):
parole = splitKeep('.', x)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if is_emoticon(x[0:2]):
index = lista.index(x)
emoticon = x[0:2]
parola = x[2:len(x)]
lista.pop(index)
if emoticon != '':
lista.insert(index, emoticon)
if parola != '':
lista.insert(index+1, parola)
else:
if is_emoticon(x[0:3]):
index = lista.index(x)
emoticon = x[0:3]
parola = x[3:len(x)]
lista.pop(index)
if emoticon != '':
lista.insert(index, emoticon)
if parola != '':
lista.insert(index+1, parola)
else:
if is_emoticon(x[len(x)-2:len(x)]):
index = lista.index(x)
parola = x[0:len(x)-2]
emoticon = x[len(x)-2:len(x)]
lista.pop(index)
if emoticon != '':
lista.insert(index, emoticon)
if parola != '':
lista.insert(index, parola)
else:
if is_emoticon(x[len(x)-3:len(x)]):
index = lista.index(x)
parola = x[0:len(x)-3]
emoticon = x[len(x)-3:len(x)]
lista.pop(index)
if emoticon != '':
lista.insert(index, emoticon)
if parola != '':
lista.insert(index, parola)
for x in lista:
if '!' in x:
parole = splitKeep('!', x)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if '?' in x:
parole = splitKeep('?', x)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if ':' in x and not is_emoticon(x) and not x.startswith("http"):
parole = splitKeep(':', x)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if ';' in x and not is_emoticon(x):
parole = splitKeep(';', x)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if ',' in x:
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if '\'' in x:
print(x)
parole = splitKeep('\'', x)
print(parole)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if '"' in x:
parole = splitKeep('"', x)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
for x in lista:
if isMultiword(x, lista):
index = lista.index(x)
#concateno le parole (per ora solo lunghezza 2)
multiparola = lista[index] + ' ' + lista[index+1]
lista.pop(index)
lista.pop(index)
lista.insert(index, multiparola)
for x in lista:
if isDate(x, lista):
index = lista.index(x)
#concateno data anno e mese
data = lista[index] + ' ' + lista[index+1]+ ' ' + lista[index+2]
lista.pop(index)
lista.pop(index)
lista.pop(index)
lista.insert(index, data)
#gestisce la dehypenation
for x in lista:
index = lista.index(x)
#non considero i casi in cui c'è solamente la "-"
if x.endswith("-") and len(x)>1 and index<len(lista):
tmp = x[0:len(x)]+lista[index+1]
if tmp in listaParoleTrattino:
parola = tmp
else:
parola = x[0:len(x)-1]+lista[index+1]
lista.pop(index)
lista.pop(index)
lista.insert(index, parola)
for x in lista:
index = lista.index(x)
if isClitic(x) == 1:
parola = x[0:len(x)-2]
clitico = x[len(x)-2:len(x)]
lista.pop(index)
lista.insert(index, parola)
lista.insert(index+1, clitico)
else:
if isClitic(x) == 2:
parola = x[0:len(x)-3]
clitico = x[len(x)-3:len(x)]
lista.pop(index)
lista.insert(index, parola)
lista.insert(index+1, clitico)
for x in lista:
index = lista.index(x)
if isCliticCumulo(x) == 1:
parola = x[0:len(x)-4]
pronome = x[len(x)-4:len(x)-2]
clitico = x[len(x)-2:len(x)]
lista.pop(index)
lista.insert(index, parola)
lista.insert(index+1, pronome)
lista.insert(index+2, clitico)
else:
if isCliticCumulo(x) == 2:
parola = x[0:len(x)-6]
pronome = x[len(x)-6:len(x)-2]
clitico = x[len(x)-2:len(x)]
lista.pop(index)
lista.insert(index, parola)
lista.insert(index+1, pronome)
lista.insert(index+2, clitico)
#gestione emoji
for x in lista:
for carattere in x:
if is_emoji(carattere):
parole = splitKeep(carattere, x)
index = lista.index(x)
lista.pop(index)
counter = 0
for y in parole:
if y != '':
lista.insert(index+counter, y)
counter=counter+1
break
return lista
lista2 = createToken(listaParole)
for x in lista2:
print(x)