成分句法分析 您所在的位置:网站首页 日语语法检测在线 成分句法分析

成分句法分析

2024-03-26 09:09| 来源: 网络整理| 查看: 265

# 成分句法分析

153/800loading# 简介

成分句法分析(Constituency Parsing、CON)是一种分析一个句子在语法上的递归构成,并将其表示为树形结构的任务。HanLP支持中文CTBopen in new window、英文PTBopen in new window和日语NPCMJopen in new window等树库标准。

# 调用方法# 创建客户端 from hanlp_restful import HanLPClient # auth不填则匿名,zh中文,mul多语种 HanLP = HanLPClient('https://www.hanlp.com/api', auth=None, language='zh') # 申请秘钥

由于服务器算力有限,匿名用户每分钟限2次调用。如果你需要更多调用次数,建议申请免费公益API秘钥authopen in new window。

# 分析

指定仅执行成分句法分析:

doc = HanLP.parse('晓美焰来到北京立方庭参观自然语义科技公司。', tasks=['pos', 'con']) print(doc)

返回值为一个Documentopen in new window。doc['con']为Tree类型,是list的子类。

# 可视化

通过doc.pretty_print(),可以在等宽字体环境中得到可视化,你需要取消换行才能对齐可视化结果。我们已经发布HTML环境的可视化,在Jupyter Notebook中自动对齐中文。

doc.pretty_print() # 括号形式

括号形式是学术界最通用的格式:

tree = doc['con'][0] print(tree) # 操作短语树的技巧

短语结构树的类型为phrasetree.tree.Tree,提供了许多接口,此处列举其中一些常用的接口。

# 按高度枚举子树 for subtree in tree.subtrees(lambda t: t.height() == 4): print(f'子树:{subtree} 标签:{subtree.label()} 短语:{subtree.leaves()}') # 按标签枚举子树 for subtree in tree.subtrees(lambda t: t.label() == 'NP'): print(subtree) # 遍历子节点 print(f'父节点{subtree}的子节点有:') for child in subtree: print(child) # 本地调用

本地调用方法请参考教程open in new window。

# 多语种支持

其他标准用于外语或多语种,请参考文档open in new window加载相应的外语或多语种模型。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有