-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuantQual_program_FINAL_updated_atom.py
1799 lines (1305 loc) · 54.3 KB
/
QuantQual_program_FINAL_updated_atom.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 collections import defaultdict
import nltk
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize,wordpunct_tokenize
import re
import os
import sys
from pathlib import Path
def main():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
while True:
try:
file_to_open =Path(input("\nYOU SELECTED OPTION 4: CONCATENATE WORDS. Please, insert your file path: "))
with open(file_to_open,'r', encoding="utf-8") as f:
words = wordpunct_tokenize(f.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
while True:
try:
dic_to_open=Path(input('\nPlease, enter your dictionary path: '))
with open(dic_to_open) as d:
dic=wordpunct_tokenize(d.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
l=[ ]
errors=[ ]
for n,word in enumerate (words):
l.append(word)
if word == "$":
exp = words[n-1] + words[n+1]
if exp in dic:
l.append(exp)
l.append("~")
errors.append(words[n-1])
errors.append(words[n+1])
else:
continue
for i, w in enumerate(l):
if w == "$":
l.remove(l[i-1])
else:
continue
for i, w in enumerate(l):
if w == "~":
l.remove(l[i+1])
else:
continue
text=' '.join(l)
#print('\n\n',text)
e=len(errors)
print('\n',float(e/2),'WORDS WERE CONCATENATED IN TEXT',errors)
user=input('\nRemove $ and ~ from text? \n1.Yes \n2.No \nSelection: ')
for x in l:
if user=='1' and x=='~':
l.remove(x)
elif user=='2' and x=='$':
l.remove(x)
else:
continue
final_text=' '.join(l)
#print('\n\n', final_text)
user2=input('\n\n1.Create a folder for the file \n\n2.Select a directory for your files \n\n3.Go to menu \n\nSelection: ')
if user2 =='1':
folder_path=Path(input('Enter your folder path and add the name of the new folder: '))
text_name=input("\n\nName your file. Extension not needed: ")
try:
os.makedirs(folder_path)
except FileExistsError:
print('This folder already exists. Try another name')
file_name = text_name+'.txt'
file = os.path.join(folder_path, file_name)
with open(file, 'w', encoding='utf-8') as f:
f.write(final_text)
print('\n\nText named',file_name +'was written to a file. Check your directory', folder_path)
elif user2 =='2':
folder_path=Path(input('Enter your chosen directory path: '))
text_name=input("\n\nName your file. Extension not needed: ")
file_name=text_name+'.txt'
folder=os.path.join(folder_path, file_name)
with open(folder,'w',encoding='utf-8') as text:
text.write(final_text)
print('\n\nText named', file_name +'was written to a file. Check your directory')
else:
print('\nOk')
while True:
choice = input('\n\n1.Run this program again \n\n2.Return to options \n\n3.Exit \n\nSelection: ')
if choice == '1':
main()
break
elif choice == '2':
menu()
break
elif choice == '3':
print("\n\nProgram Terminates")
break
else:
print('\n\nIncorrect option. Try again')
def frequency_list():
def punc_freq():
while True:
try:
punc_file_to_open =Path(input("\nYOU SELECTED OPTION 5: FREQUENCY LISTS. Please, insert your file path: "))
punc_dic_to_open=Path(input('\nPlease, enter the path of your punctuation dictionary: '))
with open(punc_file_to_open,'r', encoding="utf-8") as f:
freq = wordpunct_tokenize(f.read())
with open (punc_dic_to_open,'r', encoding="utf-8") as fr:
dic = wordpunct_tokenize(fr.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
punc=[]
words=[]
d1=defaultdict(int)
for p in freq:
if p not in dic:
words.append(p)
elif p in dic:
punc.append(p)
user=input('\n\n1.Create a folder for the file \n\n2.Select a directory for your files \n\n3.Go to menu \n\nSelection: ')
for i in punc:
d1[i]+=1
if user == '1':
punc_folder_path=Path(input('\n\nEnter the folder path and add the name of the new folder: '))
punc_file_name=input('\n\nName your file. Extension not needed: ')
f_name=punc_file_name+'.txt'
try:
os.makedirs(punc_folder_path)
except FileExistsError:
print('This folder already exists. Try another name.')
punc_file=os.path.join(punc_folder_path,f_name)
punc_fi=open(punc_file,'w',encoding='utf-8')
for p1 in sorted(d1, key=d1.get):
print(p1,d1[p1])
k=p1
fr=d1[p1]
row=k+' '+str(fr)+'\n'
punc_fi.write(row)
punc_fi.close()
print('\n\nFrequency list named',f_name,'written to a file. Check your directory', punc_folder_path)
elif user =='2':
punc_folder1_path=Path(input('\n\nEnter yor chosen directory: '))
punc_file1_name=input('\n\nName your file. Extension not needed: ')
f1_name=punc_file1_name+'.txt'
punc_file1_path=os.path.join(punc_folder1_path, f1_name)
punc_fi1=open(punc_file1_path,'w', encoding='utf-8')
for p2 in sorted(d1, key=d1.get):
#print(p2,d1[p2])
k1=p2
fr1=d1[p2]
punc_row1=k1+' '+str(fr1)+'\n'
punc_fi1.write(punc_row1)
punc_fi1.close()
print('\n\nFrequency list named',f1_name,'written to a file. Check your directory', punc_folder1_path)
else:
print('\nOk')
def word_freq():
while True:
try:
file_to_open =Path(input("\nPlease, insert your file path: "))
with open(file_to_open,'r', encoding="utf-8") as f:
freq = wordpunct_tokenize(f.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
pat=re.compile(r"[.,:;?!'%-]|\d+") #regular expression for words with apostrophes and separated by hyphen
reg= list(filter(pat.match, freq))
patt=re.compile(r"^[A-Z][a-z]+\b|^[A-Z]+\b") #regular expression for words that start with capital letters (ex: proper nouns)
c_n= list(filter(patt.match, freq))
Cap_nouns=[]
d=defaultdict(int)
d2=defaultdict(int)
d3=defaultdict(int)
for w in freq:
d[w]+=1
if w in reg:
continue
elif w in c_n:
continue
user=input('\n\n1.Create a folder for the file \n\n2.Select a directory for your files \n\n3.Go to menu \n\nSelection: ')
if user == '1':
folder_path=Path(input('\n\nEnter your folder path and add the name of your new folder: '))
file_name=input('\n\nName your file. Extension not needed: ')
try:
os.makedirs(folder_path)
except FileExistsError:
print('This folder already existis. Try another name.')
f_name=file_name+'.txt'
file=os.path.join(folder_path,f_name)
fi=open(file,'w', encoding='utf-8')
for w1 in sorted(d, key=d.get):
k=w1
fr=d[w1]
row=k+' '+str(fr)+'\n'
fi.write(row)
fi.close()
print('\n\nFrequency list named',f_name,'written to a file. Check your directory named', folder_path)
elif user == '2':
folder1_path=Path(input('\n\nEnter your chosen directory: '))
file1_name=input('\n\nName your file. Extension not needed: ')
f1_name=file1_name +'.txt'
file1_path=os.path.join(folder1_path, f1_name)
fi1=open(file1_path,'w', encoding='utf-8')
for w2 in sorted(d, key=d.get):
k1=w2
fr1=d[w2]
row1=k1+' '+str(fr1)+'\n'
fi1.write(row1)
fi1.close()
print('\n\nFrequency list named',f1_name,'written to a file. Check your directory', folder1_path)
else:
print('Ok')
def error_freq():
while True:
try:
file_to_open =Path(input("\nPlease, insert your file path: "))
dic_to_open=Path(input('\nPlease, insert your dictionary path: '))
with open(file_to_open,'r', encoding="utf-8") as f:
freq = wordpunct_tokenize(f.read())
with open (dic_to_open,'r', encoding="utf-8") as fr:
dic = word_tokenize(fr.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
pat=re.compile(r"[.,:;?!'%-]|\d+") #regular expression for words with apostrophes and separated by hyphen
reg= list(filter(pat.match, freq))
patt=re.compile(r"^[A-Z][a-z]+\b|^[A-Z]+\b") #regular expression for words that start with capital letters (ex: proper nouns)
c_n= list(filter(patt.match, freq))
Cap_nouns=[]
errors=[ ]
d=defaultdict(int)
d2=defaultdict(int)
d3=defaultdict(int)
for w in freq:
d[w]+=1
if w in reg:
continue
elif w in c_n:
Cap_nouns.append(w)
elif w not in dic:
errors.append(w)
display_errors=input('\n\nWrite the list of items not found in Dict to a file?" |\n1.Yes \n2.No: ')
folder_setup=input('\n\n1.Create a folder for the file \n\n2.Select a directory for your files \n\n3.Go to menu \n\nSelection: ')
for x1 in errors:
d2[x1]+=1
if display_errors == '1' and folder_setup == '1':
folder2_path=Path(input('\n\nEnter your folder path and add the name of your new folder: '))
file2_name=input('\n\nName your file. Extension not needed: ')
try:
os.makedirs(folder2_path)
except FileExistsError:
print('This folder already exists. Try another name.')
f2_name=file2_name+'.txt'
file2=os.path.join(folder2_path,f2_name)
fi2=open(file2,'w', encoding='utf-8')
for x2 in sorted(d2, key=d2.get):
we=x2
fre=str(d2[x2])
row2= we+' '+str(fre)+'\n'
fi2.write(row2)
fi2.close()
print("\n\nFrequency list named",f2_name, 'written to a file.Check your directory', folder2_path)
elif display_errors == '1' and folder_setup == '2':
folder3_path=Path(input('\n\nEnter the directory path: '))
file3_name=input('\n\nName your file. Extension not needed: ')
f3_name=file3_name+'.txt'
file3_path=os.path.join(folder3_path, f3_name)
fi3=open(file3_path,'w', encoding='utf-8')
for x3 in sorted(d2, key=d2.get):
k2=x3
fr2=d2[x3]
row3=k2+' '+str(fr2)+'\n'
fi3.write(row3)
fi3.close()
print('\n\nFrequency list named',file3_name,'written to a file. Check your directory:', folder3_path)
else:
print('\nOk')
def cap_freq():
while True:
try:
file_to_open =Path(input("\nPlease, insert your file path: "))
dic_to_open=Path(input('\nPlease, insert your dictionary path: '))
with open(file_to_open,'r', encoding="utf-8") as f:
freq = wordpunct_tokenize(f.read())
with open (dic_to_open,'r', encoding="utf-8") as fr:
dic = word_tokenize(fr.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
pat=re.compile(r"[.,:;?!'%-]|\d+") #regular expression for words with apostrophes and separated by hyphen
reg= list(filter(pat.match, freq))
patt=re.compile(r"^[A-Z][a-z]+\b|^[A-Z]+\b") #regular expression for words that start with capital letters (ex: proper nouns)
c_n= list(filter(patt.match, freq))
Cap_nouns=[]
d3=defaultdict(int)
for w in freq:
if w in c_n:
Cap_nouns.append(w)
display_Cap=input('\n\nWrite the list of capitalised words to a file?" |\n1.Yes \n2.No: ')
folder_setup=input('\n\n1.Create a folder for the file \n\n2.Select a directory for your files \n\n3.No file saved \n\nSelection: ')
for y in Cap_nouns:
if y not in dic:
d3[y]+=1
if display_Cap == '1' and folder_setup == '1':
folder2_path=Path(input('\n\nEnter your folder path and the add the name of your new folder: '))
file2_name=input('\n\nName your file. Extension not needed: ')
fi2_name= file2_name +'.txt'
try:
os.makedirs(folder2_path)
except FileExistsError:
print('This folder already exists. Try another name.')
file2=os.path.join(folder2_path,fi2_name)
fi2=open(file2,'w', encoding='utf-8')
for x2 in sorted(d3, key=d3.get):
we=x2
fre=str(d3[x2])
row2= we+' '+str(fre)+'\n'
fi2.write(row2)
fi2.close()
print("\n\nFrequency list named",file2_name, 'written to a file.Check your directory', folder2_path)
elif display_Cap == '1' and folder_setup == '2':
folder3_path=Path(input('\n\nEnter the directory path: '))
file3_name=input('\n\nName your file. Extension not needed: ')
fi3_name=file3_name+'.txt'
file3_path=os.path.join(folder3_path, fi3_name)
fi3=open(file3_path,'w', encoding='utf-8')
for x3 in sorted(d3, key=d3.get):
k2=x3
fr2=d3[x3]
row3=k2+' '+str(fr2)+'\n'
fi3.write(row3)
fi3.close()
print('\n\nFrequency list named',file3_name,'written to a file. Check your directory:', folder3_path)
else:
print('\nOk')
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
main_question=input('Select your task: \n\n1.List word frequency \n\n2.List error frequency \n\n3.List proper nouns frequency \n\n4.List punctuation frequency \n\nSelection: ')
while True:
if main_question == '1':
word_freq()
break
elif main_question == '2':
error_freq()
break
elif main_question == '3':
cap_freq()
break
elif main_question == '4':
punc_freq()
break
else:
print('\n\nIncorrect option. Try again')
break
while True:
choice = input("\n\nDo you want to do this again, return to options or exit? | \n\n1.Type '1' to run this program again \n\n2.Type '2' to return to options \n\n3.Type '3' to exit \n\n Selection: ")
if choice == '1':
frequency_list()
elif choice == '2':
menu()
elif choice == '3':
print("\n\nProgram Terminates")
break
else:
print('\n\nIncorrect option. Try again')
def separate_chapters ():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
print('\n\nREMINDER: TO RUN THIS PROGRAM YOUR BOOK CHAPTERS MUST BE TAGGED USING THE FOLLOWING TAG FORMAT: [@CHAPST@] AND [@CHAPFN@]')
while True:
try:
file_to_open =Path(input("\nYOU SELECTED OPTION 3: SEPARATE CHAPTERS. Please, insert your file path: "))
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
pat = re.compile(r'(?<=\[@CHAPST@\]).+?(?=\[@CHAPFN@\])', flags=re.DOTALL)
my_chapters=[]
with open(file_to_open, 'r', encoding="utf-8") as file:
for i in pat.findall(file.read()):
my_chapters.append(i)
print('\n\nThis book contains',len(my_chapters), 'chapters')
folder_path=Path(input('\n\nEnter your folder path and add the name of your new folder: '))
try:
os.makedirs(folder_path)
except FileExistsError:
print('This Folder already Exists. Try another name.')
for j in range(len(my_chapters)):
chap='Chapter'+str(j+1) +'.txt'
file = os.path.join(folder_path, chap)
with open(file, "w", encoding='utf-8') as f:
for item in my_chapters[j]:
f.write("%s" % str(item))
print('\n\n', chap)
print('\n\nChapters 1 -',len(my_chapters), 'written to a file separately. Check your directory',folder_path)
while True:
choice = input("\n\nDo you want to do this again, return to options or exit? | \n\n1.Type '1' to run this program again \n\n2.Type '2' to return to options \n\n3.Type '3' to exit \n\n Selection: ")
if choice == '1':
separate_chapters()
elif choice == '2':
menu()
elif choice == '3':
break
print("\n\nProgram Terminates")
else:
print('\n\nIncorrect option. Try again')
def separate_paratext ():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
print('\n\nREMINDER: TO RUN THIS PROGRAM YOUR PARATEXTUAL INFORMATION MUST BE TAGGED USING THE FOLLOWING TAG FORMAT: [@PARAST@] AND [@PARAFN@]')
while True:
try:
file_to_open =Path(input("\nYOU SELECTED OPTION 2: SEPARATE PARATEXT. Please, insert your file path: "))
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
pat = re.compile(r'(?<=\[@PARAST@\]).+?(?=\[@PARAFN@\])', flags=re.DOTALL)
my_paratext=[]
with open(file_to_open, 'r', encoding="utf-8") as file:
for i in pat.findall(file.read()):
my_paratext.append(i)
print('Your book has',len(my_paratext), 'paratext instances')
folder_path=Path(input('\n\nEnter your folder path and add the name of your new folder: '))
try:
os.makedirs(folder_path)
except FileExistsError:
print('This folder already exists. Try another name.')
for j in range(len(my_paratext)):
para='Paratext'+str(j+1) +'.txt'
file = os.path.join(folder_path, para)
with open(file, "w+", encoding='utf-8') as f:
for item in my_paratext[j]:
f.write("%s" % str(item))
print('\n\n', para)
print("\n\nParatexts written to a file separately. Check in your directory", folder_path)
while True:
choice = input("\n\nDo you want to do this again, return to options or exit? | \n\n1.Type '1' to run this program again \n\n2. Type '2' to return to options \n\n3.Type '3' to exit \n\n Selection: ")
if choice == '1':
separate_paratext()
elif choice == '2':
menu()
elif choice == '3':
print("\n\nProgram Terminates")
break
else:
print('\n\nIncorrect option. Try again')
def remove_paratext():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
print('\n\nREMINDER: TO RUN THIS PROGRAM YOUR PARATEXTUAL INFORMATION MUST BE TAGGED USING THE FOLLOWING TAG FORMAT: [@PARAST@] AND [@PARAPFN@]')
while True:
try:
file_to_open =Path(input("\nYOU SELECTED OPTION 3: REMOVE PARATEXT. Please, insert your file path: "))
with open(file_to_open,'r', encoding="utf-8") as t:
text=t.read()
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
pat=re.compile(r'(\[@PARAST@\]).+?(\[@PARAFN@\])', flags=re.DOTALL)
s = re.sub(pat, '', text)
user=input('\n\n1.Create a folder for the file \n\n2.Select a directory for your files \n\n3.Go to menu \n\n.Selection: ')
if user == '1':
folder_path=Path(input('\n\nEnter your folder path: '))
file_name=input('\n\nName your file. Extension not needed: ')
fil_name=file_name+'.txt'
try:
os.makedirs(folder_path)
except FileExistsError:
print("This folder already exists. Try another name.")
file=os.path.join(folder_path,fil_name)
with open(file, 'w', encoding='utf-8') as f:
f.write(s)
print('\n\nText named', fil_name, 'written to a file. Check folder named',folder_path, 'in your directory')
elif user == '2':
folder_path=Path(input('\n\nEnter your chosen directory: '))
file_name=input('\n\nName your file. Extension not needed: ')
f_name=file_name+'.txt'
file_path=os.path.join(folder_path, f_name)
with open(file_path, 'w', encoding='utf-8') as f:
f.write(s)
print('\n\nText named', f_name, 'written to a file. Check folder: ', folder_path)
else:
print('Ok')
while True:
choice = input("\n\nDo you want to do this again, return to options or exit? | \n\n1.Type '1' to run this program again \n\n2.Type '2' to return to options \n\n3.Type '3' to exit \n\nSelection: ")
if choice == '1':
remove_paratext()
elif choice == '2':
menu()
elif choice == '3':
print("\n\nProgram Terminates")
break
else:
print('\n\nIncorrect option. Try again')
def sent():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
a=Path(input("\nYOU SELECTED OPTION 6: SPLIT TEXT INTO SENTENCES. Please, enter your file path: "))
with open(a, 'r', encoding='utf-8') as f:
word=sent_tokenize(f.read())
user=input('\n\nCreate a folder?|\1.Yes \n2.No: ')
if user == '1':
folder_path=Path(input('Enter your folder path and add the name of your new folder: '))
text_name=input("\n\nName your file. Extension not needed: ")
try:
os.makedirs(folder_path)
except FileExistsError:
print("This folder already exists. Try another name.")
file_name = text_name +'.txt'
file = os.path.join(folder_path, file_name)
with open(file, 'w', encoding='utf-8') as t:
for i in word:
a='\n\n'+i
t.write(a)
t.close()
else:
folder_path=Path(input('Enter your folder path: '))
text_name=input("\n\nName your file. Extension not needed: ")
t_name=text_name+'.txt'
folder=os.path.join(folder_path, t_name)
with open(folder, 'w', encoding='utf-8') as tx:
for j in word:
b='\n\n'+j
tx.write(b)
tx.close()
print('\n\nThis text contains',len(word), 'sentences')
while True:
choice = input("\n\nDo you want to do this again, return to options or exit? | \n\n1.Type '1' to run this program again \n\n2. Type '2' to return to options \n\n3.Type '3' to exit \n\n Selection: ")
if choice == '1':
sent()
elif choice == '2':
menu()
elif choice == '3':
print("\n\nProgram Terminates")
break
else:
print('\n\nIncorrect option. Try again')
def enter_tag():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
print('\n\nREMINDER: YOUR TEXT MUST BE TAGGED USING THE FOLLOWING TAG FORMAT: [@"anystringhere"@]. The program is case sensitive.')
while True:
try:
file_to_open =Path(input("\nYOU SELECTED OPTION 7: ENTER TAG. Please, insert your file path: "))
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
tag1=input('\n\nPlease, enter the string between the @ symbols of your first tag: ')
tag2=input('\n\nPlease, enter the string between the @ symbols of your second tag: ')
tagged_items=[]
pat = re.compile(r'(?<=\[@'+tag1+'@\]).+?(?=\[@'+tag2+'@\])', flags=re.DOTALL)
#pat = re.compile(r'(?<=\%'+tag1+'\%).+?(?=\%'+tag2+'\%)', flags=re.DOTALL)
with open(file_to_open, 'r', encoding="utf-8") as file:
for i in pat.findall(file.read()):
tagged_items.append(i)
print('\n\nThis file contains',len(tagged_items), 'tagged items')
q=input('\n\n1.Create a folder for your files \n\n2.Select a directory for your files \n\n3.Go to main menu \n\nSelection: ')
if q == '1':
folder_path=Path(input('\n\nEnter your folder path and add the name of your new folder: '))
try:
os.makedirs(folder)
except FileExistsError:
print('This folder already exists. Try another name.')
for j in range(len(tagged_items)):
chap='Item'+ str(j+1) +'.txt'
file = os.path.join(folder_path, chap)
with open(file, "w", encoding='utf-8') as f:
for item in tagged_items[j]:
f.write("%s" % str(item))
print('\n\nTagged items 1 -',len(tagged_items), 'written to a file separately. Check your folder directory',folder_path)
elif q =='2':
folder1_path=Path(input('\n\nEnter your chosen directory: '))
for x in range(len(tagged_items)):
chap1='Item'+'0'+ str(x+1) +'.txt'
fi = os.path.join(folder1_path, chap1)
with open(fi, "w", encoding='utf-8') as f:
for item in tagged_items[x]:
f.write("%s" % str(item))
print('\n\nTagged items 1 -',len(tagged_items), 'written to a file separately. Check your directory',folder1_path)
else:
print('Ok')
while True:
choice = input("\n\nDo you want to do this again, return to options or exit? | \n\n1.Type '1' to run this program again \n\n2. Type '2' to return to options \n\n3.Type '3' to exit \n\n Selection: ")
if choice == '1':
enter_tag()
elif choice == '2':
menu()
elif choice == '3':
print("\n\nProgram Terminates")
break
else:
print('\n\nIncorrect option. Try again')
def sent_lgth():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
while True:
try:
file_to_open =Path(input("\nYOU SELECTED OPTION 8: CALCULATE SENTENCE LENGTH. Please, insert your file path: "))
with open(file_to_open,'r', encoding="utf-8") as f:
words = sent_tokenize(f.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
print('\n\n This file contains',len(words),'sentences in total')
sent_number=1
wordcounts = []
with open(file_to_open) as f:
text = f.read()
sentences = sent_tokenize(text)
for sentence in sentences:
w = word_tokenize(sentence)
wordcounts.append(len(w))
average_wordcount = sum(wordcounts)/len(wordcounts)
a='The longest sentence of this file contains',max(wordcounts), 'tokens'
b='The shortest sentence of this file contains',min(wordcounts),'tokens'
c='The mean sentence length of this file is: ',average_wordcount
print(a)
print(b)
print(c)
sent_number=1
u=input('\n\nPrint number of tokens per sentence or save to a file? \n\n1.Print \n\n2.Create a folder to your file \n\n3.Choose a directory to your file \n\n4.Press any key to continue \n\nSelection: ')
if u == '1':
for t in words:
a=word_tokenize(t)
print('\n\nSentence',sent_number,'contains',len(a), 'tokens')
sent_number +=1
elif u == '2':
folder_path=Path(input('\n\nEnter the directory of your new folder: '))
try:
os.makedirs(folder_path)
except FileExistsError:
print('This folder already exists. Try again.')
text_name=input("\n\nName your file. Extension not needed: ")
file_name = text_name +'.txt'
file = os.path.join(folder_path, file_name)
with open(file, 'w', encoding='utf-8') as t1:
for y in words:
x=word_tokenize(y)
pr='\nSentence '+str(sent_number)+' contains '+str(len(x))
sent_number +=1
t1.write(pr)
t1.close()
elif u == '3':
folder_path=Path(input('Enter your folder path: '))
text_name=input("\n\nName your file. Extension not needed: ")
file_name=text_name +'.txt'
folder=os.path.join(folder_path, file_name)
with open(folder, 'w', encoding='utf-8') as tx:
for s in words:
j=word_tokenize(s)
pr2='\nSentence '+str(sent_number)+' contains '+str(len(j))
sent_number +=1
tx.write(pr2)
tx.close()
else:
print('\n\nNo file saved')
while True:
choice = input("\n\nDo you want to do this again, return to options or exit? | \n\n1.Type '1' to run this program again \n\n2. Type '2' to return to options \n\n Selection: ")
if choice == '1':
sent_lgth()
elif choice == '2':
menu()
break
else:
print('\n\nIncorrect option. Try again')
def percent():
u=input('If you want to return to options, type "1": ')
while True:
if u == '1':
menu()
break
else:
break
while True:
try:
punc_file_to_open =Path(input("\nYOU SELECTED OPTION 9: CALCULATE ERROR PERCENTAGE. Please, insert your file path: "))
punc_dic_to_open=Path(input('\nPlease, insert your dictionary path: '))
with open(punc_file_to_open,'r', encoding="utf-8") as f:
freq = wordpunct_tokenize(f.read())
with open (punc_dic_to_open,'r', encoding="utf-8") as fr:
dic = wordpunct_tokenize(fr.read())
break
except FileNotFoundError:
print("\nFile not found. Better try again")
except IsADirectoryError:
print("\nIncorrect Directory path.Try again")
punc=open('/Users/nataliaresende/Dropbox/PYTHON/Dictionaries/Punctuation.txt', 'r',encoding='utf-8')
punc_dic=punc.read()
patt=re.compile(r"^[A-Z][a-z]+\b|^[A-Z]+\b|\d+")
c_n= list(filter(patt.match, freq))
errors=[]
text=[]
Cap_nouns=[]
named_entities=[]
for i in freq:
if i in dic:
text.append(i)
elif i in punc_dic:
text.append(i)
elif i in c_n:
Cap_nouns.append(i)
else:
errors.append(i)
for j in Cap_nouns:
if j not in dic:
named_entities.append(j)
print('\n\nYourfile contains a total of', len(text), 'tokens')
print('\n\nYour file contains a total of',len(errors), 'tokens not found in dictionary')
print('\n\nYour file contains a total of', len(Cap_nouns), 'capitalised words')
print('\n\nYour file contains a total of', len(named_entities), 'proper nouns')
print('\n\nThe error percentage of this file is: ', 100*float(len(errors))/float(len(text)))
print('\n\nThe percentage of capitalised words is: ', 100*float(len(Cap_nouns))/float(len(text)))
print('\n\nThe percentage of named entities is: ', 100*float(len(named_entities))/float(len(text)))
user=input('\n\n1. Print the list of errors\n\n2. Type "2" Print the list of capitalised nouns \n\n3. Print list of errors, capitalised nouns and named entities \n\n4. Go to main menu \n\n5. run this program again \n\nSelection: ')
if user =='1':
print(errors)
elif user == '2':
print(Cap_nouns)
elif user =='3':