From 2caa83accf8dc8b47230ffeab21d39d830e4a28b Mon Sep 17 00:00:00 2001 From: Erickson Silva Date: Fri, 13 Feb 2015 13:15:37 -0300 Subject: [PATCH] Corrige Iterator e Aplicador de Sinonimos --- src/new/AplicaSinonimos.py | 59 +++++++++++++++++++++++------------------------------------ src/new/Iterator.py | 50 ++++++++++++++++++++++++++------------------------ 2 files changed, 49 insertions(+), 60 deletions(-) diff --git a/src/new/AplicaSinonimos.py b/src/new/AplicaSinonimos.py index d7157cd..11153fb 100644 --- a/src/new/AplicaSinonimos.py +++ b/src/new/AplicaSinonimos.py @@ -6,45 +6,32 @@ #LAViD - Laboratório de Aplicações de Vídeo Digital -import os, csv, sys -from nltk.tree import * +import os +import csv +import sys +from nltk.tree import Tree from LerDicionarios import * class AplicaSinonimos(object): + """Classe para aplicar sinonimos após a aplicação de regras morfológicas/sintáticas""" - # Define e inicializa os atributos def __init__(self): - self.__dicionarios = LeitorDicionarios() - - def sinonimosMorfologico(self, texto): - lista = texto - for i, elem in enumerate(lista): - token = self.verificaPalavra(elem[0]) - listmp = list(elem) - listmp[0] = token - lista[i] = listmp - return lista - - - def dicionarioSinonimoFolhas(self, folhas): - dic = {} - for f in folhas: - token = self.verificaPalavra(f) - dic[f] = token - return dic - - - def sinonimosSintatico(self, texto): - folhas = Tree.leaves(texto) - dic = self.dicionarioSinonimoFolhas(folhas) - stringTree = str(texto) - for t in folhas: - stringTree.replace(t, dic[t]) - tree = Tree.fromstring(stringTree, brackets='()') - return tree - - - def verificaPalavra(self, token): - if self.__dicionarios.hasSinonimo(token): - return self.__dicionarios.getSinonimo(token) + self.dicionarios = LerDicionarios() + + # Itera sobre os tokens obtendo os sinonimos + def aplicar_sinonimos(self, analise): + lista_anotada = analise + lista_corrigida = [] + if type(analise) is not list: + lista_anotada = Tree.leaves(analise) + + for tupla in lista_anotada: + sinonimo = self.verificar_sinonimo(tupla[0]) + lista_corrigida.append(sinonimo) + return " ".join(lista_corrigida) + + # Verifica se há sinonimo do token + def verificar_sinonimo(self, token): + if self.dicionarios.hasSinonimo(token): + return self.dicionarios.getSinonimo(token) return token \ No newline at end of file diff --git a/src/new/Iterator.py b/src/new/Iterator.py index 53fa55b..bb78f55 100644 --- a/src/new/Iterator.py +++ b/src/new/Iterator.py @@ -7,52 +7,54 @@ #LAViD - Laboratório de Aplicações de Vídeo Digital class Iterator(object): + """Classe para iterar sobre as tuplas (palavra,etiqueta) após análise morfologica""" - # inicializacao das variaveis - def __init__(self): + def init(self): self.count = -1 def load(self, lista): self.reset() - self.__list = list(lista); + self.list = list(lista); self.size = len(lista) def reset(self): self.count = -1 - def getSize(self): + def get_size(self): return self.size - def getCount(self): + def get_count(self): return self.count - def getToken(self, i=None): - if(i != None): return self.__list[self.count+(i)] - return self.__list[self.count] + def get_token(self, i=None): + if(i != None): + return self.list[self.count+(i)] + return self.list[self.count] - def getAtualW(self): - return self.getToken(0)[0].upper() + def get_word(self): + return self.get_token()[0] - def getAtualT(self): - return self.getToken(0)[1] + def get_ticket(self): + return self.get_token()[1] - def getProxW(self): - return self.getToken("+")[0].upper() + def get_next_word(self): + return self.get_token(1)[0] - def getProxT(self): - return self.getToken("+")[1] + def get_next_ticket(self): + return self.get_token(1)[1] - def getAntW(self): - return self.getToken("-")[0].upper() + def get_prev_word(self): + return self.get_token(-1)[0] - def getAntT(self): - return self.getToken("-")[1] + def get_prev_ticket(self): + return self.get_token(-1)[1] - def getInterval(self, n): - if self.count+n > self.size: raise IndexError - return self.__list[self.count:self.count+n] + def get_interval(self, n): + if self.count+n > self.size: + raise IndexError + return self.list[self.count:self.count+n] - def hasNext(self): + def has_next(self): if(self.count < self.size-1): self.count += 1 return True -- libgit2 0.21.2