加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

nlp – 如何编写POS正则表达式的spacy matcher

发布时间:2020-12-14 06:04:31 所属栏目:百科 来源:网络整理
导读:Spacy有两个我想要结合的功能 – part-of-speech(POS)和 rule-based matching. 我怎样才能以简洁的方式将它们组合起来? 例如 – 假设输入是单个句子,我想验证它是否符合某些POS排序条件 – 例如动词在名词之后(类似于名词**动词正则表达式).结果应该是真的
Spacy有两个我想要结合的功能 – part-of-speech(POS)和 rule-based matching.

我怎样才能以简洁的方式将它们组合起来?

例如 – 假设输入是单个句子,我想验证它是否符合某些POS排序条件 – 例如动词在名词之后(类似于名词**动词正则表达式).结果应该是真的还是假的.那可行吗?或者匹配器在示例中是特定的

基于规则的匹配可以有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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读