-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
159 lines (136 loc) · 3.59 KB
/
utils.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
151
152
153
154
155
# -*- coding: utf-8 -*-
import numpy as np
import theano
import theano.tensor as T
# --- Utilities : Initialization functions ---
def init_ones(size, name):
return theano.shared(value=np.ones(size, dtype='float64'), name=name, borrow=True)
def init_zeros(size, name):
return theano.shared(value=np.zeros(size, dtype='float64'), name=name, borrow=True)
def init_uniform(size, name):
return theano.shared(value=np.asarray(np.random.uniform(low = -np.sqrt(6. / np.sum(size)),
high = np.sqrt(6. / np.sum(size)),
size=size), dtype='float64'), name=name, borrow=True)
def init_ortho(size, name):
W = np.random.randn(max(size[0],size[1]),min(size[0],size[1]))
u, s, v = np.linalg.svd(W)
return theano.shared(value=u.astype('float64')[:,:size[1]], name=name, borrow=True)
# --- Utilities : Activation functions ---
def relu(x):
return T.switch(x<0, 0, x)
# --- Viterbi ---
"""
Function used to backtrack the best sequence using the max/argmax matrix comptued with Viterbi inside of the network. (It's here just because it's easier to write in python than theano)
"""
def backtrack(first, argmax):
tokens = [first]
for i in xrange(argmax.shape[0]-1, -1, -1):
tokens.append(argmax[i, tokens[-1]])
return tokens[::-1]
def normalize(x, wordset):
if (wordset != None) and (x in wordset): return x #word present in wordlist
if x.isdigit(): return '_NUM'+str(len(x)) #integer number
if re.match("^\d+?\.\d+?$", x) is None: return '_FN' #floating point number
check = 1
if(check==1):
flagD,flagA=0,0
for y in x:
if y>='0' and y<='9':
flagD=1
elif y>='a' and y<='z':
flagA=1
elif y>='A' and y<='Z':
flagA=1
if(flagA==1 and flagD==1):
x='_cDA'
check=0
if(check==1):
flagD,flagDash=0,0
for y in x:
if y>='0' and y<='9':
flagD=1
elif y=='-':
flagDash=1
else:
flagD=0
flagDash=0
break
if(flagD==1 and flagDash==1):
x='_cDDash'
check=0
if(check==1):
flagD,flagSlash=0,0
for y in x:
if y>='0' and y<='9':
flagD=1
elif y=='/':
flagSlash=1
if(flagD==1 and flagSlash==1):
x='_cDSlash'
check=0
if(check==1):
flagD,flagComma=0,0
for y in x:
if y>='0' and y<='9':
flagD=1
elif y==',':
flagComma=1
if(flagD==1 and flagComma==1):
x='_cDComma'
check=0
if(check==1):
flagD,flagPeriod=0,0
for y in x:
if y>='0' and y<='9':
flagD=1
elif y=='.':
flagPeriod=1
if(flagD==1 and flagPeriod==1):
x='_cDPrd'
check=0
if(check==1):
flagD,flagA=0,0
for y in x:
if y>='0' and y<='9':
flagD=0
if y>'a' or y<'z':
flagA=1
if(flagD==1 and len(x)>1 and flagA==0):
x='_ONum'
check=0
if(check==1):
flagA,flagDash=0,0
for y in x:
if y>='a' and y<='z':
flagA=1
elif y=='-':
flagDash=1
else:
flagA=0
flagDash=0
break
if(flagA==1 and flagDash==1):
x='_aDDash'
check=0
if(check==1):
flagA,flagPeriod=0,0
for y in x:
if y>='a' and y<='z':
flagA=1
elif y=='.':
flagPeriod=1
if(flagA==1 and flagPeriod==1 and len(x)>1):
x='_APrd'
check=0
if(check==1):
flaglower=1
for y in x:
if y<'a' or y>'z':
flaglower=0
if(flaglower==1 and len(x)>1):
print(x+' ')
x='_low'
check=0
if(check==1):
x='UNK'
return x.lower()