CKIP E-HowNet¶
CKIP E-HowNet Tools¶
Author¶
Mu Yang <https://muyang.pro>
Requirements¶
Python 3.6+
PLY (Python Lex-Yacc) 3.11+
TreeLib 1.5.5+
wcwidth 0.1.7+
Installation¶
pip install -U ehownet
Usage¶
See https://ehownet.readthedocs.io/en/latest/main/grammar.html for E-HowNet grammar.
See https://ehownet.readthedocs.io/en/latest/main/parse_node.html for E-HowNet parsing nodes usage.
E-HowNet Parser¶
CLI¶
# Usage
ehn-parser <text> [<text> ...]
# Example
ehn-parser \
"{MusicTool|樂器_x:predication={own|有:possession={按鈕|PushingButton:whole={x}}}}" \
"{InstitutePlace|場所:telic={or({experiment|實驗:location={~}},{research|研究:location={~}})}}" \
"TimePoint={},manner={urgent|急}"
Output:
#1
[Entity $x] MusicTool|樂器
└── [Feature] predication
└── [Entity] own|有
└── [Feature] possession
└── [Entity] 按鈕|PushingButton
└── [Feature] whole
└── $x
#2
[Entity] InstitutePlace|場所
└── [Feature] telic
└── [FunctionEntity]
└── [Function] or
├── [Entity] experiment|實驗
│ └── [Feature] location
│ └── [TildeEntity]
└── [Entity] research|研究
└── [Feature] location
└── [TildeEntity]
#3
[Root]
├── [Feature] TimePoint
│ └── [AnyEntity]
└── [Feature] manner
└── [Entity] urgent|急
Python API¶
from ehn.parse import EhnParser
text = '{MusicTool|樂器_x:predication={own|有:possession={按鈕|PushingButton:whole={x}}}}'
parser = EhnParser()
ress = parser(text, debug=False)
for res in ress:
res.tree().show()
Output:
[Entity $x] MusicTool|樂器
└── [Feature] predication
└── [Entity] own|有
└── [Feature] possession
└── [Entity] 按鈕|PushingButton
└── [Feature] whole
└── $x
E-HowNet Grammar¶
This section describes the grammar of the E-HowNet expression.
Text¶
TEXTAny non empty string containing the following characters:
Alphabets and Numbers (
A-Za-z0-9)Unicode Characters (
\x80-\U0010FFFF)|,+,-,.,?,#.
NUMBERe.g.
1,0.1,1e-4
COINDEXx,x1,x2, …x?(refer to the not mentioned subject)
Node¶
Entity¶
-
{TEXT}{TEXT:FEATURE}{TEXT:FEATURE,FEATURE}{TEXT:FEATURE,FEATURE,...}{TEXT_COINDEX:FEATURE}{TEXT_COINDEX:FEATURE,FEATURE}{TEXT_COINDEX:FEATURE,FEATURE,...}
-
{FUNCTION}{FUNCTION:FEATURE}{FUNCTION:FEATURE,FEATURE}{FUNCTION:FEATURE,FEATURE,...}{FUNCTION_COINDEX:FEATURE}{FUNCTION_COINDEX:FEATURE,FEATURE}{FUNCTION_COINDEX:FEATURE,FEATURE,...}
-
{} -
{"TEXT"} -
{NUMBER} -
{~}Deprecated since version 0.6.
-
{COINDEX}
Feature¶
-
TEXT=ENTITYTEXT=RESTRICTION
-
FUNCTION=ENTITYFUNCTION=RESTRICTION
Function¶
-
TEXT()TEXT(RESTRICTION)TEXT(ENTITY)TEXT(ENTITY,ENTITY)TEXT(ENTITY,ENTITY,...)
Restriction¶
-
/ENTITY/ENTITY_COINDEX
Valid Expressions¶
ENTITY or any number of FEATUREs joined by ,s.
ENTITY
FEATURE
FEATURE,FEATURE
FEATURE,FEATURE,...
Parse Nodes¶
There are five types of nodes in E-HowNet expression — Entity, Feature, Function, Restriction, and Root.

