-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexecutaTrabalho2.py
97 lines (81 loc) · 2.7 KB
/
executaTrabalho2.py
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
# coding=UTF-8
"""
Bruno Cesar RA: 11078407
Juliano Gomes RA: 1118509
"""
from NFA import NFA
import os
def is_number(s):
try:
float(s.replace(',', '.'))
return True
except ValueError:
return False
def processaSimboloReceita(simbolo):
"""
Metodo utilizado para processar o que foi obtido a partir
do arquivo do NFA.
"""
acentos = {'ç':'c', 'é':'e', 'á':'a', 'ã':'a', 'í':'i', 'ó':'o', 'ô':'o', 'ê':'e', 'â':'a', 'ú':'u'}
simbolo=simbolo.lower()
if simbolo in acentos.keys():
simbolo = acentos[simbolo]
if is_number(simbolo):
return 'NUM'
elif simbolo == ' ':
return 'ESP'
elif simbolo == ',':
return 'VIR'
elif simbolo.isalpha() or simbolo in ['(', ')']:
return 'LET'
elif simbolo == '1/2':
return 'NUM'
else:
return 'ERROR'
def leituraReceitas():
resultado = []
os.chdir("./receitas")
for nomeArquivo in os.listdir("."):
if nomeArquivo.endswith(".txt"):
arquivo = open(nomeArquivo)
resultado.append(arquivo.readlines())
arquivo.close()
return resultado
def trataRespostaReceita(nfa, arquivo):
quantidade = ""
ingrediente = ""
medida = ""
for simboloEstado in nfa.simbolosESeusEstados:
temporario = simboloEstado[0]
for estado in simboloEstado[1]:
if estado in ['q1', 'q6', 'q7']:
quantidade += temporario
break
elif estado == 'q9' and ingrediente.rstrip()[len(ingrediente.rstrip())-2:] == 'de':
medida = ingrediente[:len(ingrediente)-3]
ingrediente = ''
elif estado == 'q14':
ingrediente += temporario
elif estado in ['q9']:
ingrediente += temporario
break
print ('Quantidade:' + quantidade+ ' Medida:' + medida + ' Ingrediente:' + ingrediente)
arquivo.write('Quantidade:' + quantidade+ ' Medida:' + medida + ' Ingrediente:' + ingrediente)
if __name__ == "__main__":
nfa = NFA.arquivoNFA("nfa.txt")
nfa.processaSimbolo = processaSimboloReceita
receitas = leituraReceitas()
# nfa.executa("100gr manteiga".strip())
# trataRespostaReceita(nfa)
arquivo = open('../saida.txt','w')
for receita in receitas:
for linha in receita:
nfa.executa(linha.strip())
arquivo.write(linha.strip() +"\n")
trataRespostaReceita(nfa, arquivo)
arquivo.write("\n")
arquivo.close()
# arquivo = open('saida.csv','w')
# arquivo.write('qtde;medida;ingrediente\n')
# for i in resultado:
# arquivo.write(i[0] + ';' + i[1] + ';' + i[2] + '\n')