-
Notifications
You must be signed in to change notification settings - Fork 5
/
fianl-pos-tag.py
397 lines (352 loc) · 17.8 KB
/
fianl-pos-tag.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 8 23:29:46 2019
@author: tina
"""
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import nltk
from nltk.tokenize import word_tokenize
import re
import os
os.chdir(r"C:\Users\tina\Desktop\台大\碩二\碩二上\資訊檢索與文字探勘導論\期末報告")
#os.getcwd() #印出目前工作目錄
# data combine-----------------------------------------------------------------
# function combineData: combine all training data =============================
def combineData():
# 0: reliable, 1: unreliable
name = r'C:\\Users\\tina\\Desktop\\train.csv'
kaggleData1 = pd.read_csv(name, encoding='utf-8', header=0)
kaggleData1 = kaggleData1.fillna(' ')
# bias, bs, conspiracy, fake, hate, junksci, satire, state
name = r'C:\\Users\\tina\\Desktop\\fake.csv'
kaggleData2 = pd.read_csv(name, encoding='utf-8', header=0)
kaggleData2 = kaggleData2.fillna(' ')
# true, mostly true, half true, barely-true, false, pants-fire
name = r'C:\\Users\\tina\\Desktop\\train.tsv'
ucsb1 = pd.read_csv(name, sep='\t', header=None)
ucsb1 = ucsb1.fillna(' ')
name = r'C:\\Users\\tina\\Desktop\\test.tsv'
ucsb2 = pd.read_csv(name, sep='\t', header=None)
ucsb2 = ucsb2.fillna(' ')
name = r'C:\\Users\\tina\\Desktop\\valid.tsv'
ucsb3 = pd.read_csv(name, sep='\t', header=None)
ucsb3 = ucsb3.fillna(' ')
a1 = kaggleData1[["text","label"]]
a2 = kaggleData2[["text","type"]]
a3 = ucsb1[[2,1]]
a4 = ucsb2[[2,1]]
a5 = ucsb3[[2,1]]
a2.columns = ["text","label"]
a3.columns = ["text","label"]
a4.columns = ["text","label"]
a5.columns = ["text","label"]
combine_data = pd.concat([a1,a2,a3,a4,a5],axis=0)
combine_data = combine_data.reset_index(drop=True)
combine_data['text'] = combine_data['text'].str.lower() # lowercase all characters
#combine_data.to_csv('combine_data.csv', index=False) # write to a csv
return combine_data
#==============================================================================
combine_data = combineData()
# POS analysis-----------------------------------------------------------------
# function PosTag =============================================================
def PosTag(combine_data):
postags_0 = [] # pos-tag of type 1 news: unreliable, fake, false, pants-fire
postags_1 = [] # pos-tag of type 0 news: reliable, true
postags_bias = []
postags_conspiracy = []
postags_hate = []
postags_junksci = []
postags_satire = []
postags_state = []
num = [0] * 8 # num[0]: number of type 0 news, num[1]: number of type 1 news,...
for x in range(combine_data.shape[0]):
text = word_tokenize(re.sub(r'[^a-z ]', '', combine_data.loc[x, 'text']))
#text = word_tokenize(combine_data.loc[x, 'text'])
pos = list(set(nltk.pos_tag(text, tagset='universal'))) # list(set(x)): remove repeated tuple in x
if combine_data.loc[x, 'label'] in [1,'fake','false','pants-fire']:
num[0] = num[0] + 1
postags_0.extend(pos)
elif combine_data.loc[x, 'label'] in [0,'true']:
num[1] = num[1] + 1
postags_1.extend(pos)
elif combine_data.loc[x, 'label'] == 'bias':
num[2] = num[2] + 1
postags_bias.extend(pos)
elif combine_data.loc[x, 'label'] == 'conspiracy':
num[3] = num[3] + 1
postags_conspiracy.extend(pos)
elif combine_data.loc[x, 'label'] == 'hate':
num[4] = num[4] + 1
postags_hate.extend(pos)
elif combine_data.loc[x, 'label'] == 'junksci':
num[5] = num[5] + 1
postags_junksci.extend(pos)
elif combine_data.loc[x, 'label'] == 'satire':
num[6] = num[6] + 1
postags_satire.extend(pos)
elif combine_data.loc[x, 'label'] == 'state':
num[7] = num[7] + 1
postags_state.extend(pos)
postags = [postags_0,postags_1,postags_bias,postags_conspiracy,postags_hate,postags_junksci,postags_satire,postags_state]
return [num,postags]
#==============================================================================
postags = PosTag(combine_data)
N = sum(postags[0])
num = postags[0]
postags = postags[1]
# compute document frequency for each term-------------------------------------
POS = ['NOUN','VERB','ADJ','ADV']
TAG = ['postags_0','postags_1','postags_bias','postags_conspiracy','postags_hate','postags_junksci','postags_satire','postags_state','postagsAll']
# function frequency ==========================================================
def frequency(POS,TAG,postags):
postags.append([item for sublist in postags for item in sublist])
posNum = []
for i in POS:
a = []
for j in range(len(TAG)):
fd = nltk.FreqDist(postags[j]) # 計算每個(詞,詞性)出現的頻率 (即出現在幾篇新聞中)
a.append(pd.DataFrame([(wt[0], _) for (wt, _) in fd.most_common() if wt[1] == i])) # 按照各詞性出現的頻率由高至低排列
posNum.append(dict(zip(TAG, a)))
posNum = dict(zip(POS, posNum))
termDF = [] # for x in POS, find term's document frequency
for i in POS:
df = pd.DataFrame(np.zeros((len(posNum[i]['postagsAll']), (len(TAG)-1))),index=posNum[i]['postagsAll'][0]) # zero matrix
for j in range(len(TAG)-1):
df.loc[list(posNum[i][TAG[j]][0]),j] = list(posNum[i][TAG[j]][1])
termDF.append(df)
termDF = dict(zip(POS, termDF))
return [posNum,termDF]
#==============================================================================
termDF = frequency(POS,TAG,postags)
posNum = termDF[0]
termDF = termDF[1]
# compute Xsq, log likelihood ratio, expected mutual information for each term-
# function score: xsq,llr,emi =================================================
def Score(POS,TAG,posNum,termDF,num):
import math
SCORE = []
for i in POS:
n = len(posNum[i]['postagsAll'])
xsq = np.zeros((n, (len(TAG)-1))) # zero matrix
llr = np.zeros((n, (len(TAG)-1)))
emi = np.zeros((n, (len(TAG)-1)))
for j in range(n):
for k in range(len(TAG)-1):
n11 = termDF[i].iloc[j,k]
n12 = num[k] - n11
n21 = sum(list(termDF[i].iloc[j,])) - n11
n22 = N - n11 - n12 - n21
c1 = n11 + n21
c2 = n12 + n22
r1 = n11 + n12
r2 = n21 + n22
xsq[j][k] = (n11-c1*r1/N)**2/(c1*r1/N) + (n12-c2*r1/N)**2/(c2*r1/N) + (n21-c1*r2/N)**2/(c1*r2/N) + (n22-c2*r2/N)**2/(c2*r2/N)
pt = c1/N
p1 = n11/r1
p2 = n21/r2
if pt == 0:
llr[j][k] = -10000000000000000
else:
llr[j][k] = (n11 + n21)*math.log(pt)
if (1-pt) == 0:
llr[j][k] = llr[j][k] - 10000000000000000
else:
llr[j][k] = llr[j][k] + (n12 + n22)*math.log(1-pt)
if p1 == 0:
llr[j][k] = llr[j][k] + 10000000000000000
else:
llr[j][k] = llr[j][k] - n11*math.log(p1)
if (1-p1) == 0:
llr[j][k] = llr[j][k] + 10000000000000000
else:
llr[j][k] = llr[j][k] - n12*math.log(1-p1)
if p2 == 0:
llr[j][k] = llr[j][k] + 10000000000000000
else:
llr[j][k] = llr[j][k] - n21*math.log(p2)
if (1-p2) == 0:
llr[j][k] = llr[j][k] + 10000000000000000
else:
llr[j][k] = llr[j][k] - n22*math.log(1-p2)
llr[j][k] = -2 * llr[j][k]
if (n11*N)/(c1*r1) == 0:
emi[j][k] = n11/N * (-10000000000000000)
else:
emi[j][k] = n11/N * math.log((n11*N)/(c1*r1),2)
if (n12*N)/(c2*r1) == 0:
emi[j][k] = emi[j][k] + n12/N * (-10000000000000000)
else:
emi[j][k] = emi[j][k] + n12/N * math.log((n12*N)/(c2*r1),2)
if (n21*N)/(c1*r2) == 0:
emi[j][k] = emi[j][k] + n21/N * (-10000000000000000)
else:
emi[j][k] = emi[j][k] + n21/N * math.log((n21*N)/(c1*r2),2)
if (n22*N)/(c2*r2) == 0:
emi[j][k] = emi[j][k] + n22/N * (-10000000000000000)
else:
emi[j][k] = emi[j][k] + n22/N * math.log((n22*N)/(c2*r2),2)
score = np.array([[sum(xsq[i]) for i in range(n)], [sum(llr[i]) for i in range(n)], [sum(emi[i]) for i in range(n)], [0]*n, [0]*n])
SCORE.append(pd.DataFrame(score.T, index = posNum[i]['postagsAll'][0], columns = ['xsq','llr','emi','tfidf','vote']))
SCORE = dict(zip(POS, SCORE))
return SCORE
#==============================================================================
# function: constructDictionary ===============================================
def constructDictionary(trainingData,frequency):
import re
token = []
if type(trainingData) is list:
if frequency == "term":
for i in range(len(trainingData)):
#token.extend(word_tokenize(trainingData[i]))
token.extend(word_tokenize(re.sub(r'[^a-z ]', '', trainingData[i])))
elif frequency == "doc":
for i in range(len(trainingData)):
#token.extend(set(word_tokenize(trainingData[i])))
token.extend(set(word_tokenize(re.sub(r'[^a-z ]', '', trainingData[i])))) # here we count the document frequenc
else:
if frequency == "term":
#token.extend(word_tokenize(trainingData))
token.extend(word_tokenize(re.sub(r'[^a-z ]', '', trainingData)))
elif frequency == "doc":
#token.extend(set(word_tokenize(trainingData)))
token.extend(set(word_tokenize(re.sub(r'[^a-z ]', '', trainingData)))) # here we count the document frequenc
from collections import Counter
dictionary = pd.DataFrame.from_dict(Counter(token), orient='index') # Transform a Counter object into a Pandas DataFrame
dictionary.columns = ['frequency']
dictionary.sort_index(inplace=True) # sort dataframe by index
dictionary['index'] = list(range(dictionary.shape[0]))
return dictionary
#==============================================================================
# function: isEnglish =========================================================
def isEnglish(string):
try:
string.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return False
else:
return True
#==============================================================================
# function: containChar =======================================================
def containChar(string):
letter_flag = False
for i in string:
if i.isalpha():
letter_flag = True
return letter_flag
#==============================================================================
# function tfidf ==============================================================
def tfidf(combine_data):
import math
label = [1,'fake','false','pants-fire',0,'true','bias','conspiracy','hate','junksci','satire','state']
idx = [t for t, j in enumerate(list(combine_data['label'])) if j in label]
allnews = list(combine_data.iloc[idx,0])
term_df = constructDictionary(allnews,'doc')
term_tfidf = pd.DataFrame([0] * term_df.shape[0], index = term_df.index.tolist())
term_nd = pd.DataFrame([0] * term_df.shape[0], index = term_df.index.tolist())
for news in allnews:
if not news.isspace() and isEnglish(news) and containChar(news):
tf = constructDictionary(news,'term')
tfidf = np.array(list(tf.loc[:,'frequency'])) * np.array([math.log(y) for y in list(term_df.loc[tf.index.tolist(),'frequency'])])
term_tfidf.loc[tf.index.tolist(),0] = term_tfidf.loc[tf.index.tolist(),0] + [float(i)/sum(tfidf) for i in tfidf] # normalize
term_nd.loc[tf.index.tolist(),0] = term_nd.loc[tf.index.tolist(),0] + 1
term_tfidf = np.array(list(term_tfidf.loc[:,0])) / np.array(list(term_nd.loc[:,0]))
term_tfidf = pd.DataFrame(term_tfidf, index = term_df.index.tolist())
return term_tfidf
#==============================================================================
score = Score(POS,TAG,posNum,termDF,num)
term_tfidf = tfidf(combine_data)
for i in POS:
score[i].loc[:,'tfidf'] = term_tfidf.loc[score[i].index.tolist(),0]
# vote each term if its Xsq, LLR, EMI value larger than average----------------
# function voting =============================================================
def voting(score):
for i in POS:
threshold_xsq = np.min(score[i].loc[:,'xsq']) #1=>350
threshold_llr = np.min(score[i].loc[:,'llr']) #1.75=>543
threshold_emi = np.min(score[i].loc[:,'emi'])
threshold_tfidf = np.min(score[i].loc[:,'tfidf']) #1.45=>502 數字大嚴格
score[i] = score[i][score[i].loc[:,'xsq']>threshold_xsq]
score[i] = score[i][score[i].loc[:,'llr']>threshold_llr]
score[i] = score[i][score[i].loc[:,'emi']>threshold_emi]
score[i] = score[i][score[i].loc[:,'tfidf']>threshold_tfidf]
threshold_xsq = np.mean(score[i].loc[:,'xsq'])+0*np.std(score[i].loc[:,'xsq']) #1=>350
threshold_llr = np.mean(score[i].loc[:,'llr'])+0*np.std(score[i].loc[:,'llr']) #1.75=>543
threshold_emi = np.mean(score[i].loc[:,'emi'])+0*np.std(score[i].loc[:,'emi']) #1.75=>543
threshold_tfidf = np.mean(score[i].loc[:,'tfidf'])+0*np.std(score[i].loc[:,'tfidf']) #1.45=>502 數字大嚴格
df1 = score[i][score[i].loc[:,'xsq']>threshold_xsq]
df2 = score[i][score[i].loc[:,'llr']>threshold_llr]
df3 = score[i][score[i].loc[:,'emi']>threshold_emi]
df4 = score[i][score[i].loc[:,'tfidf']>threshold_tfidf]
score[i].loc[df1.index.tolist(),'vote'] += 1
score[i].loc[df2.index.tolist(),'vote'] += 1
score[i].loc[df3.index.tolist(),'vote'] += 1
score[i].loc[df4.index.tolist(),'vote'] += 1
score[i].to_csv(i+".csv")
return score
#==============================================================================
score1 = score
score = voting(score)
# naive bayes classification---------------------------------------------------
# function: trainMultinomialNB ================================================
def trainMultinomialNB(trainingData):
flat_trainingData = [item for sublist in trainingData for item in sublist]
V = constructDictionary(flat_trainingData,"term") # terms in training set
N = len(flat_trainingData) # number of documents
prior = [0] * len(trainingData)
condprob = np.zeros((V.shape[0],len(trainingData))) # zero matrix
for i in range(len(trainingData)):
prior[i] = len(trainingData[i])/N
Vc = constructDictionary(trainingData[i],"term")
condprob[list(V.loc[Vc.index.tolist(),'index']),i] = list(Vc.loc[:,'frequency'])
condprob = condprob + 1
rowsum = np.array([sum(condprob[i]) for i in range(V.shape[0])])
condprob = condprob/rowsum[:,np.newaxis] # divide each column by a vector element
condprob = pd.DataFrame(condprob, index = V.index.tolist())
return [V,prior,condprob]
#==============================================================================
# function: testMultinomialNB =================================================
def testMultinomialNB(trainNB,featureTerms,news):
W = constructDictionary(news,"term")
c = trainNB[1]
idx = [i for i in W.index.tolist() if i in featureTerms]
for i in range(len(c)):
for j in idx:
c[i] = c[i] + trainNB[2].loc[j,i]*W.loc[j,'frequency']
return c.index(max(c))
#==============================================================================
#-----------------------------------------
# prepare training data news
trainingData = []
label = [[1,'fake','false','pants-fire'],[0,'true'],['bias'],['conspiracy'],['hate'],['junksci'],['satire'],['state']]
for i in range(len(label)):
idx = [t for t, j in enumerate(list(combine_data['label'])) if j in label[i]]
trainingData.append(list(combine_data.iloc[idx,0]))
# training
trainNB = trainMultinomialNB(trainingData)
# feature selection
featureTerms = []
for i in POS:
featureTerms.append(list(np.array(score[i].index.tolist())[[i for i, e in enumerate(list(score[i].loc[:,'vote'])) if e == 4]]))
featureTerms = dict(zip(POS, featureTerms))
# testing
name = r'C:\\Users\\tina\\Desktop\\test.csv'
testingData = pd.read_csv(name, encoding='utf-8', header=0)
testingData = testingData.fillna(' ')
POS = ['NOUN','VERB','ADJ','ADV']
for i in POS:
newsType = [0] * testingData.shape[0]
for j in range(testingData.shape[0]):
if testingData.loc[j,'text'].isspace():
newsType[j] = 0
elif isEnglish(testingData.loc[j,'text']):
text = word_tokenize(re.sub(r'[^a-z ]', '', testingData.loc[j,'text']))
pos = nltk.pos_tag(text, tagset='universal')
a = [t for (t, p) in pos if p == i]
a = ' '.join(a)
if a != "":
newsType[j] = testMultinomialNB(trainNB,featureTerms[i],a)
else:
newsType[j] = 0
testingData["label"] = newsType
testingData.loc[:,["label",'id']].to_csv('test_'+i+'.csv', index=False)