forked from Nikhil-Jones/Fake-News-Detector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmap.py
More file actions
414 lines (330 loc) · 17 KB
/
hashmap.py
File metadata and controls
414 lines (330 loc) · 17 KB
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
import csv
import re
from collections import defaultdict
from config import SOURCE_CREDIBILITY, FREQUENCY_BIAS_THRESHOLD, LEGITIMATE_NEWS_KEYWORDS
class EnhancedWordFrequencyHashMap:
def __init__(self):
self.fake_word_counts = defaultdict(int)
self.real_word_counts = defaultdict(int)
self.medical_fake_words = defaultdict(int)
self.absurd_claim_words = defaultdict(int)
self.total_fake_words = 0
self.total_real_words = 0
self._load_enhanced_datasets()
def _load_enhanced_datasets(self): # O(F + R + K) where F is fake dataset lines, R is real dataset lines, K is the suspicious keywords lines.
try:
self._load_fake_dataset()
self._load_real_dataset()
self._load_suspicious_keywords()
self._create_specialized_word_maps()
print(f"Loaded {self.total_fake_words} words from enhanced fake dataset")
print(f"Loaded {self.total_real_words} words from real dataset")
except FileNotFoundError:
print("Dataset files not found, using enhanced default frequencies")
self._create_enhanced_default_frequencies()
def _load_suspicious_keywords(self):
try:
with open('suspicious_keywords.txt', 'r', encoding='utf-8') as file:
for line in file:
line = line.strip()
if line and not line.startswith('#'):
parts = line.split(',')
if len(parts) == 2:
word = parts[0].strip()
try:
score = float(parts[1].strip())
count = int(score * 100)
self.fake_word_counts[word] += count
self.total_fake_words += count
except ValueError:
continue
except FileNotFoundError:
print("suspicious_keywords.txt not found, skipping")
def _create_specialized_word_maps(self): # O(M + A) where M = number of medical terms, A = number of absurd terms.
medical_fake_terms = {
"chocolate": 20, "cure": 50, "miracle": 40, "natural": 30,
"doctors": 25, "hate": 15, "suppress": 10, "big": 8,
"pharma": 12, "industry": 8, "secret": 18, "hidden": 10,
"ancient": 8, "remedy": 15, "healing": 12, "detox": 10,
"cleanse": 8, "toxins": 6, "breakthrough": 5
}
absurd_terms = {
"aliens": 15, "landed": 8, "ufo": 10, "extraterrestrial": 5,
"time": 12, "travel": 8, "teleport": 3, "psychic": 5,
"supernatural": 6, "immortal": 4, "forever": 6, "never": 10,
"impossible": 8, "defies": 3, "physics": 5, "laws": 4
}
for word, count in medical_fake_terms.items():
self.medical_fake_words[word] = count
for word, count in absurd_terms.items():
self.absurd_claim_words[word] = count
def _load_fake_dataset(self): # O(F*Wf) where F is the number of fake dataset lines, Wf is the avg number of word per fake article
try:
with open('dataset/fake.csv', 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
text = row.get('text', '') or row.get('content', '') or row.get('article', '')
if text:
words = self._tokenize_text(text)
for word in words:
self.fake_word_counts[word] += 1
self.total_fake_words += 1
except FileNotFoundError:
self._create_enhanced_fake_frequencies()
def _load_real_dataset(self): # O(R*Wr) where R is the number of real dataset lines, Wf is the avg number of word per real article
try:
with open('dataset/real.csv', 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
text = row.get('text', '') or row.get('content', '') or row.get('article', '')
if text:
words = self._tokenize_text(text)
for word in words:
self.real_word_counts[word] += 1
self.total_real_words += 1
except FileNotFoundError:
self._create_default_real_frequencies()
def _create_enhanced_fake_frequencies(self): # O(N) where N is number of fake words
enhanced_fake_words = {
"miracle": 80, "cure": 70, "chocolate": 30, "natural": 40,
"doctors": 35, "hate": 25, "suppress": 15, "secret": 45,
"breakthrough": 20, "ancient": 15, "remedy": 25, "healing": 20,
"aliens": 25, "landed": 15, "ufo": 20, "time": 30, "travel": 20,
"teleport": 8, "psychic": 12, "supernatural": 15, "immortal": 10,
"forever": 18, "impossible": 22, "defies": 8, "physics": 10,
"shocking": 45, "breaking": 40, "exclusive": 25, "urgent": 20,
"alert": 18, "warning": 15, "exposed": 12, "revealed": 10,
"amazing": 15, "incredible": 12, "unbelievable": 8, "viral": 10
}
for word, count in enhanced_fake_words.items():
self.fake_word_counts[word] = count
self.total_fake_words += count
def _create_enhanced_default_frequencies(self):
self._create_enhanced_fake_frequencies()
self._create_default_real_frequencies()
self._load_suspicious_keywords()
self._create_specialized_word_maps()
def _create_default_real_frequencies(self): # O(L+D) where L is the length of LEGITIMATE_NEW_KEYWORDS and D is the number of real words.
real_words = {
"according": 40, "research": 35, "study": 30, "report": 25,
"analysis": 20, "data": 18, "evidence": 15, "official": 12,
"confirmed": 10, "verified": 8, "source": 6, "journal": 5,
"published": 4, "peer": 3, "reviewed": 3, "scientists": 15,
"researchers": 12, "university": 8, "institute": 6
}
for word in LEGITIMATE_NEWS_KEYWORDS:
real_words[word] = real_words.get(word, 5) + 5
for word, count in real_words.items():
self.real_word_counts[word] = count
self.total_real_words += count
def _tokenize_text(self, text): # O(N) where N is the length of text.
words = re.findall(r'\b\w+\b', text.lower())
return words
def analyze_text_bias(self, text): # O(W) where W is the total words of input text
words = self._tokenize_text(text)
word_biases = []
fake_biased_words = []
real_biased_words = []
medical_misinformation_score = 0.0
absurd_claim_score = 0.0
for word in words:
bias = self.get_word_bias(word)
word_biases.append(bias)
if word in self.medical_fake_words:
medical_misinformation_score += 0.2
if word in self.absurd_claim_words:
absurd_claim_score += 0.2
if bias > FREQUENCY_BIAS_THRESHOLD:
fake_biased_words.append((word, bias))
elif bias < -FREQUENCY_BIAS_THRESHOLD:
real_biased_words.append((word, bias))
if word_biases:
base_bias = sum(word_biases) / len(word_biases)
else:
base_bias = 0.0
medical_penalty = min(medical_misinformation_score * 0.3, 0.5)
absurd_penalty = min(absurd_claim_score * 0.3, 0.5)
enhanced_bias = base_bias + medical_penalty + absurd_penalty
enhanced_bias = min(max(enhanced_bias, -1.0), 1.0)
problematic_patterns = self._detect_problematic_patterns(text)
fake_biased_words.sort(key=lambda x: x[1], reverse=True)
real_biased_words.sort(key=lambda x: x[1])
return {
'overall_bias': enhanced_bias,
'base_bias': base_bias,
'medical_misinformation_score': medical_misinformation_score,
'absurd_claim_score': absurd_claim_score,
'fake_biased_words': fake_biased_words[:5],
'real_biased_words': real_biased_words[:5],
'problematic_patterns': problematic_patterns,
'total_words': len(words),
'analyzed_words': len(word_biases)
}
def _detect_problematic_patterns(self, text): #
text_lower = text.lower()
patterns = []
medical_patterns = [
(r"chocolate\s+cures?\s+cancer", "Medical impossibility: chocolate curing cancer"),
(r"soda\s+improves?\s+lifespan", "Medical impossibility: soda improving health"),
(r"drinking\s+\d+\s+liters?\s+daily", "Dangerous consumption advice"),
(r"doctors?\s+hate\s+this", "Anti-medical establishment rhetoric"),
(r"natural\s+cure\s+for\s+cancer", "Unproven cancer cure claims")
]
absurd_patterns = [
(r"aliens?\s+landed", "Impossible alien encounter claim"),
(r"time\s+travel\s+(invented|discovered)", "Impossible technology claim"),
(r"live\s+forever", "Impossible longevity claim"),
(r"psychic\s+powers?\s+real", "Supernatural ability claim"),
(r"teleportation\s+(invented|possible)", "Impossible technology claim")
]
all_patterns = medical_patterns + absurd_patterns
for pattern, description in all_patterns:
if re.search(pattern, text_lower):
patterns.append(description)
return patterns
def get_word_bias(self, word): # O(1)
word = word.lower()
fake_freq = self.fake_word_counts.get(word, 0)
real_freq = self.real_word_counts.get(word, 0)
if word in self.medical_fake_words:
fake_freq += self.medical_fake_words[word]
if word in self.absurd_claim_words:
fake_freq += self.absurd_claim_words[word]
if fake_freq == 0 and real_freq == 0:
return 0.0
if fake_freq == 0:
return -1.0
if real_freq == 0:
return 1.0
fake_prob = fake_freq / (self.total_fake_words + 100)
real_prob = real_freq / (self.total_real_words + 100)
total_prob = fake_prob + real_prob
if total_prob == 0:
return 0.0
bias = (fake_prob - real_prob) / total_prob
return bias
def get_frequency_explanations(self, analysis): # O(!)
explanations = []
overall_bias = analysis['overall_bias']
base_bias = analysis['base_bias']
medical_score = analysis['medical_misinformation_score']
absurd_score = analysis['absurd_claim_score']
problematic_patterns = analysis['problematic_patterns']
if overall_bias > 0.7:
explanations.append("CRITICAL: Word frequency strongly indicates fake news")
elif overall_bias > FREQUENCY_BIAS_THRESHOLD:
explanations.append("Word frequency analysis suggests fake news")
elif overall_bias < -FREQUENCY_BIAS_THRESHOLD:
explanations.append("Word frequency suggests legitimate news")
else:
explanations.append("Word frequency analysis inconclusive")
if medical_score > 0.5:
explanations.append("HIGH: Medical misinformation language detected")
elif medical_score > 0.2:
explanations.append("Medical claim language patterns found")
if absurd_score > 0.5:
explanations.append("HIGH: Absurd/impossible claim language detected")
elif absurd_score > 0.2:
explanations.append("Unusual claim language patterns found")
if problematic_patterns:
for pattern in problematic_patterns[:2]:
explanations.append(f"DETECTED: {pattern}")
if analysis['fake_biased_words']:
top_fake_words = [word for word, _ in analysis['fake_biased_words'][:3]]
explanations.append(f"Fake-biased words: {', '.join(top_fake_words)}")
if analysis['real_biased_words']:
top_real_words = [word for word, _ in analysis['real_biased_words'][:3]]
explanations.append(f"Real-biased words: {', '.join(top_real_words)}")
return explanations
class SourceCredibilityHashMap:
def __init__(self):
self.source_map = SOURCE_CREDIBILITY.copy()
self._add_enhanced_sources()
def _add_enhanced_sources(self): # O(S) where S is the total added sources count
additional_reliable = {
"pubmed.ncbi.nlm.nih.gov": "reliable",
"scholar.google.com": "reliable",
"arxiv.org": "reliable",
"nature.com": "reliable",
"science.org": "reliable",
"nejm.org": "reliable",
"thelancet.com": "reliable",
"bmj.com": "reliable"
}
additional_unreliable = {
"healingpowers.net": "unreliable",
"alientruth.org": "unreliable",
"timetravel-news.com": "unreliable",
"psychic-powers.net": "unreliable",
"immortality-secrets.com": "unreliable",
"chocolate-cancer-cure.org": "unreliable"
}
self.source_map.update(additional_reliable)
self.source_map.update(additional_unreliable)
def check_source(self, url): # O(P) where P is number of patterns
"""Enhanced source credibility check"""
if not url:
return 'unknown'
domain = self._extract_domain(url)
if not domain:
return 'unknown'
credibility = self.source_map.get(domain, 'unknown')
if credibility == 'unknown':
credibility = self._assess_domain_patterns(domain)
return credibility
def _assess_domain_patterns(self, domain): # O(P) where P is number of patterns
domain_lower = domain.lower()
suspicious_patterns = [
"miracle", "cure", "secret", "alien", "psychic", "supernatural",
"conspiracy", "truth", "exposed", "revealed", "shocking"
]
for pattern in suspicious_patterns:
if pattern in domain_lower:
return "unreliable"
reliable_patterns = [
".edu", ".gov", ".org", "university", "institute", "research",
"medical", "health", "science", "academic"
]
for pattern in reliable_patterns:
if pattern in domain_lower:
return "reliable"
return "unknown"
def _extract_domain(self, url): # O(!)
if not url.startswith(('http://', 'https://')):
url = 'http://' + url
try:
domain = url.split('//')[1].split('/')[0]
if domain.startswith('www.'):
domain = domain[4:]
return domain
except:
return None
def get_source_explanation(self, url):
credibility = self.check_source(url)
if credibility == 'reliable':
return f"Source: {url} (verified reliable source)"
elif credibility == 'unreliable':
return f"Source: {url} (flagged as unreliable source)"
else:
return f"Source: {url} (unknown credibility - proceed with caution)"
if __name__ == "__main__":
word_freq = EnhancedWordFrequencyHashMap()
test_cases = [
"Scientists have confirmed that chocolate cures cancer in just one week.",
"Aliens landed in Times Square yesterday and gave out free iPhones.",
"New research published in peer-reviewed journal shows promising results.",
"Drinking 10 liters of soda daily will improve your lifespan by 20 years."
]
print("Enhanced HashMap Analysis:")
print("=" * 60)
for i, text in enumerate(test_cases, 1):
print(f"\n{i}. Text: {text}")
analysis = word_freq.analyze_text_bias(text)
explanations = word_freq.get_frequency_explanations(analysis)
print(f" Overall Bias: {analysis['overall_bias']:.2f}")
print(f" Medical Score: {analysis['medical_misinformation_score']:.2f}")
print(f" Absurd Score: {analysis['absurd_claim_score']:.2f}")
print(f" Explanations:")
for exp in explanations[:3]:
print(f" - {exp}")
print("-" * 60)