-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[text] add tongyi unit test && change token type to T
- Loading branch information
Showing
3 changed files
with
39 additions
and
22 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 |
---|---|---|
@@ -1,39 +1,41 @@ | ||
from abc import ABC, abstractmethod, abstractproperty | ||
from typing import Dict, List, Tuple | ||
from typing import Dict, List, Tuple, Union | ||
|
||
T = Union[str, bytes] | ||
|
||
|
||
class BaseTokenizer(ABC): | ||
|
||
def tokenize(self, line: str) -> Tuple[List[str], List[int]]: | ||
def tokenize(self, line: str) -> Tuple[List[T], List[int]]: | ||
tokens = self.text2tokens(line) | ||
ids = self.tokens2ids(tokens) | ||
return tokens, ids | ||
|
||
def detokenize(self, ids: List[int]) -> Tuple[str, List[str]]: | ||
def detokenize(self, ids: List[int]) -> Tuple[str, List[T]]: | ||
tokens = self.ids2tokens(ids) | ||
text = self.tokens2text(tokens) | ||
return text, tokens | ||
|
||
@abstractmethod | ||
def text2tokens(self, line: str) -> List[str]: | ||
def text2tokens(self, line: str) -> List[T]: | ||
raise NotImplementedError("abstract method") | ||
|
||
@abstractmethod | ||
def tokens2text(self, tokens: List[str]) -> str: | ||
def tokens2text(self, tokens: List[T]) -> str: | ||
raise NotImplementedError("abstract method") | ||
|
||
@abstractmethod | ||
def tokens2ids(self, tokens: List[str]) -> List[int]: | ||
def tokens2ids(self, tokens: List[T]) -> List[int]: | ||
raise NotImplementedError("abstract method") | ||
|
||
@abstractmethod | ||
def ids2tokens(self, ids: List[int]) -> List[str]: | ||
def ids2tokens(self, ids: List[int]) -> List[T]: | ||
raise NotImplementedError("abstract method") | ||
|
||
@abstractmethod | ||
def vocab_size(self) -> int: | ||
raise NotImplementedError("abstract method") | ||
|
||
@abstractproperty | ||
def symbol_table(self) -> Dict[str, int]: | ||
def symbol_table(self) -> Dict[T, int]: | ||
raise NotImplementedError("abstract method") |
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