-
Notifications
You must be signed in to change notification settings - Fork 0
/
mono_corpus.py
1733 lines (1373 loc) · 71.1 KB
/
mono_corpus.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askopenfilename
from tkinter import scrolledtext
from rake_nltk import Rake
import yake
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
import spacy
import Extraction as fcns
from tkinter import ttk
from tkinter.filedialog import askopenfilenames
import os
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import pstats
from matplotlib.pyplot import switch_backend
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import regex as re
import string
import numpy as np
import nltk.data
import re
from nltk.stem import WordNetLemmatizer
from nltk import word_tokenize, sent_tokenize, pos_tag
from sklearn.feature_extraction.text import TfidfVectorizer
import math
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.collocations import BigramAssocMeasures, BigramCollocationFinder, QuadgramAssocMeasures
from nltk.collocations import TrigramAssocMeasures, TrigramCollocationFinder, QuadgramCollocationFinder
from tkinter import filedialog
from tkinter.filedialog import askdirectory
import ClassNettoyage as cn
fenetre = Tk()
fenetre.geometry('1350x680+0+0')
fenetre.title('Interface Extraction des termes à partir d\'un seul CORPUS')
fenetre.configure(bg='light blue')
class App(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.counter = 0
self.create_widgets()
def create_widgets(self):
#self.button = Button(self.master, text="Cliquez ici", command=self.increment_counter)
#self.button.pack()
self.label = Label(self.master, text="Compteur : 0")
self.label.pack()
def increment_counter(self):
self.counter += 1
self.label.config(text="Compteur : {}".format(self.counter))
#root = Tk()
app = App(master=fenetre)
#app.mainloop()
def nettoyer(texte):
# Creer l'objet et nettoyer
net = cn.Nettoyage(texte, "Ponctuation.txt", "Stopword_alir3z4.txt")
net.nettoyer()
return net.chaine_net
def extraire_termes_rake(contenus):
ponctuation = {'.', ',', ';', ':', '-', '_', '(', ')', '\'', '"', '{', '}', ']', '[', '/', '\\', '|', '([' '])',
'!', '?', '@', '#', '$', '%', '^', '&', '*', '+', '=', '*', '<', '>',
'),', ').', '.)', ').', ')?', ');'}
# Créer un extracteur
extracteur = Rake(language='english', include_repeated_phrases=False, max_length=4,
stopwords=set(open('Stopword_alir3z4.txt', encoding="utf8").read().split()),
punctuations=ponctuation)
# Parcourir les contenus de fichiers
termes_par_texte = []
for contenu in contenus:
extracteur.extract_keywords_from_text(contenu)
termes_importants = extracteur.get_ranked_phrases_with_scores()[:70]
termes = [terme[1] for terme in termes_importants]
termes_par_texte.append(termes)
return termes_par_texte
def extraire_termes_yake(contenus):
termes_par_texte = []
kw_extractor = yake.KeywordExtractor(top=70)
for contenu in contenus:
keywords = kw_extractor.extract_keywords(contenu)
termes = [terme[0] for terme in keywords]
termes_par_texte.append(termes)
return termes_par_texte
def extraire_termes_TfIdf(contenus):
termes_par_texte = []
for contenu in contenus:
documents = [
contenu,
"Home gardens key to improved nutritional well-being Malnutrition not only results in increased mortality and health problems including infectious diseases, mental retardation and blindness, it is also responsible for loss of human capital and work productivity. About 39 percent of Lao PDR's population is below the national income poverty line and 22 percent are food insecure. The average per capita annual income of US$370 in 1997 instead of increasing towards the targeted US$500 in 2000, declined to US$350 and then further to US$331 by 2003 as a result of inflation. I m p r o v e d nutritional standards lead to improved health, well-being and development o p p o r t u n i t i e s . Evidence from Asian countries, particularly Viet Nam4 and some other countries5 in the region, shows that home gardens in combination with nutrition education can make a highly effective contribution towards nutrition improvement among rural poor households. Aware of the negative impact of high malnutrition levels on the national development potential, and in keeping with its commitment at the 1992 International Conference on Nutrition (ICN) and to the UN Millennium Development Goals (MDGs), the Government of Lao PDR is giving priority to diversifying food consumption to ensure a more balanced diet to its people. As this objective could not be achieved from local expertise and resources alone, the Government requested assistance from FAO. Under the US$332 000 Technical Cooperation Project (TCP) Promotion of home gardens for i m p r o v e d nutritional well-being, TCP/LAO/2902 (A), FAO collaborated with the Government of Lao PDR to promote home gardens for improving nutritional well-being of rural communities in Lao PDR. The pilot project, which commenced operations in February 2003 and concluded in August 2004, has developed and fine-tuned a suitable approach for household food security and nutrition improvement capable of replication at national level. The project included a creative participatory planning and implementation process involving local communities, district-level authorities as well as technical experts and policy makers in the Department of Agriculture (DOA). It has provided an integrated package of home gardening inputs and nutrition education to target households and communities in four villages6 of Vientiane and Bolikhamxay Provinces as well as in Vientiane Municipality. A total of 204 home gardens and four community gardens have been established. The resulting diversified diet is expected to reduce malnutrition and improve health, especially of under-five-year-old children and women of reproductive age. The project also promoted 59 micronutrient-rich foods through the home gardening programme. Another project objective was additional income generation for rural families, not only through direct sale of home produce but also from indirect savings as a result of reduced health care expenditure. The project was implemented in close collaboration with the Ministry o f Health (MOH), Ministry of Education (MOE), Lao Women's Union (LWU) and international non-governmental organizations (NGOs).",
"Home gardens key to improved nutritional well-being A significant outcome of the project was the development of provincial, district and community-level capacities for implementation and management of home gardening and nutrition improvement programmes. This was done through training of trainers (TOTs) and in-service training (IST) by FAO experts on key modules of nutrition, horticulture, small livestock and fisheries for provincial and district officials of the Department of Agriculture (DOA). A post-project evaluation found increased production of vegetables, fruits, poultry and fish among the target households and awareness created for greater consumption of home-grown produce. Comparison with baseline estimates found that moderate and severe undernutrition rates for under-five-year-old children had declined from 23 to 15.9 percent and 9.5 to 2.3 percent respectively."
"The objective of TCP/LAO/2902 (A) pilot project Promotion of Hone Gardens for Improved Nutritional Well-Being was to develop a model for household nutrition garden production, including small livestock and aquaculture. Covering a population of 1 000 people from about 200 households in four villages, the project especially targeted families with children less than five years old. The overall project objective was to reduce malnutrition and improve nutritional well-being of the Lao population through increased production and consumption of nutritious food with special emphasis on a micronutrient-rich diet. The immediate objective was to develop a nationally replicable model for increased and diversified household food production and consumption by rural families, in combination with nutrition education. The project had the following specific objectives"
"Increase the amount and variety of nutritious food for project households with special emphasis on food rich in micronutrients. Carry out an intensive public nutrition education campaign aimed at improving and diversifying family food consumption with special emphasis on children under five years old and women of reproductive age. Establish a network for collaboration between the Ministry of Agriculture and Forestry (MAF), Ministry of Health (MOH), Special Programme for Food Security (SPFS) team, FAO and relevant development partners. Evaluate the results and impact of the project and develop model home gardens for improved nutritional well-being and suitable for national implementation."
]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(documents)
feature_names = vectorizer.get_feature_names_out()
scores = X.sum(axis=0).A1
termes_importants = [feature_names[i] for i in scores.argsort()[:70]]
termes_par_texte.append(termes_importants)
return termes_par_texte
def extraction_methode(contenus,nlp):
#nlp = spacy.load('en_core_web_lg')
# Récupérer le texte de la zone_texte1
termes_par_texte = []
i=0
for contenu in contenus:
i=i+1
doc = nlp(contenu)
#Decouper le texte en phrases
sentences = list(doc.sents)
extraction = fcns.Extraction(sentences)
extraction.extractTerms(70)
app.increment_counter()
print(f"termes {i} extraits")
termes = extraction.getTerms()
termes_par_texte.append(termes)
return termes_par_texte
def extraction_nmethode(repertoire,rep):
path = repertoire
all_files = os.listdir(path+"docsutf8")
all_keys = os.listdir(path + "keys")
all_documents = []
all_keys = []
all_files_names = []
# Reading the files and storing them in a list.
for i, fname in enumerate(all_files):
with open(path+'docsutf8/'+fname, encoding="utf8") as f:
lines = f.readlines()
key_name = fname[:-4]
with open(path+'keys/'+key_name+'.key', encoding="utf8") as f:
k = f.readlines()
all_text = ' '.join(lines)
keyss = ' '.join(k)
all_documents.append(all_text)
all_keys.append(keyss.split("n"))
all_files_names.append(key_name)
print(f"boucle 1 {i+1} fois")
def preprocess_text(text):
"""
It removes numbers, http, punctuation, converts to lower case, and removes short words.
:param text: the text to be preprocessed
"""
text = remove_numbers(text)
text = remove_http(text)
text = remove_punctuation(text)
text = convert_to_lower(text)
text = remove_short_words(text)
return text
def convert_to_lower(text):
"""
It takes a string as input and returns a lowercase version of that string
:param text: The text to be converted to lowercase
:return: The text is being returned in lower case.
"""
return text.lower()
def remove_numbers(text):
"""
It takes a string as input, removes all numbers from it, and returns the resulting string
:param text: The text that you want to remove numbers from
:return: The text is being returned without any numbers.
"""
text = re.sub(r'[0-9]+' , '', text)
return text
def remove_http(text):
"""
It takes a string as input, and returns a string with all the URLs removed
:param text: The text to be cleaned
:return: A string with the http removed.
"""
text = re.sub("https?://t.co/[A-Za-z0-9]*", ' ', text)
return text
def remove_short_words(text):
"""
It takes a string as input, and returns a string with all words of length 1 or 2 removed
:param text: The text that you want to clean up
:return: The text is being returned.
"""
text = re.sub(r'bw{1,2}b', '', text)
return text
def remove_punctuation(text):
"""
It takes a string as input, and returns a string with all punctuation removed
:param text: the text to be processed
"""
text = text.replace("-", " ")
punctuations = '''!()[]{};«№»:'",`./?@=#$-(%^)+&[*_]~'''
no_punctuation = ""
for char in text:
if char not in punctuations:
no_punctuation = no_punctuation + char
return no_punctuation
def remove_white_space(text):
"""
It removes white space from the beginning and end of a string
:param text: The text to be cleaned
:return: The text is being returned.
"""
text = text.strip()
return text
#function that returns list of sipmle terms in the doc
def toknizing_termesSimple(text, stop_words):
tokens = word_tokenize(text)
## Remove Stopwords from tokens
result = [i for i in tokens if not i in stop_words]
return result
#function that returns list of stop wordin in the doc
def toknizing_stopwords(text, stop_words):
#stop_words = set(stopwords.words('english'))
tokens = word_tokenize(text)
## Remove Stopwords from tokens
result = [i for i in tokens if i in stop_words]
return result
def cal_n(term, N):
"""
It counts the number of documents a term appears in
:param term: the term you want to calculate the frequency of
:param N: number of documents
:return: The number of times the term appears in the corpus.
"""
n = 0
for i in range(N):
found = False
for j in range(len(termes[i])):
if str(term) == str(termes[i][j]):
found = True
if found:
n = n + 1
return(n)
def cal_tf(term, doc, N):
"""
It takes a term, a document, and the number of documents in the corpus, and returns the term
frequency of the term in the document
:param term: the term we're looking for
:param doc: the document number
:param N: number of documents
:return: The number of times a term appears in a document.
"""
tf = 0
len_doc = len(termes[doc])
for j in range(len_doc):
if str(term) == str(termes[doc][j]):
tf = tf + 1
return(tf)
def cal_pond(N, n, tf, len_doc, moy_len_doc):
"""
The function takes the number of documents in the corpus, the number of documents containing the
term, the term frequency in the document, the length of the document, and the average length of
documents in the corpus. It returns the TF-IDF score for the term in the document(more so ponderation)
:param N: total number of documents in the collection
:param n: number of documents containing the term
:param tf: term frequency
:param len_doc: the length of the document
:param moy_len_doc: the average length of a document in the corpus
:return: the pondered score of a term in a document.
"""
lengs = 1.5 * (len_doc / moy_len_doc)
num1 = tf/(tf + 0.5 + lengs)
num2 = math.log((N + 0.5)/(n+1)) / math.log(N + 1)
pond = 0.4 + 0.6 * num1 * num2
return(pond)
def cal_freq_corpus(term):
"""
It counts the number of times a term appears in the corpus
:param term: the term we want to calculate the frequency of
:return: The number of times a term appears in the corpus.
"""
f = 0
for i in range(N):
for j in range(len(termes[i])):
if str(term) == str(termes[i][j]):
f = f + 1
return(f)
def cal_proba(term):
"""
> The function `cal_proba` takes a term as input and returns the probability of that term in the
corpus
:param term: the term you want to calculate the probability of
:return: The probability of a term in the corpus.
"""
proba = cal_freq_corpus(term) / num_words_corpus
return(proba)
def cal_p2(term1, term2):
"""
It counts the number of times a 2-gramm appears in the corpus
:param term1: the first term
:param term2: the term that we want to calculate the probability of
:return: The number of times the two terms appear together in the same document.
"""
p = 0
for i in range(N):
for j in range(len(termes[i])-1):
k = j+1
if (term1 == termes[i][j]) and (term2 == termes[i][k]):
p = p + 1
return(p)
def cal_p3(term1, term2, term3):
p = 0
for i in range(N):
for j in range(len(termes[i])-2):
k = j + 1
l = j + 2
if (term1 == termes[i][j]) and (term2 == termes[i][k]) and (term3 == termes[i][l]):
p = p + 1
return(p)
def cal_PMI(term1, term2):
"""
The function takes two terms as input and returns the PMI of the two terms.
:param term1: the first term
:param term2: the word you want to find the PMI of
:return: The PMI of the two terms.
"""
num1 = cal_p2(term1, term2) * N #um_words_corpus
num2 = cal_freq_corpus(term1) * cal_freq_corpus(term2)
PMI = math.log(num1 / num2)
return(PMI)
def lang_simple(line):
line = line.split(" ")
lang = 0
for i in range(len(line)):
if line[i] in simplelist:
lang += 1
return lang
def som_simple(line):
somme = 0
line = line.split(" ")
for i in range(len(line)):
if line[i] in simplelist:
j = 0
while True:
p = psimplelist[j][1]
j += 1
if line[i] == psimplelist[j-1][0]:
#print(psimplelist[j-1][0])
break
somme = somme + p
return somme
def nbr_doc(line):
n = 0
line = line.split(" ")
if len(line) == 2:
for i in range(N):
found = False
for j in range(len(termes[i])-1):
if (line[0] == termes[i][j]) and (line[1] == termes[i][j+1]):
found = True
if found:
n = n + 1
elif len(line) == 3:
for i in range(N):
found = False
for j in range(len(termes[i])-2):
if (line[0] == termes[i][j]) and (line[1] == termes[i][j+1]) and (line[2] == termes[i][j+2]):
found = True
if found:
n = n + 1
elif len(line) == 4:
for i in range(N):
found = False
for j in range(len(termes[i])-3):
if (line[0] == termes[i][j]) and (line[1] == termes[i][j+1]) and (line[2] == termes[i][j+2]) and (line[3] == termes[i][j+3]):
found = True
if found:
n = n + 1
return n
def tf_comp(line, doc):
n = 0
line = line.split(" ")
if len(line) == 2:
for j in range(len(termes[doc])-1):
if (line[0] == termes[doc][j]) and (line[1] == termes[doc][j+1]):
n = n + 1
elif len(line) == 3:
for j in range(len(termes[doc])-2):
if (line[0] == termes[doc][j]) and (line[1] == termes[doc][j+1]) and (line[2] == termes[doc][j+2]):
n = n + 1
elif len(line) == 4:
for j in range(len(termes[doc])-3):
if (line[0] == termes[doc][j]) and (line[1] == termes[doc][j+1]) and (line[2] == termes[doc][j+2]) and (line[3] == termes[doc][j+3]):
n = n + 1
return n
def pond_comp(l, p, som):
num1 = 1 - (1/(l+1))
num2 = (1/(l+1)) * som
pond = num1 + p + num2
return pond
def creerRepertoire(path):
if not os.path.exists(path):
os.mkdir(path)
_log2 = lambda x: math.log(x, 2.0)
N = len(all_documents)
stop_words = set(stopwords.words('english'))
termes = []
# Preprocessing the text and tokenizing it.
s = 0
for i in range(N):
all_documents[i] = preprocess_text(all_documents[i])
termes.append(word_tokenize(all_documents[i]))
len_doc = len(termes[i])
s = s + len_doc
moy_len_doc = s / N
#print(moy_len_doc)
print(f"boucle preprocess {i+1} fois")
# Counting the number of words in the corpus.
num_words_corpus = 0
for i in range(N):
len_doc = len(termes[i])
for j in range(len_doc):
num_words_corpus = num_words_corpus + 1
#print(num_words_corpus)
for i in range(N):
# Tokenizing the terms and stopwords.
termes_simple = toknizing_termesSimple(all_documents[i], stop_words)
stopwords_simple = toknizing_stopwords(all_documents[i], stop_words)
termes_simple = list(dict.fromkeys(termes_simple))
stopwords_simple = list(dict.fromkeys(stopwords_simple))
#print(termes_simple)
#print("\n")
#print(stopwords)
#print("\n")
#chemin = rep + "Inspec\\terms\\newmethod\\termes\%s.txt"
#open (chemin%(all_files_names[i]), "w")
# Calculating the term frequency & ponderation of each term in each document.
#file_simple = open((rep+"Inspec\\terms\\newmethod\\termes\%s.txt")%(all_files_names[i]), "w", encoding="utf8")
creerRepertoire(rep + "Methode Harrathi")
creerRepertoire(rep + "Methode Harrathi/termes")
creerRepertoire(rep + "Methode Harrathi/ptermes")
file_simple = open((rep + "Methode Harrathi\\termes\%s.txt") % (all_files_names[i]), "w", encoding="utf8")
file_psimple = open((rep+"Methode Harrathi\ptermes\%s.txt")%(all_files_names[i]), "w", encoding="utf8")
for x1 in termes_simple:
n = cal_n(x1, N)
tf = cal_tf(x1, i, N)
len_doc = len(termes[i])
p = cal_pond(N, n, tf, len_doc, moy_len_doc)
psimple = cal_proba(x1)
chaine = x1 + " , " + str(p) + "\n"
file_simple.write(chaine)
chaine = x1 + " , " + str(psimple) + "\n"
file_psimple.write(chaine)
file_simple.close()
file_psimple.close()
creerRepertoire(rep + "Methode Harrathi/stopwords")
creerRepertoire(rep + "Methode Harrathi/pstopwords")
# Calculating the frequency & ponderation of each stopword in each document.
file_stopwords = open((rep+"Methode Harrathi\stopwords\%s.txt")%(all_files_names[i]), "w")
file_pstopwords = open((rep+"Methode Harrathi\pstopwords\%s.txt")%(all_files_names[i]), "w")
for x2 in stopwords_simple:
n = cal_n(x2, N)
tf = cal_tf(x2, i, N)
len_doc = len(termes[i])
p = cal_pond(N, n, tf, len_doc, moy_len_doc)
pstopword = cal_proba(x2)
chaine = x2 + " , " + str(p) + "\n"
file_stopwords.write(chaine)
chaine = x2 + " , " + str(pstopword) + "\n"
file_pstopwords.write(chaine)
file_stopwords.close()
file_pstopwords.close()
print(f"boucle 3 {i+1} fois")
for i in range(N):
file_psimple = open((rep+"Methode Harrathi\ptermes\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
num_file_psimple = sum(1 for line in file_psimple)
file_psimple.close()
file_psimple = open((rep+"Methode Harrathi\\ptermes\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
psimplelist = []
for j in range(0,num_file_psimple):
line = file_psimple.readline().strip().split(" , ")
line[1] = np.double(line[1])
psimplelist.append(line)
file_psimple.close()
file_pstopwords = open((rep+"Methode Harrathi\pstopwords\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
num_file_pstopwords = sum(1 for line in file_pstopwords)
file_pstopwords.close()
file_pstopwords = open((rep+"Methode Harrathi\\pstopwords\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
pstopwordslist = []
for j in range(0,num_file_pstopwords):
line = file_pstopwords.readline().strip().split(" , ")
line[1] = np.double(line[1])
pstopwordslist.append(line)
file_pstopwords.close()
creerRepertoire(rep+"Methode Harrathi\\2gramm")
file_2gramm = open((rep+"Methode Harrathi\\2gramm\%s.txt")%(all_files_names[i]), "w", encoding="utf8")
bigram_measures = BigramAssocMeasures()
finder2 = BigramCollocationFinder.from_words(termes[i])
#finder.apply_word_filter(lambda x: x in stop_words)
word_filter2 = lambda w1, w2: w1 in stop_words
finder2.apply_ngram_filter(word_filter2)
finder2.apply_freq_filter(1)
#print('word1 , word2, PMI')
list2gramm = []
for row in finder2.score_ngrams(bigram_measures.pmi):
data = (*row[0],row[1])
list2gramm.append(row[0])
if len(data[0]) > 0 and len(data[1]) > 0:
if data[1] in stop_words:
datalist = list(data)
#print(datalist)
j = 0
while True:
pm1 = psimplelist[j][1]
j += 1
if datalist[0] == psimplelist[j-1][0]:
#print(psimplelist[j-1][0])
break
j = 0
while True:
pm2 = pstopwordslist[j][1]
j += 1
if datalist[1] == pstopwordslist[j-1][0]:
#print(pstopwordslist[j-1][0])
break
datalist[2] = _log2((2**(datalist[2]) * pm2) / pm1)
datalist = tuple(datalist)
file_2gramm.write(str(datalist))
file_2gramm.write("\n")
else:
file_2gramm.write(str(data))
file_2gramm.write("\n")
file_2gramm.close()
creerRepertoire(rep + "Methode Harrathi\\3gramm")
file_3gramm = open((rep+"Methode Harrathi\\3gramm\%s.txt")%(all_files_names[i]), "w", encoding="utf8")
trigram_measures = TrigramAssocMeasures()
finder3 = TrigramCollocationFinder.from_words(termes[i])
#finder.apply_word_filter(lambda x: x in stop_words)
word_filter3 = lambda w1, w2, w3: (w1, w2) not in list2gramm
finder3.apply_ngram_filter(word_filter3)
finder3.apply_freq_filter(2)
#print('word1 , word2, PMI')
list3gramm = []
for row in finder3.score_ngrams(trigram_measures.pmi):
data = (*row[0],row[1])
list3gramm.append(row[0])
if len(data[0]) > 0 and len(data[1]) > 0:
if data[2] in stop_words:
datalist = list(data)
#print(datalist)
j = 0
while True:
pm2 = pstopwordslist[j][1]
j += 1
if datalist[2] == pstopwordslist[j-1][0]:
#print(pstopwordslist[j-1][0])
break
pm1 = cal_p2(datalist[0],datalist[1]) / num_words_corpus
datalist[3] = _log2((2**(datalist[3]) * pm2) / pm1)
datalist = tuple(datalist)
#print(datalist)
file_3gramm.write(str(datalist))
file_3gramm.write("\n")
else:
#print(data)
file_3gramm.write(str(data))
file_3gramm.write("\n")
file_3gramm.close()
creerRepertoire(rep + "Methode Harrathi\\4gramm")
file_4gramm = open((rep+"Methode Harrathi\\4gramm\%s.txt")%(all_files_names[i]), "w", encoding="utf8")
quadgram_measures = QuadgramAssocMeasures()
finder4 = QuadgramCollocationFinder.from_words(termes[i])
#finder.apply_word_filter(lambda x: x in stop_words)
word_filter4 = lambda w1, w2, w3, w4: (w1, w2, w3) not in list3gramm
finder4.apply_ngram_filter(word_filter4)
finder4.apply_freq_filter(2)
#print('word1 , word2, PMI')
for row in finder4.score_ngrams(quadgram_measures.pmi):
data = (*row[0],row[1])
if len(data[0]) > 0 and len(data[1]) > 0:
if data[3] in stop_words:
datalist = list(data)
#print(datalist)
j = 0
while True:
pm2 = pstopwordslist[j][1]
j += 1
if datalist[3] == pstopwordslist[j-1][0]:
#print(pstopwordslist[j-1][0])
break
pm1 = cal_p3(datalist[0],datalist[1],datalist[2]) / num_words_corpus
datalist[4] = _log2((2**(datalist[4]) * pm2) / pm1)
datalist = tuple(datalist)
#print(datalist)
file_4gramm.write(str(datalist))
file_4gramm.write("\n")
else:
#print(data)
file_4gramm.write(str(data))
file_4gramm.write("\n")
file_4gramm.close()
#print("fin")
i = i+1
print(f"boucle 4 ngrammes {i} fois")
def takeLast(elem):
return np.double(elem[-1])
for i in range(N):
len_doc = len(termes[i])
file_simple = open((rep+"Methode Harrathi\\termes\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
num_file_simple = sum(1 for line in file_simple)
file_simple.close()
file_simple = open((rep+"Methode Harrathi\\termes\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
simplelist = []
doclist = []
for j in range(0,num_file_simple):
line = file_simple.readline().strip().split(" , ")
line[1] = np.double(line[1])
doclist.append(line)
simplelist.append(line[0])
file_simple.close()
file_psimple = open((rep+"Methode Harrathi\ptermes\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
num_file_psimple = sum(1 for line in file_psimple)
file_psimple.close()
file_psimple = open((rep+"Methode Harrathi\\ptermes\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
psimplelist = []
for j in range(0,num_file_psimple):
line = file_psimple.readline().strip().split(" , ")
line[1] = np.double(line[1])
psimplelist.append(line)
file_psimple.close()
file_2gramm = open((rep+"Methode Harrathi\\2gramm\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
num_file_2gramm = sum(1 for line in file_2gramm)
file_2gramm.close()
file_2gramm = open((rep+"Methode Harrathi\\2gramm\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
for j in range(0,num_file_2gramm):
line = file_2gramm.readline().strip().replace("(","").replace(")","").replace("', '"," ").replace("'","").split(", ")
n = nbr_doc(line[0])
tf = tf_comp(line[0], i)
p = cal_pond(N, n, tf, len_doc, moy_len_doc)
l = lang_simple(line[0])
som = som_simple(line[0])
pond = pond_comp(l, p, som)
line[1] = pond
doclist.append(line)
file_2gramm.close()
file_3gramm = open((rep+"Methode Harrathi\\3gramm\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
num_file_3gramm = sum(1 for line in file_3gramm)
file_3gramm.close()
file_3gramm = open((rep+"Methode Harrathi\\3gramm\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
for j in range(0,num_file_3gramm):
line = file_3gramm.readline().strip().replace("(","").replace(")","").replace("', '"," ").replace("'","").split(", ")
n = nbr_doc(line[0])
tf = tf_comp(line[0], i)
p = cal_pond(N, n, tf, len_doc, moy_len_doc)
l = lang_simple(line[0])
som = som_simple(line[0])
pond = pond_comp(l, p, som)
line[1] = pond
doclist.append(line)
file_3gramm.close()
file_4gramm = open((rep+"Methode Harrathi\\4gramm\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
num_file_4gramm = sum(1 for line in file_4gramm)
file_4gramm.close()
file_4gramm = open((rep+"Methode Harrathi\\4gramm\%s.txt")%(all_files_names[i]), "r", encoding="utf8")
for j in range(0,num_file_4gramm):
line = file_4gramm.readline().strip().replace("(","").replace(")","").replace("', '"," ").replace("'","").split(", ")
n = nbr_doc(line[0])
tf = tf_comp(line[0], i)
p = cal_pond(N, n, tf, len_doc, moy_len_doc)
l = lang_simple(line[0])
som = som_simple(line[0])
pond = pond_comp(l, p, som)
line[1] = pond
doclist.append(line)
file_4gramm.close()
doclist.sort(key=takeLast, reverse=True)
Mcle = open((rep+"keys\%s.key")%(all_files_names[i]), 'r', encoding="utf8")
num_Mcle = sum(1 for line in Mcle) #70
Mcle.close()
creerRepertoire(rep + "Methode Harrathi\\keys")
file_keys = open((rep+"Methode Harrathi\\keys\%s.txt")%(all_files_names[i]), "w", encoding="utf8")
for j in range(num_Mcle):
file_keys.write(doclist[j][0])
if j != num_Mcle-1:
file_keys.write("\n")
file_keys.close
print(f"boucle ecriture {i+1} fois")
###########################################################
def vider_repertoire(repertoire):
# Vérifier si le répertoire existe
if os.path.exists(repertoire):
# Obtenir la liste des fichiers dans le répertoire
fichiers = os.listdir(repertoire)
# Parcourir la liste des fichiers et les supprimer un par un
for fichier in fichiers:
chemin_fichier = os.path.join(repertoire, fichier)
os.remove(chemin_fichier)
print("Répertoire vidé avec succès.")
else:
#repertoire_rake = "RAKE"
if not os.path.exists(repertoire):
os.makedirs(repertoire)
print("Le répertoire spécifié a été créer.")
def ouvrir_fichiers_Rake(repertoire):
rep_doc = repertoire + "/docsutf8"
# Vérifier si un répertoire a été sélectionné
if rep_doc:
# Récupérer la liste des fichiers texte du répertoire
filenames = [os.path.join(rep_doc, f) for f in os.listdir(rep_doc) if os.path.isfile(os.path.join(rep_doc, f)) and f.endswith(".txt")]
# Vérifier si des fichiers ont été trouvés dans le répertoire
if filenames:
contenus = []
# Ouvrir les fichiers et lire leur contenu
for filename in filenames:
with open(filename, "r", encoding="utf8") as file:
#with open(filename, "r") as file:
#contenu = nettoyer(file.read())
contenu = file.read()
#print(contenu)
#contenu = nettoyer(contenu)
#print(contenu)
contenus.append(contenu)
liste_termes = extraire_termes_rake(contenus)
#print(liste_termes)
# Chemin du répertoire à vider
repertoire_rake = repertoire+"/RAKE"
vider_repertoire(repertoire_rake)
# Écrire les listes de termes dans des fichiers individuels
for i, termes in enumerate(liste_termes):
nom_fichier = os.path.join(repertoire_rake, f"{os.path.splitext(os.path.basename(filenames[i]))[0]}.txt")
with open(nom_fichier, "w", encoding="utf8") as file:
for terme in termes:
file.write(f"{terme}\n")
print("Les termes ont été stockés dans des fichiers individuels dans le répertoire 'RAKE'.")
else:
# Afficher un message d'erreur si aucun fichier n'a été trouvé dans le répertoire
messagebox.showerror("Erreur", "Aucun fichier texte trouvé dans le répertoire sélectionné.")
else:
# Afficher un message d'erreur si aucun répertoire n'a été sélectionné
messagebox.showerror("Erreur", "Aucun répertoire sélectionné.")
def ouvrir_fichiers_Yake(repertoire):
#repertoire = "docsutf8" # Spécifier le chemin absolu du répertoire contenant les fichiers
rep_doc = repertoire + "/docsutf8"
# Récupérer la liste des fichiers texte du répertoire
filenames = [os.path.join(rep_doc, f) for f in os.listdir(rep_doc) if os.path.isfile(os.path.join(rep_doc, f)) and f.endswith(".txt")]
# Vérifier si des fichiers ont été trouvés dans le répertoire
if filenames:
contenus = []
# Ouvrir les fichiers et lire leur contenu
for filename in filenames:
#with open(filename, "r", encoding="utf8") as file:
with open(filename, "r") as file:
contenu = file.read()
contenus.append(contenu)
liste_termes = extraire_termes_yake(contenus)
# Chemin du répertoire à vider
repertoire_yake = repertoire+"/YAKE"
vider_repertoire(repertoire_yake)
# Écrire les listes de termes dans des fichiers individuels
for i, termes in enumerate(liste_termes):
nom_fichier = os.path.join(repertoire_yake, f"{os.path.splitext(os.path.basename(filenames[i]))[0]}.txt")
with open(nom_fichier, "w", encoding="utf8") as file:
for terme in termes:
file.write(f"{terme}\n")
print("Les termes ont été stockés dans des fichiers individuels dans le répertoire 'YAKE'.")
else:
# Afficher un message d'erreur si aucun fichier n'a été trouvé dans le répertoire
messagebox.showerror("Erreur", "Aucun fichier texte trouvé dans le répertoire spécifié.")
def ouvrir_fichiers_TfIdf(repertoire):
#repertoire = "docsutf8" # Spécifier le chemin absolu du répertoire contenant les fichiers
rep_doc = repertoire + "/docsutf8"
# Récupérer la liste des fichiers texte du répertoire
filenames = [os.path.join(rep_doc, f) for f in os.listdir(rep_doc) if os.path.isfile(os.path.join(rep_doc, f)) and f.endswith(".txt")]
# Vérifier si des fichiers ont été trouvés dans le répertoire
if filenames:
contenus = []
# Ouvrir les fichiers et lire leur contenu
for filename in filenames:
#with open(filename, "r", encoding="utf8") as file:
with open(filename, "r") as file:
contenu = file.read()
contenu = nettoyer(contenu)
contenus.append(contenu)
liste_termes = extraire_termes_TfIdf(contenus)
repertoire_tfidf = repertoire + "/TFIDF" # Chemin du répertoire à vider
vider_repertoire(repertoire_tfidf)
# Écrire les listes de termes dans des fichiers individuels
for i, termes in enumerate(liste_termes):
nom_fichier = os.path.join(repertoire_tfidf, f"{os.path.splitext(os.path.basename(filenames[i]))[0]}.txt")
with open(nom_fichier, "w", encoding="utf8") as file:
for terme in termes:
file.write(f"{terme}\n")
print("Les termes ont été stockés dans des fichiers individuels dans le répertoire 'TFIDF'.")
else:
# Afficher un message d'erreur si aucun fichier n'a été trouvé dans le répertoire
messagebox.showerror("Erreur", "Aucun fichier texte trouvé dans le répertoire spécifié.")
def ouvrir_fichiers_Methode(repertoire):
#repertoire = "docsutf8" # Spécifier le chemin absolu du répertoire contenant les fichiers
rep_doc = repertoire + "/docsutf8"
# Récupérer la liste des fichiers texte du répertoire
filenames = [os.path.join(rep_doc, f) for f in os.listdir(rep_doc) if os.path.isfile(os.path.join(rep_doc, f)) and f.endswith(".txt")]
# Vérifier si des fichiers ont été trouvés dans le répertoire
if filenames:
contenus = []
nlp = spacy.load('en_core_web_sm')
# Ouvrir les fichiers et lire leur contenu
for filename in filenames:
#with open(filename, "r", encoding="utf8") as file:
with open(filename, "r") as file:
contenu = file.read()
contenus.append(contenu)
liste_termes = extraction_methode(contenus, nlp)
print("termes extraits")
repertoire_methode = repertoire + "/METHODE PROPOSEE" # Chemin du répertoire à vider
vider_repertoire(repertoire_methode)
# Écrire les listes de termes dans des fichiers individuels
for i, termes in enumerate(liste_termes):
nom_fichier = os.path.join(repertoire_methode, f"{os.path.splitext(os.path.basename(filenames[i]))[0]}.txt")
with open(nom_fichier, "w", encoding="utf8") as file:
for terme in termes:
file.write(f"{terme}\n")
print("Les termes ont été stockés dans des fichiers individuels dans le répertoire 'METHODE PROPOSEE'.")
else:
# Afficher un message d'erreur si aucun fichier n'a été trouvé dans le répertoire
messagebox.showerror("Erreur", "Aucun fichier texte trouvé dans le répertoire spécifié.")
def comparer_fichiers_manuel(repertoire,rep):
repertoire_manuel = rep + "/MANUEL" # Chemin du répertoire "MANUEL"
repertoire_rake = repertoire # Chemin du répertoire
# Récupérer la liste des fichiers texte du répertoire "MANUEL"
filenames_manuel = [f for f in os.listdir(repertoire_manuel) if os.path.isfile(os.path.join(repertoire_manuel, f)) and f.endswith(".txt")]
liste_r = []
liste_p = []
liste_m = []
#print(filenames_manuel)
for filename in filenames_manuel:
#print(filename)
entier = 0
chemin_manuel = os.path.join(repertoire_manuel, filename)
chemin_rake = os.path.join(repertoire_rake, filename)
if(rep != "CORPUS02"):
if os.path.isfile(chemin_rake):
with open(chemin_manuel, "r",encoding="utf8") as fichier_manuel, open(chemin_rake, "r",encoding="utf8") as fichier_rake:
lignes_manuel = fichier_manuel.readlines()
lignes_rake = fichier_rake.readlines()[:len(lignes_manuel)]
for ligne_manuel in lignes_manuel:
ligne_manuel = ligne_manuel.lower()
if any(ligne_manuel in ligne_rake.lower() for ligne_rake in lignes_rake):
entier = entier +1
r = entier / len(lignes_manuel)
p = entier / len(lignes_rake)
if r == 0 and p == 0:
m = 0
else: