forked from nyu-dl/dl4ir-query-reformulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
181 lines (137 loc) · 4.55 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'''
Miscellaneous functions.
'''
import numpy as np
import cPickle as pkl
from nltk.tokenize import wordpunct_tokenize
import parameters as prm
from random import randint
import math
import re
def clean(txt):
'''
#remove most of Wikipedia and AQUAINT markups, such as '[[', and ']]'.
'''
txt = re.sub(r'\|.*?\]\]', '', txt) # remove link anchor
txt = txt.replace('&', ' ').replace('<',' ').replace('>',' ').replace('"', ' ').replace('\'', ' ').replace('(', ' ').replace(')', ' ').replace('.', ' ').replace('"',' ').replace(',',' ').replace(';',' ').replace(':',' ').replace('<93>', ' ').replace('<98>', ' ').replace('<99>',' ').replace('<9f>',' ').replace('<80>',' ').replace('<82>',' ').replace('<83>', ' ').replace('<84>', ' ').replace('<85>', ' ').replace('<89>', ' ').replace('=', ' ').replace('*', ' ').replace('\n', ' ').replace('!', ' ').replace('-',' ').replace('[[', ' ').replace(']]', ' ')
return txt
def BOW(words, vocab):
'''
Convert a list of words to the BoW representation.
'''
bow = {} # BoW densely represented as <vocab word idx: quantity>
for word in words:
if word in vocab:
if vocab[word] not in bow:
bow[vocab[word]] = 0.
bow[vocab[word]] += 1.
bow_v = np.asarray(bow.values())
sumw = float(bow_v.sum())
if sumw == 0.:
sumw = 1.
bow_v /= sumw
return [bow.keys(), bow_v]
def BOW2(texts, vocab, dim):
'''
Convert a list of texts to the BoW dense representation.
'''
out = np.zeros((len(texts), dim), dtype=np.int32)
mask = np.zeros((len(texts), dim), dtype=np.float32)
for i, text in enumerate(texts):
bow = BOW(wordpunct_tokenize(text), vocab)
out[i,:len(bow[0])] = bow[0]
mask[i,:len(bow[1])] = bow[1]
return out, mask
def Word2Vec_encode(texts, wemb):
out = np.zeros((len(texts), prm.dim_emb), dtype=np.float32)
for i, text in enumerate(texts):
words = wordpunct_tokenize(text)
n = 0.
for word in words:
if word in wemb:
out[i,:] += wemb[word]
n += 1.
out[i,:] /= max(1.,n)
return out
def text2idx(texts, vocab, dim, use_mask=False):
'''
Convert a list of texts to their corresponding vocabulary indexes.
'''
if use_mask:
out = -np.ones((len(texts), dim), dtype=np.int32)
mask = np.zeros((len(texts), dim), dtype=np.float32)
else:
out = -2 * np.ones((len(texts), dim), dtype=np.int32)
for i, text in enumerate(texts):
for j, symbol in enumerate(text[:dim]):
if symbol in vocab:
out[i,j] = vocab[symbol]
else:
out[i,j] = -1 # for UNKnown symbols
if use_mask:
mask[i,:j] = 1.
if use_mask:
return out, mask
else:
return out
def text2idx2(texts, vocab, dim, use_mask=False):
'''
Convert a list of texts to their corresponding vocabulary indexes.
'''
if use_mask:
out = -np.ones((len(texts), dim), dtype=np.int32)
mask = np.zeros((len(texts), dim), dtype=np.float32)
else:
out = -2 * np.ones((len(texts), dim), dtype=np.int32)
out_lst = []
for i, text in enumerate(texts):
words = wordpunct_tokenize(text)[:dim]
for j, word in enumerate(words):
if word in vocab:
out[i,j] = vocab[word]
else:
out[i,j] = -1 # Unknown words
out_lst.append(words)
if use_mask:
mask[i,:j] = 1.
if use_mask:
return out, mask, out_lst
else:
return out, out_lst
def idx2text(idxs, vocabinv, max_words=-1, char=False, output_unk=True):
'''
Convert list of vocabulary indexes to text.
'''
out = []
for i in idxs:
if i >= 0:
out.append(vocabinv[i])
elif i == -1:
if output_unk:
out.append('<UNK>')
else:
break
if max_words > -1:
if len(out) >= max_words:
break
if char:
return ''.join(out)
else:
return ' '.join(out)
def n_words(words, vocab):
'''
Counts the number of words that have an entry in the vocabulary.
'''
c = 0
for word in words:
if word in vocab:
c += 1
return c
def load_vocab(path, n_words=None):
dic = pkl.load(open(path, "rb"))
vocab = {}
if not n_words:
n_words = len(dic.keys())
for i, word in enumerate(dic.keys()[:n_words]):
vocab[word] = i
return vocab