【问题标题】:How to tokenize punctuations using the Tokenizer function tensorflow如何使用 Tokenizer 函数 tensorflow 对标点符号进行标记
【发布时间】:2020-09-29 17:53:38
【问题描述】:

我使用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 功能不包括标点符号。如何标记标点符号? (.,在这个例子中。)

【问题讨论】:

    标签: python tensorflow keras nlp tokenize


    【解决方案1】:

    一种可能性是用空格将标点符号与单词分开。我使用预处理函数pad_punctuation 来做到这一点。在此之后我申请Tokenizerfilter=''

    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 函数对所有标点均有效

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 1970-01-01
      • 2021-11-28
      • 2022-08-23
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多