【问题标题】:spacy tokenization merges the wrong tokensspacy 标记化合并了错误的标记
【发布时间】:2019-01-25 05:47:38
【问题描述】:

我想使用 spacy 来标记维基百科的抓取。理想情况下,它会像这样工作:

text = 'procedure that arbitrates competing models or hypotheses.[2][3] Researchers also use experimentation to test existing theories or new hypotheses to support or disprove them.[3][4]'

# run spacy
spacy_en = spacy.load("en")
doc = spacy_en(text, disable=['tagger', 'ner'])
tokens = [tok.text.lower() for tok in doc]

# desired output
# tokens = [..., 'models', 'or', 'hypotheses', '.', '[2][3]', 'Researchers', ...

# actual output
# tokens = [..., 'models', 'or', 'hypotheses.[2][3', ']', 'Researchers', ...]

问题是'hypotheses.[2][3]'被粘在一起成为一个标记。

如何防止 spacy 将此“[2][3]”连接到前一个令牌? 只要是从hypotheses这个词和句末的point中分离出来的,我不管怎么处理。但是个别单词和语法应该远离句法噪音。

因此,例如,以下任何一个都是理想的输出:

  • 'hypotheses', '.', '[2][', '3]'
  • 'hypotheses', '.', '[2', '][3]'

【问题讨论】:

    标签: python python-3.x nlp tokenize spacy


    【解决方案1】:

    我认为您可以尝试使用中缀:

    import re
    import spacy
    from spacy.tokenizer import Tokenizer
    
    infix_re = re.compile(r'''[.]''')
    
    def custom_tokenizer(nlp):
      return Tokenizer(nlp.vocab, infix_finditer=infix_re.finditer)
    
    nlp = spacy.load('en')
    nlp.tokenizer = custom_tokenizer(nlp)
    doc = nlp(u"hello-world! I am hypothesis.[2][3]")
    print([t.text for t in doc])
    

    更多关于https://spacy.io/usage/linguistic-features#native-tokenizers

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多