Files
2026-07-13 12:37:18 +08:00

1.3 KiB

jupytext, kernelspec
jupytext kernelspec
formats text_representation
ipynb,md:myst
extension format_name format_version jupytext_version
.md myst 0.8 1.4.2
display_name language name
Python 3 python python3

constituency

Constituency Parsing is the process of analyzing the sentences by breaking down it into sub-phrases also known as constituents.

To parse a tokenized sentence into constituency tree, first load a parser:

.. margin:: Batching is Faster

    .. Hint:: To speed up, parse multiple sentences at once, and use a GPU.
:tags: [output_scroll]
import hanlp

con = hanlp.load(hanlp.pretrained.constituency.CTB9_CON_FULL_TAG_ELECTRA_SMALL)

Then parse a sequence or multiple sequences of tokens to it.

:tags: [output_scroll]
tree = con(["2021年", "HanLPv2.1", "带来", "最", "先进", "的", "多", "语种", "NLP", "技术", "。"])

The constituency tree is a nested list of constituencies:

:tags: [output_scroll]
tree

You can str or print it to get its bracketed form:

:tags: [output_scroll]
print(tree)

All the pre-trained parsers and their scores are listed below.


.. automodule:: hanlp.pretrained.constituency
    :members: