-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeaturesSelectorNew.py
237 lines (176 loc) · 7.31 KB
/
FeaturesSelectorNew.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
# coding: utf-8
# In[57]:
import string
import pandas as pd
import requests
import json
import TextPreprocessor as tp
import ImpliedPreprocessor as ip
import NgramGenerator as ng
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfTransformer
text_analytics_base_url = "https://westcentralus.api.cognitive.microsoft.com/text/analytics/v2.0/"
from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import TfidfVectorizer
# In[58]:
st = StanfordNERTagger('C:/Users/Admin/Documents/DS/stanford-ner-2017-06-09/classifiers/english.all.3class.distsim.crf.ser.gz',
'C:/Users/Admin/Documents/DS/stanford-ner-2017-06-09/stanford-ner.jar',
encoding='utf-8')
# In[59]:
import os
java_path = "C:/Program Files/Java/jdk1.8.0_25/bin/java.exe"
os.environ['JAVAHOME'] = java_path
# In[3]:
def microsost_ngram_service(list_of_segments):
key_phrase_api_url = text_analytics_base_url + "keyPhrases"
subscription_key="e494891061104f4cabe601316fac1a5b"
assert subscription_key
i= 1
lstnew=[]
for text in list_of_segments:
dicton={}
dicton['id'] = i
dicton['text']=text
lstnew.append(dicton)
i=i+1
documents={}
documents['documents']=lstnew
headers = {"Ocp-Apim-Subscription-Key": subscription_key}
response = requests.post(key_phrase_api_url, headers=headers, json=documents)
key_phrases = response.json()
final_list = key_phrases.get('documents')
newColList=[]
for a in final_list:
if len(a['keyPhrases']) == 1:
term = a['keyPhrases'][0]
newColList.append(term)
return set(newColList)
# In[62]:
# def give_top_utility_score_features_new(tweet,corpus,local_ner_corpus):
# # import os
# # java_path = "C:/Program Files/Java/jdk1.8.0_25/bin/java.exe"
# # os.environ['JAVAHOME'] = java_path
# # list_of_segments = ng.generate_ngrams(tweet)
# tfidf = TfidfVectorizer( stop_words='english',ngram_range=(1,2))
# # messages_bow = bow_transformer.transform(corpus)
# # tfidf_transformer = TfidfTransformer().fit(messages_bow)
# # tfidf = TfidfVectorizer( stop_words='english',ngram_range=(1,2))
# tfs = tfidf.fit_transform(corpus)
# response = tfidf.transform([tweet])
# feature_names = tfidf.get_feature_names()
# for col in response.nonzero()[1]:
# print(feature_names[col], ' - ', response[0, col])
# #print("give :" ,bow_transformer.vocabulary_)
# rlist=[]
# list_of_ners=[]
# global_microsoft_list_of_ngrams=[]
# tokenized_text = word_tokenize(tweet)
# classified_text = st.tag(tokenized_text)
# filteredList=filter(lambda x: x[1]!='O', classified_text)
# filteredList = list(filteredList)
# #print(l)
# if filteredList :
# for objects in filteredList :
# list_of_ners.append(objects[0])
# global_microsoft_list_of_ngrams= microsost_ngram_service(tweet)
# #ngram_corpus=set(ngram_corpus)| microsoft_list
# for ner in list_of_ners:
# if ner not in local_ner_corpus:
# local_ner_corpus.append(ner)
# weight_of_ner=1
# else:
# weight_of_ner=2
# for ngram in global_microsoft_list_of_ngrams:
# if ngram not in feature_names:
# weight_of_ngram=featues
# else:
# if(features_names)
# weight_of_ngram=2
# return (local_ner_corpus,ner_corpus,microsoft_list)
# In[63]:
# give_top_utility_score_features_new("cinema fgfgfjkh",[" would","would","bigdata "," hjgjh"," would"," would"],["Sachin"],["hey dhoni"])
# In[4]:
def initialisation(list_of_collected_tweets):
local_ner_corpus=[]
corpus=[]
for tweet in list_of_collected_tweets:
tweet=ip.imply_preprocess(tweet)
tweet=tp.clean(tweet)
corpus.append(tweet)
tokenized_text = word_tokenize(tweet)
classified_text = st.tag(tokenized_text)
filteredList=filter(lambda x: x[1]!='O', classified_text)
filteredList = list(filteredList)
if filteredList :
for objects in filteredList :
local_ner_corpus.append(objects[0])
return (local_ner_corpus,corpus)
# In[2]:
def give_top_utility_score_features(tweet,corpus,local_ner_corpus):
final_segments={}
local_weight=0
global_weight=0
final_weight_for_segment=0
list_of_segments = ng.generate_ngrams(tweet)
tfidf = TfidfVectorizer( stop_words='english',ngram_range=(1,2))
tfs = tfidf.fit_transform(corpus)
#local_ngrams
response = tfidf.transform([tweet])
local_list_of_ngrams = tfidf.get_feature_names()
local_ngrams={}
for col in response.nonzero()[1]:
local_ngrams[local_list_of_ngrams[col]]= response[0, col]
#print(local_ngrams)
#local_list_of_ngrams=list(set(local_list_of_ngrams))
#global_ner
global_list_of_ners=[]
tokenized_text = word_tokenize(tweet)
classified_text = st.tag(tokenized_text)
filteredList=filter(lambda x: x[1]!='O', classified_text)
filteredList = list(filteredList)
if filteredList :
for objects in filteredList :
global_list_of_ners.append(objects[0])
#global_ngram
global_microsoft_list_of_ngrams=[]
try:
global_microsoft_list_of_ngrams= microsost_ngram_service(tweet)
except:
pass
for segment in list_of_segments:
final_weight_for_segment=0
if segment in global_list_of_ners:
global_weight+=0.3
if segment in global_microsoft_list_of_ngrams:
global_weight+=0.3
if segment in local_ner_corpus:
local_weight+=0.3
if segment in local_ngrams:
local_weight+=local_ngrams[segment]
final_weight_for_segment=((global_weight))+(local_weight)
#print(segment," : ",final_weight_for_segment)
if (final_weight_for_segment >=0.3):
final_segments[segment]=final_weight_for_segment
#print(segment," ; " , final_weight_for_segment)
# new_final_segments = dict(sorted(final_segments.iteritems(), key=operator.itemgetter(1), reverse=True)[:5])
new_final_segments=sorted(final_segments,key=final_segments.get,reverse = True)[:5]
#print(new_final_segments)
new_final_segments=new_final_segments+list(global_list_of_ners) + list(global_microsoft_list_of_ngrams)
#print(new_final_segments)
tweet=tp.clean(tweet)
tweet=ip.imply_preprocess(tweet)
corpus.append(tweet)
local_ner_corpus=set(local_ner_corpus) |set(global_list_of_ners)
return (new_final_segments,corpus,local_ner_corpus)
# In[65]:
# testing=[
# 'IPL is about to begin',
# 'Entertainment is important',
# 'rodger federrer is a respectable sports man',
# 'Football is a interesting game',
# 'Aishwary turns 42 this november',
# 'Dhoni is about o quit from IPL' ]
# In[68]:
# local_ner_corpus=[]