-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
88 lines (70 loc) · 2.61 KB
/
preprocess.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
import os
import pickle
class preprocess:
def __init__(self, dataset_path):
self.dataset_path = dataset_path
def token_lookup(self):
"""
Generate a dict to turn punctuation into a token.
:return: Tokenize dictionary where the key is the punctuation and the value is the token
"""
token_dict = {}
token_dict['.'] = '||Period||'
token_dict[','] = '||Comma||'
token_dict['"'] = '||Quotation_Mark||'
token_dict[';'] = '||Semicolon||'
token_dict['?'] = '||Question_Mark||'
token_dict['!'] = '||Exclamation_Mark||'
token_dict['('] = '||Left_Parentheses||'
token_dict[')'] = '||Right_Parentheses||'
token_dict['--'] = '||Dash||'
token_dict['\n'] = '||Return||'
return token_dict
def create_lookup_tables(self, text):
"""
Create lookup tables for vocabulary
:param text: The text of tv scripts split into words
:return: A tuple of dicts (vocab_to_int, int_to_vocab)
"""
vocab_to_int = {word: i for i, word in enumerate(set(text), 0)}
int_to_vocab = {i: word for i, word in enumerate(set(text), 0)}
return vocab_to_int, int_to_vocab
def load_data(self):
"""
Load Dataset from File
"""
input_file = os.path.join(self.dataset_path)
with open(input_file, "r") as f:
data = f.read()
return data
def preprocess_and_save_data(self):
"""
Preprocess Text Data
"""
text = self.load_data()
# Ignore notice, since we don't use it for analysing the data
text = text[81:]
token_dict = self.token_lookup()
for key, token in token_dict.items():
text = text.replace(key, ' {} '.format(token))
text = text.lower()
text = text.split()
vocab_to_int, int_to_vocab = self.create_lookup_tables(text)
int_text = [vocab_to_int[word] for word in text]
pickle.dump((int_text, vocab_to_int, int_to_vocab, token_dict), open('preprocess.p', 'wb'))
return pickle.load(open('preprocess.p', mode='rb'))
def load_preprocess(self):
"""
Load the Preprocessed Training data and return them in batches of <batch_size> or less
"""
return pickle.load(open('preprocess.p', mode='rb'))
def save_params(self, params):
"""
Save parameters to file
"""
pickle.dump(params, open('params.p', 'wb'))
def load_params(self):
"""
Load parameters from file
"""
return pickle.load(open('params.p', mode='rb'))