-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathtext.py
147 lines (107 loc) · 4.06 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import string
from unidecode import unidecode
import logging
import numpy as np
PUNCTUATIONS = "'""-,.!?:;"
ACCENTS = u'ãõçâêôáíóúàüóé'
class BaseParser(object):
""" Interface class for all parsers
"""
def __init__(self):
self._logger = logging.getLogger('%s.%s' % (__name__,
self.__class__.__name__))
def __call__(self, _input):
return self.map(_input)
def map(self, _input):
pass
def imap(self, _input):
pass
def is_valid(self, _input):
pass
class CharParser(BaseParser):
""" Class responsible to map any text in a certain character vocabulary
# Arguments
mode: Which type of vacabulary will be generated. Modes can be
concatenated by using pipeline '|'
'space' or 's': accepts space character
'accents' or 'a': accepts pt-br accents
'punctuation' or 'p': accepts punctuation defined in
string.punctuation
'digits': accepts all digits
'sensitive' or 'S': characters will be case sensitive
'all': shortcut that enables all modes
"""
def __init__(self, mode='space'):
self._permitted_modes = {'sensitive': 'S', 'space': 's', 'accents':
'a', 'punctuation': 'p', 'digits': 'd'}
if mode == 'all':
self.mode = self._permitted_modes.values()
else:
self.mode = []
for m in mode.split('|'):
try:
self.mode.append(self._permitted_modes[m])
except KeyError:
if m not in self._permitted_modes.values():
raise ValueError('Unknown mode %s' % m)
self.mode.append(m)
self._vocab, self._inv_vocab = self._gen_vocab()
def map(self, txt, sanitize=True):
if sanitize:
label = np.array([self._vocab[c] for c in self._sanitize(txt)],
dtype='int32')
else:
label = np.array([self._vocab[c] for c in txt], dtype='int32')
return label
def imap(self, labels):
txt = ''.join([self._inv_vocab[l] for l in labels])
return txt
def _sanitize(self, text):
# removing duplicated spaces
text = ' '.join(text.split())
if not('d' in self.mode):
text = ''.join([c for c in text if not c.isdigit()])
if not('a' in self.mode):
text = unidecode(text)
if not('p' in self.mode):
text = text.translate(
string.maketrans("-'", ' ')).translate(None,
string.punctuation)
if not ('s' in self.mode):
text = text.replace(' ', '')
if not('S' in self.mode):
text = text.lower()
return text
def is_valid(self, text):
# verify if the text is valid without sanitization
try:
_ = self.map(text, sanitize=False)
return True
except KeyError:
return False
def _gen_vocab(self):
vocab = {chr(value + ord('a')): (value)
for value in xrange(ord('z') - ord('a') + 1)}
if 'a' in self.mode:
for a in ACCENTS:
vocab[a] = len(vocab)
if 'S' in self.mode:
for char in vocab.keys():
vocab[char.upper()] = len(vocab)
if 's' in self.mode:
# Inserts space label
vocab[' '] = len(vocab)
if 'p' in self.mode:
for p in PUNCTUATIONS:
vocab[p] = len(vocab)
if 'd' in self.mode:
for num in range(10):
vocab[str(num)] = len(vocab)
inv_vocab = {v: k for (k, v) in vocab.iteritems()}
# Add blank label
inv_vocab[len(inv_vocab)] = '<b>'
return vocab, inv_vocab
simple_char_parser = CharParser()
complex_char_parser = CharParser(mode='s|p|a|d')