This repository has been archived by the owner on Feb 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic.py
101 lines (85 loc) · 2.77 KB
/
semantic.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
#
# Copyright 2014 Yong-Siang Shih.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import math
import os
import random
from collections import defaultdict
from statistics import mean
# all scores are in range [-1, 1]
def sigmoid(x):
return 1 / (1 + math.exp(-x))
def norm(x):
return (sigmoid(x)-0.5)*2
class RandomJudger(object):
def judge(self, evt):
return random.uniform(-1, 1)
class Judger(object):
def __init__(self, path, name):
with open(os.path.join(path, name), 'r') as f:
self.score_list = self.load_list(f)
def load_list(self, f):
raw_list = []
pos, neg = [], []
for l in f:
score = float(l)
raw_list.append(score)
if score >= 0:
pos.append(score)
else:
neg.append(-score)
mp, mn = mean(pos), mean(neg)
score_list = []
for x in raw_list:
if x >= 0:
score_list.append(x/mp*2)
else:
score_list.append(x/mn*2)
return [ norm(x) for x in score_list ]
def judge(self, evt):
if 'id' in evt:
text_id = evt['id']
if text_id <= len(self.score_list):
return self.score_list[text_id-1]
return RandomJudger().judge(evt)
def event_by_source(events):
by_source = defaultdict(list)
for evt in events:
by_source[evt['source']].append(evt)
return by_source
def random_semantic(events):
return semantic_simple_sum(event_by_source(events), RandomJudger())
def score_semantic(events, judger):
return semantic_simple_sum(event_by_source(events), judger)
def semantic_simple_sum(by_source, semantic_judge):
results = []
for k in by_source:
total = 0.0
positive, negative = [], []
for evt in by_source[k]:
score = semantic_judge.judge(evt)
if score < 0:
negative.append((score, evt))
else:
positive.append((score, evt))
total += score
total /= len(by_source[k])
results.append({'score': total,
'source': k,
'positive': positive,
'negative': negative,
})
random.shuffle(results)
return results