ckipnlp.util.parser module

class ckipnlp.util.parser.ParserNodeData[source]

Bases: tuple

A parser node.

role

str – the role.

pos

str – the post-tag.

term

str – the text term.

classmethod from_text(text)[source]

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

to_text()[source]

Transform to plain text.

to_dict()[source]

Transform to python dict/list.

to_json(**kwargs)[source]

Transform to JSON format.

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.
to_text()[source]

Transform to plain text.

to_dict()[source]

Transform to python dict/list.

to_json(**kwargs)[source]

Transform to JSON format.

class ckipnlp.util.parser.ParserRelation[source]

Bases: tuple

A parser relation.

head

ParserNode – the head node.

tail

ParserNode – the tail node.

relation

str – the relation.

head_first
to_dict()[source]

Transform to python dict/list.

to_json(**kwargs)[source]

Transform to JSON format.

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 #. Prepend root: at beginning.

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.

to_dict(node_id=0)[source]

Transform to python dict/list.

to_json(**kwargs)[source]

Transform to JSON format.

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

Show pretty tree.

has_dummies(node_id)[source]

Determine if a node has dummies.

Parameters:node_id (int) – ID of target node.
Returns:bool – whether or not target node has dummies.
get_dummies(node_id, deep=True, _check=True)[source]

Get dummies of a node.

Parameters:
  • node_id (int) – ID of target node.
  • deep (bool) – find dummies recursively.
Returns:

Tuple[ParserNode] – the dummies.

Raises:

LookupError – when target node has no dummy (only when _check is set).

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

Get all head nodes of a subtree.

Parameters:
  • root_id (int) – ID of the root node of target subtree.
  • deep (bool) – find heads recursively.
Returns:

  • List[ParserNode] – the head nodes (when deep is set).
  • ParserNode – the head node (when deep is not set).

Todo

Get information of nodes with pos type PP or GP.

get_relations(root_id=0)[source]

Get all relations of a subtree.

Parameters:root_id (int) – ID of the subtree root node.
Yields:ParserRelation – the relation.