-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
968 lines (677 loc) · 24.3 KB
/
main.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
# import spacy
import spacy
# load english language model
nlp = spacy.load('en_core_web_sm',disable=['ner','textcat'])
text = "This is a sample sentence."
# create spacy
doc = nlp(text)
for token in doc:
print(token.text,'->',token.pos_)
for token in doc:
# check token pos
if token.pos_=='NOUN':
# print token
print(token.text)
text = "The children love cream biscuits"
# create spacy
doc = nlp(text)
for token in doc:
print(token.text,'->',token.pos_)
from spacy import displacy
displacy.render(doc, style='dep',jupyter=True)
for token in doc:
# extract subject
if (token.dep_=='nsubj'):
print(token.text)
# extract object
elif (token.dep_=='dobj'):
print(token.text)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import glob
import re
#Folder path
folders = glob.glob('./UNGD/UNGDC 1970-2018/Converted sessions/Session*')
# Dataframe
df = pd.DataFrame(columns={'Country','Speech','Session','Year'})
# Read speeches by India
i = 0
for file in folders:
speech = glob.glob(file+'/IND*.txt')
with open(speech[0],encoding='utf8') as f:
# Speech
df.loc[i,'Speech'] = f.read()
# Year
df.loc[i,'Year'] = speech[0].split('_')[-1].split('.')[0]
# Session
df.loc[i,'Session'] = speech[0].split('_')[-2]
# Country
df.loc[i,'Country'] = speech[0].split('_')[0].split("\\")[-1]
# Increment counter
i += 1
df.head()
df.loc[0,'Speech']
# function to preprocess speech
def clean(text):
# removing paragraph numbers
text = re.sub('[0-9]+.\t','',str(text))
# removing new line characters
text = re.sub('\n ','',str(text))
text = re.sub('\n',' ',str(text))
# removing apostrophes
text = re.sub("'s",'',str(text))
# removing hyphens
text = re.sub("-",' ',str(text))
text = re.sub("— ",'',str(text))
# removing quotation marks
text = re.sub('\"','',str(text))
# removing salutations
text = re.sub("Mr\.",'Mr',str(text))
text = re.sub("Mrs\.",'Mrs',str(text))
# removing any reference to outside text
text = re.sub("[\(\[].*?[\)\]]", "", str(text))
return text
# preprocessing speeches
df['Speech_clean'] = df['Speech'].apply(clean)
# split sentences
def sentences(text):
# split sentences and questions
text = re.split('[.?]', text)
clean_sent = []
for sent in text:
clean_sent.append(sent)
return clean_sent
# sentences
df['sent'] = df['Speech_clean'].apply(sentences)
# Create a dataframe containing sentences
df2 = pd.DataFrame(columns=['Sent','Year','Len'])
# List of sentences for new df
row_list = []
# for-loop to go over the df speeches
for i in range(len(df)):
# for-loop to go over the sentences in the speech
for sent in df.loc[i,'sent']:
wordcount = len(sent.split()) # Word count
year = df.loc[i,'Year'] # Year
dict1 = {'Year':year,'Sent':sent,'Len':wordcount} # Dictionary
row_list.append(dict1) # Append dictionary to list
# Create the new df
df2 = pd.DataFrame(row_list)
import spacy
from spacy.matcher import Matcher
from spacy import displacy
import visualise_spacy_tree
from IPython.display import Image, display
# load english language model
nlp = spacy.load('en_core_web_sm',disable=['ner','textcat'])
def find_names(text):
names = []
# Create a spacy doc
doc = nlp(text)
# Define the pattern
pattern = [{'LOWER':'prime'},
{'LOWER':'minister'},
{'POS':'ADP','OP':'?'},
{'POS':'PROPN'}]
# Matcher class object
matcher = Matcher(nlp.vocab)
matcher.add("names", None, pattern)
matches = matcher(doc)
# Finding patterns in the text
for i in range(0,len(matches)):
# match: id, start, end
token = doc[matches[i][1]:matches[i][2]]
# append token to list
names.append(str(token))
# Only keep sentences containing Indian PMs
for name in names:
if (name.split()[2] == 'of') and (name.split()[3] != "India"):
names.remove(name)
return names
# Apply function
df2['PM_Names'] = df2['Sent'].apply(find_names)
# look at sentences for a specific year
for i in range(len(df2)):
if df2.loc[i,'Year'] in ['1984']:
if len(df2.loc[i,'PM_Names'])!=0:
print('->',df2.loc[i,'Sent'],'\n')
count=0
for i in range(len(df2)):
if len(df2.loc[i,'PM_Names'])!=0:
count+=1
print(count)
def prog_sent(text):
patterns = [r'\b(?i)'+'plan'+r'\b',
r'\b(?i)'+'programme'+r'\b',
r'\b(?i)'+'scheme'+r'\b',
r'\b(?i)'+'campaign'+r'\b',
r'\b(?i)'+'initiative'+r'\b',
r'\b(?i)'+'conference'+r'\b',
r'\b(?i)'+'agreement'+r'\b',
r'\b(?i)'+'alliance'+r'\b']
output = []
flag = 0
# Look for patterns in the text
for pat in patterns:
if re.search(pat, text) != None:
flag = 1
break
return flag
# Apply function
df2['Check_Schemes'] = df2['Sent'].apply(prog_sent)
# Sentences that contain the initiative words
count = 0
for i in range(len(df2)):
if df2.loc[i,'Check_Schemes'] == 1:
count+=1
print(count)
# To extract initiatives using pattern matching
def all_schemes(text,check):
schemes = []
doc = nlp(text)
# Initiatives keywords
prog_list = ['programme','scheme',
'initiative','campaign',
'agreement','conference',
'alliance','plan']
# Define pattern to match initiatives names
pattern = [{'POS':'DET'},
{'POS':'PROPN','DEP':'compound'},
{'POS':'PROPN','DEP':'compound'},
{'POS':'PROPN','OP':'?'},
{'POS':'PROPN','OP':'?'},
{'POS':'PROPN','OP':'?'},
{'LOWER':{'IN':prog_list},'OP':'+'}
]
if check == 0:
# return blank list
return schemes
# Matcher class object
matcher = Matcher(nlp.vocab)
matcher.add("matching", None, pattern)
matches = matcher(doc)
for i in range(0,len(matches)):
# match: id, start, end
start, end = matches[i][1], matches[i][2]
if doc[start].pos_=='DET':
start = start+1
# matched string
span = str(doc[start:end])
if (len(schemes)!=0) and (schemes[-1] in span):
schemes[-1] = span
else:
schemes.append(span)
return schemes
# apply function
df2['Schemes1'] = df2.apply(lambda x:all_schemes(x.Sent,x.Check_Schemes),axis=1)
count = 0
for i in range(len(df2)):
if len(df2.loc[i,'Schemes1'])!=0:
count+=1
print(count)
year = '2018'
for i in range(len(df2)):
if df2.loc[i,'Year']==year:
if len(df2.loc[i,'Schemes1'])!=0:
print('->',df2.loc[i,'Year'],',',df2.loc[i,'Schemes1'],':')
print(df2.loc[i,'Sent'])
# Printing dependency tree
doc = nlp(' Last year, I spoke about the Ujjwala programme , through which, I am happy to report, 50 million free liquid-gas connections have been provided so far')
png = visualise_spacy_tree.create_png(doc)
display(Image(png))
doc = nlp('Prime Minister Modi, together with the Prime Minister of France, launched the International Solar Alliance')
png = visualise_spacy_tree.create_png(doc)
display(Image(png))
# rule to extract initiative name
def sent_subtree(text):
# pattern match for schemes or initiatives
patterns = [r'\b(?i)'+'plan'+r'\b',
r'\b(?i)'+'programme'+r'\b',
r'\b(?i)'+'scheme'+r'\b',
r'\b(?i)'+'campaign'+r'\b',
r'\b(?i)'+'initiative'+r'\b',
r'\b(?i)'+'conference'+r'\b',
r'\b(?i)'+'agreement'+r'\b',
r'\b(?i)'+'alliance'+r'\b']
schemes = []
doc = nlp(text)
flag = 0
# if no initiative present in sentence
for pat in patterns:
if re.search(pat, text) != None:
flag = 1
break
if flag == 0:
return schemes
# iterating over sentence tokens
for token in doc:
for pat in patterns:
# if we get a pattern match
if re.search(pat, token.text) != None:
word = ''
# iterating over token subtree
for node in token.subtree:
# only extract the proper nouns
if (node.pos_ == 'PROPN'):
word += node.text+' '
if len(word)!=0:
schemes.append(word)
return schemes
# derive initiatives
df2['Schemes2'] = df2['Sent'].apply(sent_subtree)
count = 0
for i in range(len(df2)):
if len(df2.loc[i,'Schemes2'])!=0:
count+=1
print(count)
year = '2018'
for i in range(len(df2)):
if df2.loc[i,'Year']==year:
if len(df2.loc[i,'Schemes2'])!=0:
print('->',df2.loc[i,'Year'],',',df2.loc[i,'Schemes2'],':')
print(df2.loc[i,'Sent'])
plt.hist(df2['Len'],bins=20,range=[0,100])
plt.xticks(np.arange(0,100,5));
row_list = []
# df2 contains all sentences from all speeches
for i in range(len(df2)):
sent = df2.loc[i,'Sent']
if (',' not in sent) and (len(sent.split()) <= 15):
year = df2.loc[i,'Year']
length = len(sent.split())
dict1 = {'Year':year,'Sent':sent,'Len':length}
row_list.append(dict1)
# df with shorter sentences
df3 = pd.DataFrame(columns=['Year','Sent',"Len"])
df3 = pd.DataFrame(row_list)
from random import randint
def rand_sent(df):
index = randint(0, len(df))
print('Index = ',index)
doc = nlp(df.loc[index,'Sent'][1:])
displacy.render(doc, style='dep',jupyter=True)
return index
# function to check output percentage for a rule
def output_per(df,out_col):
result = 0
for out in df[out_col]:
if len(out)!=0:
result+=1
per = result/len(df)
per *= 100
return per
# To download dependency graphs to local folder
from pathlib import Path
text = df3.loc[9,'Sent'][1:]
doc = nlp(text)
img = displacy.render(doc, style='dep',jupyter=True)
img
# To save to folder
# output_path = Path("./img1.svg")
# output_path.open("w", encoding="utf-8").write(img)
# Function for rule 1: noun(subject), verb, noun(object)
def rule1(text):
doc = nlp(text)
sent = []
for token in doc:
# If the token is a verb
if (token.pos_=='VERB'):
phrase =''
# Only extract noun or pronoun subjects
for sub_tok in token.lefts:
if (sub_tok.dep_ in ['nsubj','nsubjpass']) and (sub_tok.pos_ in ['NOUN','PROPN','PRON']):
# Add subject to the phrase
phrase += sub_tok.text
# Save the root of the word in phrase
phrase += ' '+token.lemma_
# Check for noun or pronoun direct objects
for sub_tok in token.rights:
# Save the object in the phrase
if (sub_tok.dep_ in ['dobj']) and (sub_tok.pos_ in ['NOUN','PROPN']):
phrase += ' '+sub_tok.text
sent.append(phrase)
return sent
# Create a df containing sentence and its output for rule 1
row_list = []
for i in range(len(df3)):
sent = df3.loc[i,'Sent']
year = df3.loc[i,'Year']
output = rule1(sent)
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule1 = pd.DataFrame(row_list)
# Rule 1 achieves 20% result on simple sentences
output_per(df_rule1,'Output')
# Create a df containing sentence and its output for rule 1
row_list = []
# df2 contains all the sentences from all the speeches
for i in range(len(df2)):
sent = df2.loc[i,'Sent']
year = df2.loc[i,'Year']
output = rule1(sent)
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule1_all = pd.DataFrame(row_list)
# Check rule1 output on complete speeches
output_per(df_rule1_all,'Output')
# selecting non-empty output rows
df_show = pd.DataFrame(columns=df_rule1_all.columns)
for row in range(len(df_rule1_all)):
if len(df_rule1_all.loc[row,'Output'])!=0:
df_show = df_show.append(df_rule1_all.loc[row,:])
# reset the index
df_show.reset_index(inplace=True)
df_show.drop('index',axis=1,inplace=True)
df_rule1_all.shape, df_show.shape
# separate subject, verb and object
verb_dict = dict()
dis_dict = dict()
dis_list = []
# iterating over all the sentences
for i in range(len(df_show)):
# sentence containing the output
sentence = df_show.loc[i,'Sent']
# year of the sentence
year = df_show.loc[i,'Year']
# output of the sentence
output = df_show.loc[i,'Output']
# iterating over all the outputs from the sentence
for sent in output:
# separate subject, verb and object
n1 = sent.split()[:1]
v = sent.split()[1]
n2 = sent.split()[2:]
# append to list, along with the sentence
dis_dict = {'Sent':sentence,'Year':year,'Noun1':n1,'Verb':v,'Noun2':n2}
dis_list.append(dis_dict)
# counting the number of sentences containing the verb
verb = sent.split()[1]
if verb in verb_dict:
verb_dict[verb]+=1
else:
verb_dict[verb]=1
df_sep = pd.DataFrame(dis_list)
sort = sorted(verb_dict.items(), key = lambda d:(d[1],d[0]), reverse=True)
# top 10 most used verbs in sentence
sort[:10]
# support verb
df_sep[df_sep['Verb']=='support']
# face
df_sep[df_sep['Verb']=='face']
text = 'Our people are expecting a better life.'
print(text)
doc = nlp(text)
img = displacy.render(doc, style='dep',jupyter=True)
img
#output_path = Path("./img2.svg")
#output_path.open("w", encoding="utf-8").write(img)
# function for rule 2
def rule2(text):
doc = nlp(text)
pat = []
# iterate over tokens
for token in doc:
phrase = ''
# if the word is a subject noun or an object noun
if (token.pos_ == 'NOUN')\
and (token.dep_ in ['dobj','pobj','nsubj','nsubjpass']):
# iterate over the children nodes
for subtoken in token.children:
# if word is an adjective or has a compound dependency
if (subtoken.pos_ == 'ADJ') or (subtoken.dep_ == 'compound'):
phrase += subtoken.text + ' '
if len(phrase)!=0:
phrase += token.text
if len(phrase)!=0:
pat.append(phrase)
return pat
# Create a df containing sentence and its output for rule 2
row_list = []
for i in range(len(df3)):
sent = df3.loc[i,'Sent']
year = df3.loc[i,'Year']
# Rule 2
output = rule2(sent)
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule2 = pd.DataFrame(row_list)
# Rule 2 output
output_per(df_rule2,'Output')
# create a df containing sentence and its output for rule 2
row_list = []
# df2 contains all the sentences from all the speeches
for i in range(len(df2)):
sent = df2.loc[i,'Sent']
year = df2.loc[i,'Year']
output = rule2(sent)
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule2_all = pd.DataFrame(row_list)
# check rule output on complete speeches
output_per(df_rule2_all,'Output')
# Selecting non-empty outputs
df_show2 = pd.DataFrame(columns=df_rule2_all.columns)
for row in range(len(df_rule2_all)):
if len(df_rule2_all.loc[row,'Output'])!=0:
df_show2 = df_show2.append(df_rule2_all.loc[row,:])
# Reset the index
df_show2.reset_index(inplace=True)
df_show2.drop('index',axis=1,inplace=True)
def rule2_mod(text,index):
doc = nlp(text)
phrase = ''
for token in doc:
if token.i == index:
for subtoken in token.children:
if (subtoken.pos_ == 'ADJ'):
phrase += ' '+subtoken.text
break
return phrase
# rule 1 modified function
def rule1_mod(text):
doc = nlp(text)
sent = []
for token in doc:
# root word
if (token.pos_=='VERB'):
phrase =''
# only extract noun or pronoun subjects
for sub_tok in token.lefts:
if (sub_tok.dep_ in ['nsubj','nsubjpass']) and (sub_tok.pos_ in ['NOUN','PROPN','PRON']):
adj = rule2_mod(text,sub_tok.i)
phrase += adj + ' ' + sub_tok.text
# save the root word of the word
phrase += ' '+token.lemma_
# check for noun or pronoun direct objects
for sub_tok in token.rights:
if (sub_tok.dep_ in ['dobj']) and (sub_tok.pos_ in ['NOUN','PROPN']):
adj = rule2_mod(text,sub_tok.i)
phrase += adj+' '+sub_tok.text
sent.append(phrase)
return sent
# create a df containing sentence and its output for modified rule 1
row_list = []
# df2 contains all the sentences from all the speeches
for i in range(len(df2)):
sent = df2.loc[i,'Sent']
year = df2.loc[i,'Year']
output = rule1_mod(sent)
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule1_mod_all = pd.DataFrame(row_list)
# check rule1 output on complete speeches
output_per(df_rule1_mod_all,'Output')
text = "India has once again shown faith in democracy."
print(text)
doc = nlp(text)
img = displacy.render(doc, style='dep',jupyter=True)
img
#output_path = Path("./img3.svg")
# output_path.open("w", encoding="utf-8").write(img)
# displacy.render(doc, style='dep',jupyter=True)
# rule 3 function
def rule3(text):
doc = nlp(text)
sent = []
for token in doc:
# look for prepositions
if token.pos_=='ADP':
phrase = ''
# if its head word is a noun
if token.head.pos_=='NOUN':
# append noun and preposition to phrase
phrase += token.head.text
phrase += ' '+token.text
# check the nodes to the right of the preposition
for right_tok in token.rights:
# append if it is a noun or proper noun
if (right_tok.pos_ in ['NOUN','PROPN']):
phrase += ' '+right_tok.text
if len(phrase)>2:
sent.append(phrase)
return sent
# create a df containing sentence and its output for rule 4
row_list = []
for i in range(len(df3)):
sent = df3.loc[i,'Sent']
year = df3.loc[i,'Year']
# Rule 3
output = rule3(sent)
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule3 = pd.DataFrame(row_list)
# Rule 3 achieves 40% result
output_per(df_rule3,'Output')
# create a df containing sentence and its output for rule 1
row_list = []
# df2 contains all the sentences from all the speeches
for i in range(len(df2)):
sent = df2.loc[i,'Sent']
year = df2.loc[i,'Year']
output = rule3(sent) # Output
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule3_all = pd.DataFrame(row_list)
# check rule1 output on complete speeches
output_per(df_rule3_all,'Output')
# select non-empty outputs
df_show3 = pd.DataFrame(columns=df_rule3_all.columns)
for row in range(len(df_rule3_all)):
if len(df_rule3_all.loc[row,'Output'])!=0:
df_show3 = df_show3.append(df_rule3_all.loc[row,:])
# reset the index
df_show3.reset_index(inplace=True)
df_show3.drop('index',axis=1,inplace=True)
# separate noun, preposition and noun
prep_dict = dict()
dis_dict = dict()
dis_list = []
# iterating over all the sentences
for i in range(len(df_show3)):
# sentence containing the output
sentence = df_show3.loc[i,'Sent']
# year of the sentence
year = df_show3.loc[i,'Year']
# output of the sentence
output = df_show3.loc[i,'Output']
# iterating over all the outputs from the sentence
for sent in output:
# separate subject, verb and object
n1 = sent.split()[0]
p = sent.split()[1]
n2 = sent.split()[2:]
# append to list, along with the sentence
dis_dict = {'Sent':sentence,'Year':year,'Noun1':n1,'Preposition':p,'Noun2':n2}
dis_list.append(dis_dict)
# counting the number of sentences containing the verb
prep = sent.split()[1]
if prep in prep_dict:
prep_dict[prep]+=1
else:
prep_dict[prep]=1
df_sep3= pd.DataFrame(dis_list)
sort = sorted(prep_dict.items(), key = lambda d:(d[1],d[0]), reverse=True)
sort[:10]
# 'against'
df_sep3[df_sep3['Preposition']=='against']
df_sep3.loc[11272,'Sent']
df_sep3.loc[11513,'Sent']
df_sep3.loc[11618,'Sent']
df_sep3.loc[11859,'Sent']
# rule 0
def rule0(text, index):
doc = nlp(text)
token = doc[index]
entity = ''
for sub_tok in token.children:
if (sub_tok.dep_ in ['compound','amod']):# and (sub_tok.pos_ in ['NOUN','PROPN']):
entity += sub_tok.text+' '
entity += token.text
return entity
# rule 3 function
def rule3_mod(text):
doc = nlp(text)
sent = []
for token in doc:
if token.pos_=='ADP':
phrase = ''
if token.head.pos_=='NOUN':
# appended rule
append = rule0(text, token.head.i)
if len(append)!=0:
phrase += append
else:
phrase += token.head.text
phrase += ' '+token.text
for right_tok in token.rights:
if (right_tok.pos_ in ['NOUN','PROPN']):
right_phrase = ''
# appended rule
append = rule0(text, right_tok.i)
if len(append)!=0:
right_phrase += ' '+append
else:
right_phrase += ' '+right_tok.text
phrase += right_phrase
if len(phrase)>2:
sent.append(phrase)
return sent
# create a df containing sentence and its output for rule 3
row_list = []
# df2 contains all the sentences from all the speeches
for i in range(len(df_show3)):
sent = df_show3.loc[i,'Sent']
year = df_show3.loc[i,'Year']
output = rule3_mod(sent)
dict1 = {'Year':year,'Sent':sent,'Output':output}
row_list.append(dict1)
df_rule3_mod = pd.DataFrame(row_list)
# separate noun, preposition and noun
prep_dict = dict()
dis_dict = dict()
dis_list = []
# iterating over all the sentences
for i in range(len(df_rule3_mod)):
# sentence containing the output
sentence = df_rule3_mod.loc[i,'Sent']
# year of the sentence
year = df_rule3_mod.loc[i,'Year']
# output of the sentence
output = df_rule3_mod.loc[i,'Output']
# iterating over all the outputs from the sentence
for sent in output:
# separate subject, verb and object
n1 = sent[0]
p = sent[1]
n2 = sent[2:]
# append to list, along with the sentence
dis_dict = {'Sent':sentence,'Year':year,'Noun1':n1,'Preposition':p,'Noun2':n2}
dis_list.append(dis_dict)
# counting the number of sentences containing the verb
prep = sent[1]
if prep in prep_dict:
prep_dict[verb]+=1
else:
prep_dict[verb]=1
df_sep3_mod = pd.DataFrame(dis_list)