Commit 85d80585eeeebfcbcc98dbb7dee0a40d93d0859d
1 parent
61ffafeb
Exists in
master
and in
1 other branch
Atualiza TraduzSentencas como Thread e PortGlosa como classe principal
Showing
3 changed files
with
76 additions
and
44 deletions
Show diff stats
src/new/ConverteExtenso.py
... | ... | @@ -0,0 +1,50 @@ |
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 | + | |
11 | +def traduz(texto): | |
12 | + glosa = iniciar_traducao(texto) | |
13 | + if glosa: | |
14 | + return glosa | |
15 | + return "selecione_texto" | |
16 | + | |
17 | +def iniciar_traducao(texto): | |
18 | + texto_quebrado = quebrar_texto(texto.lower()) | |
19 | + num_threads = len(texto_quebrado) | |
20 | + texto_traduzido = [] | |
21 | + threads = [] | |
22 | + | |
23 | + for i in range(num_threads): | |
24 | + if texto_quebrado[i] > 0 and texto_quebrado[i] != " ": | |
25 | + threads.insert(i, TraduzSentencas(texto_quebrado[i])) | |
26 | + threads[i].start() | |
27 | + for i in range(num_threads): | |
28 | + threads[i].join() | |
29 | + texto_traduzido.append(threads[i].obter_glosa()) | |
30 | + | |
31 | + try: | |
32 | + return " ".join(texto_traduzido) | |
33 | + except: | |
34 | + return None | |
35 | + | |
36 | +def quebrar_texto(texto): | |
37 | + quantidade_pontos = texto.count('. ') | |
38 | + sentencas = [] | |
39 | + for i in range(quantidade_pontos): | |
40 | + posicao_ponto = texto.find('.') | |
41 | + if texto[posicao_ponto+2].isupper(): | |
42 | + sentencas.append(texto[:posicao_ponto]) | |
43 | + texto = texto[posicao_ponto+2:] | |
44 | + if len(texto) > 0: | |
45 | + sentencas.append(texto) | |
46 | + return sentencas | |
47 | + | |
48 | +def help(): | |
49 | + #TODO: Adicionar um pequeno tuto aqui | |
50 | + print "Help" | |
0 | 51 | \ No newline at end of file | ... | ... |
src/new/TraduzSentencas.py
... | ... | @@ -6,51 +6,33 @@ |
6 | 6 | |
7 | 7 | #LAViD - Laboratório de Aplicações de Vídeo Digital |
8 | 8 | |
9 | +from threading import Thread | |
9 | 10 | import alexp |
10 | 11 | from AplicaSinonimos import * |
11 | 12 | from AplicaRegras import * |
12 | 13 | |
13 | -aplic_sinonimos = AplicaSinonimos() | |
14 | -aplic_regras = AplicaRegras() | |
15 | - | |
16 | - | |
17 | -def traduz(texto): | |
18 | - try: | |
19 | - texto_codificado = texto.decode("utf-8") | |
20 | - except: | |
21 | - texto_codificado = texto.decode("iso-8859-1") | |
22 | - glosa = iniciar_traducao(texto_codificado) | |
23 | - if glosa: return glosa | |
24 | - return "selecione um texto" | |
25 | - | |
26 | -def iniciar_traducao(texto): | |
27 | - texto_quebrado = texto.lower().split(".") | |
28 | - texto_traduzido = [] | |
29 | - for sentenca in texto_quebrado: | |
30 | - if len(sentenca) > 0 and sentenca != " ": | |
31 | - analise = gerar_analise(sentenca) | |
32 | - texto_traduzido.append(analise) | |
33 | - try: | |
34 | - return " ".join(texto_traduzido) | |
35 | - except: | |
36 | - return "" | |
37 | - | |
38 | -def gerar_analise(sentenca): | |
39 | - try: | |
40 | - analise_sintatica = alexp.run(sentenca) | |
41 | - except: | |
42 | - analise_sintatica = None | |
43 | - | |
44 | - analise_morfologica = alexp.getAnaliseMorfologica() | |
45 | - if (isinstance(analise_sintatica,type(None))): | |
46 | - regras_aplicadas = aplic_regras.aplicar_regras_morfo(analise_morfologica) | |
47 | - else: | |
48 | - regras_aplicadas = aplic_regras.aplicar_regras_sint(analise_morfologica, analise_sintatica) | |
14 | +class TraduzSentencas(Thread): | |
49 | 15 | |
50 | - aplic_regras.separar_new_tokens(regras_aplicadas) | |
51 | - sinonimos_aplicados = aplic_sinonimos.aplicar_sinonimos(regras_aplicadas) | |
52 | - return sinonimos_aplicados.encode('utf-8') | |
53 | - | |
54 | -def help(): | |
55 | - #TODO: Adicionar um pequeno tuto aqui | |
56 | - print "Help" | |
57 | 16 | \ No newline at end of file |
17 | + def __init__(self, sentenca): | |
18 | + Thread.__init__(self) | |
19 | + self.sentenca = sentenca | |
20 | + self.glosa = "" | |
21 | + self.aplic_sinonimos = AplicaSinonimos() | |
22 | + self.aplic_regras = AplicaRegras() | |
23 | + | |
24 | + def run(self): | |
25 | + analise_sintatica = alexp.run(self.sentenca) | |
26 | + analise_morfologica = alexp.getAnaliseMorfologica() | |
27 | + | |
28 | + if (isinstance(analise_sintatica,type(None))): | |
29 | + regras_aplicadas = self.aplic_regras.aplicar_regras_morfo(analise_morfologica) | |
30 | + else: | |
31 | + regras_aplicadas = self.aplic_regras.aplicar_regras_sint(analise_morfologica, analise_sintatica) | |
32 | + | |
33 | + # separar tokens quando tem um newtoken | |
34 | + self.aplic_regras.separar_new_tokens(regras_aplicadas) | |
35 | + sinonimos_aplicados = self.aplic_sinonimos.aplicar_sinonimos(regras_aplicadas) | |
36 | + self.glosa = sinonimos_aplicados.encode('utf-8') | |
37 | + | |
38 | + def obter_glosa(self): | |
39 | + return self.glosa | |
58 | 40 | \ No newline at end of file | ... | ... |