-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.py
executable file
·379 lines (322 loc) · 16.3 KB
/
search.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from bs4 import BeautifulSoup
import os, nltk
class Search():
def __init__(self, keywords = False, corpus = False, lang = 1):
self.keywords = set(keywords)
self.corpus = corpus
self.lang = lang
# get a tagged text: exemple [je suis libre] => [('je', P), ('suis', VB), ('libre', ADJ)]
def getTagged(self, text):
from nltk.tag import StanfordPOSTagger
if self.lang == 1:
jar = 'stanford-pos-tagger/stanford-postagger-3.8.0.jar'
model = 'stanford-pos-tagger/french.tagger'
pos_tagger = StanfordPOSTagger(model, jar, encoding='utf8' )
tokenizedText = nltk.word_tokenize(text.lower())
taggedText = pos_tagger.tag(tokenizedText)
else:
jar = 'stanford-pos-tagger/stanford-postagger-3.8.0.jar'
model = 'stanford-pos-tagger/arabic.tagger'
pos_tagger = StanfordPOSTagger(model, jar, encoding='utf8' )
tokenizedText = nltk.word_tokenize(text.lower())
taggedText = pos_tagger.tag(tokenizedText)
print(taggedText)
return taggedText
def getStemmedText(self, text):
stemmedText = []
if self.lang == 1:
stemmer = nltk.stem.snowball.FrenchStemmer()
stemmedText = [stemmer.stem(word) for word in text if word.isalpha()]
else:
from tashaphyne.stemming import ArabicLightStemmer
ArListem = ArabicLightStemmer()
for word in text:
if word.isalpha():
stem = ArListem.light_stem(word)
root = ArListem.get_root()
stemmedText.append(root)
return stemmedText
# transform my XML page to Beautifull soup object
def getCorpusSoup(self):
file = open(self.corpus, 'r')
content = file.read()
return BeautifulSoup(content,"lxml")
# just to split sentence to words
def text2list(self, text):
tokenizedText = nltk.word_tokenize(text.lower())
return tokenizedText
# check if my recipe contain a denied ingredient (porc, liqueur...)
def isDenied(self, content, denied):
# Content: title or recipe or category
# Denied: my word list to denied
splitedContent = self.text2list(content)
for ing in denied:
if ing in splitedContent:
return False
return True
# just to build correctly my denied word list: french stop words + colors ...etc
def getDeniedWords(self):
deniedList = []
if self.lang == 1:
colors = ['blanc', 'noir', 'rouge', 'jaune'] # à compléter
otherWord = ['tous', 'les', 'ingrédients', 'moitiés', 'trait', 'frais', 'ensemble', 'petits', 'grand', 'préparation', 'préparations', 'poisson', 'légumes', 'fruits', 'épices', 'service', 'plat']
haram = ['vin', 'porc', 'liqueur', 'cochenille', 'mulet', 'jambon', 'alcool', 'bière', 'rhum', 'vodka', 'whisky', 'tequila', 'Gin', 'cognac', 'pastis', 'lardon', 'champagne']#à compléter
from nltk.corpus import stopwords
stopWords = stopwords.words('french')
deniedList.extend(colors)
deniedList.extend(otherWord)
deniedList.extend(haram)
deniedList.extend(stopWords)
else:
colors = [] # à compléter
otherWord = []
from stop_words import get_stop_words
stopWords = get_stop_words('arabic')
deniedList.extend(colors)
deniedList.extend(otherWord)
deniedList.extend(stopWords)
return self.getStemmedText(deniedList)
# transform a recipe on XML format to a keywords list
def getKeyWords(self, text):
deniedList = set(self.getDeniedWords())
keywords = []
if self.lang == 1:
for line in text:
tline = self.getTagged(line)
ntline = [taggedWord[0] for taggedWord in tline if taggedWord[1].startswith('N')]
stemmedNTLine = set(self.getStemmedText(ntline))
cleanText = stemmedNTLine - deniedList
keywords.extend(list(cleanText))
else:
# from nltk.stem.isri import ISRIStemmer
# stemmer = ISRIStemmer()
for line in text:
tline = self.getTagged(line)
tline = [(item[1].split('/')[0], item[1].split('/')[1]) for item in tline]
ntline = [taggedWord[0] for taggedWord in tline if (taggedWord[1].startswith('N') or taggedWord[1].startswith('DTN'))]
stemmedNTLine = set(self.getStemmedText(ntline))
cleanText = stemmedNTLine - deniedList
keywords.extend(list(cleanText))
# cleanText = [stemmer.stem(word) for word in ntline if stemmer.stem(word) not in deniedList and word.isalpha()]
# keywords.extend(cleanText)
return set(keywords)
# search a recipe by ID
def idSearch(self, idRecipe):
soup = self.getCorpusSoup()
recipe = [ balise.text for balise in soup.find_all("rec", attrs={"id" : idRecipe}) ]
return recipe
# get recipe ID
def findById(self, rec):
soup = BeautifulSoup(rec)
idRecipe = soup.find("rec")['id']
return idRecipe
def existNb(self, keywords, recipeKeyWords):
return len(set(set(keywords)).intersection(set(recipeKeyWords)))
def compare(self, keywords, recipeKeyWords):
nbWords = self.existNb(keywords, recipeKeyWords)
lenRecipeKeyWords = len(set(recipeKeyWords))
prop = round(float((nbWords / lenRecipeKeyWords) * 100), 2)
if prop == 0:
return -2, prop
elif prop < 30:
return -1, prop
elif prop >= 30 and prop < 60:
return 0, prop
else:
return 1, prop
# non desirable ingredient
def getNonDesirable(self, choice):
if self.lang == 1:
notBio = ['acide'] #à compléter
notVegan = ['viandes', 'poisson', 'poulet', 'escalope', 'canard', 'chèvre', 'œuf', 'graisse'] #à compélter
diab = ['sucres', 'miel', 'tarte', 'pâtisserie'] #à compélter
noCrustace = ['homard', 'langoustes', 'tourteaux', 'crabes', 'araignée', 'écrevisse', 'étrille' 'crevettes','fruits de mer', 'moules', 'huîtres', 'oursin', 'saumon'] #à compélter
noGlutin = ['blé', 'seigle','orge','avoine', 'blé','épeautre','engrain', 'farine', 'semoule', 'boulgour', 'amidon', 'muesli', 'pâtes'] #à compélter
noArachides = ['arachide', 'arachin', 'cacahuète', 'conarachin', 'mandelonas', 'noix'] #à compélter
noLait = ['lait', 'fromage', 'beurre', 'yaourts', 'kéfir', 'viili', 'bifidus', 'caséine', 'babeurre', 'crème'] #à compélter
haram = ['vin', 'porc', 'liqueur', 'cochenille', 'mulet', 'jambon', 'alcool', 'bière', 'rhum', 'vodka', 'whisky', 'tequila', 'Gin', 'cognac', 'pastis', 'lardon', 'champagne']#à compléter
else:
notBio = ['acide'] #à compélter
notVegan = ['viandes', 'poisson', 'poulet']#à compélter
diab = ['sucres', 'miel'] #à compélter
noCrustace = [''] #à compélter
noGlutin = [''] #à compélter
noArachides = [''] #à compélter
noLait = [''] #à compélter
haram = ['vin', 'porc', 'liqueur', 'cochenille', 'mulet']#à compléter
nonDesirable = []
if 1 in choice:
nonDesirable.extend(notBio)
if 2 in choice:
nonDesirable.extend(notVegan)
if 3 in choice:
nonDesirable.extend(diab)
if 4 in choice:
nonDesirable.extend(noCrustace)
if 5 in choice:
nonDesirable.extend(noGlutin)
if 6 in choice:
nonDesirable.extend(noArachides)
if 7 in choice:
nonDesirable.extend(noLait)
nonDesirable.extend(haram)
return self.getStemmedText(nonDesirable)
def getBySet(self):
frInverse = open('corpusFr/frInverse.txt', 'r')
queryKeywords = ['croustill', 'beignet']
for Qkeyword in queryKeywords:
for line in frInverse.readlines():
lineSplited = line.split(',')
keyword = lineSplited[0]
if Qkeyword == keyword:
print(lineSplited[1:])
print(Qkeyword)
#lang=1 french / lang=2 arabe
def getResult(self, queryKeywords, lang):
lang = self.lang
corpusKeyWords = open('corpusFr/frInverse.txt', 'r') if lang == 1 else open('corpusAr/arKeyWords.txt', 'r')
content = corpusKeyWords.readlines()
content = [w[:-1] for w in content]
idRecipes = {}
recipesList = []
for line in content:
lineContent = line.split(',')
keyWord = lineContent[0]
if keyWord in queryKeywords:
recipesList.append(lineContent[1:len(lineContent)])
idRecipes = set(recipesList[0])
for wordList in recipesList:
idRecipes.intersection(set(wordList))
return idRecipes
# transform my XML page to Beautifull soup object
def getCorpusSoupById(self, idRecipe):
if idRecipe >= 1 and idRecipe < 1000 :
corpus = 'corpusFr/frCorpus1.txt'
elif idRecipe >= 1000 and idRecipe < 2000 :
corpus = 'corpusFr/frCorpus2.txt'
elif idRecipe >= 2000 and idRecipe < 3000 :
corpus = 'corpusFr/frCorpus3.txt'
elif idRecipe >= 3000 and idRecipe < 4000 :
corpus = 'corpusFr/frCorpus4.txt'
elif idRecipe >= 4000 and idRecipe < 5000 :
corpus = 'corpusFr/frCorpus5.txt'
elif idRecipe >= 5000 and idRecipe < 6000 :
corpus = 'corpusFr/frCorpus6.txt'
elif idRecipe >= 6000 and idRecipe < 7000 :
corpus = 'corpusFr/frCorpus7.txt'
elif idRecipe >= 7000 and idRecipe < 8000 :
corpus = 'corpusFr/frCorpus8.txt'
elif idRecipe >= 8000 and idRecipe < 9000 :
corpus = 'corpusFr/frCorpus78.txt'
elif idRecipe >= 9000 and idRecipe < 10000 :
corpus = 'corpusFr/frCorpus9.txt'
elif idRecipe >= 10000 and idRecipe < 11000 :
corpus = 'corpusFr/frCorpus10.txt'
elif idRecipe >= 11000 and idRecipe < 12000 :
corpus = 'corpusFr/frCorpus11.txt'
elif idRecipe >= 12000 and idRecipe < 13000 :
corpus = 'corpusFr/frCorpus12.txt'
elif idRecipe >= 13000 and idRecipe < 14000 :
corpus = 'corpusFr/frCorpus13.txt'
elif idRecipe >= 14000 and idRecipe < 15000 :
corpus = 'corpusFr/frCorpus14.txt'
elif idRecipe >= 15000 and idRecipe < 16000 :
corpus = 'corpusFr/frCorpus15.txt'
elif idRecipe >= 16000 and idRecipe < 17000 :
corpus = 'corpusFr/frCorpus16.txt'
elif idRecipe >= 17000 and idRecipe < 18000 :
corpus = 'corpusFr/frCorpus17.txt'
elif idRecipe >= 18000 and idRecipe < 18440 :
corpus = 'corpusFr/frCorpus18.txt'
else:
corpus = 'corpusFr/newCorpus.txt'
file = open(corpus, 'r')
content = file.read()
return BeautifulSoup(content,"lxml")
# search a recipe by ID specific data
def idSearchRecipeData(self, idRecipe, checked):
soup = self.getCorpusSoupById(int(idRecipe))
title, cat, ing, prep, eng, inf = [], [], [], [], [], []
deniedList = self.getNonDesirable(checked)
for div in soup.find_all("rec", attrs={"id" : str(idRecipe)}):
check = False
i = 0
while not check and i < len(deniedList) :
if deniedList[i] in div.text:
check = True
i += 1
if not check:
for d in div.find_all('title'):
title.append(d.text)
for d in div.find_all('cat'):
cat.append(d.text)
for d in div.find_all('ing'):
ing.append(d.text)
for d in div.find_all('prep'):
prep.append(d.text)
for d in div.find_all('eng'):
eng.append(d.text)
for d in div.find_all('inf'):
inf.append(d.text)
return title, cat, ing, prep, eng, inf
def updateCorpus(self, url):
from urllib.request import urlopen
from urllib.parse import quote
import re
from string import punctuation
page = urlopen(url).read().decode('utf-8')
soup = BeautifulSoup(page,'html.parser')
lastIdToSave = open('corpusFr/lastIdToSave.txt', 'r')
lastID = lastIdToSave.read()
lastIdToSave.close()
newCorpus = open('corpusFr/newCorpus.txt', 'a')
urls, i = [], int(lastID)+1
for a in soup.find_all('a', attrs={"class", "recipe-card"}):
urls.append(str('http://www.marmiton.org'+a['href']))
print(urls)
for url in urls[:5]:
page = urlopen(url).read().decode('utf-8')
soup = BeautifulSoup(page,'html.parser')
titles = [ title.text for title in soup.find_all("h1", attrs={"class" : "main-title"}) ]
ingredients = [ qt.text+ing.text for (qt,ing) in zip(soup.find_all("span", attrs={"class" : "recipe-ingredient-qt"}), soup.find_all("span", attrs={"class" : "ingredient"})) ]
preparations = [ prep.text.split('\t\t\t')[1].split('.')[0] for prep in soup.find_all("li", attrs={"class" : "recipe-preparation__list__item"}) ]
preparations = re.sub('\t', ' ', '. '.join(preparations))
timePrep = ""
for divpTime in soup.find_all("div", attrs={"class" : "recipe-infos__timmings__preparation"}):
for pTime in divpTime.find_all("span", attrs={"class" : "recipe-infos__timmings__value"}):
timePrep = pTime.text.strip()
cookPrep = ""
for divpTime in soup.find_all("div", attrs={"class" : "recipe-infos__timmings__cooking"}):
for pTime in divpTime.find_all("span", attrs={"class" : "recipe-infos__timmings__value"}):
cookPrep = pTime.text.strip()
info = "<inf>Auteur : www.marmiton.org \nNiveau : \nPréparation : "+timePrep+" \nCuisson :"+cookPrep+"</inf>"
categories = []
for tagsCat in soup.find_all("ul", attrs={"class" : "mrtn-tags-list"}):
for tag in tagsCat.find_all("li", attrs={"class" : "mrtn-tag"}):
categories.append(tag.text)
#Remove punctuation
titles = ['<title>'+''.join(c for c in s if c not in punctuation)+'</title>' for s in titles]
categories = ['<cat>'+''.join(c for c in s if c not in punctuation)+'</cat>' for s in categories]
ingredients = ['<ing>'+''.join(c for c in s if c not in punctuation)+'</ing>' for s in ingredients]
preparations = '<prep>'+preparations+'</prep>'
calories = '<eng>0</eng>'
#Convert to String
strTitles = "\n".join(titles)
strTitles = " ".join(strTitles.split())
strCategories = "\n".join(categories)
strCategories = " ".join(strCategories.split())
strIngredients = "\n".join(ingredients)
#String to write in my Corpus
toWrite = '<rec id='+str(i)+'>\n'+strTitles+'\n'+info+'\n'+strCategories+'\n'+strIngredients+'\n'+preparations+'\n'+calories+'\n</rec>\n'
newCorpus.write(toWrite)
i += 1
lastIdToSave = open('corpusFr/lastIdToSave.txt', 'w')
lastIdToSave.write(str(i-1))
lastIdToSave.close()
if __name__ == "__main__":
searchObj = Search([], 'corpus/arCorpus.txt', 2)
words = ['ﺔﻧﻭﺮﻜﻌﻤﻟا' ,'ﺎﺑﺎﻄﺒﻟا' ,'ﻞﺼﺒﻟا' ,'ﻞﻔﻠﻔﻟا']
stemmed = searchObj.getStemmedText(words)
query = ["ﻖﺘﺴﻔﻟاﻭ" ,"ﺓﺕﻻﻮﻛﻮﺷﻝا" ,"ﻉﺏﺎﺻا"]
keyWords = searchObj.getKeyWords(query)
print(keyWords)