-
Notifications
You must be signed in to change notification settings - Fork 5
/
P2N-TrizifyerE.py
304 lines (204 loc) · 8.96 KB
/
P2N-TrizifyerE.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
"""
Created on Fri Aug 9 14:01:22 2019
@author: cherrabi
"""
from P2N_Lib import GenereListeFichiers # import
from P2N_Config import LoadConfig #
import os # importation de la bibliothèque os qui sert à
from textblob import TextBlob # importation de textblob outil liguistique
from nltk.corpus import stopwords
import nltk
from sematch.semantic.similarity import WordNetSimilarity
from nltk.corpus import wordnet as wn
import pandas as pd
import re
import shutil
import sys
from nltk.corpus import stopwords
import numpy as np
import pandas as pd
import re
import umap
import matplotlib.pyplot as plt
import seaborn as sns
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.tokenize import word_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
import string
import gensim
from gensim import corpora
from gensim.corpora import Dictionary
from sklearn.decomposition import TruncatedSVD
import os
import re
import codecs
import logging
import time
from operator import add
from textblob import TextBlob # importation de textblob outil liguistique
from nltk.corpus import stopwords
from P2N_Lib import LoadBiblioFile
from P2N_Lib import GenereListeFichiers
from P2N_Config import LoadConfig
from nltk.corpus import wordnet
import spacy
import en_core_web_sm
from itertools import product
ListeBrevet = [] # The patent List
stop_words = set(stopwords.words('english'))
configFile = LoadConfig()
requete = configFile.requete
BiblioPath = configFile.ResultBiblioPath
GatherContent = configFile.GatherContent
GatherBiblio = configFile.GatherBiblio
GatherPatent = configFile.GatherPatent
GatherFamilly = configFile.GatherFamilly
IsEnableScript = configFile.GatherIramuteq
ResultBiblioPath = configFile.ResultBiblioPath
ndf = configFile.ndf
DataBrevet = LoadBiblioFile(BiblioPath, ndf)
InventorList = []
InventorList = DataBrevet['brevets']
# preparing parsing data for indicator scientific publication and inventive production
inventor_list = [auth['inventor'] for auth in DataBrevet['brevets']]
label_list = [auth['label'] for auth in DataBrevet['brevets']]
title_list = [auth['title'] for auth in DataBrevet['brevets']]
dict = { 'label' : label_list, 'title' : title_list, 'inventor' : inventor_list }
df = pd.DataFrame(dict)
df.to_csv("data_inventor.csv", header=False, index=False)
temporPath = configFile.temporPath
ResultAbstractPath = configFile.ResultAbstractPath
#ResultClaimsPath = configFile.ResultClaimsPath
#add here templateFlask directory local to the request directory normalize path for windows
ResultPathContent= configFile.ResultContentsPath.replace('\\', '/' )
ResultTemplateFlask = os.path.join(ResultPathContent,'Trizifiier').replace('\\','/')
bigram_measures = nltk.collocations.BigramAssocMeasures()
trigram_measures = nltk.collocations.TrigramAssocMeasures()
if not os.path.exists(ResultTemplateFlask): #creation des dossiers templates et dataFormat
os.mkdir(ResultTemplateFlask)
if not os.path.exists(ResultTemplateFlask+'/templates'): #creation des dossiers templates et dataFormat
os.mkdir(ResultTemplateFlask+'/templates')
if not os.path.exists(ResultTemplateFlask+'/DataFormat'): #creation des dossiers templates et dataFormat
os.mkdir(ResultTemplateFlask+'/DataFormat')
#add here tempo dir
temporar = configFile.temporPath
wns = WordNetSimilarity()
i=0
# build file list
#direct = os.path.normpath(ResultBiblioPath)
#direct = os.path.normpath(ResultClaimsPath)
direct = os.path.normpath(ResultAbstractPath)
# affiche url de chaque documents txt dans le dossier de la requete inseree , EN tous les url dossier pour en ect...
Fr, En, Unk = GenereListeFichiers(direct)
def convert_tag(tag):
tag_dict = {'N': 'n', 'J': 'a', 'R': 'r', 'V': 'v'}
try:
return tag_dict[tag[0]]
except KeyError:
return None
CountFile_R = 0
CountFile_W = 0
FichierOrg={}
# compter les nombre de caractere de EN
#if len(En)
PSW = [] # liste de mots vide à compléter au fur et à mesure des recherches
# minimalistic HTML for result file in html format
dataF = """""" # va contenir tous les abstracts du dossier de la requete
import codecs
#DejaVus = dict()
f=open(ResultTemplateFlask + '/DataFormat/FileDataAnalysisTrizWikiE.csv','w')
entetes = [
u'i',
u'label',
u'classe',
u'Action',
u'indiceSimAction',
u'abstract',
u'urlEspacenet'
]
ligneEntete=",".join(entetes)+"\n"
f.write(ligneEntete)
d= pd.read_csv("trizOxfordData.csv",delimiter=";")
dnew= pd.read_csv("FileTrizNewList.csv",delimiter=",")
classes = pd.DataFrame(dnew,columns=['Ref_classe'])
classes_syn = pd.DataFrame(dnew,columns=['syn_classe'])
classesUnique = classes.drop_duplicates(keep = False)
expansionTriz = classes_syn.drop_duplicates(keep = False)
tal = spacy.load('en_core_web_sm')
#lecture des fichiers txt en boucle et placement element dans dataF
for fic in En:
with codecs.open(fic, 'r', 'utf8') as File:
dataF = File.readlines() #single File ne pas lire la première ligne de l'abstract
# dataF = '\n'.join(dataF)
# FichierOrg = dataF
abstract = '\n'.join(dataF[1:])
NumberBrevet= fic.split('-')[1]
#NumberBrevet=NumberBrevet.replace('*Label_','')
NumberBrevet=NumberBrevet.replace('.txt','')
#sys.exit(0)
# tokenization
abstract = re.sub("[^a-zA-Z#]", " ",str(abstract))
brevet = tal(abstract)
#Blob = TextBlob(abstract)
#wordlist=Blob.words #should give best results@ DR
# remove stop-words and words less 3 caracters
filtered_sentence = [mot.lemma_ for mot in brevet if mot.pos_ == "NOUN" or mot.pos_ == "VERB"]
#for w in wordlist:
#if w not in stop_words and len(w) > 3:
#filtered_sentence.append(w)
#Document-Term Matrix
#print(filtered_sentence)
#print(resultType)
urlEspacenet="https://worldwide.espacenet.com/searchResults?submitted=true&locale=fr_EP&DB=EPODOC&ST=advanced&TI=&AB=&PN="+format(NumberBrevet)
matriceListe = []
matricelistePaire = []
matricelistePaireSort=[]
matricelistePaireAction = []
matricelistePaireObject = []
for classe in expansionTriz.keys() :
ExpansionClasse = expansionTriz[classe]
allsyns1 = set(ss for word in ExpansionClasse for ss in wordnet.synsets(word))
allsyns2 = set(ss for word in filtered_sentence for ss in wordnet.synsets(word))
best = max((wordnet.wup_similarity(s1, s2) or 0, s1, s2) for s1, s2 in product(allsyns1, allsyns2))
#print("allsyns1 ========",allsyns1)
#print("\n")
#print("allsyns2========",allsyns2)
print("best: ", best)
print("\n")
sys.exit()
f.close()
sys.exit()
#open file data semantic classification
d= pd.read_csv(ResultTemplateFlask + "/DataFormat/FileDataAnalysisTrizWikiE.csv")
df = pd.DataFrame(d,columns=['i','label','Term','Action','indiceSimAction','abstract','urlEspacenet'])
df.to_csv(ResultTemplateFlask + '/DataFormat/tableauTriE.csv')
sys.exit(0) # je veux le csv généré ici, car a partir de cette ligne je vais changer pour afficher les classes autrement
# sorted data by id and term ascending
dfmax = df.sort_values(by=['i','Term','indiceSimAction'],ascending=[True,True,False])
dfmax.to_csv(ResultTemplateFlask + '/DataFormat/tableauTri.csv')
# selected just top indice similiraty for term / action
dresult = dfmax.drop_duplicates(['Term'],keep='first')
dresult.to_csv(ResultTemplateFlask + '/DataFormat/tableauDrop.csv')
dresultmaxI=dresult.sort_values(by='indiceSimAction')
# create file formated datas to use in tabulator html
dresultmaxI.to_csv(ResultTemplateFlask + '/DataFormat/resultatParserV2.csv')
dd=pd.read_csv(ResultTemplateFlask + '/DataFormat/resultatParserV2.csv')
dff = pd.DataFrame(dd,columns=['i','label','Action','Term','Patent Tags','indiceSimAction','abstract','urlEspacenet'])
dfjson= pd.DataFrame(dd,columns=['label','Action','Term','Patent Tags','abstract','urlEspacenet'])
dfjson.to_json(ResultTemplateFlask +'/DataFormat/caraTrizWikisemantic.json', orient='records', lines=False)
#shutil.copyfile("templates/sources", ResultTemplateFlask+"/sources")
ResFolder = configFile.ResultPath.replace('\\', '//')
ResFolder = ResFolder.replace('//','/')
shutil.copy("templates/P2N-Trizifyer-semantic.html", ResFolder)
#add variable vars json_data datatable
src = open(ResultTemplateFlask +'/DataFormat/caraTrizWikisemantic.json','r')
lineadd = " var json_data = "
online=src.readlines()
online.insert(0,lineadd)
src.close
src = open(ResultTemplateFlask +'/DataFormat/caraTrizWikisemantic.json','w')
src.writelines(online)
src.close