Commit 61ffafeb40b1062641a3e7137b5d7c1ba2f46267
1 parent
1bf3fe85
Exists in
master
and in
1 other branch
Adiciona tratamento para caso não consiga realizar a classificação morfológica
Showing
1 changed file
with
7 additions
and
29 deletions
Show diff stats
src/new/alexp.py
... | ... | @@ -27,16 +27,15 @@ |
27 | 27 | |
28 | 28 | """Este módulo contém funções que permitem utilizar o Aelius para etiquetar uma sentença, construindo entradas lexicais com base nas etiquetas atribuídas às palavras da sentença. Essas entradas lexicais são integradas em uma gramática CFG dada, que é transformada em um parser, utilizado para gerar uma árvore de estrutura sintagmática da sentença. |
29 | 29 | """ |
30 | -import re, string,nltk,platform | |
30 | +import re,nltk,platform, time, random | |
31 | 31 | from os.path import expanduser |
32 | 32 | from Aelius.Extras import carrega |
33 | 33 | from Aelius import AnotaCorpus |
34 | -from nltk_tgrep import tgrep_positions, tgrep_nodes | |
35 | 34 | from unicodedata import normalize |
36 | 35 | |
37 | 36 | |
38 | 37 | sentenca_anotada="" |
39 | - | |
38 | +sleep_times=[0.1,0.2] | |
40 | 39 | |
41 | 40 | def toqueniza(s): |
42 | 41 | """Decodifica string utilizando utf-8, retornando uma lista de tokens em unicode. |
... | ... | @@ -52,6 +51,9 @@ def etiquetaSentenca(s): |
52 | 51 | """ |
53 | 52 | etiquetador = carrega("AeliusHunPos") |
54 | 53 | anotada = AnotaCorpus.anota_sentencas([s],etiquetador,"hunpos")[0] |
54 | + while (len(anotada) is 0): | |
55 | + time.sleep(random.choice(sleep_times)) | |
56 | + anotada = AnotaCorpus.anota_sentencas([s],etiquetador,"hunpos")[0] | |
55 | 57 | anotada[0] = (anotada[0][0].lower(), anotada[0][1]) |
56 | 58 | return anotada |
57 | 59 | |
... | ... | @@ -64,7 +66,7 @@ def geraEntradasLexicais(lista): |
64 | 66 | # que não são aceitos pelo NLTK como símbolos não terminais |
65 | 67 | c=re.sub(r"[-+]","_",e[1]) |
66 | 68 | c=re.sub(r"\$","_S",c) |
67 | - entradas.append("%s -> '%s'" % (c, removeAcento(e[0].lower()))) | |
69 | + entradas.append("%s -> '%s'" % (c, removeAcento(e[0]))) | |
68 | 70 | return entradas |
69 | 71 | |
70 | 72 | def corrigeAnotacao(lista): |
... | ... | @@ -76,8 +78,6 @@ def corrigeAnotacao(lista): |
76 | 78 | lista[i]=(lista[i][0],"VB-PP") |
77 | 79 | i+=1 |
78 | 80 | |
79 | -# a função abaixo parece muito restritiva; talvez não seja necessário | |
80 | -# que o arquivo esteja no diretório nltk_data | |
81 | 81 | def encontraArquivo(): |
82 | 82 | """Encontra arquivo na pasta vlibras-translate. |
83 | 83 | """ |
... | ... | @@ -119,31 +119,9 @@ def constroiAnalisador(s): |
119 | 119 | cfg=nltk.CFG.fromstring(gramatica) |
120 | 120 | return nltk.ChartParser(cfg) |
121 | 121 | |
122 | -def corrigeArvore(arvore): | |
123 | - lista_pos_arv = [] | |
124 | - for tupla in sentenca_anotada: | |
125 | - string_grep = adaptaAnotacao(tupla[1]) + " < " + tupla[0].lower() | |
126 | - node = tgrep_positions(arvore, string_grep) | |
127 | - if not node: | |
128 | - string_grep = adaptaAnotacao(tupla[1]) + " < " + removeAcento(tupla[0].lower()) | |
129 | - node = tgrep_positions(arvore, string_grep) | |
130 | - if node[0] in lista_pos_arv: | |
131 | - node.reverse() | |
132 | - lista_pos_arv.append(node[0]) | |
133 | - for i in range(0, len(sentenca_anotada)): | |
134 | - if arvore[lista_pos_arv[i]][0] != sentenca_anotada[i][0]: | |
135 | - arvore[lista_pos_arv[i]][0] = sentenca_anotada[i][0] | |
136 | - return arvore | |
137 | - | |
138 | 122 | def removeAcento(texto): |
139 | 123 | return normalize('NFKD', texto.encode('utf-8').decode('utf-8')).encode('ascii', 'ignore') |
140 | 124 | |
141 | -def adaptaAnotacao(anotacao): | |
142 | - split = anotacao.split('_') | |
143 | - for i in range(0, len(split)): | |
144 | - split[i] = split[i].replace('-','_') | |
145 | - return "-".join(split) | |
146 | - | |
147 | 125 | def exibeArvores(arvores): |
148 | 126 | """Função 'wrapper' para a função de exibição de árvores do NLTK""" |
149 | 127 | nltk.draw.draw_trees(*arvores) |
... | ... | @@ -151,4 +129,4 @@ def exibeArvores(arvores): |
151 | 129 | def run(sentenca): |
152 | 130 | tokens=toqueniza(sentenca) |
153 | 131 | tree=analisaSentenca(tokens) |
154 | - return corrigeArvore(tree) | |
155 | 132 | \ No newline at end of file |
133 | + return tree | |
156 | 134 | \ No newline at end of file | ... | ... |