ckipnlp.util.parser module

class ckipnlp.util.parser.ParserNodeData[source]

Bases: tuple

A parser node.

property role

str – the role.

property pos

str – the post-tag.

property term

str – the text term.

classmethod from_text(text)[source]

Construct an instance from ckipnlp.parser.CkipParser output.

Parameters

data (str) – text such as 'Head:Na:中文字'.

Notes

  • 'Head:Na:中文字' -> role = 'Head', pos = 'Na', term = '中文字'

  • 'Head:Na' -> role = 'Head', pos = 'Na', term = None

  • 'Na' -> role = None, pos = 'Na', term = None

to_text()[source]

Transform to plain text.

Returns

str

classmethod from_dict(data)[source]

Construct an instance from python built-in containers.

Parameters

data (dict) – dictionary such as { 'role': 'Head', 'pos': 'Na', 'term': '中文字' }

to_dict()[source]

Transform to python built-in containers.

Returns

dict

classmethod from_json(data, **kwargs)[source]

Construct an instance from JSON format.

Parameters

data (str) – please refer from_dict() for format details.

to_json(**kwargs)[source]

Transform to JSON format.

Returns

str

class ckipnlp.util.parser.ParserNode(tag=None, identifier=None, expanded=True, data=None)[source]

Bases: treelib.node.Node

A parser node for tree.

data
Type

ParserNodeData

See also

treelib.tree.Node

Please refer https://treelib.readthedocs.io/ for built-in usages.

data_class

alias of ParserNodeData

to_dict()[source]

Transform to python built-in containers.

Returns

dict

to_json(**kwargs)[source]

Transform to JSON format.

Returns

str

class ckipnlp.util.parser.ParserRelation[source]

Bases: tuple

A parser relation.

property head

ParserNode – the head node.

property tail

ParserNode – the tail node.

property relation

str – the relation.

to_dict()[source]

Transform to python built-in containers.

Returns

dict

to_json(**kwargs)[source]

Transform to JSON format.

Returns

str

class ckipnlp.util.parser.ParserTree(tree=None, deep=False, node_class=None)[source]

Bases: treelib.tree.Tree

A parsed tree.

See also

treereelib.tree.Tree

Please refer https://treelib.readthedocs.io/ for built-in usages.

node_class

alias of ParserNode

static normalize_text(tree_text)[source]

Text normalization for ckipnlp.parser.CkipParser output.

Remove leading number and trailing #.

classmethod from_text(tree_text, *, normalize=True)[source]

Create a ParserTree object from ckipnlp.parser.CkipParser output.

Parameters
to_text(node_id=0)[source]

Transform to plain text.

Returns

str

classmethod from_dict(data)[source]

Construct an instance from python built-in containers.

Parameters

data (dict) – dictionary such as { 'id': 0, 'data': { ... }, 'children': [ ... ] }, where 'data' is a dictionary with the same format as ParserNodeData.to_dict(), and 'children' is a list of dictionaries of subtrees with the same format as this tree.

to_dict(node_id=0)[source]

Transform to python built-in containers.

Returns

dict

classmethod from_json(data, **kwargs)[source]

Construct an instance from JSON format.

Parameters

data (str) – please refer from_dict() for format details.

to_json(node_id=0, **kwargs)[source]

Transform to JSON format.

Returns

str

show(*, key=<function ParserTree.<lambda>>, idhidden=False, **kwargs)[source]

Show pretty tree.

get_children(node_id, *, role)[source]

Get children of a node with given role.

Parameters
  • node_id (int) – ID of target node.

  • role (str) – the target role.

Yields

ParserNode – the children nodes with given role.

get_heads(root_id=0, *, semantic=True, deep=True)[source]

Get all head nodes of a subtree.

Parameters
  • root_id (int) – ID of the root node of target subtree.

  • semantic (bool) – use semantic/syntactic policy. For semantic mode, return DUMMY or head instead of syntactic Head.

  • deep (bool) – find heads recursively.

Yields

ParserNode – the head nodes.

get_relations(root_id=0, *, semantic=True)[source]

Get all relations of a subtree.

Parameters
  • root_id (int) – ID of the subtree root node.

  • semantic (bool) – please refer get_heads() for policy detail.

Yields

ParserRelation – the relations.