-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
160 lines (139 loc) · 5.38 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
import os
import numpy as np
import random
import tensorflow as tf
def length_sort(items, lengths, descending=True):
"""In order to use pytorch variable length sequence package"""
items = list(zip(items, lengths))
items.sort(key=lambda x: x[1], reverse=True)
items, lengths = zip(*items)
return list(items), list(lengths)
def batchify(data, bsz, maxlen, shuffle=False):
if shuffle:
random.shuffle(data)
nbatch = len(data) // bsz
batches = []
for i in range(nbatch):
# Pad batches to maximum sequence length in batch
batch = data[i*bsz:(i+1)*bsz]
# subtract 1 from lengths b/c includes BOTH starts & end symbols
lengths = [len(x)-1 for x in batch]
# sort items by length (decreasing)
batch, lengths = length_sort(batch, lengths)
# source has no end symbol
source = [x[:-1] for x in batch]
# target has no start symbol
target = [x[1:] for x in batch]
# find length to pad to
for idx, x in enumerate(source):
zeros = (maxlen-len(x)) * [0]
source[idx] += zeros
target[idx] += zeros
source = np.array(source)
target = np.array(target)
#target = np.array(np.hstack(target))
batches.append((source, target, lengths))
return batches
class Dictionary(object):
def __init__(self):
self.word2idx = {}
self.idx2word = {}
self.word2idx['<pad>'] = 0
self.word2idx['<sos>'] = 1
self.word2idx['<eos>'] = 2
self.word2idx['<oov>'] = 3
self.wordcounts = {}
# to track word counts
def add_word(self, word):
if word not in self.wordcounts:
self.wordcounts[word] = 1
else:
self.wordcounts[word] += 1
# prune vocab based on count k cutoff or most frequently seen k words
def prune_vocab(self, k=5, cnt=False):
# get all words and their respective counts
vocab_list = [(word, count) for word, count in self.wordcounts.items()]
if cnt:
# prune by count
self.pruned_vocab = {pair[0]: pair[1] for pair in vocab_list if pair[1] > k}
else:
# prune by most frequently seen words
vocab_list.sort(key=lambda x: (x[1], x[0]), reverse=True)
k = min(k, len(vocab_list))
self.pruned_vocab = [pair[0] for pair in vocab_list[:k]]
# sort to make vocabulary determistic
self.pruned_vocab.sort()
# add all chosen words to new vocabulary/dict
for word in self.pruned_vocab:
if word not in self.word2idx:
self.word2idx[word] = len(self.word2idx)
print("original vocab {}; pruned to {}".format(len(self.wordcounts), len(self.word2idx)))
self.idx2word = {v: k for k, v in self.word2idx.items()}
def __len__(self):
return len(self.word2idx)
class Corpus(object):
def __init__(self, path, maxlen, vocab_size=11000, lowercase=False):
self.dictionary = Dictionary()
self.maxlen = maxlen
self.lowercase = lowercase
self.vocab_size = vocab_size
self.train_path = os.path.join(path, 'train.txt')
self.test_path = os.path.join(path, 'test.txt')
# make the vocabulary from training set
self.make_vocab()
self.train = self.tokenize(self.train_path)
self.test = self.tokenize(self.test_path)
def make_vocab(self):
assert os.path.exists(self.train_path)
# Add words to the dictionary
with open(self.train_path, 'r') as f:
for line in f:
if self.lowercase:
# -1 to get rid of \n character
words = line[:-1].lower().split(" ")
else:
words = line[:-1].split(" ")
for word in words:
self.dictionary.add_word(word)
# prune the vocabulary
self.dictionary.prune_vocab(k=self.vocab_size, cnt=False)
def tokenize(self, path):
"""Tokenizes a text file."""
dropped = 0
with open(path, 'r') as f:
linecount = 0
lines = []
for line in f:
linecount += 1
if self.lowercase:
words = line[:-1].lower().strip().split(" ")
else:
words = line[:-1].strip().split(" ")
if len(words) > (self.maxlen - 2):
dropped += 1
continue
words = ['<sos>'] + words
words += ['<eos>']
# vectorize
vocab = self.dictionary.word2idx
unk_idx = vocab['<oov>']
indices = [vocab[w] if w in vocab else unk_idx for w in words]
lines.append(indices)
print("Number of sentences dropped from {}: {} out of {} total".
format(path, dropped, linecount))
return lines
def get_string(max_indices, corpus):
sentences = []
for idx in max_indices:
# generated sentence
words = [corpus.dictionary.idx2word[x] for x in idx]
# truncate sentences to first occurrence of <eos>
sentence = []
for w in words:
if w != '<eos>':
sentence.append(w)
else:
sentence.append('\n')
break
sentences.append(" ".join(sentence))
return sentences