Commit a19544c1224086449abbe4caa1ca13b902d24dda

Authored by Erickson Silva
1 parent 10f79e1a
Exists in master and in 1 other branch devel

Adiciona metodo para aplicação de regras baseado no arquivo xml

Showing 1 changed file with 126 additions and 1 deletions   Show diff stats
src/new/AplicaRegras.py
... ... @@ -6,18 +6,143 @@
6 6  
7 7 #LAViD - Laboratório de Aplicações de Vídeo Digital
8 8  
  9 +from collections import deque
  10 +import xml.etree.ElementTree as ET
  11 +from os.path import expanduser
  12 +import platform
9 13 from LeitorDicionarios import *
10 14 from Iterator import *
11 15 from StringAux import *
12 16 from ConversorExtenso import *
13   -from collections import deque
14 17  
15 18 class AplicaRegras(object):
16 19  
17 20 # inicializa todos as variaveis
18 21 def __init__(self):
  22 +
  23 + self.__root = self.getRoot()
19 24 self.__dicionarios = LeitorDicionarios()
20 25  
  26 + def getRoot(self):
  27 +
  28 + so = platform.system()
  29 + if so == 'Windows':
  30 + return ET.parse(expanduser("~")+'\\vlibras-translate\data\\regras.xml').getroot()
  31 + else:
  32 + return ET.parse(expanduser("~")+'/vlibras-translate/data/regras.xml').getroot()
  33 +
  34 + def aplicarRegrasMorpho(self, lista):
  35 +
  36 + self.__especificos = {"advt" : self.verificarAdvTempo, "v" : self.verificarVbInfinitivo, "x" : self.verificarPrepos, "c" : self.verificarSubs2Generos, "a" : self.verificarArtigo, "l" : self.verificarVbLigacao, "i": self.verificarAdvIntensidade, "vbi":"zero", "n":"zero", "abmn":"zero", "adji":"zero", "adjn":"zero", "advi":"zero"}
  37 + self.pularIteracoes = 0
  38 + self.__tAux = []
  39 + it = Iterator()
  40 + it.load(lista)
  41 +
  42 + while(it.hasNext()):
  43 + if self.pularIteracoes > 0:
  44 + self.pularIteracoes-=1
  45 + continue
  46 +
  47 + for morpho in self.__root.findall('morphological'):
  48 + self.hasRule = False
  49 + for rule in morpho.findall('rule'): # procura a tag rule
  50 + if rule.find('active').text == "true" and rule.get('name').split("_")[0] == it.getAtualT():
  51 + count = int(rule.find('count').text)
  52 + self.listaIter = []
  53 + if count == 1:
  54 + self.listaIter = [it.getToken()]
  55 + else:
  56 + try:
  57 + self.listaIter = it.getInterval(count)
  58 + self.pularIteracoes = count-1
  59 + except:
  60 + continue
  61 +
  62 +
  63 + self.nomeRegra = self.gerarNomeDaRegra(self.listaIter)
  64 + if rule.get('name') == self.nomeRegra: # verifica se a regra é aplicavel e a mesma esta ativa
  65 + print "Achou regra: " + self.nomeRegra
  66 + #subIter = Iterator()
  67 + #subIter.load(self.listaIter)
  68 + #while(subIter.hasNext()):
  69 + self.hasRule = True
  70 + self.listaTmp = count * [None]
  71 + self.countListaIter = -1
  72 + for classe in rule.iter('class'): # for nas tags class
  73 + title = classe.find('title')
  74 + newpos = classe.find('newpos')
  75 + newprop = classe.find('newprop')
  76 + newtoken = classe.find('newtoken')
  77 + newtokenpos = classe.find('newtokenpos')
  78 + self.specific = classe.find('specific')
  79 +
  80 + self.countListaIter += 1
  81 + token = self.listaIter[self.countListaIter]
  82 +
  83 + if self.specific is not None:
  84 + self.specific = self.__especificos[self.specific.text](token[0])
  85 + if newprop is not None and type(self.specific) != bool:
  86 + self.__tAux.append([self.specific,newprop.text])
  87 +
  88 + if newpos is not None:
  89 + if newpos.text != "-1":
  90 + if type(self.specific) == bool:
  91 + self.listaTmp[int(newpos.text)] = token
  92 + else:
  93 + self.__tAux.append([self.specific, title.text])
  94 +
  95 + if newtoken is not None:
  96 + self.listaTmp[int(newtokenpos.text)] = [newtoken.text, "NEWTOKEN"]
  97 +
  98 + self.listaTmp = filter(None, self.listaTmp)
  99 + for i in self.listaTmp:
  100 + self.__tAux.append(i)
  101 +
  102 + break
  103 +
  104 + if (self.hasRule == False): self.__tAux.append(it.getToken())
  105 + if self.__tAux: return self.__tAux
  106 + return lista # retorna a lista sem alteracoes (nao existe regra)
  107 +
  108 +
  109 + def gerarNomeDaRegra(self, lista):
  110 + self.__nomeRegra = []
  111 + for t in lista:
  112 + self.__nomeRegra.append(t[1])
  113 + return "_".join(self.__nomeRegra)
  114 +
  115 + def verificarAdvTempo(self, lista):
  116 + for i in lista:
  117 + if i[1][:3] == "ADV":
  118 + if (self.__dicionarios.hasTempoVerbal(i[0])):
  119 + return True
  120 + return False
  121 +
  122 + def verificarVbInfinitivo(self, token):
  123 + if self.__dicionarios.hasVerboInfinitivo(token): # verifica se ha um verbo infinitivo desse token
  124 + return self.__dicionarios.getVerboInfinitivo(token)
  125 + return False
  126 +
  127 + #TODO
  128 + def verificarPrepos(self, token):
  129 + return None
  130 +
  131 + def verificarSubs2Generos(self, token):
  132 + return self.__dicionarios.hasSubst2Genero(token)
  133 +
  134 + #TODO
  135 + def verificarArtigo(self, token):
  136 + return None
  137 +
  138 + #TODO
  139 + def verificarVbLigacao(self, token):
  140 + return None
  141 +
  142 + #TODO
  143 + def verificarAdvIntensidade(self, token):
  144 + return None
  145 +
21 146 # retira artigos e preposicoes; passa verbos para infinitivo e verificar se há sinonimos
22 147 def inicializar(self, texto):
23 148 it = Iterator()
... ...