-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess_text.py
75 lines (60 loc) · 2.56 KB
/
preprocess_text.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
from tensorflow.python.keras.preprocessing.text import Tokenizer
import numpy as np
mark_start = 'ssss '
mark_end = ' eeee'
def preprocess(data,total_train_size,num_words):
captions_train=data[" comment"]
captions_train=list(captions_train.head(5*total_train_size))
for i in range(len(captions_train)):
captions_train[i]=[(captions_train[i])]
captions_train_marked = mark_captions(list(captions_train))
final_captions=[]
i=0
while(i<len(captions_train_marked)):
final_captions.append(captions_train_marked[i])
i=i+5
final_captions=np.asarray(final_captions)
captions_train_flat=flatten(final_captions)
tokenizer = TokenizerWrap(texts=captions_train_flat,num_words=num_words)
token_start = tokenizer.word_index[mark_start.strip()]
token_end = tokenizer.word_index[mark_end.strip()]
tokens_train = tokenizer.captions_to_tokens(final_captions)
for i in range(len(tokens_train)):
tokens_train[i]=tokens_train[i][0]
return tokens_train
class TokenizerWrap(Tokenizer):
def __init__(self, texts, num_words=None):
Tokenizer.__init__(self, num_words=num_words)
self.fit_on_texts(texts)
self.index_to_word = dict(zip(self.word_index.values(),
self.word_index.keys()))
def token_to_word(self, token):
word = " " if token == 0 else self.index_to_word[token]
return word
def tokens_to_string(self, tokens):
words = [self.index_to_word[token]
for token in tokens
if token != 0]
text = " ".join(words)
return text
def captions_to_tokens(self, captions_listlist):
tokens = [self.texts_to_sequences(captions_list)
for captions_list in captions_listlist]
return tokens
def get_random_caption_tokens(idx):
result = []
for i in idx:
j = np.random.choice(len(tokens_train[i]))
tokens = tokens_train[i][j]
result.append(tokens)
return result
def mark_captions(captions_listlist):
captions_marked = [[mark_start + str(caption) + mark_end
for caption in captions_list]
for captions_list in captions_listlist]
return captions_marked
def flatten(captions_listlist):
captions_list = [caption
for captions_list in captions_listlist
for caption in captions_list]
return captions_list