使用 SpaCy 进行短语结构句法分析:Python 代码示例
以下是使用 SpaCy 包实现句子的短语结构句法分析的 Python 代码示例:
import spacy
# 加载英文模型
nlp = spacy.load('en_core_web_sm')
# 定义待分析的句子
text = 'John saw the cat on the mat.'
# 对句子进行分析
doc = nlp(text)
# 输出分析结果
for chunk in doc.noun_chunks:
print(chunk.text, chunk.root.text, chunk.root.dep_, chunk.root.head.text)
输出结果为:
John John nsubj saw
the cat cat dobj saw
the mat mat prep on
其中,doc.noun_chunks 返回一个可迭代的对象,包含句子中的所有名词短语。对于每个名词短语,我们可以通过 chunk.text 获取其文本表示,通过 chunk.root.text 获取其主干词,通过 chunk.root.dep_ 获取其依存关系类型,通过 chunk.root.head.text 获取其依存关系的头部词。
原文地址: https://www.cveoy.top/t/topic/jYZr 著作权归作者所有。请勿转载和采集!