python - 如何使用 Tokenizer 函数 tensorflow 对标点符号进行标记
问题描述
我使用 as 中的Tokenizer()
函数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 函数不包括标点符号。如何标记标点符号?( .
,在这个例子中。)
解决方案
一种可能性是将标点符号与带有空格的单词分开。我使用预处理功能来做到这一点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
功能对所有标点均有效
推荐阅读
- cdi - 使用 Mojarra 2.2.x 和 openwebbeans 1.0 (CDI 1.0) 使用 viewscoped bean 导航期间的 NPE
- r - 在 R Shiny 中模仿 VBA 输入 ListControl
- r - 创建多部分栅格的矢量足迹 [R]
- matlab - 为什么我会从随机神经网络中获得可预测的输出?
- reactjs - 我应该使用 React 提供多少静态 HTML?
- python - 如何在pygame中替换图片并检查与新图像和运动图像的碰撞
- javascript - 有没有办法在 R / DT 中默认隐藏组条目并显示聚合行
- php - 在 SentryServiceProvider.php 中调用未定义的方法 Illuminate\Foundation\Application::share()
- python - 添加 XML 元素并保存文件?
- c++ - OpenGL glGetUniformBlockIndex 在 nvidea gpus 上返回 INVALID_INDEX