-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
64 lines (54 loc) · 1.96 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
import random
import numpy as np
def read_data(filename):
with open(filename, encoding="utf-8") as f:
data = f.read()
data = list(data)
return data
def index_data(sentences, dictionary):
shape = sentences.shape
sentences = sentences.reshape([-1])
index = np.zeros_like(sentences, dtype=np.int32)
for i in range(len(sentences)):
try:
index[i] = dictionary[sentences[i]]
except KeyError:
index[i] = dictionary['UNK']
return index.reshape(shape)
def get_train_data(vocabulary, batch_size, num_steps):
##################
# Your Code here
##################
raw_x = vocabulary
raw_y = vocabulary[1:]
data_length = len(raw_x)
batch_partition_length = data_length//batch_size
data_x = np.zeros([batch_size, batch_partition_length], dtype=np.str_)
data_y = np.zeros([batch_size, batch_partition_length], dtype=np.str_)
for i in range(batch_size):
data_x[i] = raw_x[batch_partition_length * i:batch_partition_length * (i + 1)]
data_y[i] = raw_y[batch_partition_length * i:batch_partition_length * (i + 1)]
epoch_size = batch_partition_length // num_steps
for i in range(epoch_size):
x = data_x[:, i * num_steps:(i + 1) * num_steps]
y = data_y[:, i * num_steps:(i + 1) * num_steps]
yield (x, y)
def build_dataset(words, n_words):
count = [['UNK', -1]]
count.extend(collections.Counter(words).most_common(n_words - 1))
dictionary = dict()
for word, _ in count:
dictionary[word] = len(dictionary)
data = list()
unk_count = 0
for word in words:
index = dictionary.get(word, 0)
if index == 0: # dictionary['UNK']
unk_count += 1
data.append(index)
count[0][1] = unk_count
reversed_dictionary = dict(zip(dictionary.values(), dictionary.keys()))
return data, count, dictionary, reversed_dictionary