-
Notifications
You must be signed in to change notification settings - Fork 0
/
phrases.py
164 lines (138 loc) · 4.38 KB
/
phrases.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
import pandas as pd
from pdb import set_trace as trace
import nltk
import pickle
from nltk import Nonterminal, nonterminals, Production, parse_cfg
from nltk.corpus import treebank
from nltk.tag.stanford import NERTagger
df = pd.read_csv('input_data_set.csv') # Reading Data File
cmpy_data = pd.read_csv('List_Of_Company_Names.csv', header=None)
cmpy_data.rename(columns={0: 'Company_Names'}, inplace=True)
count = 0
df = df.fillna('') # Filling NaN
extra_row = []
lst = []
"""
Grammars For Detecting Loss Cause...(Filtering Important Phrases)
"""
pattern = r"""
NP: {<NNP>*<NN>*<NNP>+}
XY: {<DT>?<NN|NNPS|NNS>?<NN|VB|VBZ|VBD|VBN|VBP>*<IN>*<JJ|JJR>+<TO|IN>*<VB|NNS|NNPS|NN>+<RB>*<VB>*<JJ>*}
{<NN|NNPS>+<MD|VBP|VBZ>+<RB>+<VB>+}
{<NP>+<VBZ>+<TO>*<JJ>}
{<RB>+<CD>*<JJ>*<TO|IN>*<JJ|NN|NNS|NNP|NNPS|NP>+}
{<JJ|JJS|JJR>+<NNP>+}
{<RB>*<VBG>+<NN>+}
{<NNP>+<IN>+<NNP|NN|NNS|NNPS>+}
ZX: {<VBD|JJ>+<TO|VB>+<NP>}
"""
NPChunker = nltk.RegexpParser(pattern) # Creating Chunks corresponding to above mentioned Grammars
cmpy_lst=[]
cause_str_lst = []
"""
Calculating Parts Of Speech Tags and getting Chunked data.
"""
for index, row in df.iterrows():
text = row['Loss/Cancel Details']
text = text.replace('.',' . ')
if text!='':
#if index == 8:
# trace()
#tokens = text.split()
tokens = nltk.word_tokenize(text)
result = NPChunker.parse(nltk.pos_tag(tokens))
count = 0
cmpy = []
cause = []
tmp_lst1 = []
tmp_lst2 = []
while count<len(result):
x = result[count]
if isinstance(x, nltk.tree.Tree):
root = x.node
if root == "NP":
tmp = []
for (a, b) in x.leaves():
tmp.append(a)
tmp_lst1.append(" ".join(tmp))
elif root == "XY":
tmp = []
for (a, b) in x.leaves():
tmp.append(a)
tmp_lst2.append(" ".join(tmp))
elif root == "ZX":
tmp = []
for (a, b) in x.leaves():
if b=="NNP":
tmp.append(a)
tmp_lst1.append(" ".join(tmp))
count+=1
cmpy_lst.append(tmp_lst1)
cause_str_lst.append(tmp_lst2)
lst.append(nltk.pos_tag(tokens))
else:
lst.append('')
cmpy_lst.append('')
cause_str_lst.append('')
df['Pos_Tags'] = lst
tree = []
org_lst = []
"""
Getting Organization List Using NLP-NER Techniques
"""
for index, row in df.iterrows():
lst = []
tags = row['Pos_Tags']
if tags == "":
org_lst.append('')
continue
entities = nltk.chunk.ne_chunk(tags)
x = entities.subtrees()
l = len(entities)
count = 0
while count < l:
if isinstance(entities[count], nltk.tree.Tree):
if entities[count].__dict__.get('node') == "ORGANIZATION":
lst.append(entities[count].leaves()[0][0])
count+=1
org_lst.append(list(set(lst)))
tree.append(entities)
word_lst = []
"""
Getting list of Adj, Verb and Adverb appears in sentence
"""
for index, row in df.iterrows():
lst = []
x = row['Pos_Tags']
if x != "":
for (a,b) in x:
if "JJ" in b:
lst.append(a)
elif "RB" in b:
lst.append(a)
elif "VB" in b:
lst.append(a)
word_lst.append(lst)
else:
word_lst.append('')
df['Word_lst'] = word_lst # Adding Adj, Verb, Adverb info to Data set
df['Org_lst'] = org_lst # Adding Organization List
df['Lost_Cause'] = cause_str_lst # Adding Loss Cause
df['Company_Names'] = cmpy_lst # Adding Organization Names (Assume NNP Tags are most of the times corresponds to ORGANIZATION)
df.to_csv('write_into_file.csv', index=False)
print 'Company Names Verification'
company_lst = []
for item in cmpy_lst:
company_names = " ".join(cmpy_data.Company_Names)
clst=[]
if not item:
company_lst.append('')
continue
else:
for it in item:
lst = it.split()
clst.append([i for i in lst if i in company_names])
company_lst.append(clst)
trace()
df.to_csv('file_intel_1.csv', index=False) # Writing Data Frame to CSV File.
trace() # TO halt the program at this stage.