-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.py
More file actions
38 lines (30 loc) · 1.46 KB
/
Copy pathtokenizer.py
File metadata and controls
38 lines (30 loc) · 1.46 KB
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
import abc
import re
from abc import ABC
from typing import List
class Tokenizer(ABC):
"""
A utility class interface for processing text into tokens for searching.
This class abstracts the tokenization process to allow for different implementations
and parameters.
"""
@abc.abstractmethod
def tokenize(self, data: str) -> List[str]:
"""
Processes the raw string input by splitting it into tokens.
:param data: The raw text string to break into separate tokens
:return: A list strings containing the original text separated into tokens
"""
pass
class NaiveTokenizer(Tokenizer):
"""
A utility class for processing text into tokens for searching.
This class implements tokenization by taking a minimal/naive approach.
Text made lowercase and is broken up into tokens of individual words
and punctuation separated. Punctuation is NOT removed.
"""
def tokenize(self, data: str) -> List[str]:
tokens = re.sub(r'(\W)', r' \1 ', data.lower()) # Split off all non-word chars.
tokens = re.sub(r'(\w+)\s(\')\s(\w+)', r'\1\2\3', tokens) # Fix apostrophes in the middle of words by removing spaces from previous step.
tokens = re.sub(r'\.\s+\.\s+\.', r' ...', tokens) # When we see three periods separated by spaces, group them into one '...' ellipsis token.
return tokens.split() # Split processed string into tokens.