【问题标题】:Spacy pattern matching rule formationSpacy模式匹配规则形成
【发布时间】:2021-06-23 18:41:35
【问题描述】:

在Spacy模式匹配中,如何编写一个可以由以下模式触发的SINGLE模式匹配规则:

"red", apple"   # ie, the rule must trigger when the input sentence contains "red apple"
"large", "white", "building" # ie, the rule must trigger when the input sentence contains "large white building"
["lamp", "light", "fan"] # ie, the rule must trigger when the input sentence contains a token "light"

在上面,每个部分没有相同数量的token,并且每个这样的部分可能不止一个token长(所以我不能使用Spacy运算符“IN”来组合所有这些部分)

【问题讨论】:

    标签: nlp spacy


    【解决方案1】:

    您不能编写单个令牌匹配器模式来做到这一点。

    但是您可以为多个模式分配相同的标签,因此没有理由这样做。如果你想比标签更具体,你也可以add IDs to patterns

    以下是您如何使用您的图案制作一个标签:

    import spacy
    
    nlp = spacy.blank("en")
    
    ruler = nlp.add_pipe("entity_ruler")
    
    patterns = [{"label": "SOMETHING", "pattern": "red apple"},
                {"label": "SOMETHING", "pattern": "large white building"},
                {"label": "SOMETHING", "pattern": "lamp"},
                {"label": "SOMETHING", "pattern": "light"},
                {"label": "SOMETHING", "pattern": "fan"}
                ]
    
    ruler.add_patterns(patterns)
    
    text = "I saw a red apple under a lamp in the large white building"
    
    for ent in nlp(text).ents:
        print(ent.label_, ent, sep="\t")
    

    我在这里使用了 EntityRuler,但如果您想使用 PhraseMatcher 来使用 on_match 功能,您可以执行 matcher.add("SOMETHING", pattern, myfunc) 之类的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      • 2022-11-10
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多