-
Notifications
You must be signed in to change notification settings - Fork 6
/
preprocess.py
44 lines (38 loc) · 1.56 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
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(iterator):
for value in iterator:
yield list(value)
class MyVocabularyProcessor(learn.preprocessing.VocabularyProcessor):
def __init__(self,
max_document_length,
min_frequency=0,
vocabulary=None,
tokenizer_fn=tokenizer):
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