forked from dhwajraj/deep-siamese-text-similarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
52 lines (45 loc) · 1.78 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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import re
import numpy as np
import six
from tensorflow.contrib import learn
from tensorflow.python.platform import gfile
from tensorflow.contrib import learn # pylint: disable=g-bad-import-order
TOKENIZER_RE = re.compile(r"[A-Z]{2,}(?![a-z])|[A-Z][a-z]+(?=[A-Z])|[\'\w\-]+",
re.UNICODE)
def tokenizer_char(iterator):
for value in iterator:
yield list(value)
def tokenizer_word(iterator):
for value in iterator:
yield TOKENIZER_RE.findall(value)
class MyVocabularyProcessor(learn.preprocessing.VocabularyProcessor):
def __init__(self,
max_document_length,
min_frequency=0,
vocabulary=None,
is_char_based=True):
if is_char_based:
tokenizer_fn=tokenizer_char
else:
tokenizer_fn=tokenizer_word
self.sup = super(MyVocabularyProcessor,self)
self.sup.__init__(max_document_length,min_frequency,vocabulary,tokenizer_fn)
def transform(self, raw_documents):
"""Transform documents to word-id matrix.
Convert words to ids with vocabulary fitted with fit or the one
provided in the constructor.
Args:
raw_documents: An iterable which yield either str or unicode.
Yields:
x: iterable, [n_samples, max_document_length]. Word-id matrix.
"""
for tokens in self._tokenizer(raw_documents):
word_ids = np.zeros(self.max_document_length, np.int64)
for idx, token in enumerate(tokens):
if idx >= self.max_document_length:
break
word_ids[idx] = self.vocabulary_.get(token)
yield word_ids