-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
107 lines (86 loc) · 2.9 KB
/
preprocess.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
import csv
import nltk
import numpy as np
from nltk.corpus import stopwords
from gensim.models import Word2Vec
from sklearn.feature_extraction.text import CountVectorizer
def get_duration(st, et):
minutes = int((et - st) / 60)
seconds = (et - st) % 60
duration = f"{minutes} min {seconds:.2f} sec"
return duration
def check_periods(full_text):
checked_text = []
for char in full_text:
if char in ['.', ',', ':', ';']:
char = char + " "
checked_text.append(char)
return "".join(checked_text)
def preprocess_line(line):
sent_tokens = [word.casefold() for word in nltk.tokenize.word_tokenize(line)]
processed_tokens = []
for word in sent_tokens:
if word.isalpha():
processed_tokens.append(word)
elif word.isnumeric():
processed_tokens.append("<NUM>")
return processed_tokens
def preprocess(text):
all_lines = []
all_tokens = []
for line in nltk.tokenize.sent_tokenize(text):
all_lines.append(line)
processed_tokens = preprocess_line(line)
all_tokens.append(processed_tokens)
return all_lines, all_tokens
def get_all_texts(fraction=1):
all_titles = []
all_texts = []
all_proc_texts = []
true_or_fake = []
with open("data/Fake.csv", "r") as csvfile:
for row in csv.reader(csvfile):
title, text, _, _ = row
text = check_periods(text)
all_titles.append(title)
all_texts.append(text)
all_proc_texts.append(preprocess(text))
true_or_fake.append(1)
with open("data/True.csv", "r") as csvfile:
for row in csv.reader(csvfile):
title, text, _, _ = row
text = check_periods(text)
all_titles.append(title)
all_texts.append(text)
all_proc_texts.append(preprocess(text))
true_or_fake.append(0)
return all_titles, all_texts, all_proc_texts, true_or_fake
def get_clusters():
clusters = []
with open("data/clusters.txt", "r") as source:
for line in source:
clusters.append(int(line.rstrip()))
return clusters
def get_word_embedding(word, model):
sw = stopwords.words('english')
try:
word_emb = model.wv[word]
except:
word_emb = 0
if word in sw:
word_emb = word_emb * 0.5
return word_emb
def get_sent_embedding(sent_tokens, model):
sent_embedding = np.zeros([100,])
for word in sent_tokens:
tok_emb = get_word_embedding(word, model=model)
sent_embedding += tok_emb
return sent_embedding
def get_text_embedding(prep_text, model):
text_embedding = np.zeros([100,])
for sent in prep_text:
sent_emb = get_sent_embedding(sent, model=model)
text_embedding += sent_emb
return text_embedding
def get_bow(texts_list, sw="english"):
return CountVectorizer(stop_words=sw).fit_transform(texts_list).toarray()