forked from spro/char-rnn.pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
37 lines (28 loc) · 756 Bytes
/
helpers.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
# https://github.com/spro/char-rnn.pytorch
import unidecode
import string
import random
import time
import math
import torch
# Reading and un-unicode-encoding data
all_characters = string.printable
n_characters = len(all_characters)
def read_file(filename):
file = unidecode.unidecode(open(filename).read())
return file, len(file)
# Turning a string into a tensor
def char_tensor(string):
tensor = torch.zeros(len(string)).long()
for c in range(len(string)):
try:
tensor[c] = all_characters.index(string[c])
except:
continue
return tensor
# Readable time elapsed
def time_since(since):
s = time.time() - since
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)