我刚开始使用 Spacy。 我需要在一个句子中将介词动词(如果存在)与其介词组合在一起。 示例:“下车” 我知道我可以使用 POS 来识别动词和 ADP,首先我想继续使用连续出现的 VERB+ADP 规则,但这涵盖了无论介词如何,动词都是独立的情况。 你能支持吗?
我尝试了 subtree 和 children 选项,但据我所见,没有创建介词动词的动词+介词(无新含义)仍将被视为同一短语的一部分 提前致谢,
您可以使用此任务的依赖属性。
例如,假设您有以下句子:
You should get off the buss at the next stop, then you can wait on the right.
以下代码会打印出
get off
但不会打印出wait on
:
import spacy
from spacy.matcher import Matcher, DependencyMatcher
nlp = spacy.load('en_core_web_sm')
doc = nlp("You should get off the buss at the next stop up the road.")
matcher = DependencyMatcher(nlp.vocab)
pattern = [
{
"RIGHT_ID": "verb",
"RIGHT_ATTRS": {"POS": "VERB"}
},
{
"LEFT_ID": "verb",
"REL_OP": ">",
"RIGHT_ID": "prop",
"RIGHT_ATTRS": {"DEP": "prt"}
}
]
matcher.add("PROPOSITIONAL_VERB", [pattern])
matches = matcher(doc)
for match in matches:
verb = doc[match[1][0]]
prop = doc[match[1][1]]
print(f'{verb} - {prop}')
对于
get off
,POS标签如下:
对于
wait on
,这是完全一样的,所以POS标签没有帮助,正如你所解释的:
然而,spaCy 的小模型如下推断依赖标签,因此它们允许我们区分两个块并选择我们需要的一个:
这就是为什么上面的代码示例对动词使用 POS 标签,而对命题使用依赖标签。
您可以查看以下页面以了解 spaCy 中的依赖匹配器:https://spacy.io/api/dependencyparser
我还强烈建议您使用 spaCy 中内置的依赖项可视化工具,以更好地了解依赖项标签的工作原理:https://spacy.io/usage/visualizers
我希望这有帮助。