-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_data.py
135 lines (109 loc) · 3.8 KB
/
make_data.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
import nltk
import jieba
import heapq
import re
import pickle
import copy
import numpy as np
from sklearn.svm import SVC
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer
def content(test, news):
test_copy = list(test)
for i in range(len(test_copy)):
id_str = test_copy[i][1].split(',')
id_list = [int(i) for i in id_str]
title_list = []
content_list = []
for j in range(len(news)):
a = news[j]
content = a['content']
title = a['title']
id = a['id']
if id in id_list:
title_list.append(title)
content_list.append(content)
test_copy[i][1] = content_list
# test_copy[i].append(title_list)
# test_copy[i].append(content_list)
return test_copy
def del_stopword(train):
train = train.copy()
for each in train:
result = []
for sentence in each[1]:
# print(sentence)
cut = jieba.cut(sentence)
temp = []
for word in cut:
if word not in stopWords and judge(word):
temp.append(word)
result.extend(temp)
each[1] = set(result)
# print(each[1])
# break
return train
def judge(num):
pattern = re.compile(r'[0-9a-zA-Z\s]')
result = pattern.match(num)
if result:
return False
else:
return True
def chi_2(X_test, lable, k=1000):
tfidf = TfidfVectorizer(token_pattern=r"(?u)\b\w+\b")
weight = tfidf.fit_transform(X_test).toarray()
word = tfidf.get_feature_names()
ch2 = SelectKBest(chi2, k=k)
x = ch2.fit_transform(weight, lable)
word = np.array(word)[ch2.get_support(indices=True)]
return list(word)
def prepare(train,vocab):
train_sample = []
pattern = dict()
for each in vocab:
pattern[each] = False
for each in train:
words = each[1]
pattern1 = copy.deepcopy(pattern)
for word in words:
if word in vocab:
pattern1[word] = True
train_sample.append((pattern1, each[0]))
return train_sample
def score(result,test):
f = open(result,'r')
predictions = [i.split() for i in f.readlines()]
# print(predictions)
f.close()
n = len(predictions)
TP = len([i for i in range(n) if predictions[i][1] == '+1' and predictions[i][0] == '+1'])
FP = len([i for i in range(n) if predictions[i][1] == '-1' and predictions[i][0] == '+1'])
FN = len([i for i in range(n) if predictions[i][1] == '+1' and predictions[i][0] == '-1'])
TN = len([i for i in range(n) if predictions[i][1] == '-1' and predictions[i][0] == '-1'])
recall = TP/(TP+FN)
precision = TP/(TP+FP)
accuracy = (TP+TN)/n
error_rate = 1-accuracy
F1 = 2 * precision * recall/(precision+recall)
return recall,precision,accuracy,error_rate,F1
f = open('news.txt', mode='r', encoding='utf-8')
news = [eval(i) for i in f.readlines()]
f.close()
f = open('train.txt', mode='r', encoding='utf-8')
train = [i.split() for i in f.readlines()]
f.close
f = open('test.txt', mode='r', encoding='utf-8')
test = [i.split() for i in f.readlines()]
f.close
f = open('stopwords.txt', mode='r', encoding='utf-8')
stopWords = set([i for i in f.read() if i != '\n'])
f.close()
new_train = content(train, news)
new_test = content(test, news)
del_new_train = del_stopword(new_train)
del_new_test = del_stopword(new_test)
print('saving data ...')
pickle.dump(del_new_train, open("del_new_train.pkl", "wb"), True)
pickle.dump(del_new_test, open("del_new_test.pkl", "wb"), True)