Spacy DependencyMatcher 返回空值
Spacy DependencyMatcher returning empty value
教程中的代码:https://spacy.io/usage/rule-based-matching#dependencymatcher
import spacy
import en_core_web_sm
from spacy.matcher import DependencyMatcher
nlp = en_core_web_sm.load()
matcher = DependencyMatcher(nlp.vocab)
pattern = [
{
"RIGHT_ID": "anchor_founded",
"RIGHT_ATTRS": {"ORTH": "founded"}
},
{
"LEFT_ID": "anchor_founded",
"REL_OP": ">",
"RIGHT_ID": "founded_subject",
"RIGHT_ATTRS": {"DEP": "nsubj"},
},
{
"LEFT_ID": "anchor_founded",
"REL_OP": ">",
"RIGHT_ID": "founded_object",
"RIGHT_ATTRS": {"DEP": "dobj"},
},
{
"LEFT_ID": "founded_object",
"REL_OP": ">",
"RIGHT_ID": "founded_object_modifier",
"RIGHT_ATTRS": {"DEP": {"IN": ["amod", "compound"]}},
}
]
matcher.add("FOUNDED", [pattern])
doc = nlp("Lee, an experienced CEO, has FOUNDED two AI startups.")
matches = matcher(doc)
print(matches)
我应该得到的结果 [(4851363122962674176, [6, 0, 10, 9])]
我得到的是这个 []
我的环境
- spaCy 版本:3.0.5
- 平台:Windows-10
- Python版本:3.6.13
- en_core_web_sm = 3.0.0
您正在使用 ORTH 查找“founded”。这是区分大小写的。您应该将 ORTH 替换为 LOWER,或者在您输入的句子中将 FOUNDED 简单地小写
教程中的代码:https://spacy.io/usage/rule-based-matching#dependencymatcher
import spacy
import en_core_web_sm
from spacy.matcher import DependencyMatcher
nlp = en_core_web_sm.load()
matcher = DependencyMatcher(nlp.vocab)
pattern = [
{
"RIGHT_ID": "anchor_founded",
"RIGHT_ATTRS": {"ORTH": "founded"}
},
{
"LEFT_ID": "anchor_founded",
"REL_OP": ">",
"RIGHT_ID": "founded_subject",
"RIGHT_ATTRS": {"DEP": "nsubj"},
},
{
"LEFT_ID": "anchor_founded",
"REL_OP": ">",
"RIGHT_ID": "founded_object",
"RIGHT_ATTRS": {"DEP": "dobj"},
},
{
"LEFT_ID": "founded_object",
"REL_OP": ">",
"RIGHT_ID": "founded_object_modifier",
"RIGHT_ATTRS": {"DEP": {"IN": ["amod", "compound"]}},
}
]
matcher.add("FOUNDED", [pattern])
doc = nlp("Lee, an experienced CEO, has FOUNDED two AI startups.")
matches = matcher(doc)
print(matches)
我应该得到的结果 [(4851363122962674176, [6, 0, 10, 9])] 我得到的是这个 []
我的环境
- spaCy 版本:3.0.5
- 平台:Windows-10
- Python版本:3.6.13
- en_core_web_sm = 3.0.0
您正在使用 ORTH 查找“founded”。这是区分大小写的。您应该将 ORTH 替换为 LOWER,或者在您输入的句子中将 FOUNDED 简单地小写