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
-
classmethod
from_dict
(data)[source]¶ Construct an instance from python built-in containers.
- Parameters
data (dict) – dictionary such as
{ 'role': 'Head', 'pos': 'Na', 'term': '中文字' }
-
classmethod
from_json
(data, **kwargs)[source]¶ Construct an instance from JSON format.
- Parameters
data (str) – please refer
from_dict()
for format details.
-
property
-
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
See also
treelib.tree.Node
Please refer https://treelib.readthedocs.io/ for built-in usages.
-
data_class
¶ alias of
ParserNodeData
-
-
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.
-
property
-
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 fromckipnlp.parser.CkipParser
output.- Parameters
text (str) – A parsed tree from
ckipnlp.parser.CkipParser
output.normalize (bool) – Do text normalization using
normalize_text()
.
-
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 asParserNodeData.to_dict()
, and'children'
is a list of dictionaries of subtrees with the same format as this tree.
-
classmethod
from_json
(data, **kwargs)[source]¶ Construct an instance from JSON format.
- Parameters
data (str) – please refer
from_dict()
for format details.
-
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
orhead
instead of syntacticHead
.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.