From 7b76db41f6c160168ce399bb2370d28eb81651a4 Mon Sep 17 00:00:00 2001 From: Erickson Silva Date: Fri, 23 Oct 2015 15:55:16 -0300 Subject: [PATCH] Adiciona conversão de números romanos --- src/AplicaRegras.py | 14 ++++++++------ src/ConverteExtenso.py | 23 ++++++++++++++++++++++- src/alexp.py | 3 +-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/AplicaRegras.py b/src/AplicaRegras.py index fbc7987..c56cd2b 100644 --- a/src/AplicaRegras.py +++ b/src/AplicaRegras.py @@ -38,12 +38,12 @@ class AplicaRegras(object): # Gera arvore a partir do arquivos regras.xml def get_root(self): '''Verifica qual o SO e gera o path de onde se encontra o diretório data. - ''' - if "TRANSLATE_DATA" in environ: + ''' + if "TRANSLATE_DATA" in environ: arq_regras = path.join(environ.get("TRANSLATE_DATA"), "regras.xml") return ET.parse(arq_regras).getroot() - elif platform.system() == 'Windows': - return ET.parse(environ.get("HOMEDRIVE")+'\\vlibras-libs\\vlibras-translate\data\\regras.xml').getroot() + elif platform.system() == "Windows": + return ET.parse(environ.get("HOMEDRIVE")+'\\vlibras-libs\\vlibras-translate\\data\\regras.xml').getroot() return ET.parse(expanduser("~")+'/vlibras-translate/data/regras.xml').getroot() # Aplica regras morfológicas apartir do arquivo regras.xml @@ -399,9 +399,11 @@ class AplicaRegras(object): tag = it.get_ticket() if tag == "NUM": + num_romano = roman_to_int(it.get_word()) + lista_simplificada[it.get_count()] = [num_romano.decode('utf-8'), 'NUM-R'] num = True - if tag[-2:] == "-P" and self.verificar_excecao_plural(it.get_word()): + if tag[-2:] == "-P" or tag[-2:] == "_P" and self.verificar_excecao_plural(it.get_word()): singular = self.analisar_plural(it.get_word()) lista_simplificada[it.get_count()][0] = singular @@ -438,7 +440,7 @@ class AplicaRegras(object): return token[:-2]+"l" return token elif(token[-1] == "s"): - #TODO: Palavras paroxítonas ou proparoxítonas terminadas em S. Ex: lápis, vírus, tagênis, ônibus, etc + #TODO: Palavras paroxítonas ou proparoxítonas terminadas em S. Ex: lápis, vírus, tagênis, ônibus, etc return token[:-1] else: return token diff --git a/src/ConverteExtenso.py b/src/ConverteExtenso.py index 96ba274..bb0a0e8 100644 --- a/src/ConverteExtenso.py +++ b/src/ConverteExtenso.py @@ -25,7 +25,28 @@ ext = [{"um":"1", "dois":"2", "tres":"3", "quatro":"4", "cinco":"5", "seis":"6", und = {"mil":1000, "milhao":1000000, "bilhao":1000000000, "trilhao":1000000000000} unds = {"mil":"000", "milhao":"000000","milhoes":"000000", "bilhao":"000000000","bilhoes":"000000000", "trilhao":"000000000000", "trilhoes":"000000000000"} - +numeral_map = zip( + (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1), + ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I') +) + +def int_to_roman(i): + result = [] + for integer, numeral in numeral_map: + count = int(i / integer) + result.append(numeral * count) + i -= integer * count + return ''.join(result) + +def roman_to_int(n): + n = unicode(n).upper() + + i = result = 0 + for integer, numeral in numeral_map: + while n[i:i + len(numeral)] == numeral: + result += integer + i += len(numeral) + return str(result) def oneDigit(x): return ext[0][x] diff --git a/src/alexp.py b/src/alexp.py index ff15a06..d9a3b48 100644 --- a/src/alexp.py +++ b/src/alexp.py @@ -98,10 +98,9 @@ def corrigeAnotacao(lista): def encontraArquivo(): """Encontra arquivo na pasta vlibras-translate. """ - so = platform.system() if "TRANSLATE_DATA" in environ: return path.join(environ.get("TRANSLATE_DATA"), "cfg.syn.nltk") - elif so == 'Windows': + elif platform.system() == 'Windows': return environ.get("HOMEDRIVE") + "\\vlibras-libs\\vlibras-translate\data\cfg.syn.nltk" return expanduser("~") + "/vlibras-translate/data/cfg.syn.nltk" -- libgit2 0.21.2