猿问

如何使用 Tokenizer 函数tensorflow 对标点符号进行标记

我使用asTokenizer()中的函数tensorflow.keras.preprocessing.text:


from tensorflow.keras.preprocessing.text import Tokenizer

s = ["The quick brown fox jumped over the lazy dog."]

t = Tokenizer()

t.fit_on_texts(s)

print(t.word_index)

输出 :


{'the': 1, 'quick': 2, 'brown': 3, 'fox': 4, 'jumped': 5, 'over': 6, 'lazy': 7, 'dog': 8}

Tokenizer 函数排除标点符号。如何标记标点符号?( .,在此示例中。)


红颜莎娜
浏览 96回答 1
1回答

12345678_0001

一种可能性是用空格将标点符号与单词分开。我用预处理函数来做到这一点pad_punctuation。之后我Tokenizer申请filter=''import reimport stringfrom tensorflow.keras.preprocessing.text import Tokenizerdef pad_punctuation(s): return re.sub(f"([{string.punctuation}])", r' \1 ', s)S = ["The quick brown fox jumped over the lazy dog."]S = [pad_punctuation(s) for s in S]t = Tokenizer(filters='')t.fit_on_texts(S)print(t.word_index)结果:{'the': 1, 'quick': 2, 'brown': 3, 'fox': 4, 'jumped': 5, 'over': 6, 'lazy': 7, 'dog': 8, '.': 9}该pad_punctuation功能对所有标点符号都有效
随时随地看视频慕课网APP

相关分类

Python
我要回答