-
Notifications
You must be signed in to change notification settings - Fork 0
/
setment.py
143 lines (119 loc) · 4.07 KB
/
setment.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
from collections import Counter
import jieba
import csv
import toolz
import numpy
from scipy.stats.stats import pearsonr
import re
test_ind = numpy.random.choice(range(200),30)
r = '[’!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~!?,﹚﹞!╱」-()…*“》”∼,.:「→;《?|︶︸︺︼︾▲﹀﹂﹄﹏、~+、。【】〔〕]'
# =============================================================================
# r = '''[:!),.:;?]}¢'"、。〉》」』】〕〗〞︰︱︳﹐、﹒
# ﹔﹕﹖﹗﹚﹞!),.:;?|︶︸︺︼︾﹀﹂﹄﹏、~¢
# 々‖•·ˇˉ′’”([{£¥'"‵〈《「『【〔〖([{£¥〝︵︷︹︻
# ︽︿﹁﹃﹙﹛﹝({“‘-—_…]+'''
# =============================================================================
with open('C:/Users/TSR/Desktop/project data/ptt_tag_V1.csv') as csvfile:
spamreader = csv.reader(csvfile)
score = []
content = []
i = 0
for row in spamreader:
i = i + 1
if i >= 202 :
break
elif i >= 2:
score.append(int(row[2]))
string = re.sub(r,'',row[3])
content.append(string)
with open('C:/Users/TSR/Desktop/python/stopword.txt',encoding = 'utf8') as stopfile:
spamreader = stopfile.read()
stop1 = spamreader.split('\n')
stop1.append('\n')
stop1.append('\r\n')
stop1.append(' ')
# =============================================================================
#
# r = '[’!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~\n!?,]+'
# test = content[1]
#
# string = re.sub(r,'',test)
# print('%r' % string)
# print('%r' % test)
# =============================================================================
# =============================================================================
# score1 = score
# content1 = content
# score = []
# content = []
# score_test = []
# content_test = []
#
# for i in range(len(score1)):
# if i not in test_ind:
# score.append(score1[i])
# content.append(content1[i])
# else:
# score_test.append(score1[i])
# content_test.append(content1[i])
#
# =============================================================================
x = {}
for a, b in zip(score, content):
if a not in x:
x[a] = ""
x[a] = x[a] + b
jieba.load_userdict('C:/Users/TSR/Desktop/python/stopword.txt')
def content_to_dict(x):
list1 = list(jieba.cut(x))
list_nostop = filter(lambda x: x not in stop1, list1)
return dict(Counter(list_nostop))
train_dict = toolz.valmap(content_to_dict, x)
train_dict.keys()
train_dict[-3]
# =============================================================================
# for i in range(9):
# print(train_dict[i].get("樂天", 0))
# print(train_dict[-i].get("樂天", 0))
#
# =============================================================================
def get_str_score(x):
count = []
weight = []
score = []
for key in train_dict.keys():
if x in train_dict[key] :
# 取得字詞出現次數
count.append(train_dict[key][x])
#留下對應權重
weight.append(int(key))
else :
count.append(0)
weight.append(0)
for j in range(len(weight)):
score.append(count[j]*weight[j])
if sum(count) == 0:
return 0
else:
return sum(score)/sum(count)
def get_content_score(x):
str_cut = list(jieba.cut(x))
total_count = list(map(get_str_score,str_cut))
return sum(total_count)/len(total_count)
# =============================================================================
# predict = []
# for i in content_test:
# predict.append(get_content_score(i))
#
# print(pearsonr(predict, score_test))
# =============================================================================
# =============================================================================
#
# predict = []
# for i in range(170,200):
# predict.append(get_content_score(content[i]))
#
# print(pearsonr(predict, score[170:200]))
#
#
# =============================================================================