-
Notifications
You must be signed in to change notification settings - Fork 3
/
NLProcessor.py
159 lines (140 loc) · 3.9 KB
/
NLProcessor.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
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from spacy import load
from goose import Goose
from requests import get
import requests
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
nlp = load('en')
def generateEntity(text):
seen = set()
result = []
index = 0
processed_text = nlp(unicode(text))
for ent in processed_text.ents:
label = ent.label_
if label == u'PERSON' or label == u'NORP' or label == u'ORG' or label == u'GPE':
if label == u'PERSON':
result.append(ent.text)
seen.add(ent.text)
elif not ent.text in seen:
if index % 3 == 0:
result.append(ent.text)
seen.add(ent.text)
index+=1
return result
def countEntity(text):
entityCount = []
PERSON = 0
NORP = 1
FACILITY = 2
ORG = 3
GPE = 4
LOC = 5
PRODUCT = 6
EVENT = 7
WORK_OF_ART = 8
LANGUAGE = 9
DATE = 10
TIME = 11
PERCENT = 12
MONEY = 13
QUANTITY = 14
ORDINAL = 15
CARDINAL = 16
OTHER = 17
i = 0
while( i < 18):
entityCount.append(0)
i+=1
processed_text = nlp(unicode(text))
for ent in processed_text.ents:
if ent.label_ == u'PERSON':
entityCount[PERSON]+=1
elif ent.label_ == u'NORP':
entityCount[NORP]+=1
elif ent.label_ == u'FACILITY':
entityCount[FACILITY] +=1
elif ent.label_ == u'ORG':
entityCount[ORG] +=1
elif ent.label_ == u'GPE':
entityCount[GPE] += 1
elif ent.label_ == u'LOC':
entityCount[LOC] +=1
elif ent.label_ == u'PRODUCT':
entityCount[PRODUCT] += 1
elif ent.label_ == u'MONEY':
entityCount[MONEY] +=1
elif ent.label_ == u'QUANTITY':
entityCount[QUANTITY] +=1
elif ent.label_ == u'ORDINAL':
entityCount[ORDINAL] +=1
elif ent.label_ == u'CARDINAL':
entityCount[CARDINAL] += 1
else:
entityCount[OTHER] += 1
return entityCount
def getLocations(text):
seen = set()
locations = []
processed_text = nlp(unicode(text))
for ent in processed_text.ents:
if ent.label_ == u'GPE':
if not ent.text.upper() in seen:
locations.append(str(ent.text.encode("ascii", "ignore").upper()))
seen.add(ent.text.upper())
return locations
def getNGrams(text):
text = re.sub(r'[^\w]', ' ', text)
N_GRAM_SIZE = 4
ngrams = []
processed_text = nlp(unicode(text))
index = 0
gramCount = 0
for i in range(0,len(processed_text) - 3):
localGram = []
for i in range(0,4):
localGram.append(str(processed_text[index]).lower())
index = index + 1
gramCount+=1
index = gramCount
ngrams.append(localGram)
return ngrams
def HTMLParser(url):
response = get(url)
extractor = Goose()
article = extractor.extract(raw_html=response.content)
text = article.cleaned_text
results = getLocations(text)
return results
def sentiment(textbody):
neg = 0.0
pos = 0.0
neu = 0.0
compound = 0.0
total = 0
sentences = nlp(unicode(textbody))
sith = SentimentIntensityAnalyzer()
for sentence in sentences.sents:
score = sith.polarity_scores(sentence.text)
neg += score['neg']
neu += score['neu']
pos += score['pos']
compound += score['compound']
total+=1
results = []
results.append(neg / total)
results.append(pos / total)
results.append(neu / total)
results.append(compound / total)
return results
def getMLP(textbody):
send = []
entities = countEntity(textbody)
ngrams = getNGrams(textbody)
sent = sentiment(textbody)
send.append(entities)
send.append(sent)
return send