-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOLD-classify.py
More file actions
62 lines (52 loc) · 1.86 KB
/
Copy pathOLD-classify.py
File metadata and controls
62 lines (52 loc) · 1.86 KB
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
from pickle import load
import random
from chunker import *
from top_words import top_words
import matplotlib.pyplot as pt
def feature(tokens):
"""构造特征字典"""
dic = dict()
for word in tokens.split():
dic["have(%s)" % word] = True
return dic
train_set = [] # 构造训练集
for i in ['business', 'enter', 'pol', 'sport', 'tech']:
# for i in ["athletics", "cricket", "football", "rugby", "tennis"]:
inp = open('data/pre-lda86.6/train_word_%s.pkl' % i, 'rb') # 载入分类文档
t = load(inp)
inp.close()
for line in t:
train_set.append((feature(line), i)) # 处理成关键词和标签的字典
random.shuffle(train_set)
print("train set loaded")
classifier = nltk.NaiveBayesClassifier.train(train_set) # 训练分类器
print("classifier built")
# 分类器正确率评估
test_set = [] # 构造测试集
for i in ['business', 'enter', 'pol', 'sport', 'tech']:
# for i in ["athletics", "cricket", "football", "rugby", "tennis"]:
inp = open('data/pre-lda86.6/test_word_%s.pkl' % i, 'rb') # 载入分类文档
t = load(inp)
inp.close()
for line in t:
test_set.append((feature(line), i)) # 处理成关键词和标签的字典
random.shuffle(test_set)
print("test set loaded")
print("accuracy:", nltk.classify.accuracy(classifier, test_set) * 100, "%") # 输出正确率
# 单文本标记
news = ''' ''' # 要打标签的文本
print(classifier.classify(feature(top_words(news))))
# 正确率影响因素研究
features = train_set
random.shuffle(features)
si = []
acc = []
for i in range(1, 40, 1):
proportion = i / 100
size = int(len(features) * proportion)
train_set, test_set = features[size:], features[:size]
classifier = nltk.NaiveBayesClassifier.train(train_set)
acc.append(nltk.classify.accuracy(classifier, test_set))
si.append(proportion)
pt.plot(si, acc)
pt.show()