forked from PaddlePaddle/PaddleNLP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.py
136 lines (111 loc) Β· 5 KB
/
tokenizer.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import jieba
from .vocab import Vocab
def get_idx_from_word(word, word_to_idx, unk_word):
if word in word_to_idx:
return word_to_idx[word]
return word_to_idx[unk_word]
class BaseTokenizer(object):
def __init__(self, vocab):
self.vocab = vocab
def get_tokenizer(self):
return self.tokenizer
def cut(self, sentence):
pass
def encode(self, sentence):
pass
class JiebaTokenizer(BaseTokenizer):
"""
Constructs a tokenizer based on `jieba <https://github.com/fxsjy/jieba>`__.
It supports :meth:`cut` method to split the text to tokens, and :meth:`encode`
method to covert text to token ids.
Args:
vocab(paddlenlp.data.Vocab): An instance of :class:`paddlenlp.data.Vocab`.
"""
def __init__(self, vocab):
super(JiebaTokenizer, self).__init__(vocab)
self.tokenizer = jieba.Tokenizer()
# initialize tokenizer
self.tokenizer.FREQ = {key: 1 for key in self.vocab.token_to_idx.keys()}
self.tokenizer.total = len(self.tokenizer.FREQ)
self.tokenizer.initialized = True
def cut(self, sentence, cut_all=False, use_hmm=True):
"""
The method used to cut the text to tokens.
Args:
sentence(str): The text that needs to be cuted.
cut_all(bool, optional): Whether to use the full mode. If True,
using full mode that gets all the possible words from the
sentence, which is fast but not accurate. If False, using
accurate mode that attempts to cut the sentence into the most
accurate segmentations, which is suitable for text analysis.
Default: False.
use_hmm(bool, optional): Whether to use the HMM model. Default: True.
Returns:
list[str]: A list of tokens.
Example:
.. code-block:: python
from paddlenlp.data import Vocab, JiebaTokenizer
# The vocab file. The sample file can be downloaded firstly.
# wget https://bj.bcebos.com/paddlenlp/data/senta_word_dict.txt
vocab_file_path = './senta_word_dict.txt'
# Initialize the Vocab
vocab = Vocab.load_vocabulary(
vocab_file_path,
unk_token='[UNK]',
pad_token='[PAD]')
tokenizer = JiebaTokenizer(vocab)
tokens = tokenizer.cut('ζη±δ½ δΈε½')
print(tokens)
# ['ζη±δ½ ', 'δΈε½']
"""
return self.tokenizer.lcut(sentence, cut_all, use_hmm)
def encode(self, sentence, cut_all=False, use_hmm=True):
"""
The method used to convert the text to ids. It will firstly call
:meth:`cut` method to cut the text to tokens. Then, convert tokens to
ids using `vocab`.
Args:
sentence(str): The text that needs to be cuted.
cut_all(bool, optional): Whether to use the full mode. If True,
using full mode that gets all the possible words from the
sentence, which is fast but not accurate. If False, using
accurate mode that attempts to cut the sentence into the most
accurate segmentations, which is suitable for text analysis.
Default: False.
use_hmm(bool, optional): Whether to use the HMM model. Default: True.
Returns:
list[int]: A list of ids.
Example:
.. code-block:: python
from paddlenlp.data import Vocab, JiebaTokenizer
# The vocab file. The sample file can be downloaded firstly.
# wget https://bj.bcebos.com/paddlenlp/data/senta_word_dict.txt
vocab_file_path = './senta_word_dict.txt'
# Initialize the Vocab
vocab = Vocab.load_vocabulary(
vocab_file_path,
unk_token='[UNK]',
pad_token='[PAD]')
tokenizer = JiebaTokenizer(vocab)
ids = tokenizer.encode('ζη±δ½ δΈε½')
print(ids)
# [1170578, 575565]
"""
words = self.cut(sentence, cut_all, use_hmm)
return [
get_idx_from_word(word, self.vocab.token_to_idx,
self.vocab.unk_token) for word in words
]