-
Notifications
You must be signed in to change notification settings - Fork 14
/
utility.py
executable file
·150 lines (135 loc) · 5.64 KB
/
utility.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
import pandas as pd
import numpy as np
import random
import scipy.special as special
def read_file(path, is_test = False):
fp = open(path, encoding = 'utf-8')
dataset = []
for line in fp.readlines():
line = line.strip().split('\t')
if is_test:
line.append(-1)
dataset.append(line)
data = pd.DataFrame(dataset)
data.columns = ['prefix', 'query_prediction', 'title', 'tag', 'label']
return data
def lcsubstr_lens(s1, s2):
m=[[0 for i in range(len(s2)+1)] for j in range(len(s1)+1)] #生成0矩阵,为方便后续计算,比字符串长度多了一列
mmax=0 #最长匹配的长度
p=0 #最长匹配对应在s1中的最后一位
for i in range(len(s1)):
for j in range(len(s2)):
if s1[i]==s2[j]:
m[i+1][j+1]=m[i][j]+1
if m[i+1][j+1]>mmax:
mmax=m[i+1][j+1]
p=i+1
return mmax
def lcseque_lens(s1, s2):
# 生成字符串长度加1的0矩阵,m用来保存对应位置匹配的结果
m = [ [ 0 for x in range(len(s2)+1) ] for y in range(len(s1)+1) ]
# d用来记录转移方向
d = [ [ None for x in range(len(s2)+1) ] for y in range(len(s1)+1) ]
for p1 in range(len(s1)):
for p2 in range(len(s2)):
if s1[p1] == s2[p2]: #字符匹配成功,则该位置的值为左上方的值加1
m[p1+1][p2+1] = m[p1][p2]+1
d[p1+1][p2+1] = 'ok'
elif m[p1+1][p2] > m[p1][p2+1]: #左值大于上值,则该位置的值为左值,并标记回溯时的方向
m[p1+1][p2+1] = m[p1+1][p2]
d[p1+1][p2+1] = 'left'
else: #上值大于左值,则该位置的值为上值,并标记方向up
m[p1+1][p2+1] = m[p1][p2+1]
d[p1+1][p2+1] = 'up'
(p1, p2) = (len(s1), len(s2))
s = []
while m[p1][p2]: #不为None时
c = d[p1][p2]
if c == 'ok': #匹配成功,插入该字符,并向左上角找下一个
s.append(s1[p1-1])
p1 -= 1
p2 -= 1
if c == 'left': #根据标记,向左找下一个
p2 -= 1
if c == 'up': #根据标记,向上找下一个
p1 -= 1
return len(s)
def compute_convert(pos, sums, label):
if np.isnan(sums): # not oppear in train
return -1
if label != -1 and sums == 1: # only oppear once
return -1
if label == 1:
return (pos - 1) / (sums - 1)
elif label == 0:
return pos / (sums - 1)
else:
return pos / sums
def find_longest_prefix(str_list):
if not str_list:
return ''
str_list.sort(key = lambda x: len(x))
shortest_str = str_list[0]
max_prefix = len(shortest_str)
flag = 0
for i in range(max_prefix):
for one_str in str_list:
if one_str[i] != shortest_str[i]:
return shortest_str[:i]
break
return shortest_str
def printlog(strr, is_print):
if is_print:
print(strr)
np.random.seed(0)
class HyperParam(object):
def __init__(self, alpha, beta):
self.alpha = alpha
self.beta = beta
def sample_from_beta(self, alpha, beta, num, imp_upperbound):
sample = np.random.beta(alpha, beta, num)
I = []
C = []
for click_ratio in sample:
imp = random.random() * imp_upperbound
#imp = imp_upperbound
click = imp * click_ratio
I.append(imp)
C.append(click)
return I, C
def update_from_data_by_FPI(self, tries, success, iter_num, epsilon):
'''estimate alpha, beta using fixed point iteration'''
for i in range(iter_num):
new_alpha, new_beta = self.__fixed_point_iteration(tries, success, self.alpha, self.beta)
if abs(new_alpha-self.alpha)<epsilon and abs(new_beta-self.beta)<epsilon:
break
self.alpha = new_alpha
self.beta = new_beta
def __fixed_point_iteration(self, tries, success, alpha, beta):
'''fixed point iteration'''
sumfenzialpha = 0.0
sumfenzibeta = 0.0
sumfenmu = 0.0
for i in range(len(tries)):
sumfenzialpha += (special.digamma(success[i]+alpha) - special.digamma(alpha))
sumfenzibeta += (special.digamma(tries[i]-success[i]+beta) - special.digamma(beta))
sumfenmu += (special.digamma(tries[i]+alpha+beta) - special.digamma(alpha+beta))
return alpha*(sumfenzialpha/sumfenmu), beta*(sumfenzibeta/sumfenmu)
def update_from_data_by_moment(self, tries, success):
'''estimate alpha, beta using moment estimation'''
mean, var = self.__compute_moment(tries, success)
#print 'mean and variance: ', mean, var
#self.alpha = mean*(mean*(1-mean)/(var+0.000001)-1)
self.alpha = (mean+0.000001) * ((mean+0.000001) * (1.000001 - mean) / (var+0.000001) - 1)
#self.beta = (1-mean)*(mean*(1-mean)/(var+0.000001)-1)
self.beta = (1.000001 - mean) * ((mean+0.000001) * (1.000001 - mean) / (var+0.000001) - 1)
def __compute_moment(self, tries, success):
'''moment estimation'''
ctr_list = []
var = 0.0
for i in range(len(tries)):
ctr_list.append(float(success[i])/(tries[i] + 0.000000001))
mean = sum(ctr_list)/len(ctr_list)
for ctr in ctr_list:
var += pow(ctr-mean, 2)
return mean, var/(len(ctr_list)-1)