-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataJDM.py
415 lines (310 loc) · 14.9 KB
/
dataJDM.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import os
import hashlib
import urllib.parse
from concurrent.futures import ThreadPoolExecutor, as_completed, wait
from datetime import datetime
from os.path import exists, getmtime
from time import sleep, time
import pickle
import numpy as np
import pandas as pd
import requests
from requests.adapters import HTTPAdapter,Retry,ReadTimeoutError
types_dict = {}
with open("data/types_dict","rb") as fp:
types_dict = pickle.load(fp)
from JDM_objects import Relation_Types, Request_Types, Search_Types, Data_Type
from server_relations import corrected_valitidy
def url_encode_plus(word: str) -> str:
return urllib.parse.quote_plus(word.encode("cp1252")).replace("/","%2F")
def lastmodification_date(path: str) -> datetime:
return datetime.fromtimestamp(getmtime(path))
def oneweek_between(date1 : datetime, date2 : datetime) -> bool:
return (date1-date2).days>=7
def cut_dataJDM(html :str, start : str, end: str) -> str:
start_i = html.find(start)
end_i = html.rfind(end)
return html[start_i+len(start):end_i]
def word_to_md5(word: str) -> str:
return hashlib.md5(word.encode()).hexdigest()
def save(path: str, data: str):
with open(path,"w",encoding="utf-8") as data_file:
data_file.write(data)
def file_exists_case_sensitive(file_path):
directory = os.path.dirname(file_path)
file_name = os.path.basename(file_path)
files = os.listdir(directory)
for file in files:
if file == file_name:
return True
return False
def names_by_nodes(nodes: pd.DataFrame) -> list:
return nodes["name"].values
def jdm_request(converted_word: str, relation: int, request_type: Request_Types) -> str:
s = requests.Session()
retries = Retry(total=50,
backoff_factor=1,
status_forcelist=[ 429 ,500, 502, 503, 504 ])
s.mount('http://', HTTPAdapter(max_retries=retries))
return s.get('https://www.jeuxdemots.org/rezo-dump.php?gotermsubmit=Chercher&gotermrel='+converted_word+'&rel='+str(relation)+request_type.value).text
def save_word_data(word: str, relation: int, request_type: Request_Types) -> None:
converted_word = url_encode_plus(word)
word_md5 = word_to_md5(word)
if request_type == Request_Types.OUTGOING:
path = "saves/"+converted_word+"-ro"+str(relation)+"nodes"+" - "+word_md5+".csv"
else:
path = "saves/"+converted_word+"-ri"+str(relation)+"nodes"+" - "+word_md5+".csv"
if not(exists(path)) or oneweek_between(datetime.now(),lastmodification_date(path)):
#print(f"Enregistrement de {word} ...")
html = jdm_request(converted_word,relation,request_type)
if html.find("MUTED")!=-1 or html.find("Le terme '"+word+"' n'existe pas !")!=-1:
return -1
nodes_data = cut_dataJDM(html,"(Entries) : ","// les types de relations (Relation Types) : ")
if request_type == Request_Types.OUTGOING:
rel_data = cut_dataJDM(html,"relations sortantes : ","// END")
save(path,nodes_data)
save("saves/"+converted_word+"-ro"+str(relation)+" - "+word_md5+".csv",rel_data)
else:
rel_data = cut_dataJDM(html,"// les relations entrantes :","// END")
save(path,nodes_data)
save("saves/"+converted_word+"-ri"+str(relation)+" - "+word_md5+".csv",rel_data)
def save_data(wordlist: list, relation: int, request_type: Request_Types):
futures = []
with ThreadPoolExecutor(max_workers=10) as executor:
futures = [executor.submit(save_word_data,word,relation, request_type) for word in wordlist]
wait(futures)
for future in futures:
if future.result()==-1:
return -1
def read_csv_data(path: str) -> pd.DataFrame:
try:
res = pd.read_csv(path,delimiter=";",quotechar="'",on_bad_lines='skip')
except:
res = None
print("Pas de colonne à lire")
return res
def read_word_rel(word: str, rel: int, request_type: Request_Types, data_type : Data_Type):
word_c = url_encode_plus(word)
word_md5 = word_to_md5(word)
if request_type == Request_Types.OUTGOING:
return read_csv_data("saves/"+word_c+"-ro"+str(rel)+("nodes" if data_type == Data_Type.NODES else "")+" - "+word_md5+".csv")
else:
return read_csv_data("saves/"+word_c+"-ri"+str(rel)+("nodes" if data_type == Data_Type.NODES else "")+" - "+word_md5+".csv")
def names_with_eids(nodes_data: pd.DataFrame, eids: list, size: int) -> list:
names = []
i=0
while len(names)<size and i<len(eids):
eid = eids[i]
nodes = nodes_data[nodes_data["eid"]==eid]["name"].values
if len(nodes)==0:
break
name = nodes.item()
if name[len(name)-1]!="'" and name not in names and name!="'name'":
names.append(name)
i+=1
return names
def research_relation(word: str, obj: str, relation: str, request_type: Request_Types) -> bool:
if save_word_data(word,relation,request_type) == -1:
return -1
if word=="'name'":
return False,None
#print("mot",word)
nodes_st = read_word_rel(word,relation,request_type,Data_Type.NODES)
r_data = read_word_rel(word,relation,request_type,Data_Type.REL)
try:
if obj in nodes_st["name"].values:
objid = nodes_st[nodes_st["name"]==obj]['eid'].item()
rs_data_weight = r_data[r_data["w "]>0]
if objid in r_data["node2"].values:
if objid in rs_data_weight["node2"].values:
return True,word,obj
else:
return False,word,obj
except:
print("Erreur de lecture du fichier")
return False,None
def research_with_names(names: list, obj: str, relation: str) -> bool:
futures = []
with ThreadPoolExecutor(max_workers=len(names)) as executor:
futures = [executor.submit(research_relation,word,obj,relation,Request_Types.OUTGOING) for word in names]
for future in as_completed(futures):
res = future.result()
if res == -1:
return -1
if res[1] != None:
return res[0],res[1],res[2]
return False,None
def research(subject: str, relation: int, obj: str, search_type: Search_Types) -> bool:
#print(subject,relation,obj,search_type)
## Renvoyer False quand DEDUCTION + r_hyp
if search_type == Search_Types.DEDUCTION and relation == Relation_Types.r_hypo.value:
return False,None
if search_type == Search_Types.INDUCTION and relation == int(Relation_Types.r_hypo.value):
relation = int(Relation_Types.r_isa.value)
match search_type:
case Search_Types.INDUCTION:
search_type_rel = "6"
request_type = Request_Types.INGOING
node = "node1"
case _:
if type(search_type)==Search_Types:
search_type_rel = search_type.value
else:
search_type_rel = search_type
request_type = Request_Types.OUTGOING
node = "node2"
nodes_isa_data = read_word_rel(subject,search_type_rel,request_type,Data_Type.NODES)
r_isa_data = read_word_rel(subject,search_type_rel,request_type,Data_Type.REL)
if not(type(nodes_isa_data)==pd.DataFrame) or not(type(r_isa_data)==pd.DataFrame):
return -1
eids = r_isa_data[r_isa_data["w "]>50][node].values
names = names_with_eids(nodes_isa_data,eids,50)
if len(names)==0:
return False,None
#Sprint(search_type,names)
# if save_data(names,relation, Request_Types.OUTGOING)==-1:
# print("ERREUR LORS DE LA SAUVEGARDE \n")
# return -1
research_res = research_with_names(names,obj,relation)
if research_res==-1:
return -1
return research_res
def save_word_relations(word: str, relations: list) -> None:
futures = []
with ThreadPoolExecutor(max_workers=len(relations)) as executor:
futures = [executor.submit(save_word_data,word,relation,request_type) for request_type,relation in relations]
wait(futures)
for future in futures:
if future.result()==-1:
return -1
def nodes_name_with_relation(word :str,relation: int, ceil: int) -> list:
nodes = read_word_rel(word,relation,Request_Types.OUTGOING,Data_Type.NODES)
r = read_word_rel(word,relation,Request_Types.OUTGOING,Data_Type.REL)
r = r[r["w "]>ceil]["node2"].values
nodes = nodes[nodes["eid"].isin(r)]["name"].values
return nodes
def get_refinement(word: str) -> str:
if type(word)==str and word.find(">")!=-1:
return word.split(">")[1]
return ""
def semantic_refinements(subject:str, word: str) -> list:
## Téléchargement des données
if save_word_relations(subject,[(Request_Types.OUTGOING,0),(Request_Types.OUTGOING,24)]) == -1:
return -1
if save_word_relations(word,[(Request_Types.OUTGOING,1),(Request_Types.OUTGOING,0)]) == -1:
return -1
## Lecture des raffinements + Lecture des associés
nodes = read_word_rel(word,Relation_Types.r_raff_sem.value,Request_Types.OUTGOING,Data_Type.NODES)
## Obtention de tous les raffinements
nodes = nodes[nodes["name"]!=word]
refinements_dict = pd.Series(nodes["name"].values,index=nodes["formated name "]).to_dict()
refinements_dict[word+">sens générique"] = word
## Obtention des associés et agent-1 avec poids >10
subject_nodes_associated = nodes_name_with_relation(subject,relation=0,ceil=30)
# nodes_agent_1 = nodes_name_with_relation(subject_md5,subject_c,24,10
for refinement in refinements_dict.values():
if refinement in subject_nodes_associated: # refinement in nodes_agent_1:
return refinement
for refinement in list(refinements_dict.keys()):
if type(refinement)==float:
del refinements_dict[refinement]
ref = get_refinement(refinement)
if ref in subject_nodes_associated:
return word
return refinements_dict
def double_rel_research(word: str, subject: str, relation: int, relation_search: int) -> list:
## Lecture de la relation des deux mots
word_nodes = read_word_rel(word,relation_search,Request_Types.OUTGOING,Data_Type.NODES)
save_word_data(subject,Relation_Types.r_syn.value,Request_Types.OUTGOING)
subject_nodes = read_word_rel(subject,relation_search,Request_Types.OUTGOING,Data_Type.NODES)
word_names = word_nodes["name"].values
subject_names = subject_nodes["name"].values
if len(word_names)>20:
word_names = word_names[0:19]
if len(subject_names)>20:
subject_names = subject_names[0:19]
#print(word_names)
#print(subject_names)
with ThreadPoolExecutor(max_workers=len(word_names)) as executor:
futures = [executor.submit(research_with_names,word_names,subject,relation) for subject in subject_names]
for future in as_completed(futures):
res = future.result()
if res == -1:
return -1
elif res[1] != None:
return res
return False,None
def sentence_validity(sentence: list) -> list:
subject, relation, obj = sentence[0], sentence[1], sentence[2]
# Obtention des données des deux mots
relations = [(Request_Types.OUTGOING,6),(Request_Types.INGOING,6),(Request_Types.OUTGOING,relation),(Request_Types.OUTGOING,Search_Types.SYNONYMUM.value)]
if save_word_relations(subject,relations) == -1:
return -1
# Vérification réponse dans base serveur
corrected_response = corrected_valitidy(sentence)
if corrected_response[1] != None:
print(corrected_response)
return ["bdd",corrected_response]
# Vérification réponse dans relation directe
if relation == int(Relation_Types.r_hypo.value):
res = research_relation(subject,obj,int(Relation_Types.r_isa.value),Request_Types.OUTGOING)
else:
res = research_relation(subject,obj,relation,Request_Types.OUTGOING)
if res[1] != None:
return ["direct",res]
# Vérification dans isa (déduction)/ hypo (induction)
with ThreadPoolExecutor(max_workers=3) as executor:
deduction = executor.submit(research,subject,relation,obj,Search_Types.DEDUCTION)
induction = executor.submit(research,subject,relation,obj,Search_Types.INDUCTION)
# synonyme = executor.submit(double_rel_research,subject,obj,relation,Search_Types.SYNONYMUM.value)
wait([deduction])
deduction_res = deduction.result()
if deduction_res == -1:
return -1
if deduction_res[1] != None:
print("déduction")
return ["r_isa",deduction_res]
else:
wait([induction])
induction_res = induction.result()
if induction_res == -1:
return -1
if induction_res[1] != None:
print("induction")
return ["r_hypo",induction_res]
synonymum_res = double_rel_research(subject,obj,relation,Search_Types.SYNONYMUM.value)
if synonymum_res == -1:
return -1
if synonymum_res[1] == None and relation==int(Relation_Types.r_lieu.value):
return sentence_validity([sentence[2],int(Relation_Types.r_has_part.value),sentence[0]])
else:
return ["r_syn",synonymum_res]
start_time = time()
print("--- %s seconds ---" % (time() - start_time))
def search_good_rel(word: str, subject: str, rel: int, max_rel: int) -> list:
all_rel = [int(r.value) for r in Relation_Types]
find_num = 0
i = 0
results = []
while find_num!=max_rel and i<len(all_rel):
relation = all_rel[i]
res = research(word,rel,subject,relation)
if res!=-1 and res[0]==True:
find_num +=1
results.append([[k for k, v in types_dict.items() if v == str(relation)][0],res])
i+=1
return results
def other_relations_between(subject: str, object: str) -> list:
for relation1 in range(0,32):
save_word_data(subject,relation1,Request_Types.OUTGOING)
nodes = read_word_rel(subject,relation1,Request_Types.OUTGOING,Data_Type.NODES)
names = names_by_nodes(nodes)
for name in names:
for relation2 in range(0,32):
if name.find("_")==-1 or name.find("::"):
save_word_data(name,relation2,Request_Types.OUTGOING)
res =research_relation(name,object,relation2,Request_Types.OUTGOING)
if res[1] != None:
return relation1,relation2,name,res
print(relation1,names)
## Homme r_lieu restaurant oui car homme r_agent-1 manger et manger r_lieu restaurant