在 Python 中提取人物关系可以使用自然语言处理(NLP)技术。
下面是一个例子,使用 Python 库 spacy 提取人物关系:
import spacy
# 加载 spacy 模型
nlp = spacy.load('en_core_web_sm')
# 输入文本
text = "John and Mary are siblings. They both work at Google."
# 处理文本
doc = nlp(text)
# 遍历文档中的实体
for ent in doc.ents:
# 如果实体是人名,则打印其关系
if ent.label_ == 'PERSON':
print(f"{ent.text} is related to {ent.text} as {ent.label_}")