forked from aarsanjani/AlternusVera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensational.py
172 lines (162 loc) · 3.94 KB
/
sensational.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
import numpy as np
import pandas as pd
import warnings
import pickle
import string
import random
import nltk
nltk.download('punkt')
from gensim.models.doc2vec import Doc2Vec, TaggedDocument
from nltk.tokenize import word_tokenize
sensationalist_phrases = [
# From Analyst's Desktop Binder of Homeland Security https://www.scribd.com/doc/82701103/Analyst-Desktop-Binder-REDACTED
'Assassination',
'Attack',
'Domestic security',
'Law enforcement',
'Disaster',
'National preparedness',
'Response',
'Recovery',
'Emergency response',
'First responder',
'Militia',
'Shooting',
'Evacuation',
'Hostage',
'Explosion',
'Organized crime',
'Gangs',
'National security',
'State of emergency',
'Security breach',
'Threat',
'Standoff',
'Lockdown',
'Bomb',
'Riot',
'Emergency Landing',
'Incident',
'Suspicious',
'Nuclear threat',
'Hazardous',
'Infection',
'Outbreak',
'Contamination',
'Terror',
'Epidemic',
'Critical Infrastructure',
'National infrastructure',
'Transportation security',
'Grid',
'Outage',
'Disruption',
'Violence',
'Drug cartel',
'Narcotics',
'Shootout',
'Trafficking',
'Kidnap',
'Illegal',
'Smuggling',
'Al Qaeda',
'Terror attack',
'Weapon',
'Improvised explosive device',
'Suicide bomber',
'Suicide attack',
'Hurricane',
'Tornado',
'Tsunami',
'Earthquake',
'Tremor',
'Flood',
'Storm',
'Extreme weather',
'Forest fire',
'Ice',
'Stranded',
'Wildfire',
'Avalanche',
'Blizzard',
'Lightening',
'Emergency Broadcast System',
'Cyber Security',
'DDOS',
'Denial of service',
'Malware',
'Phishing',
'Cyber attack',
'Cyber terror'
]
def sensphrasedetect(str):
sum =0
for x in sensationalist_phrases:
if x.lower() in str.lower():
sum+=1
return sum
def prediction(xtest):
pickleModel = "/content/gdrive/My Drive/Drifters/Models/sensational_Model.pkl"
pickle_in = open(pickleModel, "rb")
loadData = pickle.load(pickle_in)
dataset = loadData.predict(xtest)
for i in dataset:
if(i==0):
return 0
return 1
def processFakeNews(fnews):
count = lambda l1,l2: sum([1 for x in l1 if x in l2])
pcCount=[]
capCount=[]
digCount=[]
lenCount=[]
profanCount=[]
sensphrCount=[]
f_news = pd.DataFrame()
pcCount.append(sum(1 for c in fnews if c=="!" or c=="?"))
capCount.append(sum(1 for c in fnews if c.isupper()))
digCount.append(sum(1 for c in fnews if c.isdigit()))
lenCount.append(len(fnews))
sensphrCount.append(sensphrasedetect(fnews))
# for x in test_reviewed_docs:
# try:
# profanCount.append(float(len([w for w in x if w.lower() in PROFANITY]))/len(x))
# except:
# profanCount.append(0)
data = {'puncCount': pcCount,
'capCount': capCount,
'digCount': digCount,
'lenCount': lenCount,
'profanCount': profanCount,
'sensphrCount': sensphrCount}
# f_news = pd.DataFrame(data)
f_news['puncCount']=pcCount
f_news['capCount']=capCount
f_news['digCount']=digCount
f_news['lenCount']=lenCount
# f_news['profanCount']=profanCount
f_news['profanCount']=0
f_news['sensPhrCount']=sensphrCount
return f_news
def newDataset(xtest):
return xtest
def buildSensationalCol(fnews,f_news):
savedModel = "/content/gdrive/My Drive/Drifters/Models/sensationalism.model"
sensationCol=[]
model= Doc2Vec.load(savedModel)
test_data = word_tokenize(fnews.lower())
v1 = model.infer_vector(test_data)
similar_doc = model.docvecs.most_similar([v1])
sensationCol.append(similar_doc[0][0])
sensationCol=list(map(int, sensationCol))
f_news['sensationCol']=sensationCol
return f_news
class sensational:
def __init__(self, fnews):
self.f_news = processFakeNews(fnews)
self.xtest = buildSensationalCol(fnews,self.f_news)
self.x_test = self.xtest
def predict(self):
return prediction(self.x_test)
def checkNewDataset(self):
return newDataset(self.x_test)