-
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.
- Loading branch information
1 parent
d5601f2
commit c47ec6e
Showing
1 changed file
with
24 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,6 +192,30 @@ cnt_info = automaton.prepend_feed(state, '来') | |
assert state.is_nil() | ||
``` | ||
|
||
### `GreedyTokenizer` | ||
|
||
```python | ||
vocab = ['歌曲', '聆听歌曲', '播放歌曲', '歌词', '查看歌词', '听歌', '曲折'] | ||
trie, token_to_trie_node = build_trie_from_chars(vocab) | ||
|
||
trie_node_to_token = [-1] * trie.num_of_nodes() | ||
for i, j in enumerate(token_to_trie_node): | ||
trie_node_to_token[j] = i | ||
|
||
sam = GeneralSAM.from_trie(trie) | ||
tokenizer = GreedyTokenizer.from_sam_and_trie(sam, trie) | ||
|
||
def tokenize(s: str): | ||
return [(trie_node_to_token[i], j) for i, j in tokenizer.tokenize_str(s)] | ||
|
||
assert tokenize('歌曲折') == [(0, 2), (-1, 1)] | ||
assert tokenize('听歌曲') == [(5, 2), (-1, 1)] | ||
assert tokenize('听歌曲折') == [(5, 2), (6, 2)] | ||
assert tokenize('聆听歌曲折') == [(1, 4), (-1, 1)] | ||
assert tokenize('查看歌词歌曲') == [(4, 4), (0, 2)] | ||
assert tokenize('一起播放歌曲并共享歌词') == [(-1, 2), (2, 4), (-1, 3), (3, 2)] | ||
``` | ||
|
||
## License | ||
|
||
- © 2023 Chielo Newctle \<[[email protected]](mailto:[email protected])\> | ||
|