forked from aisingapore/sgnlp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[aisingapore#61] In progress - preprocessor.py
- Loading branch information
Showing
3 changed files
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import json | ||
import re | ||
import unicodedata | ||
|
||
class baseTokenizer(): | ||
"""Base Tokenizer Class (Inherited by all subclasses) """ | ||
def __init__(self): | ||
self.PAD_WORD = '[PAD]' | ||
self.UNK_WORD = '[UNK]' | ||
|
||
def unicodeToAscii(self, utterance): | ||
""" Normalize strings""" | ||
return ''.join( | ||
c for c in unicodedata.normalize('NFD', utterance) | ||
if unicodedata.category(c) != 'Mn' | ||
) | ||
|
||
def normalizeString(self, raw_utterance): | ||
"""Remove nonalphabetics for each utterance""" | ||
str = self.unicodeToAscii(raw_utterance.lower().strip()) | ||
str = re.sub(r"([,.'!?])", r" \1", str) | ||
str = re.sub(r"[^a-zA-Z,.'!?]+", r" ", str) | ||
return str | ||
|
||
def process(self, utterance): | ||
pass | ||
|
||
class gloveTokenizer(baseTokenizer): | ||
"""Glove Tokenizer for Glove Embedding (End2End Model)""" | ||
def __init__(self, vocab_path): | ||
super(gloveTokenizer, self).__init__() | ||
self.PAD = 0 | ||
self.UNK = 1 | ||
self.word2id = None | ||
self.loadVocabFromJson(vocab_path) | ||
|
||
def loadVocabFromJson(self, path): | ||
self.word2id = json.load(open(path)) | ||
|
||
def process(self, utterance): | ||
# baseTokenizer.normalizeString : remove nonalphabetics | ||
utterance = self.normalizeString(utterance) | ||
# transform into lower mode. | ||
wordList = [word.lower() for word in utterance.split()] | ||
indexes = [self.word2id.get(word, self.UNK) for word in wordList] # unk: 1 | ||
return indexes | ||
|
||
class BieruPreprocessor: | ||
def __init__(self): | ||
pass | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters