-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.py
More file actions
123 lines (86 loc) · 2.53 KB
/
translate.py
File metadata and controls
123 lines (86 loc) · 2.53 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from collections import Counter
import codecs
import pickle
import numpy as np
def data_to_timesteps(data, steps, shift=1):
X = data.reshape(data.shape[0], -1)
Npoints, features = X.shape
stride0, stride1 = X.strides
shape = (Npoints - steps*shift, steps, features)
strides = (shift*stride0, stride0, stride1)
X = np.lib.stride_tricks.as_strided(data, shape=shape, strides=strides)
y = data[steps:]
return X, y
def to_categorical (arr):
'''
Converts a vector of labels into one-hot encoding format
Parameters
----------
arr : array-like 1D
array of integer labels (without holes)
Returns
-------
2D matrix in one-hot encoding format
'''
n = len(arr)
uniques, index = np.unique(np.asarray(arr), return_inverse=True)
categorical = np.zeros(shape=(n, uniques.size), dtype=float)
categorical[range(0, n), index] = 1.
return categorical
def from_categorical (categoricals):
'''
Convert a one-hot encoding format into a vector of labels
Parameters
----------
categoricals : array-like 2D
one-hot encoding format of a label set
Returns
-------
Corresponding labels in 1D array
'''
return np.argmax(categoricals, axis=-1)
def find_unique (filename):
'''
Find unique characters inside the file
'''
with codecs.open(filename,'r','utf8') as fin:
x = Counter(fin.read())
return x
def translations (filename):
'''
Create dicts {char : int} and {int : char} for every unique character
'''
counter = find_unique(filename)
unique_characters = counter.keys()
char2int = {c : i for i,c in enumerate(unique_characters)}
int2char = {i : c for i,c in enumerate(unique_characters)}
return char2int, int2char
def text_to_one_hot (filename, dictionaries=None):
'''
Translate the file into a one hot encoded one
'''
if dictionaries is None:
char2int, int2char = translations (filename)
else:
char2int, int2char = dictionaries
# TODO: faster method
encoding = np.array([])
with open(filename, 'r') as f:
for line in f:
for char in line:
encoding = np.append(encoding, char2int[char])
translation = to_categorical(encoding)
return translation, char2int, int2char
def one_hot_to_text (arr, int2char, filename):
'''
converts a one hot encoded array into text, saved in the data directory.
'''
cat = from_categorical(arr)
text = ''.join([int2char[i] for i in cat])
with open(filename, 'w') as f:
f.write(text)
if __name__ == '__main__':
pass