如何编写 POS 正则表达式的 spacy 匹配器 [英] how to write spacy matcher of POS regex
问题描述
Spacy 有两个我想结合的功能 - 部分演讲(POS) 和基于规则的匹配.
Spacy has two features I'd like to combine - part-of-speech (POS) and rule-based matching.
我怎样才能将它们巧妙地结合起来?
How can I combine them in a neat way?
例如 - 假设输入是一个句子,我想验证它是否满足某些 POS 排序条件 - 例如动词在名词之后(类似于 noun**verb regex).结果应该是真或假.那可行吗?或者匹配器是特定的,如示例中
For example - let's say input is a single sentence and I'd like to verify it meets some POS ordering condition - for example the verb is after the noun (something like noun**verb regex). result should be true or false. Is that doable? or the matcher is specific like in the example
基于规则的匹配可以有POS规则吗?
Rule-based matching can have POS rules?
如果没有 - 这是我目前的计划 - 将所有内容收集在一个字符串中并应用正则表达式
If not - here is my current plan - gather everything in one string and apply regex
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 属性.
Sure, simply use the POS attribute.
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)
这篇关于如何编写 POS 正则表达式的 spacy 匹配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!