【发布时间】:2019-06-30 04:34:31
【问题描述】:
我想使用 Spacy 匹配器从 Wikipedia 中挖掘“is a”(和其他)关系,以构建知识数据库。
我有以下代码:
nlp = spacy.load("en_core_web_lg")
text = u"""Garfield is a large comic strip cat that lives in Ohio. Cape Town is the oldest city in South Africa."""
doc = nlp(text)
sentence_spans = list(doc.sents)
# Write a pattern
pattern = [
{"POS": "PROPN", "OP": "+"},
{"LEMMA": "be"},
{"POS": "DET"},
{"POS": "ADJ", "OP": "*"},
{"POS": "NOUN", "OP": "+"}
]
# Add the pattern to the matcher and apply the matcher to the doc
matcher.add("IS_A_PATTERN", None, pattern)
matches = matcher(doc)
# Iterate over the matches and print the span text
for match_id, start, end in matches:
print("Match found:", doc[start:end].text)
不幸的是,这匹配:
Match found: Garfield is a large comic strip
Match found: Garfield is a large comic strip cat
Match found: Town is the oldest city
Match found: Cape Town is the oldest city
而我只是想要:
Match found: Garfield is a large comic strip cat
Match found: Cape Town is the oldest city
此外,我不介意能够说明匹配的第一部分必须是句子的主语,最后一部分是谓词。
我也想以这种方式分开返回:
['Garfield', 'is a', 'large comic strip cat', 'comic strip cat']
['Cape Town', 'is the', 'oldest city', 'city']
这样我就可以获得城市列表。
这在 Spacy 中是否可行,或者等效的 Python 代码是什么?
【问题讨论】:
-
我不确定您的目标是什么,但很有可能您从头开始没有任何意义。许多人已经研究了使用维基百科数据提取信息的方法。开始寻找现有工具/方法的一个地方可能是 DBpedia 项目。