gpt4 book ai didi

nlp - 如何编写 POS 正则表达式的 spacy 匹配器

转载 作者:行者123 更新时间:2023-12-04 22:42:25 25 4
gpt4 key购买 nike

Spacy 有两个我想结合的功能 - part-of-speech (POS) 和 rule-based matching .

我怎样才能以一种巧妙的方式将它们结合起来?

例如 - 假设输入是一个句子,我想验证它是否满足某些 POS 排序条件 - 例如动词在名词之后(类似于 noun**verb regex)。结果应该是真或假。那可行吗?或者匹配器是特定的,如示例中所示

基于规则的匹配可以有POS规则吗?

如果没有 - 这是我目前的计划 - 将所有内容收集在一个字符串中并应用正则表达式

    import spacy
nlp = spacy.load('en')
#doc = nlp(u'is there any way you can do it')
text=u'what are the main issues'
doc = nlp(text)

concatPos = ''
print(text)
for word in doc:
print(word.text, word.lemma, word.lemma_, word.tag, word.tag_, word.pos, word.pos_)
concatPos += word.text +"_" + word.tag_ + "_" + word.pos_ + "-"
print('-----------')
print(concatPos)
print('-----------')

# output of string- what_WP_NOUN-are_VBP_VERB-the_DT_DET-main_JJ_ADJ-issues_NNS_NOUN-

最佳答案

当然,只需使用 POS 属性。

import spacy
nlp = spacy.load('en')
from spacy.matcher import Matcher
from spacy.attrs import POS
matcher = Matcher(nlp.vocab)
matcher.add_pattern("Adjective and noun", [{POS: 'ADJ'}, {POS: 'NOUN'}])

doc = nlp(u'what are the main issues')
matches = matcher(doc)

关于nlp - 如何编写 POS 正则表达式的 spacy 匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42830248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com