| 142 | |
| 143 | |
| 144 | class SimpleTokenizer(Tokenizer): |
| 145 | ALPHA_NUM = r"[\p{L}\p{N}\p{M}]+" |
| 146 | NON_WS = r"[^\p{Z}\p{C}]" |
| 147 | |
| 148 | def __init__(self, **kwargs): |
| 149 | """ |
| 150 | Args: |
| 151 | annotators: None or empty set (only tokenizes). |
| 152 | """ |
| 153 | self._regexp = regex.compile( |
| 154 | "(%s)|(%s)" % (self.ALPHA_NUM, self.NON_WS), |
| 155 | flags=regex.IGNORECASE + regex.UNICODE + regex.MULTILINE, |
| 156 | ) |
| 157 | if len(kwargs.get("annotators", {})) > 0: |
| 158 | logger.warning( |
| 159 | "%s only tokenizes! Skipping annotators: %s" % (type(self).__name__, kwargs.get("annotators")) |
| 160 | ) |
| 161 | self.annotators = set() |
| 162 | |
| 163 | def tokenize(self, text): |
| 164 | data = [] |
| 165 | matches = [m for m in self._regexp.finditer(text)] |
| 166 | for i in range(len(matches)): |
| 167 | # Get text |
| 168 | token = matches[i].group() |
| 169 | |
| 170 | # Get whitespace |
| 171 | span = matches[i].span() |
| 172 | start_ws = span[0] |
| 173 | if i + 1 < len(matches): |
| 174 | end_ws = matches[i + 1].span()[0] |
| 175 | else: |
| 176 | end_ws = span[1] |
| 177 | |
| 178 | # Format data |
| 179 | data.append( |
| 180 | ( |
| 181 | token, |
| 182 | text[start_ws:end_ws], |
| 183 | span, |
| 184 | ) |
| 185 | ) |
| 186 | return Tokens(data, self.annotators) |
nothing calls this directly
no outgoing calls
no test coverage detected