-
Notifications
You must be signed in to change notification settings - Fork 70
/
util.py
162 lines (124 loc) · 4.93 KB
/
util.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
import re
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.layers import Layer
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from nltk.corpus import stopwords
from gensim.models import KeyedVectors
import gensim
import numpy as np
import itertools
def text_to_word_list(text):
# Pre process and convert texts to a list of words
text = str(text)
text = text.lower()
# Clean the text
text = re.sub(r"[^A-Za-z0-9^,!.\/'+-=]", " ", text)
text = re.sub(r"what's", "what is ", text)
text = re.sub(r"\'s", " ", text)
text = re.sub(r"\'ve", " have ", text)
text = re.sub(r"can't", "cannot ", text)
text = re.sub(r"n't", " not ", text)
text = re.sub(r"i'm", "i am ", text)
text = re.sub(r"\'re", " are ", text)
text = re.sub(r"\'d", " would ", text)
text = re.sub(r"\'ll", " will ", text)
text = re.sub(r",", " ", text)
text = re.sub(r"\.", " ", text)
text = re.sub(r"!", " ! ", text)
text = re.sub(r"\/", " ", text)
text = re.sub(r"\^", " ^ ", text)
text = re.sub(r"\+", " + ", text)
text = re.sub(r"\-", " - ", text)
text = re.sub(r"\=", " = ", text)
text = re.sub(r"'", " ", text)
text = re.sub(r"(\d+)(k)", r"\g<1>000", text)
text = re.sub(r":", " : ", text)
text = re.sub(r" e g ", " eg ", text)
text = re.sub(r" b g ", " bg ", text)
text = re.sub(r" u s ", " american ", text)
text = re.sub(r"\0s", "0", text)
text = re.sub(r" 9 11 ", "911", text)
text = re.sub(r"e - mail", "email", text)
text = re.sub(r"j k", "jk", text)
text = re.sub(r"\s{2,}", " ", text)
text = text.split()
return text
def make_w2v_embeddings(df, embedding_dim=300, empty_w2v=False):
vocabs = {}
vocabs_cnt = 0
vocabs_not_w2v = {}
vocabs_not_w2v_cnt = 0
# Stopwords
stops = set(stopwords.words('english'))
# Load word2vec
print("Loading word2vec model(it may takes 2-3 mins) ...")
if empty_w2v:
word2vec = EmptyWord2Vec
else:
word2vec = KeyedVectors.load_word2vec_format("./data/GoogleNews-vectors-negative300.bin.gz", binary=True)
# word2vec = gensim.models.word2vec.Word2Vec.load("./data/Quora-Question-Pairs.w2v").wv
for index, row in df.iterrows():
# Print the number of embedded sentences.
if index != 0 and index % 1000 == 0:
print("{:,} sentences embedded.".format(index), flush=True)
# Iterate through the text of both questions of the row
for question in ['question1', 'question2']:
q2n = [] # q2n -> question numbers representation
for word in text_to_word_list(row[question]):
# Check for unwanted words
if word in stops:
continue
# If a word is missing from word2vec model.
if word not in word2vec.vocab:
if word not in vocabs_not_w2v:
vocabs_not_w2v_cnt += 1
vocabs_not_w2v[word] = 1
# If you have never seen a word, append it to vocab dictionary.
if word not in vocabs:
vocabs_cnt += 1
vocabs[word] = vocabs_cnt
q2n.append(vocabs_cnt)
else:
q2n.append(vocabs[word])
# Append question as number representation
df.at[index, question + '_n'] = q2n
embeddings = 1 * np.random.randn(len(vocabs) + 1, embedding_dim) # This will be the embedding matrix
embeddings[0] = 0 # So that the padding will be ignored
# Build the embedding matrix
for word, index in vocabs.items():
if word in word2vec.vocab:
embeddings[index] = word2vec.word_vec(word)
del word2vec
return df, embeddings
def split_and_zero_padding(df, max_seq_length):
# Split to dicts
X = {'left': df['question1_n'], 'right': df['question2_n']}
# Zero padding
for dataset, side in itertools.product([X], ['left', 'right']):
dataset[side] = pad_sequences(dataset[side], padding='pre', truncating='post', maxlen=max_seq_length)
return dataset
# --
class ManDist(Layer):
"""
Keras Custom Layer that calculates Manhattan Distance.
"""
# initialize the layer, No need to include inputs parameter!
def __init__(self, **kwargs):
self.result = None
super(ManDist, self).__init__(**kwargs)
# input_shape will automatic collect input shapes to build layer
def build(self, input_shape):
super(ManDist, self).build(input_shape)
# This is where the layer's logic lives.
def call(self, x, **kwargs):
self.result = K.exp(-K.sum(K.abs(x[0] - x[1]), axis=1, keepdims=True))
return self.result
# return output shape
def compute_output_shape(self, input_shape):
return K.int_shape(self.result)
class EmptyWord2Vec:
"""
Just for test use.
"""
vocab = {}
word_vec = {}