-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
54 lines (41 loc) · 1.29 KB
/
stats.py
File metadata and controls
54 lines (41 loc) · 1.29 KB
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
"""
``ler_dicionario`` carrega o dicionário do LibreOffice::
>>> voc = Vocabulario()
>>> voc[0]
'a'
>>> voc[-1]
'µUI'
``existe`` diz se a palavra existe::
>>> voc.existe('casaco')
True
>>> voc.existe('jaguadarte')
False
>>> voc.existe('avião')
True
``existe`` ignora acentos na pesquisa::
>>> voc.existe('casáco')
True
"""
import unicodedata
import collections
def shave_marks(txt):
"""Remove todas as marcas de diacríticos"""
norm_txt = unicodedata.normalize('NFD', txt)
shaved = ''.join(c for c in norm_txt
if not unicodedata.combining(c))
return unicodedata.normalize('NFC', shaved)
class Vocabulario:
def __init__(self):
with open('pt_BR.dic', encoding='latin1') as arq_dic:
palavras = arq_dic.readlines()
palavras = [s.split('/')[0].strip() for s in palavras[1:]]
self.palavras = palavras
self.indice = collections.defaultdict(list)
for palavra in palavras:
sem_acentos = shave_marks(palavra)
self.indice[sem_acentos].append(palavra)
def __getitem__(self, indice):
return self.palavras[indice]
def existe(self, palavra):
sem_acentos = shave_marks(palavra)
return sem_acentos in self.indice