-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCosine_similarity.py
167 lines (152 loc) · 5.05 KB
/
Cosine_similarity.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
import numpy as np
import pandas as pd
import csv
import demoji
import re
import json
import nltk
import string
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
nltk.download('stopwords')
nltk.download('punkt')
def clean_text(df,input_list=False):
all_reviews = list()
if input_list:
lines = df
else:
lines = df["text_en"].values.tolist()
for text in lines:
text = text.lower()
pattern = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
text = pattern.sub('', text)
text = re.sub(r"[,.\"!@#$%^&*(){}?/;`~:<>+=-]", "", text)
text = re.sub('^rt ', ' ', text) # remove 'rt'
text = re.sub('\n', ' ', text) # remove '\n'
text = re.sub('[0-9]+', ' ', text) # remove numbers
text = re.sub('\s+', ' ', text) # remove extra whitespaces
text = re.sub('^\s+', '', text) # remove space(s) at start
text = re.sub('\s+$', '', text) # remove space(s) at end
tokens = word_tokenize(text)
table = str.maketrans('', '', string.punctuation)
stripped = [w.translate(table) for w in tokens]
words = [word for word in stripped if word.isalpha()]
stop_words = set(stopwords.words("english"))
stop_words.discard("not")
PS = PorterStemmer()
words = [PS.stem(w) for w in words if not w in stop_words]
words = ' '.join(words)
all_reviews.append(words)
return all_reviews
a1 = open('/Users/alisalehi/PycharmProjects/MT/TFIDFCOSINE/all_TR_50638_Topics.json')
all_tweets = json.load(a1)
sc = open('/Users/alisalehi/PycharmProjects/MT/TFIDFCOSINE/FinalJJ_scored_2664.json')
manuscored = json.load(sc)
#extracting tweets related to the list below
vaxrel = ['vaccine','vaccination','vaxx','vaxxer','pfizer', 'moderna', 'johnson', 'j&j', 'astrazeneca', 'sputnik', 'booster', 'dose', 'vaccinated', 'unvaccinated','vaccinehesitancy']
kw_tweets = []
for tweet in all_tweets:
for item in vaxrel:
if item in tweet['text_en']:
kw_tweets.append(tweet)
break
print('len of keyword related',len(kw_tweets)) #6906
print(kw_tweets[0])
ids = []
filtered =[]
for dic1 in kw_tweets:
dic2 = {}
dic2['id'] = dic1['id']
dic2['text_en'] = dic1['text_en']
dic2['Stance'] = ''
filtered.append(dic2)
# ids.append(dic1['id'])
print('len of columns filtered',len(filtered))
#extracting the scored ones and add all to a list
all_scored = []
for tw in manuscored:
if tw['Stance'] == '1':
all_scored.append(tw)
for tw in manuscored:
if tw['Stance'] == '0':
all_scored.append(tw)
for tw in manuscored:
if tw['Stance'] == '-1':
all_scored.append(tw)
print('len of allscored' ,len(all_scored)) #737
# print([t2['id'] for t2 in all_scored])
noscores_kw_tweets = []
for t1 in filtered:
if int(t1['id']) not in [int(t2['id']) for t2 in all_scored]:
# if t1['text_en'] == [t2['text_en'] for t2 in all_scored]:
noscores_kw_tweets.append(t1)
ids.append(t1['id'])
print('len of noscores_kw_tweets',len(noscores_kw_tweets))
total = all_scored+noscores_kw_tweets
print('len of total', len(total))
print('this', all_scored[238] ,total[238])
#1--237 0--479 -1--21 ''--6485
# test = []
# for i in total:
# if i['Stance'] == '':
# test.append(i)
# print(len(test))
# #extracting tex_en
all_txten = []
for twt in total:
all_txten.append(twt['text_en'])
print('alltxten',len(all_txten))
print(all_txten[0])
clean = clean_text(all_txten, input_list=True)
print(clean[0])
#
print(len(clean))
# #
from sklearn.feature_extraction.text import TfidfVectorizer
TV1 = TfidfVectorizer(min_df=5)
allvec = TV1.fit_transform(clean).toarray()
# print(type(allvec))
print(allvec.shape) #(7195, 4054)
# # #
from sklearn.metrics.pairwise import cosine_similarity,cosine_distances
#
cs_sim = cosine_similarity(allvec)
print(cs_sim.shape)
pro_sim = cs_sim[0:237, 737:].max(axis =0)
print(pro_sim.shape)
print(pro_sim)
# # # #
neut_sim = cs_sim[237:479, 737:].max(axis = 0)
print(neut_sim)
# # # #
against_sim = cs_sim[479:737, 737:].max(axis=0)
print(against_sim)
for i in range(0,len(ids)):
max = np.argmax([pro_sim[i],neut_sim[i],against_sim[i]])
if max == 0:
st = 'pro'
elif max == 1:
st = 'neutral'
else:
st = 'against'
for tweet in total[737:]:
if int(tweet['id']) == int(ids[i]):
tweet['Stance'] = st
break
# elif tweet['Stance'] == '1':
# tweet['Stance'] = 'pro'
# elif tweet['Stance'] == '0':
# tweet['Stance'] = 'neutral'
# elif tweet['Stance'] == '-1':
# tweet['Stance'] = 'against'
for tweet in total[:737]:
if tweet['Stance'] == '1':
tweet['Stance'] = 'pro'
elif tweet['Stance'] == '0':
tweet['Stance'] = 'neutral'
elif tweet['Stance'] == '-1':
tweet['Stance'] = 'against'
n = json.dumps(total)
stadded = open('/Users/alisalehi/PycharmProjects/MT/TFIDFCOSINE/stance5_added_7195_mean.json', 'w')
stadded.write(n)