Simplificador.py
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Autor: Erickson Silva <erickson.silva@lavid.ufpb.br> <ericksonsilva@live.com>
from WorkCSV import *
from Iterator import *
from StringAux import *
class Simplificador(object):
# inicializa todos as variaveis
def __init__(self):
self.it = Iterator()
self.__csv = WorkCSV()
self.__dicInf = {}
self.__dicSin = {}
self.__dicWords = {}
self.__dic2Gen = {}
self.__dicTemVerbs = {}
self.executeWorkCSV()
# retira artigos e preposicoes; passa verbos para infinitivo e verificar se há sinonimos
def simplificar(self, texto):
self.__ts = []
self.it.load(texto)
self.__verb = False
self.__adv = False;
self.__countVerb = 0
self.__countAdv = 0
countWords = 0
while(self.it.hasNext()):
w = self.auxConvert(self.it.getAtualW())
t = self.it.getAtualT()
self.__b = False
if self.__dicWords.has_key(t) == False: # verifica se nao eh artigo/preposicao
wu = w.upper() # deixa o token maiusculo
#if t[:2] == "VB":
if t == "VB-P" or t == "VB-D" or t == "VB-R":
self.__verb = True
self.__countVerb += 1
if t[:3] == "ADV":
self.__adv = True
self.__countAdv += 1
if self.__dicInf.has_key(wu): # verifica se ha um verbo infinitivo desse token
sAux = self.__dicInf[wu] # se sim, adiciona numa string aux
if self.__dicSin.has_key(sAux): # verifica se ha um sinonimo para esse verbo infinitivo
self.__ts.append([self.__dicSin[sAux],t]) # se sim, entao adiciona na lista
self.__b = True
else:
self.__ts.append([sAux,t]) # caso contrario, adiciona so o verbo infinitivo msm
self.__b = True
if self.__b == False and self.__dicSin.has_key(wu): # verifica se nao foi encontrado verbo infinitivo e se ha sinonimo
self.__ts.append([self.__dicSin[wu],t]) # adiciona na o sinonimo lista
self.__b = True
if self.__dic2Gen.has_key(wu):
del self.__ts[-1]
lenTicket = len(self.it.getAntT())
if ((self.__dicWords.has_key(self.it.getAntT())) and (self.it.getAntT()[lenTicket-1:] == "F") or (self.it.getAntT()[lenTicket-3:] == "F-P")):
self.__ts.append(["MULHER " + wu,t])
else:
self.__ts.append(["HOMEM " + wu,t])
self.__b = True
if self.__b == False: # verifica se nao encontrou nem verbo infinito ou sinonimo
self.__ts.append([wu,t])
countWords += 1
self.it.reset()
if self.__verb == True:
return self.verbalAnalysis(self.__ts)
return self.__ts
# cria e recupera todos os dicionarios (verbos inf., sinonimos e artigos/preposicoes)
def executeWorkCSV(self):
self.__dicInf = self.__csv.getDicInf()
self.__dicSin = self.__csv.getDicSin()
self.__dicWords = self.__csv.getDicWords()
self.__dic2Gen = self.__csv.getDic2Gen()
self.__dicTemVerbs = self.__csv.getDicTemVerbs()
# converte romano para numero/numero para palavra
def auxConvert(self, t):
try:
txt = roman_to_int(t)
return extenso(txt).decode("utf-8")
except:
if t.isdigit():
return extenso(t).decode("utf-8")
return t
def verbalAnalysis(self, lista):
lv = []
self.it.load(lista)
hasFut = False
hasPas = False
count = 0
while(self.it.hasNext()):
w = self.it.getAtualW().upper()
t = self.it.getAtualT()
if(t[:3] == "ADV"):
if (self.__dicTemVerbs.has_key(w)):
self.it.reset()
#print "ADV: retornou lista original"
return lista
if(t == "VB-P"):
if (self.__countVerb > 1):
count += 1
#print "VB-P: Incrementou"
if(count == self.__countVerb):
#print "VB-P Adicionou " + w
lv.append([w,t])
else:
#print "VB-P: retornou lista original"
self.it.reset()
return lista
elif(t == "VB-D"):
count += 1
hasPas = True
#print "VB-D: Incrementou"
if(count == self.__countVerb):
#print "VB-D Adicionou " + w
lv.append([w,t])
elif(t == "VB-R"):
count += 1
hasFut = True
#print "VB-R: Incrementou"
if(count == self.__countVerb):
#print "VB-R Adicionou " + w
lv.append([w,t])
else:
lv.append([w,t])
if (hasFut):
lv.append(["FUTURO", "TVB"])
elif (hasPas):
lv.append(["PASSADO", "TVB"])
self.it.reset()
return lv