This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
251 lines (199 loc) · 7.45 KB
/
main.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
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
import sys
import re
import json
import requests
import yaml
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
import twitter.download
import utils.preprocess
import utils.discretization
import zemberek
from vector import vector
from predictors.predict import Predict
from web.db import DB
try:
config_yaml = open("config.yml")
except:
exit("config.yml file is missing, run setup.py")
CONFIG = yaml.safe_load(config_yaml)
def calculate_vector(username, auth_pair, from_file=False, debug=False, verbose=False, r_hash=None):
# Data Collection
if from_file is True:
all_tweets = twitter.download.read_csv(username, r_hash)
else:
all_tweets = twitter.download.get_all_tweets(
username, CONFIG, auth_pair, debug, False, verbose)
# Data Preprocess
## Preprocess
preprocessed = [tweet.map_tweet(utils.preprocess.preprocess)
for tweet in all_tweets]
## Normalization
normalized = []
for tweet in preprocessed:
try:
lang_id = zemberek.find_lang_id(tweet.get_tweet())
if lang_id == "tr":
# continue, tweet is turkish
n_response = zemberek.normalize(tweet.get_tweet())
if n_response.normalized_input:
tweet.set_normalized_tweet(n_response.normalized_input)
normalized.append(tweet)
else:
# not sure if raising an error will cause the halting of the app, if that's the case, we can use a simple print for debugging purposes.
raise AttributeError(
'Problem normalizing input : ' + n_response.error)
else:
# do not handle, tweet is turkish
pass
except zemberek.grpc._channel._InactiveRpcError:
print("Cannot communicate with Zemberek, exiting while normalizing.")
exit()
## Lemmatization
for tweet in normalized:
try:
analysis_result = zemberek.analyze(tweet.get_normalized_tweet())
tweet_lemmas = []
tweet_pos = []
tweet_plural = 0
tweet_words = 0
tweet_full_stop = 0
tweet_unknown = 0
plural_regex = r"A[1-3]pl"
for a in analysis_result.results:
best = a.best
lemmas = ""
for l in best.lemmas:
if l != "UNK":
tweet_lemmas.append(l)
tweet_pos.append(best.pos)
else:
tweet_unknown += 1
if re.search(plural_regex, best.analysis, flags=re.S) is not None:
tweet_plural += 1
if a.token == ".":
tweet_full_stop += 1
tweet_words += 1
for i in tweet_pos:
tweet.add_pos(i)
tweet.set_pos("Plur", tweet_plural)
tweet.set_pos("Word", tweet_words)
tweet.set_pos("Fstop", tweet_full_stop)
tweet.set_pos("Inc", tweet_unknown)
tweet.set_lemma(set(tweet_lemmas))
except zemberek.grpc._channel._InactiveRpcError:
print("Cannot communicate with Zemberek, exiting while analyzing.")
exit()
# Vector Construction
for tweet in normalized:
v = vector.Vector()
v.set_vector(tweet)
tweet.set_vector(v)
## Feature Extraction
## Feature Reduction
## Normalization
sum_vector = np.array([0] * 20)
sum_lemmas = []
for tweet in normalized:
lemma_list = list(tweet.get_lemma())
sum_lemmas += lemma_list
v = np.array(tweet.get_vector().get_vector())
sum_vector = np.add(sum_vector, v)
sum_transformed = sum_vector.reshape(-1, 1)
discretizator = utils.discretization.Discretizer(sum_transformed)
normalized = np.array(discretizator.get_discretized())
if verbose is True:
print(normalized.reshape(1, 20))
print(sum_lemmas)
## TF-IDF Weighting and Word2Vec based Word Embedding
cv = CountVectorizer(max_features=CONFIG["word2vec"]["vector"]["max_features"], ngram_range=(1, 1), max_df=0.8)
top_words = []
try:
word_count_vector = cv.fit_transform(sum_lemmas)
tfidf_transformer = TfidfTransformer(smooth_idf=True, use_idf=True)
tfidf_transformer.fit(word_count_vector)
count_vector = cv.transform(sum_lemmas)
tf_idf_vector = tfidf_transformer.transform(count_vector)
top_words = cv.get_feature_names()
except Exception as e:
print("error accured")
if verbose is True:
print(top_words)
base_url = CONFIG["word2vec"]["service"]["url"] + ":" + \
str(CONFIG["word2vec"]["service"]["port"]) + "/word2vec?word="
vector_np = np.array([0] * CONFIG["word2vec"]["vector"]["dimension"])
total = 0
boundaries = CONFIG["word2vec"]["vector"]["boundaries"]
min_limits = [e['min'] for e in boundaries]
max_limits = [e['max'] for e in boundaries]
for word in top_words:
link = base_url + word
req = requests.get(link)
try:
v = req.json()['word2vec']
if v == '':
v = [0] * CONFIG["word2vec"]["vector"]["dimension"]
v_norm = []
c = 0
for i in v:
v_norm.append((i - min_limits[c]) / ((max_limits[c] - min_limits[c]) / 2) - 1)
c += 1
v_np = np.array(v_norm)
vector_np = np.add(vector_np, v_np)
total += 1
except:
pass
vv = (vector_np/float(total)).tolist()
## Composition of Extracted Features and Word2Vec Vectors
all_vector = vv + normalized.reshape(1, 20).tolist()[0]
if verbose is True:
print(all_vector)
return all_vector
def cluster(vector):
# Clustering
p = Predict()
return p.predict(vector)
def get_ocean(username, user_id, r_hash):
db = DB(CONFIG)
print("Calculating vectors for {}".format(username))
auth_pair = db.get_tokens_by_id(user_id)
alltweets, outtweets = twitter.download.get_all_tweets(username, CONFIG, auth_pair, False, True)
if alltweets:
twitter.download.create_csv(outtweets, username, r_hash)
twitter.download.create_csv(alltweets, username, r_hash, True)
vector = calculate_vector(username, auth_pair, True, False, False, r_hash)
ocean = cluster(vector)
print("OCEAN scores for {} calculated".format(username))
db.finalize_ocean(r_hash, ocean)
def get_public_ocean(username):
pass
# -- TODO --
# generate a push id
# add user to db
# download tweets
# calculate vector
# cluster
# finalize ocean
if __name__ == '__main__':
debugging = False
from_file = False
verbose = False
args = sys.argv
if len(args) > 1:
username = args[1]
if "--debug" in args:
debugging = True
if "--file" in args:
from_file = True
if "--verbose" in args:
verbose = True
else:
username = input("Enter username: ")
auth_pair = ('', '') # fill as needed
vector = calculate_vector(username, auth_pair, from_file, debugging, verbose, "")
print("'{}': {},".format(username, vector))
#print(vector)
print("Predicted OCEAN score: "),
print(cluster(vector))