[英]How to tokenize punctuations using the Tokenizer function tensorflow
我使用tensorflow.keras.preprocessing.text
的Tokenizer()
函數作為:
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 函數不包括標點符號。 如何標記標點符號? ( .
,在這個例子中。)
一種可能性是用空格將標點符號與單詞分開。 我用預處理函數pad_punctuation
做到這pad_punctuation
。 在此之后,我將Tokenizer
與filter=''
import re
import string
from tensorflow.keras.preprocessing.text import Tokenizer
def 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
函數對所有標點符號都有效
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.