Major Nodes¶
-
class
EhnParseNode The prototype of E-HowNet parsing nodes.
-
head: str¶ The head of this node.
-
get_features()¶ Get the features (or
[]if not exists)
-
get_arguments()¶ Get the arguments (or
[]if not exists)
-
get_value()¶ Get the value (or
Noneif not exists)
-
get_function()¶ Get the function (or
Noneif not exists)
-
get_anchor()¶ Get the anchor (or
Noneif not exists)
-
children()¶ Yields all direct child nodes of this node.
-
descendant()¶ Yields all descendant nodes (including self) of this node.
-
decode()¶ Converts to text representation.
-
tree() → treelib.Tree¶ Generates a tree representation of this node and all its descendant nodes.
-
Entity¶
-
class
EhnParseEntityBase The base class of E-HowNet parsing entity nodes.
Subclasses:
EhnParseNormalEntityA normal entity. Can be an anchor.EhnParseFunctionEntityAn entity with function head. Can be an anchor.EhnParseAnyEntityA placeholder entity.EhnParseNameEntityA name entity.EhnParseNumberEntityA number entity.EhnParseTildeEntityAn entity refers to the parent entity.EhnParseCoindexEntityAn entity refers to an anchor entity.
Feature¶
-
class
EhnParseFeatureBase The base class of E-HowNet parsing feature nodes.
Subclasses:
EhnParseNormalFeatureA normal feature.EhnParseFunctionFeatureAn feature with function head.
-
property
value¶ Can be either Entity or Restriction.
Function¶
-
class
EhnParseFunctionBase The base class of E-HowNet parsing function nodes.
Subclasses:
-
property
arguments¶ A list of Entities or Restriction
-
property
Restriction¶
-
class
EhnParseRestrictionBase The base class of E-HowNet parsing function nodes.
Subclasses:
EhnParseRestriction. Can be an anchor.
Root¶
ehn package¶
Subpackages
ehn.parse package¶
Subpackages
ehn.parse.node package¶
Please refer the tutorial “Parse Nodes”.
Submodules
ehn.parse.node.base module¶
Please refer the tutorial “Parse Nodes”.
-
class
ehn.parse.node.base.EhnParseTree(tree=None, deep=False, node_class=None, identifier=None)[source]¶ Bases:
treelib.tree.Tree-
show(*args, data_property='_tree_label', **kwargs)[source]¶ Print the tree structure in hierarchy style.
You have three ways to output your tree data, i.e., stdout with
show(), plain text file withsave2file(), and json string withto_json(). The former two use the same backend to generate a string of tree structure in a text graph.Version >= 1.2.7a*: you can also specify the
line_typeparameter, such as ‘ascii’ (default), ‘ascii-ex’, ‘ascii-exr’, ‘ascii-em’, ‘ascii-emv’, ‘ascii-emh’) to the change graphical form.
- Parameters
nid – the reference node to start expanding.
level – the node level in the tree (root as level 0).
idhidden – whether hiding the node ID when printing.
filter – the function of one variable to act on the
Nodeobject. When this parameter is specified, the traversing will not continue to following children of node whose condition does not pass the filter.key – the
keyparam for sortingNodeobjects in the same level.reverse – the
reverseparam for sortingNodeobjects in the same level.line_type –
data_property – the property on the node data object to be printed.
- Returns
None
-
-
class
ehn.parse.node.base.EhnParseEntityBase[source]¶ Bases:
ehn.parse.node.base.EhnParseNodeE-HowNet Parsing: Base Entity Node
-
class
ehn.parse.node.base.EhnParseFeatureBase[source]¶ Bases:
ehn.parse.node.base.EhnParseNodeE-HowNet Parsing: Base Feature Node
-
class
ehn.parse.node.base.EhnParseFunctionBase[source]¶ Bases:
ehn.parse.node.base.EhnParseNodeE-HowNet Parsing: Base Function Node
-
class
ehn.parse.node.base.EhnParseRestrictionBase[source]¶ Bases:
ehn.parse.node.base.EhnParseNodeE-HowNet Parsing: Base Function Node
-
class
ehn.parse.node.base.EhnParseRootBase[source]¶ Bases:
ehn.parse.node.base.EhnParseNodeE-HowNet Parsing: Base Root Node
-
class
ehn.parse.node.base.EhnParseAnchor(head=None)[source]¶ Bases:
objectE-HowNet Parsing: Node Anchor
-
class
ehn.parse.node.base.EhnParseStrHead(head)[source]¶ Bases:
objectE-HowNet Parsing: Base Node with String Head
-
class
ehn.parse.node.base.EhnParseFunctionHead(function)[source]¶ Bases:
objectE-HowNet Parsing: Base Node with Function Head
-
class
ehn.parse.node.base.EhnParseValueBody(value)[source]¶ Bases:
objectE-HowNet Parsing: Base Node with Value
-
class
ehn.parse.node.base.EhnParseFeatureBody(*features)[source]¶ Bases:
objectE-HowNet Parsing: Base Node with Feature
ehn.parse.node.entity module¶
Please refer the tutorial “Parse Nodes”.
-
class
ehn.parse.node.entity.EhnParseNormalEntity(head, *features, anchor=None)[source]¶ Bases:
ehn.parse.node.base.EhnParseEntityBase,ehn.parse.node.base.EhnParseStrHead,ehn.parse.node.base.EhnParseFeatureBody,ehn.parse.node.base.EhnParseAnchorBodyE-HowNet Parsing: Normal Entity Node
-
feature_type¶
-
-
class
ehn.parse.node.entity.EhnParseFunctionEntity(function, *features, anchor=None)[source]¶ Bases:
ehn.parse.node.base.EhnParseEntityBase,ehn.parse.node.base.EhnParseFunctionHead,ehn.parse.node.base.EhnParseFeatureBody,ehn.parse.node.base.EhnParseAnchorBodyE-HowNet Parsing: Function Entity Node
-
feature_type¶
-
-
class
ehn.parse.node.entity.EhnParseAnyEntity[source]¶ Bases:
ehn.parse.node.base.EhnParseEntityBaseE-HowNet Parsing: Any Entity Node
-
class
ehn.parse.node.entity.EhnParseNameEntity(head)[source]¶ Bases:
ehn.parse.node.base.EhnParseEntityBase,ehn.parse.node.base.EhnParseStrHeadE-HowNet Parsing: Name Entity Node
-
class
ehn.parse.node.entity.EhnParseNumberEntity(head)[source]¶ Bases:
ehn.parse.node.base.EhnParseEntityBase,ehn.parse.node.base.EhnParseStrHeadE-HowNet Parsing: Number Entity Node
-
class
ehn.parse.node.entity.EhnParseTildeEntity[source]¶ Bases:
ehn.parse.node.base.EhnParseEntityBaseE-HowNet Parsing: Tilde Entity Node
-
class
ehn.parse.node.entity.EhnParseCoindexEntity(head)[source]¶ Bases:
ehn.parse.node.base.EhnParseEntityBase,ehn.parse.node.base.EhnParseStrHeadE-HowNet Parsing: Coindex Entity Node
ehn.parse.node.feature module¶
Please refer the tutorial “Parse Nodes”.
-
class
ehn.parse.node.feature.EhnParseNormalFeature(head, value)[source]¶ Bases:
ehn.parse.node.base.EhnParseFeatureBase,ehn.parse.node.base.EhnParseStrHead,ehn.parse.node.base.EhnParseValueBodyE-HowNet Parsing: Normal Feature Node
-
class
ehn.parse.node.feature.EhnParseFunctionFeature(function, value)[source]¶ Bases:
ehn.parse.node.base.EhnParseFeatureBase,ehn.parse.node.base.EhnParseFunctionHead,ehn.parse.node.base.EhnParseValueBodyE-HowNet Parsing: Function Feature Node
ehn.parse.node.other module¶
Please refer the tutorial “Parse Nodes”.
-
class
ehn.parse.node.other.EhnParseRoot(*features)[source]¶ Bases:
ehn.parse.node.base.EhnParseRootBase,ehn.parse.node.base.EhnParseFeatureBodyE-HowNet Parsing: Root Node
-
feature_type¶
-
-
class
ehn.parse.node.other.EhnParseFunction(head, *arguments)[source]¶ Bases:
ehn.parse.node.base.EhnParseFunctionBase,ehn.parse.node.base.EhnParseArgumentBody,ehn.parse.node.base.EhnParseStrHeadE-HowNet Parsing: Function Node
-
class
ehn.parse.node.other.EhnParseRestriction(value, anchor=None)[source]¶ Bases:
ehn.parse.node.base.EhnParseRestrictionBase,ehn.parse.node.base.EhnParseValueBody,ehn.parse.node.base.EhnParseAnchorBodyE-HowNet Parsing: Restriction Node
-
value_type¶
-
Submodules
