-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
218 lines (185 loc) · 5.62 KB
/
train.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
#!/usr/bin/env python
# coding=utf-8
import re
import math
# process twitter text
def processTweet(tweet):
# 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]+', 'AT_USER', 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('"')
tweet = tweet.strip(',')
tweet = tweet.strip('.')
tweet = tweet.strip(':')
tweet = tweet.strip('!')
tweet = tweet.strip('?')
tweet = tweet.strip(')')
tweet = tweet.strip('(')
tweet = tweet.strip(';')
return tweet
# end
def calculateMIScore(feature, tweets):
# print("--------------------计算--------------------------")
Y = 0.00
N = 0.00
A_Y = 0.00
A_N = 0.00
noA_Y = 0.00
noA_N = 0.00
for index in range(len(tweets)):
tweet = tweets[index]
tweet = processTweet(tweet)
words = tweet.split()
if words[1] == "y":
Y = Y + 1
if feature[0] in tweet and feature[1] in tweet:
A_Y = A_Y + 1
else:
noA_Y = noA_Y + 1
else:
N = N + 1
if feature[0] in tweet and feature[1] in tweet:
A_N = A_N + 1
else:
noA_N = noA_N + 1
sum = N + Y
pY = Y / sum
pN = N / sum
pA_Y = A_Y / sum
pA_N = A_N / sum
pnoA_Y = noA_Y / sum
pnoA_N = noA_N / sum
pA = (A_Y + A_N) / sum
pnoA = 1 - pA
# print("p(y)=%s" % (pY))
# print("p(n)=%s" % (pN))
# print("p(a,y)=%s" % (pA_Y))
# print("p(a,n)=%s" % (pA_N))
# print("p(!a,y)=%s" % (pnoA_Y))
# print("p(!a,n)=%s" % (pnoA_N))
# print("p(a)=%s" % (pA))
pmi_A_Y = 0
if pA * pA_Y != 0:
pmi_A_Y = math.log(pA_Y / (pA * pY), 2)
pmi_noA_Y = 0
if pnoA * pnoA_Y != 0:
pmi_noA_Y = math.log(pnoA_Y / (pnoA * pY), 2)
pmi_A_N = 0
if pA * pA_N != 0:
pmi_A_N = math.log(pA_N / (pA * pN), 2)
pmi_noA_N = 0
if pnoA * pnoA_N != 0:
pmi_noA_N = math.log(pnoA_N / (pnoA * pN), 2)
mi = (pA_Y * pmi_A_Y) + (pnoA_Y * pmi_noA_Y) + (pA_N * pmi_A_N) + (pnoA_N * pmi_noA_N)
return mi
# end
# def calculateSingleMIScore(feature, tweets):
# # print("--------------------计算--------------------------")
# Y = 0.00
# N = 0.00
# A_Y = 0.00
# A_N = 0.00
# noA_Y = 0.00
# noA_N = 0.00
# for index in range(len(tweets)):
# tweet = tweets[index]
# tweet = processTweet(tweet)
# words = tweet.split()
# if words[1] == "y":
# Y = Y + 1
# if feature in tweet:
# A_Y = A_Y + 1
# else:
# noA_Y = noA_Y + 1
# else:
# N = N + 1
# if feature in tweet:
# A_N = A_N + 1
# else:
# noA_N = noA_N + 1
# sum = N + Y
# pY = Y / sum
# pN = N / sum
# pA_Y = A_Y / sum
# pA_N = A_N / sum
# pnoA_Y = noA_Y / sum
# pnoA_N = noA_N / sum
# pA = (A_Y + A_N) / sum
# pnoA = 1 - pA
# # print("p(y)=%s" % (pY))
# # print("p(n)=%s" % (pN))
# # print("p(a,y)=%s" % (pA_Y))
# # print("p(a,n)=%s" % (pA_N))
# # print("p(!a,y)=%s" % (pnoA_Y))
# # print("p(!a,n)=%s" % (pnoA_N))
# # print("p(a)=%s" % (pA))
# pmi_A_Y = 0
# if pA * pA_Y != 0:
# pmi_A_Y = math.log(pA_Y / (pA * pY), 2)
# pmi_noA_Y = 0
# if pnoA * pnoA_Y != 0:
# pmi_noA_Y = math.log(pnoA_Y / (pnoA * pY), 2)
# pmi_A_N = 0
# if pA * pA_N != 0:
# pmi_A_N = math.log(pA_N / (pA * pN), 2)
# pmi_noA_N = 0
# if pnoA * pnoA_N != 0:
# pmi_noA_N = math.log(pnoA_N / (pnoA * pN), 2)
# mi = (pA_Y * pmi_A_Y) + (pnoA_Y * pmi_noA_Y) + (pA_N * pmi_A_N) + (pnoA_N * pmi_noA_N)
# return mi
# # end
if __name__ == '__main__':
tweetList = []
tweets = []
twoGramList = []
twoGramList2 = []
resultList = []
ySet = set()
nSet = set()
file = open("train.txt")
for lines in file:
tweets.append(lines)
tweetList.append(processTweet(lines.split("\t")[2].strip()))
#print tweetList
for words in tweetList:
for i in range(0,len(words.split(" "))-1):
print len(words.split(" ")[i])
if len(words.split(" ")[i]) > 1 and words.split(" ")[i+1] > 1 :
theList = [words.split(" ")[i],words.split(" ")[i+1]]
twoGramList.append(theList)
for i in twoGramList:
if not i in twoGramList2:
twoGramList2.append(i)
# print twoGramList2
print len(twoGramList2)
# for num in range(0,len(twoGramList2)):
# result = calculateMIScore(twoGramList2[num],tweets)
# print str(num)+" "
# print twoGramList2[num]
# print result
# resultList.append(result)
# # resultList = []
# # tokenList = []
# # trainList = []
# # for words in tweetList:
# # for word in words.split(" "):
# # tokenList.append(word)
# # tokenList = list(set(tokenList))
# #print tokenList
# # for word in tokenList:
# # result = calculateSingleMIScore(word,tweets)
# # print word+"\n"
# # print result
# # print "\n"
# # resultList.append(result)
# print resultList.index(max(resultList))
# print twoGramList2[resultList.index(max(resultList))]
# print resultList[resultList.index(max(resultList))]