-
Notifications
You must be signed in to change notification settings - Fork 8
/
ac.pyx
209 lines (171 loc) · 5.05 KB
/
ac.pyx
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# distutils: language = c++
"""
Created on Mon Aug 26 11:04:11 2019
@author: Milk
@Concat: [email protected]
"""
import numpy as np
import time
from cython cimport boundscheck,wraparound
import re
import jieba
'''
************************EN SEG1************************
'''
@boundscheck(False)
@wraparound(False)
def clean_text_en_seg1(dat,sentence_len):
cdef:
int n = len(dat)
int i = 0
REPLACE_BY_SPACE_RE = re.compile('["/(){}\[\]\|@,;]')
BAD_SYMBOLS_RE = re.compile('[^0-9a-zA-Z #+_]')
if sentence_len is None:
ret = []
for i in range(n):
line = dat[i]
line = line.lower()
line = REPLACE_BY_SPACE_RE.sub(' ', line)
line = BAD_SYMBOLS_RE.sub('', line)
line = line.strip()
line = line.split(' ')
ret.append(line)
return ret
else:
ret = []
for i in range(n):
line = dat[i]
line = line[0:sentence_len]
line = line.lower()
line = REPLACE_BY_SPACE_RE.sub(' ', line)
line = BAD_SYMBOLS_RE.sub('', line)
line = line.strip()
line = line.split(' ')
ret.append(line)
return ret
'''
************************EN SEG2************************
'''
@boundscheck(False)
@wraparound(False)
def clean_text_en_seg2(str[:] dat,sentence_len):
cdef:
int n = len(dat)
int i = 0
REPLACE_BY_SPACE_RE = re.compile('["/(){}\[\]\|@,;]')
BAD_SYMBOLS_RE = re.compile('[^0-9a-zA-Z #+_]')
ret = []
for i in range(n):
line = dat[i]
line = REPLACE_BY_SPACE_RE.sub(' ', line)
line = BAD_SYMBOLS_RE.sub('', line)
line = line.strip()
line = line.split(' ')
ret.append(line)
return ret
'''
************************ZH SEG1************************
'''
@boundscheck(False)
@wraparound(False)
def clean_text_zh_seg1(str[:] dat,sentence_len):
cdef:
int n = len(dat)
int i = 0
REPLACE_BY_SPACE_RE = re.compile('[“”【】/():!~「」、|,;。"/(){}\[\]\|@,\.;]')
if sentence_len is None:
ret = []
for i in range(n):
line = dat[i]
line = REPLACE_BY_SPACE_RE.sub('', line)
ret.append(line)
return ret
else:
ret = []
for i in range(n):
line = dat[i]
line = line[0:sentence_len]
line = REPLACE_BY_SPACE_RE.sub('', line)
ret.append(line)
return ret
'''
************************ZH SEG2************************
'''
def _tokenize_chinese_words(text):
gen = jieba.cut(text, cut_all=False, HMM = False)
ans = []
for i in gen:
ans.append(i)
return ans
@boundscheck(False)
@wraparound(False)
def clean_text_zh_seg2(dat,int sentence_len):
cdef:
int n = len(dat)
int i = 0
REPLACE_BY_SPACE_RE = re.compile('[“”【】/():!~「」、|,;。"/(){}\[\]\|@,\.;]')
ret = []
for i in range(n):
line = dat[i]
line = REPLACE_BY_SPACE_RE.sub(' ', line)
line = line.strip()
ret.append(_tokenize_chinese_words(line))
return ret
'''
************************Sequentical************************
'''
@boundscheck(False)
@wraparound(False)
def bulid_index(str[:] data,int num_sentence):
# s1 = time.time()
cdef:
int min_df = 3
int [:] text_lens = np.zeros( num_sentence,dtype=np.int32 )
dict word2cnt = {}
dict word2index = {}
int i = 0
int ind = 1
for i in range(num_sentence):
line = data[i]
for w in line:
if w in word2cnt:
word2cnt[w] += 1
else:
word2cnt[w] = 1
text_lens[i] = len(line)
for k,v in word2cnt.items():
if v >= min_df:
word2index[k] = ind
ind += 1
MAX_VOCAB_SIZE = ind
MAX_SEQ_LENGTH = np.sort(text_lens)[int(num_sentence*0.95)]
return MAX_VOCAB_SIZE, MAX_SEQ_LENGTH, word2index,text_lens
@boundscheck(False)
@wraparound(False)
def texts_to_sequences_and_pad(str[:] data,int num_sentence,dict word2index,int max_length,int[:] text_lens,int data_type):
ans = np.zeros((num_sentence, max_length), dtype=np.int32)
cdef:
int k = len(word2index) + 1
int [:,:] x_train = ans
int i,j,n
if data_type == 0:
for i in range(num_sentence):
line = data[i]
n = min(max_length, text_lens[i])
for j in range(n):
w = line[j]
if w in word2index:
x_train[i][j] = word2index[w]
else:
x_train[i][j] = k
else:
for i in range(num_sentence):
line = data[i]
n = min(max_length, len(line))
for j in range(n):
w = line[j]
if w in word2index:
x_train[i][j] = word2index[w]
else:
x_train[i][j] = k
return ans