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,7 +256,8 @@ class AplicaRegras(object): | ||
| 256 | for i in range(0, len(morfo)): | 256 | for i in range(0, len(morfo)): |
| 257 | if morfo[i] is not None and morfo[i][1] == "NTK": | 257 | if morfo[i] is not None and morfo[i][1] == "NTK": |
| 258 | new_node = self.gerar_no(morfo[i]) | 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 | try: | 261 | try: |
| 261 | lista_pos_arv.insert(i,lista_pos_arv[i]) | 262 | lista_pos_arv.insert(i,lista_pos_arv[i]) |
| 262 | except: | 263 | except: |
| @@ -278,6 +279,8 @@ class AplicaRegras(object): | @@ -278,6 +279,8 @@ class AplicaRegras(object): | ||
| 278 | elif arv_ticket != morfo[i][1]: | 279 | elif arv_ticket != morfo[i][1]: |
| 279 | arvore[lista_pos_arv[i]].set_label(morfo[i][1]) | 280 | arvore[lista_pos_arv[i]].set_label(morfo[i][1]) |
| 280 | 281 | ||
| 282 | + draw.draw_trees(arvore) | ||
| 283 | + | ||
| 281 | # Converte árvore sintática para uma lista de tuplas (igual a lista morfológica) | 284 | # Converte árvore sintática para uma lista de tuplas (igual a lista morfológica) |
| 282 | def converter_arv_para_lista(self, arvore): | 285 | def converter_arv_para_lista(self, arvore): |
| 283 | folhas = filter(None, arvore.leaves()) | 286 | folhas = filter(None, arvore.leaves()) |
| @@ -365,7 +368,6 @@ class AplicaRegras(object): | @@ -365,7 +368,6 @@ class AplicaRegras(object): | ||
| 365 | def get_token_intensidade(self, token): | 368 | def get_token_intensidade(self, token): |
| 366 | print 'TODO' | 369 | print 'TODO' |
| 367 | 370 | ||
| 368 | - | ||
| 369 | # Simplifica a sentença para que possa evitar a ditalogia | 371 | # Simplifica a sentença para que possa evitar a ditalogia |
| 370 | def simplificar_sentenca(self, lista): | 372 | def simplificar_sentenca(self, lista): |
| 371 | lista_simplificada = list(lista) | 373 | lista_simplificada = list(lista) |
src/AplicaSinonimos.py
| @@ -24,7 +24,10 @@ class AplicaSinonimos(object): | @@ -24,7 +24,10 @@ class AplicaSinonimos(object): | ||
| 24 | for tupla in lista_anotada: | 24 | for tupla in lista_anotada: |
| 25 | sinonimo = self.verificar_sinonimo(tupla[0]) | 25 | sinonimo = self.verificar_sinonimo(tupla[0]) |
| 26 | lista_corrigida.append(sinonimo) | 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 | # Verifica se há sinonimo do token | 32 | # Verifica se há sinonimo do token |
| 30 | def verificar_sinonimo(self, token): | 33 | def verificar_sinonimo(self, token): |
src/LerDicionarios.py
| @@ -11,7 +11,14 @@ from os import environ | @@ -11,7 +11,14 @@ from os import environ | ||
| 11 | import csv | 11 | import csv |
| 12 | import platform | 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 | def __init__(self): | 23 | def __init__(self): |
| 17 | self.path = self.get_path() | 24 | self.path = self.get_path() |
| @@ -26,7 +33,7 @@ class LerDicionarios(object): | @@ -26,7 +33,7 @@ class LerDicionarios(object): | ||
| 26 | self.set_vb_ligacao = [] | 33 | self.set_vb_ligacao = [] |
| 27 | self.dic_vb_muda_negacao = [] | 34 | self.dic_vb_muda_negacao = [] |
| 28 | self.file = '' | 35 | self.file = '' |
| 29 | - self.carregar_dicionarios() | 36 | + self.carregar_dicionarios() |
| 30 | 37 | ||
| 31 | def get_path(self): | 38 | def get_path(self): |
| 32 | if platform.system() == 'Windows': | 39 | if platform.system() == 'Windows': |
src/PortGlosa.py
| @@ -6,27 +6,31 @@ | @@ -6,27 +6,31 @@ | ||
| 6 | 6 | ||
| 7 | #LAViD - Laboratório de Aplicações de Vídeo Digital | 7 | #LAViD - Laboratório de Aplicações de Vídeo Digital |
| 8 | 8 | ||
| 9 | +from ThreadTradutor import * | ||
| 9 | from TraduzSentencas import * | 10 | from TraduzSentencas import * |
| 10 | 11 | ||
| 11 | -def traduzir(texto): | 12 | + |
| 13 | +tradutor = TraduzSentencas() | ||
| 14 | + | ||
| 15 | +def traduzir(texto, threads=False): | ||
| 12 | if texto.isspace() or texto == "": | 16 | if texto.isspace() or texto == "": |
| 13 | return "SELECIONE UM TEXTO" | 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 | num_threads = len(texto_quebrado) | 27 | num_threads = len(texto_quebrado) |
| 24 | texto_traduzido = [] | 28 | texto_traduzido = [] |
| 25 | threads = [] | 29 | threads = [] |
| 26 | 30 | ||
| 27 | for i in range(num_threads): | 31 | for i in range(num_threads): |
| 28 | if texto_quebrado[i] > 0 and texto_quebrado[i] != " ": | 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 | threads[i].start() | 34 | threads[i].start() |
| 31 | for i in range(num_threads): | 35 | for i in range(num_threads): |
| 32 | threads[i].join() | 36 | threads[i].join() |
| @@ -36,13 +40,9 @@ def iniciar_traducao(texto): | @@ -36,13 +40,9 @@ def iniciar_traducao(texto): | ||
| 36 | return " ".join(texto_traduzido) | 40 | return " ".join(texto_traduzido) |
| 37 | except: | 41 | except: |
| 38 | return None | 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 | def quebrar_texto(texto): | 47 | def quebrar_texto(texto): |
| 48 | quantidade_pontos = texto.count('. ') | 48 | quantidade_pontos = texto.count('. ') |
| @@ -0,0 +1,24 @@ | @@ -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 | \ No newline at end of file | 25 | \ No newline at end of file |
src/TraduzSentencas.py
| @@ -6,30 +6,24 @@ | @@ -6,30 +6,24 @@ | ||
| 6 | 6 | ||
| 7 | #LAViD - Laboratório de Aplicações de Vídeo Digital | 7 | #LAViD - Laboratório de Aplicações de Vídeo Digital |
| 8 | 8 | ||
| 9 | -from threading import Thread | ||
| 10 | import alexp | 9 | import alexp |
| 11 | from AplicaSinonimos import * | 10 | from AplicaSinonimos import * |
| 12 | from AplicaRegras import * | 11 | from AplicaRegras import * |
| 13 | import logging | 12 | import logging |
| 14 | import traceback | 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 | self.aplic_sinonimos = AplicaSinonimos() | 18 | self.aplic_sinonimos = AplicaSinonimos() |
| 24 | self.aplic_regras = AplicaRegras() | 19 | self.aplic_regras = AplicaRegras() |
| 25 | logging.basicConfig(filename='translate.log', | 20 | logging.basicConfig(filename='translate.log', |
| 26 | format='%(asctime)s - %(levelname)s:\n\n%(message)s\n\n\n##############################################\n\n', | 21 | format='%(asctime)s - %(levelname)s:\n\n%(message)s\n\n\n##############################################\n\n', |
| 27 | level=logging.ERROR) | 22 | level=logging.ERROR) |
| 28 | 23 | ||
| 29 | - | ||
| 30 | - def run(self): | 24 | + def iniciar_traducao(self, sentenca): |
| 31 | try: | 25 | try: |
| 32 | - analise_sintatica = alexp.run(self.sentenca) | 26 | + analise_sintatica = alexp.run(sentenca) |
| 33 | except Exception as ex: | 27 | except Exception as ex: |
| 34 | self.salvar_log(str(traceback.format_exc())) | 28 | self.salvar_log(str(traceback.format_exc())) |
| 35 | analise_sintatica = None | 29 | analise_sintatica = None |
| @@ -45,11 +39,11 @@ class TraduzSentencas(): | @@ -45,11 +39,11 @@ class TraduzSentencas(): | ||
| 45 | 39 | ||
| 46 | sentenca_corrigida = self.aplic_regras.simplificar_sentenca(regras_aplicadas) | 40 | sentenca_corrigida = self.aplic_regras.simplificar_sentenca(regras_aplicadas) |
| 47 | sinonimos_aplicados = self.aplic_sinonimos.aplicar_sinonimos(sentenca_corrigida) | 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 | def salvar_log(self, erro): | 47 | def salvar_log(self, erro): |
| 55 | - logging.error(erro) | ||
| 56 | \ No newline at end of file | 48 | \ No newline at end of file |
| 49 | + logging.error(erro) | ||
| 50 | + | ||
| 57 | \ No newline at end of file | 51 | \ No newline at end of file |