Commit c31c3e4c96afc684c80f7ad41d5ebb3ca2a59b42
1 parent
ba6d0420
Exists in
master
and in
1 other branch
Atualiza compatibilidade para a versão 3 do NLTK
Showing
5 changed files
with
80 additions
and
75 deletions
Show diff stats
src/AplicaSinonimos
@@ -1,47 +0,0 @@ | @@ -1,47 +0,0 @@ | ||
1 | -#!/usr/bin/python | ||
2 | -# -*- coding: utf-8 -*- | ||
3 | - | ||
4 | -#Autor: Erickson Silva | ||
5 | -#Email 1: <erickson.silva@lavid.ufpb.br> | ||
6 | -#Email 2: <ericksonsilva@live.com> | ||
7 | - | ||
8 | -#LAViD - Laboratório de Aplicações de Vídeo Digital | ||
9 | - | ||
10 | -import os, csv, sys, LeitorDicionarios | ||
11 | -from nltk.tree import * | ||
12 | - | ||
13 | - | ||
14 | -class AplicaSinonimos(object): | ||
15 | - | ||
16 | - # Define e inicializa os atributos | ||
17 | - def __init__(self): | ||
18 | - self.__dicionarios = LeitorDicionarios() | ||
19 | - | ||
20 | - def sinonimosMorfologico(self, texto): | ||
21 | - lista = texto | ||
22 | - for i, elem in enumerate(lista): | ||
23 | - token = self.verificaPalavra(elem[0]) | ||
24 | - texto[i][0] = token | ||
25 | - return lista | ||
26 | - | ||
27 | - def sinonimosSintatico(self, texto): | ||
28 | - folhas = nltk.tree.Tree.leaves(*texto) | ||
29 | - dic = dicionarioSinonimoFolhas(folhas) | ||
30 | - stringTree = str(texto) | ||
31 | - for t in folhas: | ||
32 | - stringTree.replace(t, dic[t]) | ||
33 | - | ||
34 | - return Tree.fromstring(stringTree, brackets='[]') | ||
35 | - | ||
36 | - def dicionarioSinonimoFolhas(folhas): | ||
37 | - dic = {} | ||
38 | - for f in folhas: | ||
39 | - token = verificaPalavra(f) | ||
40 | - dic[f] = token | ||
41 | - return dic | ||
42 | - | ||
43 | - def verificaPalavra(self, token): | ||
44 | - if self.__dicionarios.hasSinonimo(token): | ||
45 | - return self.__dicionarios.getSinonimo(token) | ||
46 | - return token | ||
47 | - |
@@ -0,0 +1,49 @@ | @@ -0,0 +1,49 @@ | ||
1 | +#!/usr/bin/python | ||
2 | +# -*- coding: utf-8 -*- | ||
3 | + | ||
4 | +#Autor: Erickson Silva | ||
5 | +#Email 1: <erickson.silva@lavid.ufpb.br> | ||
6 | +#Email 2: <ericksonsilva@live.com> | ||
7 | + | ||
8 | +#LAViD - Laboratório de Aplicações de Vídeo Digital | ||
9 | + | ||
10 | +import os, csv, sys | ||
11 | +from nltk.tree import * | ||
12 | +from LeitorDicionarios import * | ||
13 | + | ||
14 | +class AplicaSinonimos(object): | ||
15 | + | ||
16 | + # Define e inicializa os atributos | ||
17 | + def __init__(self): | ||
18 | + self.__dicionarios = LeitorDicionarios() | ||
19 | + | ||
20 | + def sinonimosMorfologico(self, texto): | ||
21 | + lista = texto | ||
22 | + for i, elem in enumerate(lista): | ||
23 | + token = self.verificaPalavra(elem[0]) | ||
24 | + listmp = list(elem) | ||
25 | + listmp[0] = token | ||
26 | + lista[i] = listmp | ||
27 | + return lista | ||
28 | + | ||
29 | + | ||
30 | + def dicionarioSinonimoFolhas(self, folhas): | ||
31 | + dic = {} | ||
32 | + for f in folhas: | ||
33 | + token = self.verificaPalavra(f) | ||
34 | + dic[f] = token | ||
35 | + return dic | ||
36 | + | ||
37 | + def sinonimosSintatico(self, texto): | ||
38 | + folhas = Tree.leaves(texto) | ||
39 | + dic = self.dicionarioSinonimoFolhas(folhas) | ||
40 | + stringTree = str(texto) | ||
41 | + for t in folhas: | ||
42 | + stringTree.replace(t, dic[t]) | ||
43 | + tree = Tree.fromstring(stringTree, brackets='()') | ||
44 | + return tree | ||
45 | + | ||
46 | + def verificaPalavra(self, token): | ||
47 | + if self.__dicionarios.hasSinonimo(token): | ||
48 | + return self.__dicionarios.getSinonimo(token) | ||
49 | + return token | ||
0 | \ No newline at end of file | 50 | \ No newline at end of file |
src/LeitorDicionarios.py
@@ -26,77 +26,78 @@ class LeitorDicionarios(object): | @@ -26,77 +26,78 @@ class LeitorDicionarios(object): | ||
26 | 26 | ||
27 | # Define e inicializa os atributos | 27 | # Define e inicializa os atributos |
28 | def __init__(self): | 28 | def __init__(self): |
29 | - self.__path = "vlibras_user/vlibras-core/data/" | 29 | + self.__path = "/home/erickson/vlibras-translate/data/" |
30 | self.__dicInf = {} | 30 | self.__dicInf = {} |
31 | self.__dicSin = {} | 31 | self.__dicSin = {} |
32 | self.__dicWords = {} | 32 | self.__dicWords = {} |
33 | self.__dic2Gen = {} | 33 | self.__dic2Gen = {} |
34 | - self.__dicTemVerbs = {} | 34 | + self.__dicTemVerbs = {} |
35 | + self.__fileDic = '' | ||
35 | self.carregarVerbosInfinitivos() | 36 | self.carregarVerbosInfinitivos() |
36 | self.carregarSinonimos() | 37 | self.carregarSinonimos() |
37 | self.carregarPalavrasIgnoradas() | 38 | self.carregarPalavrasIgnoradas() |
38 | self.carregarSubst2Generos() | 39 | self.carregarSubst2Generos() |
39 | self.carregarTemposVerbais() | 40 | self.carregarTemposVerbais() |
40 | 41 | ||
41 | - # Abre o arquivo que contem os verbos no infinitivo e preenche o dicionario com os mesmos | 42 | + # Abre o self.__fileDic que contem os verbos no infinitivo e preenche o dicionario com os mesmos |
42 | def carregarVerbosInfinitivos(self): | 43 | def carregarVerbosInfinitivos(self): |
43 | try: | 44 | try: |
44 | - arquivo = csv.reader(open(self.__path+"dicPortGlosa.csv"), delimiter=";") | 45 | + self.__fileDic = csv.reader(open(self.__path+"dicPortGlosa.csv"), delimiter=";") |
45 | except IOError, (errno, strerror): | 46 | except IOError, (errno, strerror): |
46 | print "I/O error(%s): %s" % (errno, strerror) | 47 | print "I/O error(%s): %s" % (errno, strerror) |
47 | print "carregarVerbosInfinitivos" | 48 | print "carregarVerbosInfinitivos" |
48 | 49 | ||
49 | - for row in arquivo: | 50 | + for row in self.__fileDic: |
50 | if row[1] != "": | 51 | if row[1] != "": |
51 | try: | 52 | try: |
52 | self.__dicInf[row[0].decode("utf-8")] = row[1].decode("utf-8") | 53 | self.__dicInf[row[0].decode("utf-8")] = row[1].decode("utf-8") |
53 | except UnicodeDecodeError: | 54 | except UnicodeDecodeError: |
54 | self.__dicInf[row[0].decode('iso8859-1').encode('utf-8')] = row[1].decode('iso8859-1').encode('utf-8') | 55 | self.__dicInf[row[0].decode('iso8859-1').encode('utf-8')] = row[1].decode('iso8859-1').encode('utf-8') |
55 | 56 | ||
56 | - # Abre o arquivo que contem os sinonimos e preenche o dicionario com os mesmos | 57 | + # Abre o self.__fileDic que contem os sinonimos e preenche o dicionario com os mesmos |
57 | def carregarSinonimos(self): | 58 | def carregarSinonimos(self): |
58 | try: | 59 | try: |
59 | - arquivo = csv.reader(open(self.__path+"portuguesGlosa.csv"), delimiter=";") | 60 | + self.__fileDic = csv.reader(open(self.__path+"portuguesGlosa.csv"), delimiter=";") |
60 | except IOError, (errno, strerror): | 61 | except IOError, (errno, strerror): |
61 | print "I/O error(%s): %s" % (errno, strerror) | 62 | print "I/O error(%s): %s" % (errno, strerror) |
62 | print "carregarSinonimos" | 63 | print "carregarSinonimos" |
63 | 64 | ||
64 | - for row in arquivo: | 65 | + for row in self.__fileDic: |
65 | if row[1] != "": | 66 | if row[1] != "": |
66 | self.__dicSin[row[0].decode("utf-8")] = row[1].decode("utf-8") | 67 | self.__dicSin[row[0].decode("utf-8")] = row[1].decode("utf-8") |
67 | 68 | ||
68 | 69 | ||
69 | - # Abre o arquivo que contem os tempos verbais | 70 | + # Abre o self.__fileDic que contem os tempos verbais |
70 | def carregarTemposVerbais(self): | 71 | def carregarTemposVerbais(self): |
71 | try: | 72 | try: |
72 | - arquivo = csv.reader(open(self.__path+"temposVerbais.csv"), delimiter=";") | 73 | + self.__fileDic = csv.reader(open(self.__path+"temposVerbais.csv"), delimiter=";") |
73 | except IOError, (errno, strerror): | 74 | except IOError, (errno, strerror): |
74 | print "I/O error(%s): %s" % (errno, strerror) | 75 | print "I/O error(%s): %s" % (errno, strerror) |
75 | print "carregarTemposVerbais" | 76 | print "carregarTemposVerbais" |
76 | 77 | ||
77 | - for row in arquivo: | 78 | + for row in self.__fileDic: |
78 | self.__dicTemVerbs[row[0].decode("utf-8")] = row[0].decode("utf-8") | 79 | self.__dicTemVerbs[row[0].decode("utf-8")] = row[0].decode("utf-8") |
79 | 80 | ||
80 | - # Abre o arquivo que contem os artigos e preposicoes de acordo com o modelo de idioma passado na chamada e preenche o dicionario com os mesmos | 81 | + # Abre o self.__fileDic que contem os artigos e preposicoes de acordo com o modelo de idioma passado na chamada e preenche o dicionario com os mesmos |
81 | def carregarPalavrasIgnoradas(self): | 82 | def carregarPalavrasIgnoradas(self): |
82 | try: | 83 | try: |
83 | - arquivo = csv.reader(open(self.__path+"hWordsRemove.csv"), delimiter=";") | 84 | + self.__fileDic = csv.reader(open(self.__path+"hWordsRemove.csv"), delimiter=";") |
84 | except IOError, (errno, strerror): | 85 | except IOError, (errno, strerror): |
85 | print "I/O error(%s): %s" % (errno, strerror) | 86 | print "I/O error(%s): %s" % (errno, strerror) |
86 | print "carregarPalavrasIgnoradas" | 87 | print "carregarPalavrasIgnoradas" |
87 | 88 | ||
88 | - for row in arquivo: | 89 | + for row in self.__fileDic: |
89 | self.__dicWords[row[0].decode("utf-8")] = row[0].decode("utf-8") | 90 | self.__dicWords[row[0].decode("utf-8")] = row[0].decode("utf-8") |
90 | 91 | ||
91 | - # Abre o arquivo que contem os substantivos que sao comuns dos 2 generos e preenche o dicionario com os mesmos | 92 | + # Abre o self.__fileDic que contem os substantivos que sao comuns dos 2 generos e preenche o dicionario com os mesmos |
92 | def carregarSubst2Generos(self): | 93 | def carregarSubst2Generos(self): |
93 | try: | 94 | try: |
94 | - arquivo = csv.reader(open(self.__path+"subs2Generos.csv"), delimiter=";") | 95 | + self.__fileDic = csv.reader(open(self.__path+"subs2Generos.csv"), delimiter=";") |
95 | except IOError, (errno, strerror): | 96 | except IOError, (errno, strerror): |
96 | print "I/O error(%s): %s" % (errno, strerror) | 97 | print "I/O error(%s): %s" % (errno, strerror) |
97 | print "carregarSubst2Generos" | 98 | print "carregarSubst2Generos" |
98 | 99 | ||
99 | - for row in arquivo: | 100 | + for row in self.__fileDic: |
100 | self.__dic2Gen[row[0].decode("utf-8")] = row[0].decode("utf-8") | 101 | self.__dic2Gen[row[0].decode("utf-8")] = row[0].decode("utf-8") |
101 | 102 | ||
102 | # Retorna o dicionario dos verbos no infinitivo | 103 | # Retorna o dicionario dos verbos no infinitivo |
src/Tradutor.py
@@ -7,8 +7,10 @@ | @@ -7,8 +7,10 @@ | ||
7 | 7 | ||
8 | #LAViD - Laboratório de Aplicações de Vídeo Digital | 8 | #LAViD - Laboratório de Aplicações de Vídeo Digital |
9 | 9 | ||
10 | -from alexp import run | ||
11 | -from AplicaSinonimos import sinonimosMorfologico, sinonimosSintatico | 10 | +from alexp import run,getAnaliseMorfologica |
11 | +from AplicaSinonimos import * | ||
12 | + | ||
13 | +sin = AplicaSinonimos() | ||
12 | 14 | ||
13 | 15 | ||
14 | def iniciarTraducao(texto): | 16 | def iniciarTraducao(texto): |
@@ -17,17 +19,18 @@ def iniciarTraducao(texto): | @@ -17,17 +19,18 @@ def iniciarTraducao(texto): | ||
17 | 19 | ||
18 | def gerarAnalise(sentenca): | 20 | def gerarAnalise(sentenca): |
19 | analise = run(sentenca) | 21 | analise = run(sentenca) |
20 | - if (len(analise) > 0): | ||
21 | - analiseSintatica(analise) | ||
22 | - else: | ||
23 | - analise = getAnaliseMorfologica() | 22 | + if (isinstance(analise,type(None))): |
23 | + analise = getAnaliseMorfologica() | ||
24 | analiseMorfologica(analise) | 24 | analiseMorfologica(analise) |
25 | + else: | ||
26 | + analiseSintatica(analise) | ||
27 | + | ||
25 | 28 | ||
26 | 29 | ||
27 | def analiseMorfologica(sentenca): | 30 | def analiseMorfologica(sentenca): |
28 | - analise = sinonimosMorfologico(sentenca) | 31 | + analise = sin.sinonimosMorfologico(sentenca) |
29 | print analise | 32 | print analise |
30 | 33 | ||
31 | def analiseSintatica(sentenca): | 34 | def analiseSintatica(sentenca): |
32 | - analise = sinonimosSintatico(sentenca) | 35 | + analise = sin.sinonimosSintatico(sentenca) |
33 | print analise | 36 | print analise |
34 | \ No newline at end of file | 37 | \ No newline at end of file |
src/alexp.py
@@ -108,7 +108,7 @@ def analisaSentenca(sentenca): | @@ -108,7 +108,7 @@ def analisaSentenca(sentenca): | ||
108 | """ | 108 | """ |
109 | parser=constroiAnalisador(sentenca) | 109 | parser=constroiAnalisador(sentenca) |
110 | codificada=[w.encode("utf-8") for w in sentenca] | 110 | codificada=[w.encode("utf-8") for w in sentenca] |
111 | - trees=parser.nbest_parse(codificada) | 111 | + trees=parser.parse_one(codificada) |
112 | return trees | 112 | return trees |
113 | 113 | ||
114 | def constroiAnalisador(s): | 114 | def constroiAnalisador(s): |
@@ -120,7 +120,7 @@ def constroiAnalisador(s): | @@ -120,7 +120,7 @@ def constroiAnalisador(s): | ||
120 | entradas=geraEntradasLexicais(SENTENCA_ANOTADA) | 120 | entradas=geraEntradasLexicais(SENTENCA_ANOTADA) |
121 | lexico="\n".join(entradas) | 121 | lexico="\n".join(entradas) |
122 | gramatica="%s\n%s" % (extraiSintaxe(DIR).strip(),lexico) | 122 | gramatica="%s\n%s" % (extraiSintaxe(DIR).strip(),lexico) |
123 | - cfg=nltk.parse_cfg(gramatica) | 123 | + cfg=nltk.CFG.fromstring(gramatica) |
124 | return nltk.ChartParser(cfg) | 124 | return nltk.ChartParser(cfg) |
125 | 125 | ||
126 | def exibeArvores(arvores): | 126 | def exibeArvores(arvores): |
@@ -128,7 +128,6 @@ def exibeArvores(arvores): | @@ -128,7 +128,6 @@ def exibeArvores(arvores): | ||
128 | nltk.draw.draw_trees(*arvores) | 128 | nltk.draw.draw_trees(*arvores) |
129 | 129 | ||
130 | def run(sentenca): | 130 | def run(sentenca): |
131 | - #SENTENCA_ANOTADA = [] | ||
132 | tokens=toqueniza(sentenca) | 131 | tokens=toqueniza(sentenca) |
133 | trees=analisaSentenca(tokens) | 132 | trees=analisaSentenca(tokens) |
134 | return trees | 133 | return trees |
135 | \ No newline at end of file | 134 | \ No newline at end of file |