-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (102 loc) · 3.89 KB
/
main.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
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
from __future__ import unicode_literals
from hazm import *
import codecs
import timeit
import re
def find_word(list, word):
'''for finding ingredients.txt string'''
result = []
for i in range(len(list)):
if list[i].find(word) != -1 and len(list[i]) < 40:
result.append(i)
return result
def find_word2(list, word):
'''for finding recipe string'''
result = []
for i in range(len(list)):
if list[i].startswith(word):
# list[i].replace(word, "")
result.append(i)
return result
def find_next(list, m):
try:
return next(val for val in list if val > m)
except StopIteration:
return None
def set_default(sentencess):
''' find the first line with more than 40 characters '''
i = 0
while len(sentencess[i]) < 40:
i += 1
if i == len(sentencess):
return []
else:
return i
# def is_char(c):
# 'check a charecter is persion or not'
# if c in 'اآبپتسجچهخدذرزسشصضطظعغفقکلمنوهیي':
# return True
# return False
def run(input:str):
normalizer = Normalizer()
separators = [normalizer.normalize(x.strip()) for x in codecs.open('separators.txt', 'r', 'utf-8').readlines()]
preparators = [normalizer.normalize(x.strip()) for x in codecs.open('preparation.txt', 'r', 'utf-8').readlines()]
ingredient_start = [normalizer.normalize(x.strip()) for x in
codecs.open('ingredients.txt', 'r', 'utf-8').readlines()]
start = timeit.default_timer()
sentencess = input.split('\n')
result = {"ingredients": [], "quantity": [], "recipie": [], "span_ingredients": [], "span_recipie": []}
'''مواد لازم از ایندکس چندم شروع می شن '''
l1 = []
for i in ingredient_start:
l1 += find_word2(sentencess, i)
'''طرز تهیه ها از ایندکس چندم شروع می شن '''
l2 = []
for preparator in preparators:
l2 += find_word2(sentencess, preparator)
if len(l2) == 0:
'''حالت پیش فرض'''
l2.append(set_default(sentencess))
l3 = l1 + l2
l3.sort()
for i in l2:
next = find_next(l3, i)
if len(sentencess[i]) < 40:
i = i + 1
else:
sentencess[i] = re.sub(r'^.+:', '', sentencess[i])
recipe = "\n".join(sentencess[i: next])
loc = input.index(recipe)
result["span_recipie"].append([loc, loc + len(recipe)])
result["recipie"].append(recipe.replace("\n", ""))
num = ['۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
for j in l1:
next = find_next(l3, j)
for t in range(j + 1, next):
z = len(sentencess[t])
if z == 0:
continue
if sentencess[t] in num:
sentencess[t] = sentencess[t].split(maxsplit=1)[1]
for separator in separators:
curr_index = sentencess[t].find(separator)
'''اگر اشتباهی مچ کرده بود(مثلا دو در دوغ رو)'''
# if is_char(sentencess[t][curr_index-1]) or is_char(sentencess[t][curr_index+1]):
if sentencess[t][curr_index - 1] != ' ' and sentencess[t][curr_index + 1] != ' ':
continue
if curr_index != -1 and curr_index < z:
z = curr_index
loc = input.index(sentencess[t])
result["span_ingredients"].append([loc, loc + len(sentencess[t])])
result["ingredients"].append(re.sub(r'^[\W_]+|[\W_]+$', '', sentencess[t][:z]))
result["quantity"].append(sentencess[t][z:])
stop = timeit.default_timer()
result["time"] = stop - start
return result
def main():
file = open("input.txt", "r", encoding='utf-8')
file_contents = file.read()
file.close()
print(run(file_contents))
if __name__ == '__main__':
main()