forked from vikumkbv/Hacktoberfest-2k19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_mapping.py
52 lines (33 loc) · 1.24 KB
/
ext_mapping.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
# Input text needs to be preprocessed and in string format.
import logging, numpy as np
class seq_map:
def __init__(self, word_index, index_word):
self.word_index = None
self.index_word = None
def build_vocab(self, text):
logging.basicConfig(level=logging.INFO)
logging.info('Building vocabulary.')
var_ = {}
var_['UNK'] = 0
for n, word in enumerate(text.split()):
n += 1
if word not in var_:
var_[word] = n
self.word_index = var_
var_ = {}
for key, value in self.word_index:
var_[value] = key
self.index_word = var_
def transform(self, text, max_len):
'max_len :Max length for text sequence'
logging.basicConfig(level=logging.INFO)
logging.info('Transforming text to sequence.')
array_seq = np.zeros((max_len,), dtype=int)
for n, word in enumerate(text.split()):
if n == max_len:
logging.basicConfig(level=logging.INFO)
logging.info('Length of text over max limit.')
return array_seq
if word in self.word_index:
array_seq[n] = self.word_index[word]
return array_seq