【问题标题】:Merging sentences after merging spans合并跨度后合并句子
【发布时间】:2018-02-06 18:18:24
【问题描述】:

假设我有一句话

'使用 Data.Frames 可以让您有效地处理数据'

使用spacy这个例子将分为两句话:

>> example = 'Using Data.Frames allows you to handle data efficiently'
>> doc = nlp(example)
>> list(doc.sents)
[Using Data., Frames allows you to handle data efficiently]

按照recommendation from this other question,我可以通过跨度合并部分解决这个问题,就像这样

>> doc[1:4].merge()
>> doc[1]
Data.Frame

但是这不会合并之前拆分的句子

>>> list(doc.sents)
[Using Data.Frames, allows you to handle data efficiently]

在应用了一组合并操作之后,重新处理句子标记化的最佳方法是什么?这对我来说很重要,因为之后我需要导航解析树。

附:在我的实际应用程序中,我需要使用正则表达式来识别这种特殊情况的缩写(大致为[A-Za-z0-9-:]+.[A-Z][a-z0-9A-Z]+)。因此我不能使用 spacy 的 add_special_case。也许我可以使用自定义标记器,但我不知道该怎么做。


  • Python 版本: 2.7.12
  • 平台: Linux-4.4.0-21-generic-x86_64-with-LinuxMint-18-sarah
  • spaCy 版本: 2.0.5

【问题讨论】:

    标签: python spacy


    【解决方案1】:

    我设法通过基于自定义标记器的方法解决了我的问题,这意味着句子从一开始就不会被破坏,避免了以后需要合并它。

    根据文档中的信息here,当您创建自定义标记器时,token_match 参数可用于匹配不应该被破坏的模式。像这样,你可以设置:

    def custom_en_tokenizer(nlp):
        pattern_re = re.compile('<some regex martching a pattern that is broken>')
        return spacy.tokenizer.Tokenizer(nlp.vocab,
                                         English.Defaults.tokenizer_exceptions,
                                         English.Defaults.prefixes,
                                         English.Defaults.suffixes,
                                         English.Defaults.infixes,
                                         token_match=pattern_re.match)
    
    nlp = spacy.load('en')
    nlp.tokenizer = custom_en_tokenizer(nlp)
    

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 1970-01-01
      • 2012-07-20
      • 2023-03-25
      • 1970-01-01
      • 2021-03-30
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      相关资源
      最近更新 更多