如何使用 Tokenizer 函数 tensorflow 标记标点符号
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}
分词器功能排除了标点符号。如何标记标点符号? (.
,在这个例子中。)
一种可能是用空格将标点符号与单词分开。我使用预处理函数 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' ', 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
功能对所有标点有效
我使用 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}
分词器功能排除了标点符号。如何标记标点符号? (.
,在这个例子中。)
一种可能是用空格将标点符号与单词分开。我使用预处理函数 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' ', 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
功能对所有标点有效