Commit bbe566e56897960625b5c57e210eeddef7137d62
1 parent
5ddbd724
Exists in
master
and in
1 other branch
Implementa tradutor sem threads como default e com threads sendo opcional
Showing
6 changed files
with
66 additions
and
36 deletions
Show diff stats
src/AplicaRegras.py
... | ... | @@ -256,7 +256,8 @@ class AplicaRegras(object): |
256 | 256 | for i in range(0, len(morfo)): |
257 | 257 | if morfo[i] is not None and morfo[i][1] == "NTK": |
258 | 258 | new_node = self.gerar_no(morfo[i]) |
259 | - arvore[lista_pos_arv[i-1][:-2]].insert(2, new_node) | |
259 | + arvore[lista_pos_arv[i-1][:-3]].insert(2, new_node) | |
260 | + #arvore[lista_pos_arv[i-1][:-2]].insert(2, new_node) | |
260 | 261 | try: |
261 | 262 | lista_pos_arv.insert(i,lista_pos_arv[i]) |
262 | 263 | except: |
... | ... | @@ -278,6 +279,8 @@ class AplicaRegras(object): |
278 | 279 | elif arv_ticket != morfo[i][1]: |
279 | 280 | arvore[lista_pos_arv[i]].set_label(morfo[i][1]) |
280 | 281 | |
282 | + draw.draw_trees(arvore) | |
283 | + | |
281 | 284 | # Converte árvore sintática para uma lista de tuplas (igual a lista morfológica) |
282 | 285 | def converter_arv_para_lista(self, arvore): |
283 | 286 | folhas = filter(None, arvore.leaves()) |
... | ... | @@ -365,7 +368,6 @@ class AplicaRegras(object): |
365 | 368 | def get_token_intensidade(self, token): |
366 | 369 | print 'TODO' |
367 | 370 | |
368 | - | |
369 | 371 | # Simplifica a sentença para que possa evitar a ditalogia |
370 | 372 | def simplificar_sentenca(self, lista): |
371 | 373 | lista_simplificada = list(lista) | ... | ... |
src/AplicaSinonimos.py
... | ... | @@ -24,7 +24,10 @@ class AplicaSinonimos(object): |
24 | 24 | for tupla in lista_anotada: |
25 | 25 | sinonimo = self.verificar_sinonimo(tupla[0]) |
26 | 26 | lista_corrigida.append(sinonimo) |
27 | - return " ".join(lista_corrigida) | |
27 | + try: | |
28 | + return " ".join(lista_corrigida) | |
29 | + except: | |
30 | + return " ".join([str(x[0]) for x in lista_anotada]) | |
28 | 31 | |
29 | 32 | # Verifica se há sinonimo do token |
30 | 33 | def verificar_sinonimo(self, token): | ... | ... |
src/LerDicionarios.py
... | ... | @@ -11,7 +11,14 @@ from os import environ |
11 | 11 | import csv |
12 | 12 | import platform |
13 | 13 | |
14 | -class LerDicionarios(object): | |
14 | +class Singleton(object): | |
15 | + | |
16 | + def __new__(cls, *args, **kwargs): | |
17 | + if '_inst' not in vars(cls): | |
18 | + cls._inst = object.__new__(cls, *args, **kwargs) | |
19 | + return cls._inst | |
20 | + | |
21 | +class LerDicionarios(Singleton): | |
15 | 22 | |
16 | 23 | def __init__(self): |
17 | 24 | self.path = self.get_path() |
... | ... | @@ -26,7 +33,7 @@ class LerDicionarios(object): |
26 | 33 | self.set_vb_ligacao = [] |
27 | 34 | self.dic_vb_muda_negacao = [] |
28 | 35 | self.file = '' |
29 | - self.carregar_dicionarios() | |
36 | + self.carregar_dicionarios() | |
30 | 37 | |
31 | 38 | def get_path(self): |
32 | 39 | if platform.system() == 'Windows': | ... | ... |
src/PortGlosa.py
... | ... | @@ -6,27 +6,31 @@ |
6 | 6 | |
7 | 7 | #LAViD - Laboratório de Aplicações de Vídeo Digital |
8 | 8 | |
9 | +from ThreadTradutor import * | |
9 | 10 | from TraduzSentencas import * |
10 | 11 | |
11 | -def traduzir(texto): | |
12 | + | |
13 | +tradutor = TraduzSentencas() | |
14 | + | |
15 | +def traduzir(texto, threads=False): | |
12 | 16 | if texto.isspace() or texto == "": |
13 | 17 | return "SELECIONE UM TEXTO" |
14 | 18 | |
15 | - glosa = iniciar_traducao(texto) | |
16 | - | |
17 | - if glosa: | |
18 | - return glosa | |
19 | - return "HOUVE UM ERRO. TENTE NOVAMENTE" | |
19 | + elif threads: | |
20 | + return iniciar_com_threads(texto) | |
21 | + | |
22 | + else: | |
23 | + return iniciar_sem_threads(texto) | |
20 | 24 | |
21 | -def iniciar_traducao(texto): | |
22 | - '''texto_quebrado = quebrar_texto(texto) | |
25 | +def iniciar_com_threads(texto): | |
26 | + texto_quebrado = quebrar_texto(texto) | |
23 | 27 | num_threads = len(texto_quebrado) |
24 | 28 | texto_traduzido = [] |
25 | 29 | threads = [] |
26 | 30 | |
27 | 31 | for i in range(num_threads): |
28 | 32 | if texto_quebrado[i] > 0 and texto_quebrado[i] != " ": |
29 | - threads.insert(i, TraduzSentencas(texto_quebrado[i])) | |
33 | + threads.insert(i, ThreadTradutor(texto_quebrado[i])) | |
30 | 34 | threads[i].start() |
31 | 35 | for i in range(num_threads): |
32 | 36 | threads[i].join() |
... | ... | @@ -36,13 +40,9 @@ def iniciar_traducao(texto): |
36 | 40 | return " ".join(texto_traduzido) |
37 | 41 | except: |
38 | 42 | return None |
39 | - ''' | |
40 | 43 | |
41 | - ts = TraduzSentencas(texto) | |
42 | - try: | |
43 | - return ts.run() | |
44 | - except: | |
45 | - return None | |
44 | +def iniciar_sem_threads(texto): | |
45 | + return tradutor.iniciar_traducao(texto) | |
46 | 46 | |
47 | 47 | def quebrar_texto(texto): |
48 | 48 | quantidade_pontos = texto.count('. ') | ... | ... |
... | ... | @@ -0,0 +1,24 @@ |
1 | +#!/usr/bin/python | |
2 | +# -*- coding: utf-8 -*- | |
3 | + | |
4 | +#Autor: Erickson Silva | |
5 | +#Email: <erickson.silva@lavid.ufpb.br> <ericksonsilva@live.com> | |
6 | + | |
7 | +#LAViD - Laboratório de Aplicações de Vídeo Digital | |
8 | + | |
9 | +from TraduzSentencas import * | |
10 | +from threading import Thread | |
11 | + | |
12 | +class ThreadTradutor(Thread): | |
13 | + | |
14 | + def __init__(self, sentenca): | |
15 | + Thread.__init__(self) | |
16 | + self.sentenca = sentenca | |
17 | + self.glosa = "" | |
18 | + self.tradutor = TraduzSentencas() | |
19 | + | |
20 | + def run(self): | |
21 | + self.glosa = self.tradutor.iniciar_traducao(self.sentenca) | |
22 | + | |
23 | + def obter_glosa(self): | |
24 | + return self.glosa | |
0 | 25 | \ No newline at end of file | ... | ... |
src/TraduzSentencas.py
... | ... | @@ -6,30 +6,24 @@ |
6 | 6 | |
7 | 7 | #LAViD - Laboratório de Aplicações de Vídeo Digital |
8 | 8 | |
9 | -from threading import Thread | |
10 | 9 | import alexp |
11 | 10 | from AplicaSinonimos import * |
12 | 11 | from AplicaRegras import * |
13 | 12 | import logging |
14 | 13 | import traceback |
15 | 14 | |
16 | -class TraduzSentencas(): | |
17 | -#class TraduzSentencas(Thread): | |
18 | - | |
19 | - def __init__(self, sentenca): | |
20 | - #Thread.__init__(self) | |
21 | - self.sentenca = sentenca | |
22 | - self.glosa = "" | |
15 | +class TraduzSentencas(object): | |
16 | + | |
17 | + def __init__(self): | |
23 | 18 | self.aplic_sinonimos = AplicaSinonimos() |
24 | 19 | self.aplic_regras = AplicaRegras() |
25 | 20 | logging.basicConfig(filename='translate.log', |
26 | 21 | format='%(asctime)s - %(levelname)s:\n\n%(message)s\n\n\n##############################################\n\n', |
27 | 22 | level=logging.ERROR) |
28 | 23 | |
29 | - | |
30 | - def run(self): | |
24 | + def iniciar_traducao(self, sentenca): | |
31 | 25 | try: |
32 | - analise_sintatica = alexp.run(self.sentenca) | |
26 | + analise_sintatica = alexp.run(sentenca) | |
33 | 27 | except Exception as ex: |
34 | 28 | self.salvar_log(str(traceback.format_exc())) |
35 | 29 | analise_sintatica = None |
... | ... | @@ -45,11 +39,11 @@ class TraduzSentencas(): |
45 | 39 | |
46 | 40 | sentenca_corrigida = self.aplic_regras.simplificar_sentenca(regras_aplicadas) |
47 | 41 | sinonimos_aplicados = self.aplic_sinonimos.aplicar_sinonimos(sentenca_corrigida) |
48 | - self.glosa = sinonimos_aplicados.upper().encode('utf-8') | |
49 | - return self.glosa | |
50 | 42 | |
51 | - def obter_glosa(self): | |
52 | - return self.glosa | |
43 | + if sinonimos_aplicados: | |
44 | + return sinonimos_aplicados.upper().encode('utf-8') | |
45 | + return "HOUVE UM ERRO. TENTE NOVAMENTE" | |
53 | 46 | |
54 | 47 | def salvar_log(self, erro): |
55 | - logging.error(erro) | |
56 | 48 | \ No newline at end of file |
49 | + logging.error(erro) | |
50 | + | |
57 | 51 | \ No newline at end of file | ... | ... |