-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathriset.py
executable file
·203 lines (190 loc) · 5.47 KB
/
riset.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
#import regex
import re
import csv
import pprint
import nltk.classify
#initialize stopWords
stopWords = []
#start replaceTwoOrMore
def replaceTwoOrMore(s):
#look for 2 or more repetitions of character and replace with the character itself
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
return pattern.sub(r"\1\1", s)
#end
def processTweet(tweet):
# process the tweets
#Convert to lower case
tweet = tweet.lower()
#Convert www.* or https?://* to URL
tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','URL',tweet)
#Convert @username to AT_USER
tweet = re.sub('@[^\s]+','',tweet)
#Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
#Replace #word with word
tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
#trim
tweet = tweet.strip('\'"')
return tweet
#start getStopWordList
def getStopWordList(stopWordListFileName):
#read the stopwords file and build a list
stopWords = []
stopWords.append('AT_USER')
stopWords.append('URL')
fp = open(stopWordListFileName, 'r')
line = fp.readline()
while line:
word = line.strip()
stopWords.append(word)
line = fp.readline()
fp.close()
return stopWords
#end
#start getfeatureVector
def getFeatureVector(tweet,stopWords):
featureVector = []
#split tweet into words
words = tweet.split()
for w in words:
#replace two or more with two occurrences
w = replaceTwoOrMore(w)
#strip punctuation
w = w.strip('\'"?,.')
#check if the word stats with an alphabet
val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*$", w)
#ignore if it is a stop word
if(w in stopWords or val is None):
continue
else:
featureVector.append(w.lower())
return featureVector
#end
def extract_features(tweet):
features = {}
for word in featureList:
features['fitur(%s)' % word] = (word in tweet)
return features
def getSVMFeatureVectorAndLabels(tweets, featureList):
sortedFeatures = sorted(featureList)
map = {}
feature_vector = []
labels = []
for t in tweets:
label = 0
map = {}
#Initialize empty map
for w in sortedFeatures:
map[w] = 0
tweet_words = t[0]
tweet_opinion = t[1]
#Fill the map
for word in tweet_words:
#process the word (remove repetitions and punctuations)
word = replaceTwoOrMore(word)
word = word.strip('\'"?,.')
#set map[word] to 1 if word exists
if word in map:
map[word] = 1
#end for loop
values = map.values()
feature_vector.append(values)
if(tweet_opinion == 'positive'):
label = 0
elif(tweet_opinion == 'negative'):
label = 1
elif(tweet_opinion == 'neutral'):
label = 2
labels.append(label)
#return the list of feature_vector and labels
return {'feature_vector' : feature_vector, 'labels': labels}
#end
#end
#Read the tweets one by one and process it
fp = open('twiit.csv', 'r')
line = fp.readline()
st = open('stopword.txt', 'r')
stopWords = getStopWordList('kamus/stopword.txt')
inpTweets = csv.reader(open('twiit.csv', 'rb'), delimiter=';', quotechar='|')
count=0
tweets = []
featureList= []
for row in inpTweets:
tweet = row[0]
sentiment = row[1]
processedTweet = processTweet(tweet)
featureVector = getFeatureVector(processedTweet, stopWords)
featureList.extend(featureVector)
tweets.append((featureVector, sentiment))
print featureVector
featureList = list(set(featureList))
print getSVMFeatureVectorAndLabels(tweets,featureList)
"""
while line:
processedTweet = processTweet(line)
featureVector = getFeatureVector(processedTweet)
print featureVector
line = fp.readline()
#end loop
"""
fp.close()
"""
def replaceTwoOrMore(s):
#look for 2 or more repetitions of character and replace with the character itself
pattern = re.compile(r"(.)\1{1,}", re.DOTALL)
return pattern.sub(r"\1\1", s)
#start process_tweet
def processTweet(tweet):
# process the tweets
#Convert to lower case
tweet = tweet.lower()
#Convert www.* or https?://* to URL
tweet = re.sub('((www\.[^\s]+)|(https?://[^\s]+))','URL',tweet)
#Convert @username to AT_USER
tweet = re.sub('@[^\s]+','',tweet)
#Remove additional white spaces
tweet = re.sub('[\s]+', ' ', tweet)
#Replace #word with word
tweet = re.sub(r'#([^\s]+)', r'\1', tweet)
#trim
tweet = tweet.strip('\'"')
return tweet
def getFeatureVector(tweet):
featureVector = []
#split tweet into words
words = tweet.split()
for w in words:
#strip punctuation
w = w.strip('\'"?,.')
#check if it consists of only words
val = re.search(r"^[a-zA-Z][a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*$", w)
#ignore if it is a stopWord
if(w in stopWords or val is None):
continue
else:
featureVector.append(w.lower())
return featureVector
#get stopwords
def getStopWordList(filename):
stopWords=[]
fp=open('stopword.txt','r')
line=fp.readline()
while line:
word=line.strip()
stopWords.append(word)
line=fp.readline()
fp.close()
return stopWords
tr=open('twiit.csv','r')
line=tr.readline()
print line
st=open('stopword.txt','r')
stopword=getStopWordList(st)
while line:
processedTweet = processTweet(line)
featureVector = getFeatureVector(processedTweet)
#print featureVector
#print featureVector
line = fp.readline()
fp.close()
"""