-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_tools.py
259 lines (213 loc) · 6.66 KB
/
word_tools.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
252
253
254
255
256
257
258
259
import re
import sys
from parse import parse_tweet, parse_archive, write_archive
def get_list_tweet(filestr) :
list_tw = []
file = open(filestr, "r")
for line in file.readlines() :
tw = parse_tweet(line)
if tw['tag'] != "irr" :
list_tw.append(tw['text'])
return list_tw
def format_sentence(sentence):
toReturn = []
for word in re.sub(r'[^\w\s]', '', sentence).split(" ") :
# print(word)
if len(word) > 0 and word[0] != "@" :
if len(word) > 0 and word[0] == "#" :
toReturn.append(word[1:].lower())
else :
toReturn.append(word.lower())
return toReturn
def parse_sentence(sentence, dictionary) :
for word in sentence :
if not word in dictionary :
dictionary.append(word)
def build_dico(list_tweet) : # text is a list of sentences.
dictionary = []
for tw in list_tweet :
processed = processTweet(tw).split(" ")
for word in processed :
if not word in dictionary :
dictionary.append(word)
return dictionary
# helper function to clean tweets
def processTweet(tweet):
from string import punctuation
# Remove HTML special entities (e.g. &)
tweet = re.sub(r'\&\w*;', '', tweet)
#Convert @username to AT_USER
tweet = re.sub(r'@[^\s]+','',tweet)
# remove numbers
tweet = re.sub(r'\d+', ' ', tweet)
tweet = re.sub(r'([a-z])([A-Z])','\\1 \\2', tweet)
# Remove tickers
tweet = re.sub(r'\$\w*', '', tweet)
# To lowercase
tweet = tweet.lower()
# Remove hyperlinks
tweet = re.sub(r'https:\/\/t.co\/.{9}', '', tweet)
# Remove hashtags
tweet = re.sub(r'#', ' ', tweet)
# Remove Punctuation and split 's, 't, 've with a space for filter
tweet = re.sub(r'[' + punctuation.replace('@', '') + ']+', ' ', tweet)
tweet = re.sub(r'[^\w\s]', ' ', tweet)
# Remove words with 2 or fewer letters
tweet = re.sub(r'\b\w{1,2}\b', '', tweet)
# Remove whitespace (including new line characters)
tweet = re.sub(r'\s\s+', ' ', tweet)
# Remove single space remaining at the front of the tweet.
tweet = tweet.lstrip(' ')
# Remove characters beyond Basic Multilingual Plane (BMP) of Unicode:
tweet = ''.join(c for c in tweet if c <= '\uFFFF')
return tweet
def make_bagofwords(filestr, word_count) :
values = parse_archive(filestr)
bow = {}
for id in values:
for word in values[id]['text'].split(" ") :
if word in bow :
bow[word] += 1
else :
bow[word] = 1
bowL = []
for key in bow :
bowL.append((key, bow[key]))
bowL.sort(key=lambda x: x[1])
return [x[0] for x in bowL[-word_count:]]
def copy_tweet_database(filestr, copystr) :
file = open(filestr, 'r')
fileout = open(copystr, 'w')
for line in file.readlines() :
tw = parse_tweet(line)
try:
fileout.write("(" + str(tw['id']) + "," + tw['tag'] + ")" + processTweet(tw['text']) + "\n")
except:
print(line)
file.close()
fileout.close()
def vectorize_tweets(filestr, dico) :
values = parse_archive(filestr)
vectors = {}
import numpy as np
max_tweet_size = np.max([len(values[id]['text'].split(' ')) for id in values])
for id in values :
if values[id]['tag'] != "???" :
vectors[id] = {'text' : values[id]['text']}
if values[id]['tag'] == 'irr' :
vectors[id]['label'] = [1, 0, 0, 0]
elif values[id]['tag'] == 'neg' :
vectors[id]['label'] = [0, 1, 0, 0]
elif values[id]['tag'] == 'neu' :
vectors[id]['label'] = [0, 0, 1, 0]
elif values[id]['tag'] == 'pos' :
vectors[id]['label'] = [0, 0, 0, 1]
else :
vectors[id]['label'] = [0, 0, 0, 0]
vectors[id]['label'] = np.array(vectors[id]['label'])
tmp_vect = []
for word in vectors[id]['text'].split(' ') :
try :
tmp_vect.append(dico.index(word))
except:
tmp_vect.append(0)
if len(tmp_vect) < max_tweet_size :
tmp_vect += [0 for i in range(max_tweet_size - len(tmp_vect))]
vectors[id]['vectorized'] = np.array(tmp_vect)
return vectors
def vectorize_tweetsNB(filestr, dico) :
values = parse_archive(filestr)
vectors = {}
import numpy as np
max_tweet_size = np.max([len(values[id]['text'].split(' ')) for id in values])
for id in values :
if values[id]['tag'] != "???" :
vectors[id] = {'text' : values[id]['text']}
vectors[id]['label'] = np.array(values[id]['tag'])
tmp_vect = []
for word in vectors[id]['text'].split(' ') :
try :
tmp_vect.append(dico.index(word))
except:
tmp_vect.append(0)
if len(tmp_vect) < max_tweet_size :
tmp_vect += [0 for i in range(max_tweet_size - len(tmp_vect))]
vectors[id]['vectorized'] = np.array(tmp_vect)
return vectors
def vectorize_tweet(tw, tag, dico, input_size) :
vector = {}
import numpy as np
max_tweet_size = input_size
vector = {'text' : tw}
if tag == 'irr' :
vector['label'] = [1, 0, 0, 0]
elif tag == 'neg' :
vector['label'] = [0, 1, 0, 0]
elif tag == 'neu' :
vector['label'] = [0, 0, 1, 0]
elif tag == 'pos' :
vector['label'] = [0, 0, 0, 1]
else :
vector['label'] = [0, 0, 0, 0]
vector['label'] = np.array(vector['label'])
tmp_vect = []
for word in vector['text'].split(' ') :
try :
tmp_vect.append(dico.index(word))
except:
tmp_vect.append(0)
if len(tmp_vect) < max_tweet_size :
tmp_vect += [0 for i in range(max_tweet_size - len(tmp_vect))]
vector['vectorized'] = np.array(tmp_vect)
return vector
def load_dictionary(filestr) :
dico = {}
file = open(filestr, 'r')
for line in file.readlines() :
if line != " " :
dico[line.split("=")[0]] = line.split("=")[1].split("\n")[0]
file.close()
return dico
def write_dictionary(dictionary) :
file = open("dico.txt", "w", encoding='utf-8')
count = 1
dictionary.sort()
for word in dictionary :
file.write(word + "=" + str(count) + "\n")
count +=1
file.close()
if len(sys.argv) > 1 :
if sys.argv[1] == '--rebuild' :
copy_tweet_database(sys.argv[2])
list_tweet = get_list_tweet(sys.argv[2])
dico = build_dico(list_tweet)
write_dictionary(dico)
elif sys.argv[1] == '--buildic' :
dico = load_dictionary("dico.txt")
elif sys.argv[1] == '--update' :
copy_tweet_database(sys.argv[2])
import sys
if len(sys.argv) > 1 :
if sys.argv[1] == "--format" :
copy_tweet_database(sys.argv[2], sys.argv[3])
# print(len(dico))
# vects = vectorize_tweets("tw_db_prepared.data", "dico.txt")
# print(len([k for k in vects]))
# print(list_tweet[0])
# print(type(processTweet(list_tweet[0])))
# dico = build_dico(list_tweet)
# write_dictionary(dico)
# def load_dico(filestr) :
# file = open(filestr, 'r')
# valuesA = parse_archive("g5remi.data")
# valuesR = parse_archive("g5antoine.data")
# idsA = [k for k in valuesA]
# idsR = [k for k in valuesR]
# iddiff = [id for id in idsA if valuesA[id]['tag'] != valuesR[id]['tag']]
# print(len(iddiff))
# wrong = {}
# for k in iddiff :
# wrong[k] = valuesA[k]
# for k in wrong :
# wrong[k]['tag'] = '???'
# write_archive(wrong, "convergence.txt")