【问题标题】:is there a method of rule based matching of spacy to match patterns?有没有一种基于规则的 spacy 匹配方法来匹配模式?
【发布时间】:2020-12-21 16:35:41
【问题描述】:

我想使用基于规则的匹配 我有一个像每个单词一样带有POS的文本:

 text1= "it_PRON is_AUX a_DET beautiful_ADJ  apple_NOUN"

 text2= "it_PRON is_AUX a_DET beautiful_ADJ and_CCONJ big_ADJ apple_NOUN"

所以我想创建一个基于规则的匹配,如果我们有一个 ADJ 后跟名词 (NOUN) 或一个 ADJ 后跟 (PUNCT 或 CCONJ) 后跟一个 ADJ 后跟一个名词 (NOUN)

所以,我想输出:

text1 = [beautiful_ADJ  apple_NOUN]
text2= [beautiful_ADJ and_CCONJ big_ADJ apple_NOUN]

我试图这样做,但我没有找到允许这样做的正确模式:

from spacy.matcher import Matcher,PhraseMatcher
import spacy
import spacy
from spacy.matcher import Matcher

matchers = {"first_processing": Matcher(nlp.vocab, validate=True)}
nlp = spacy.load("en_core_web_sm")
pattern = [{},{},{}]  #################################### we must find the right pattern
matchers["first_processing"].add("process_1", None, pattern)

nlp = spacy.load("en_core_web_sm")
doc = nlp("it_PRON is_AUX a_DET beautiful_ADJ and_CCONJ big_ADJ apple_NOUN")
a=matcher(doc)
for match_id, start, end in a:
    text = doc[start:end].text
    print(text)

【问题讨论】:

    标签: python-3.x regex spacy regex-greedy


    【解决方案1】:

    我了解到您拥有texts = ["it is a beautiful apple", "it is a beautiful and big apple"],并计划定义几个Matcher 模式来提取您拥有的文本中的某些POS 模式。

    您可以定义具有所需模式的列表列表,并作为第三个+参数传递给matcher.add

    from spacy.matcher import Matcher,PhraseMatcher
    import spacy
    from spacy.matcher import Matcher
    
    nlp = spacy.load("en_core_web_sm")
    matcher = Matcher(nlp.vocab,validate=True)
    patterns = [
        [{'POS': 'ADJ'}, {'POS': 'NOUN'}],
        [{'POS': 'ADJ'}, {'POS': 'CCONJ'}, {'POS': 'ADJ'}, {'POS': 'NOUN'}],
        [{'POS': 'ADJ'}, {'POS': 'PUNCT'}, {'POS': 'ADJ'}, {'POS': 'NOUN'}]
    ]
    matcher.add("process_1", None, *patterns)
    
    texts= ["it is a beautiful apple", "it is a beautiful and big apple"]
    for text in texts:
        doc = nlp(text)
        matches = matcher(doc)
        for _, start, end in matches:
            print(doc[start:end].text)
       
    # => beautiful apple
    #    beautiful and big apple
    #    big apple 
    

    【讨论】:

    • 非常感谢!只是我还有一个问题:如果我们在 texts = ["it is a beautiful and big apple"] 并且认为漂亮的语言模型(例如)是“ADJ”但我想在申请之前将其修改为“QUALIF”模式和模式中我将添加: [{'POS': 'QUALIF'}, {'POS': 'PUNCT'}, {'POS': 'ADJ'}, {'POS': 'NOUN'}] 。 ......那么我如何在应用模式匹配器之前修改 POS
    • 好的,已经是another question了。
    【解决方案2】:

    我不知道spacy,但这里有一个re(标准库模块)解决方案:

    import re
    
    REGEX = re.compile(r"\w+_ADJ +(?:\w+(?:_CCONJ|_PUNCT) +\w+_ADJ +)*\w+_NOUN")
    
    def extract(s):
        try:
            [extracted] = re.findall(REGEX, s)
        except ValueError:
            return []
        else:
            return extracted.split()
    
    >>> extract("it_PRON is_AUX a_DET beautiful_ADJ and_CCONJ big_ADJ apple_NOUN")
    ['beautiful_ADJ', 'and_CCONJ', 'big_ADJ', 'apple_NOUN']
    
    >>> extract("it_PRON is_AUX a_DET beautiful_ADJ apple_NOUN")
    ['beautiful_ADJ', 'apple_NOUN']
    

    【讨论】:

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