-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepro.py
183 lines (150 loc) · 5.05 KB
/
prepro.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
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
# From: https://github.com/kamalkraj/Named-Entity-Recognition-with-Bidirectional-LSTM-CNNs
import numpy as np
import random
from keras.preprocessing.sequence import pad_sequences
def readfile(filename, *, encoding="UTF8"):
'''
read file
return format :
[ ['EU', 'B-ORG'], ['rejects', 'O'], ['German', 'B-MISC'], ['call', 'O'], ['to', 'O'], ['boycott', 'O'], ['British', 'B-MISC'], ['lamb', 'O'], ['.', 'O'] ]
'''
with open(filename, mode='rt', encoding=encoding) as f:
sentences = []
sentence = []
for line in f:
if len(line) == 0 or line.startswith('-DOCSTART') or line[0] == "\n":
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
continue
splits = line.split(' ')
sentence.append([splits[0], splits[-1]])
if len(sentence) > 0:
sentences.append(sentence)
sentence = []
return sentences
# define casing s.t. NN can use case information to learn patterns
def getCasing(word, caseLookup):
casing = 'other'
numDigits = 0
for char in word:
if char.isdigit():
numDigits += 1
digitFraction = numDigits / float(len(word))
if word.isdigit(): # Is a digit
casing = 'numeric'
elif digitFraction > 0.5:
casing = 'mainly_numeric'
elif word.islower(): # All lower case
casing = 'allLower'
elif word.isupper(): # All upper case
casing = 'allUpper'
elif word[0].isupper(): # is a title, initial char upper, then all lower
casing = 'initialUpper'
elif numDigits > 0:
casing = 'contains_digit'
return caseLookup[casing]
# return batches ordered by words in sentence
def createEqualBatches(data):
# num_words = []
# for i in data:
# num_words.append(len(i[0]))
# num_words = set(num_words)
n_batches = 100
batch_size = len(data) // n_batches
num_words = [batch_size*(i+1) for i in range(0, n_batches)]
batches = []
batch_len = []
z = 0
start = 0
for end in num_words:
# print("start", start)
for batch in data[start:end]:
# if len(batch[0]) == i: # if sentence has i words
batches.append(batch)
z += 1
batch_len.append(z)
start = end
return batches, batch_len
def createBatches(data):
l = []
for i in data:
l.append(len(i[0]))
l = set(l)
batches = []
batch_len = []
z = 0
for i in l:
for batch in data:
if len(batch[0]) == i:
batches.append(batch)
z += 1
batch_len.append(z)
return batches,batch_len
# returns matrix with 1 entry = list of 4 elements:
# word indices, case indices, character indices, label indices
def createMatrices(sentences, word2Idx, label2Idx, case2Idx, char2Idx):
unknownIdx = word2Idx['UNKNOWN_TOKEN']
paddingIdx = word2Idx['PADDING_TOKEN']
dataset = []
wordCount = 0
unknownWordCount = 0
for sentence in sentences:
wordIndices = []
caseIndices = []
charIndices = []
labelIndices = []
for word, char, label in sentence:
wordCount += 1
if word in word2Idx:
wordIdx = word2Idx[word]
elif word.lower() in word2Idx:
wordIdx = word2Idx[word.lower()]
else:
wordIdx = unknownIdx
unknownWordCount += 1
charIdx = []
for x in char:
charIdx.append(char2Idx[x])
# Get the label and map to int
wordIndices.append(wordIdx)
caseIndices.append(getCasing(word, case2Idx))
charIndices.append(charIdx)
labelIndices.append(label2Idx[label])
dataset.append([wordIndices, caseIndices, charIndices, labelIndices])
return dataset
def iterate_minibatches(dataset, batch_len):
start = 0
for i in batch_len:
tokens = []
caseing = []
char = []
labels = []
data = dataset[start:i]
start = i
for dt in data:
t, c, ch, l = dt
l = np.expand_dims(l, -1)
tokens.append(t)
caseing.append(c)
char.append(ch)
labels.append(l)
yield np.asarray(labels), np.asarray(tokens), np.asarray(caseing), np.asarray(char)
# returns data with character information in format
# [['EU', ['E', 'U'], 'B-ORG\n'], ...]
def addCharInformation(Sentences):
for i, sentence in enumerate(Sentences):
for j, data in enumerate(sentence):
chars = [c for c in data[0]]
Sentences[i][j] = [data[0], chars, data[1]]
return Sentences
# 0-pads all words
def padding(Sentences):
maxlen = 52
for sentence in Sentences:
char = sentence[2]
for x in char:
maxlen = max(maxlen, len(x))
for i, sentence in enumerate(Sentences):
Sentences[i][2] = pad_sequences(Sentences[i][2], 52, padding='post')
return Sentences