forked from riccardotommasini/twitter-sentiment-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
54 lines (42 loc) · 1.2 KB
/
preprocessing.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
import re
import csv
from sys import argv
#script, file_ = argv
def processRow(row):
tweet = row[2]
#Lower case
tweet.lower()
#convert any url to URL
tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','URL',tweet)
#Convert any @Username to "AT_USER"
tweet = re.sub('@[^\s]+','AT_USER',tweet)
#Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
tweet = re.sub('[\n]+', ' ', tweet)
#Remove not alphanumeric symbols white spaces
tweet = re.sub(r'[^\w]', ' ', tweet)
#Replace #word with word
tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
#Remove :( or :)
tweet = tweet.replace(':)','')
tweet = tweet.replace(':(','')
#trim
tweet = tweet.strip('\'"')
row[2] = tweet
return row
# end processTweet
if __name__ == '__main__':
#Read the tweets one by one and process it
clnfile = open("CLN-"+file_,'wb')
filewriter = csv.DictWriter(clnfile, fieldnames=["TweetID", "User_ID", "Text", "Sentiment"])
with open(file_, "rb") as csvfile:
filereader = csv.reader(csvfile)
for row in filereader:
new_row = processRow(row)
filewriter.writerow({
"TweetID" : new_row[0],
"User_ID" : new_row[1],
"Text" : new_row[2],
"Sentiment" : new_row[3]
})
clnfile.close()