CKIP E-HowNet¶
CKIP E-HowNet Tools¶
Git¶
PyPI¶
Documentation¶
Requirements¶
Python 3.6+
PLY (Python Lex-Yacc) 3.11+
TreeLib 1.6.0+
wcwidth 0.2.5+
dataclasses (Python 3.6 only)
Installation¶
pip install -U ehownet
One may download the database file manually from https://ckip.iis.sinica.edu.tw/CKIP/ehownet_reg/ .
License¶
Copyright (c) 2018-2021 CKIP Lab under the GPL-3.0 License.
Tutorials¶
Grammar¶
This section describes the grammar of the E-HowNet expression.
Tokens¶
TEXT
Any non empty string containing the following characters:
Alphabets and Numbers (
A-Za-z0-9
)Unicode Characters (
\x80-\U0010FFFF
)|
,#
.
NUMBER
e.g.
1
,0.1
,1e-4
COINDEX
x
,x1
,x2
, …
x?
(refer to the unmentioned subject entity)
Nodes¶
Entity¶
Entities are basic elements in E-HowNet definition.
-
A normal entity.
Syntaxes:
{TEXT}
{TEXT:FEATURE}
{TEXT:FEATURE,FEATURE}
{TEXT:FEATURE,FEATURE,...}
{TEXT_COINDEX}
{TEXT_COINDEX:FEATURE}
{TEXT_COINDEX:FEATURE,FEATURE}
{TEXT_COINDEX:FEATURE,FEATURE,...}
Description:
TEXT
is the head (the label of the inherited concept) of this entity.FEATURE
s are the additional features to this entity.COINDEX
is the anchor of this entity for further reference.
Example:
{human|人:kind={other|另}}
-
An entity with function as its head.
Syntaxes:
{FUNCTION}
{FUNCTION:FEATURE}
{FUNCTION:FEATURE,FEATURE}
{FUNCTION:FEATURE,FEATURE,...}
{FUNCTION_COINDEX}
{FUNCTION_COINDEX:FEATURE}
{FUNCTION_COINDEX:FEATURE,FEATURE}
{FUNCTION_COINDEX:FEATURE,FEATURE,...}
Description:
Similar to normal entity, but replace the head by a function (
FUNCTION
) of entity/entities.
-
A “name”.
Syntaxes:
{"TEXT"}
Example:
{country|國家:location={Europe|歐洲},quantifier={definite|定指},name={"英國"}}
The name of this country is 英國.
-
A number.
Syntaxes:
{NUMBER}
Example:
{month|月:sequence={1}}
The sequence of this mouth if 1.
Reference¶
References refer to other entities.
-
Refers to previous mentioned entity.
Syntaxes:
{COINDEX}
Description:
Refers to the entity with anchor
_COINDEX
.If
_COINDEX
does not exist, represent that all{COINDEX}
with the same name are the same placeholder.
Example:
{A_x1:r={B:b={x1}}}
{x1}
refers to{A_x1:...}
.{vehicle|交通工具_x1:predication={fly|飛:theme={x1}}}
{x1}
refers to{vehicle|交通工具_x1:...}
.
-
Refers to unmentioned subject entity.
Syntaxes:
{x?}
Example:
r={B:b={x?}}
{x?}
refers to the unmentioned subject entityS
with the following attribute{S_x1:r={B:b={x1}}}
.現役:
predication={當兵|ServeInArmy:agent={x?},aspect={Vgoingon|進展}}
{x?}
refers to the unmentioned subject entityS
. For example, the word 軍人 of 現役軍人.
-
Refers to the root entity.
Syntaxes:
{~}
Example:
{A:r={B:b={~}}}
{~}
refers to{A:...}
.{vehicle|交通工具_x1:predication={fly|飛:theme={~}}}
{~}
refers to the root entity{vehicle|交通工具_x1:...}
.
Note
Deprecated since version 3.0.
Placeholder¶
Placeholders represent any entities under the given restriction.
EhnParseRestrictionPlaceholder
A restriction placeholder.
Syntaxes:
/ENTITY
/ENTITY_COINDEX
Description:
ENTITY
shows that this node can be replace by any hyponymy/instance(s) of theENTITY
.COINDEX
is the anchor of this restriction for further reference.
Example:
{CentrePart(/{place|地方})}
The argument of CentrePart must be a hyponymy/instance of place|地方.
-
A placeholder without restriction.
Syntaxes:
{}
Description:
Represent a placeholder without any restriction. Only used as the value of a feature.
Example:
feature={}
{}
represent that the value of this feature can be any entity.
Feature¶
Features provides extra information to entities.
-
A normal feature.
Syntaxes:
TEXT=ENTITY
TEXT=REFERENCE
TEXT=RESTRICTION
TEXT={}
Description:
TEXT
is the head (the name) of the this feature.ENTITY
/RESTRICTION
is the value of this feature.
Example:
{thing|萬物:qualification={concrete|具體}}
The qualification of thing|萬物 is concrete|具體. Here
qualification={concrete|具體}
is a normal feature.
-
A function feature.
Syntaxes:
FUNCTION=ENTITY
FUNCTION=REFERENCE
FUNCTION=RESTRICTION
FUNCTION={}
Description:
Similar to normal feature, but replace the head by a function (
FUNCTION
) of entity/entities.Example:
{animate|生物:ability({SelfMove|自移})={very|很}}
The ability of SelfMove|自移 of animate|生物 is very|很. Here
ability({SelfMove|自移})={very|很}
is a function feature.
Function¶
Functions act on entities.
EhnParseFunction
A function of entity/entities or restriction.
Syntaxes:
TEXT()
TEXT(RESTRICTION)
TEXT(ENTITY)
TEXT(ENTITY,ENTITY)
TEXT(ENTITY,ENTITY,...)
Description:
TEXT
is the head (the name) of the this function.ENTITY
s are the arguments of this function; everyENTITY
can be replaced by aREFERENCE
.RESTRICTION
represent that the arguments of this function can be anything under this restriction.TEXT()
represent that the arguments of this function can be any entity/entities.
Note
TEXT({})
is not valid. UseTEXT()
instead.
Valid Expressions¶
A valid expression can be an ENTITY
or any number of FEATURE
s joined by ,
s.
ENTITY
FEATURE
FEATURE,FEATURE
FEATURE,FEATURE,...
Database¶
This section describes the E-HowNet database.
One may download the database file manually from https://ckip.iis.sinica.edu.tw/CKIP/ehownet_reg/ .
- class EhnDb(*, db_file)
The E-HowNet database.
See also:
ehn.db.core.EhnDb
- Parameters
db_file (str) – The path to the SQLite3 database file.
- text2nid_concept: dict¶
A dictionary that maps concept label to its node ID.
- text2nid_word: dict¶
A dictionary that maps word label to its node ID.
- text2nid_partial: dict¶
A dictionary that maps any subtext of concept label to its node ID.
For example, both “entity” and “事物” maps to the node ID of the concept “entity|事物”.
- get_nids(text, *, concept=True, word=True, full_match=False)¶
Query node IDs.
- Parameters
text (str) – the query text.
concept (boolean) – returns concept node.
word (boolean) – returns word node.
full_match (boolean) – returns only the nodes that fully match their label.
- Returns
A list of node IDs.
- class EhnDbNode
The E-HowNet database node.
See also:
ehn.db.data.EhnDbNode
- nid: int¶
The node ID.
- label: str¶
The node label.
- data: ~ehn.db.data.EhnDbNodeData¶
The node data.
Note that one may access data attribute directly (e.g. obj.defn of this object obj returns obj.data.defn).
- class EhnDbNodeData
The E-HowNet database node data.
See also:
ehn.db.data.EhnDbNodeData
- defn: str¶
The node definition.
- type: ~ehn.db.data.EhnDbNodeType¶
The node type.
- words: List[~ehn.db.data.EhnDbWordData]¶
The list of attached words.
- definite: bool¶
Whether this node is an instance of is parent node of not.
- class EhnDbWordData
The E-HowNet database word data.
See also:
ehn.db.data.EhnDbWordData
- word: str¶
The word.
- sense_no: str¶
The sense number ID.
- class EhnDbNodeType
The enum class of E-HowNet database node type.
See also:
ehn.db.data.EhnDbNodeType
- C = 'C'¶
The concept type.
- W = 'W'¶
The word type.
Parser¶
This section describes the E-HowNet parser.
Python API¶
from ehn.parse import EhnParser
text = '{MusicTool|樂器_x1:predication={own|有:possession={按鈕|PushingButton:whole={x1}}}}'
parser = EhnParser()
ress = parser(text, debug=False)
for res in ress:
res.tree().show()
Output:
[Entity $x1] MusicTool|樂器
└── [Feature] predication
└── [Entity] own|有
└── [Feature] possession
└── [Entity] 按鈕|PushingButton
└── [Feature] whole
└── $x1
CLI¶
One may also use the parser in command line directly.
# Usage
ehn-parser <text> [<text> ...]
# Example
ehn-parser \
"{MusicTool|樂器_x1:predication={own|有:possession={按鈕|PushingButton:whole={x1}}}}" \
"{InstitutePlace|場所:telic={or({experiment|實驗:location={~}},{research|研究:location={~}})}}" \
"{festival|節:TimePoint={x?},telic={congratulate|祝賀:content={year|年:qualification={new|新}}}}" \
"TimePoint={},manner={urgent|急}" \
"direction={toward()}"
Output:
#1
[Entity $x1] MusicTool|樂器
└── [Feature] predication
└── [Entity] own|有
└── [Feature] possession
└── [Entity] 按鈕|PushingButton
└── [Feature] whole
└── [Reference] $x1
#2
[Entity] InstitutePlace|場所
└── [Feature] telic
└── [FunctionEntity]
└── [Function] or
├── [Entity] experiment|實驗
│ └── [Feature] location
│ └── [TildeReference]
└── [Entity] research|研究
└── [Feature] location
└── [TildeReference]
#3
[Entity] festival|節
├── [Feature] TimePoint
│ └── [SubjectReference] $x?
└── [Feature] telic
└── [Entity] congratulate|祝賀
└── [Feature] content
└── [Entity] year|年
└── [Feature] qualification
└── [Entity] new|新
#4
[Subject $x?]
├── [Feature] TimePoint
│ └── [Any]
└── [Feature] manner
└── [Entity] urgent|急
#5
[Subject $x?]
└── [Feature] direction
└── [FunctionEntity]
└── [Function] toward
└── [Any]
Parse Nodes¶
There are five types of nodes in E-HowNet expression — Entity, Reference, Placeholder, Feature, Function, and Subject.


Node Prototype¶
- class EhnParseNode
The prototype of E-HowNet parsing nodes.
See also:
ehn.parse.node.base.EhnParseNode
- head: str¶
The head of this node.
- get_features(key=None)¶
Get the features (or
[]
if not exists).If key is set, returns only the features with feature.head==key.
- get_arguments()¶
Get the arguments (or
[]
if not exists).
- get_value()¶
Get the value (or
None
if not exists).
- get_function()¶
Get the function (or
None
if not exists).
- get_anchor()¶
Get the anchor (or
None
if not exists).
- get_coindex()¶
Get the coindex key (the head of the anchor, the head of reference nodes, or
None
otherwise).
- children()¶
Yields all direct child nodes of this node.
- descendant()¶
Yields all descendant nodes (including self) of this node.
- dumps()¶
Converts to text representation.
- tree() ehn.parse.node.base.EhnParseTree ¶
Generates a tree representation of this node and all its descendant nodes.
One may use
node.tree().show()
to show a pretty tree representation.
Entity-Like Nodes¶
Entity¶
- class EhnParseEntityBase
The base class of E-HowNet parsing entity nodes.
See also:
ehn.parse.node.base.EhnParseEntityBase
Subclasses:
EhnParseNormalEntity
A normal entity. Can be an anchor.EhnParseFunctionEntity
An entity with function head. Can be an anchor.EhnParseNameEntity
A name entity.EhnParseNumberEntity
A number entity.
Reference¶
- class EhnParseReferenceBase
The base class of E-HowNet parsing reference nodes.
See also:
ehn.parse.node.base.EhnParseReferenceBase
Subclasses:
EhnParseCoindexReference
An entity refers to an anchor entity.EhnParseSubjectReference
An entity refers to the unmentioned subject entity (EhnParseSubject
in feature-based expressions.)EhnParseTildeReference
An entity refers to the root entity.
Placeholder¶
- class EhnParsePlaceholderBase
The base class of E-HowNet parsing restriction nodes.
See also:
ehn.parse.node.base.EhnParsePlaceholderBase
Subclasses:
EhnParseRestrictionPlaceholder
. Can be an anchor.EhnParseAnyPlaceholder
A placeholder without restriction.
- property value¶
Must be an Entity (for
EhnParseRestrictionPlaceholder
) orNone
(forEhnParseAnyPlaceholder
).
Non-Entity-Like Nodes¶
Feature¶
- class EhnParseFeatureBase
The base class of E-HowNet parsing feature nodes.
See also:
ehn.parse.node.base.EhnParseFeatureBase
Subclasses:
EhnParseNormalFeature
A normal feature.EhnParseFunctionFeature
An feature with function head.
- property value¶
Can be a Entity-Like Node.
Function¶
- class EhnParseFunctionBase
The base class of E-HowNet parsing function nodes.
See also:
ehn.parse.node.base.EhnParseFunctionBase
Subclasses:
EhnParseFunction
.
- property arguments¶
A list of Entity-Like Nodes.
Subject¶
- class EhnParseSubjectBase
The base class of E-HowNet parsing unmentioned subject nodes. Works similar to entities but is not an entity. Used only in feature-based expressions.
See also:
ehn.parse.node.base.EhnParseSubjectBase
Subclasses:
EhnParseSubject
. Always an anchor ofx?
.
Partial Nodes¶
Function Head¶
- class EhnParseFunctionHead
The base class of nodes with a function as its head.
Note that the attribute obj.head of this object obj returns obj.function.head.
See also:
ehn.parse.node.base.EhnParseFunctionHead
Subclasses:
Anchor Body¶
- class EhnParseAnchorBody
The base class of anchor nodes.
See also:
ehn.parse.node.base.EhnParseAnchorBody
Subclasses:
Anchor¶
- class EhnParseAnchor
The coindex target.
See also:
ehn.parse.node.base.EhnParseAnchor
- head: str¶
The coindex of this anchor.
Graph Builder¶
This package provides are two type of graphs — the standard graph and the vis.js graph — for E-HowNet definitions.
Standard Graph¶
- class EhnStandardGraphBuilder
Generates graphs from E-HowNet definitions.
See also:
ehn.graph.standard.EhnStandardGraphBuilder
- __call__(root)¶
- Parameters
root (EhnParseNode) – The root parse node of a E-HowNet definition.
- Return type
- class EhnStandardGraph
See also:
ehn.graph.standard.EhnStandardGraph
- nodes: dict¶
A dictionary that maps the node ID (a random UUID or a coindex) to a list of parse nodes.
- edges: list¶
A list of triplets (subject node ID, predicate node ID, object node ID). The IDs are the keys in nodes.
- functions: list¶
A list of pairs (function node ID, argument node ID). The IDs are the keys in nodes. Note that different argument of a single function node will be listed separately.
- restrictions: list¶
A list of pairs (placeholder node ID, restriction node ID). The IDs are the keys in nodes.
- root_id: int¶
The ID of the root node in the definition.
Vis Graph¶
- class EhnVisGraphBuilder(definite_labels=None)
Generates graphs from E-HowNet definitions for vis.js.
See also:
ehn.graph.standard.EhnVisGraphBuilder
- Parameters
definite_labels (set) – a set of the labels of the definite concepts.
- __call__(root)¶
- Parameters
root (EhnParseNode) – The root parse node of a E-HowNet definition.
- Return type
EhnVisGraph
- class EhnVisGraph
Please refers vis.js’s documentation.
See also:
ehn.graph.standard.EhnVisGraph
- nodes: dict¶
The nodes.
- edges: list¶
The edges
ehn package¶
Subpackages
ehn.db package¶
Please refer the tutorial “Database”.
Submodules
ehn.db.core module¶
Please refer the tutorial “Database”.
ehn.db.data module¶
Please refer the tutorial “Database”.
- class ehn.db.data.EhnDbNodeType(value)[source]¶
Bases:
enum.Enum
E-HowNet Database Node Type.
- C = 'C'¶
concept.
- W = 'W'¶
word.
- class ehn.db.data.EhnDbWordData(word: str, sense_no: int)[source]¶
Bases:
object
E-HowNet Database Word Data.
- word: str¶
the word.
- sense_no: int¶
the sense number.
- class ehn.db.data.EhnDbNodeData(type: ehn.db.data.EhnDbNodeType, defn: Optional[str] = None, words: List[ehn.db.data.EhnDbWordData] = <factory>, definite: bool = False)[source]¶
Bases:
object
E-HowNet Database Node Data.
- type: ehn.db.data.EhnDbNodeType¶
the node type.
- defn: str = None¶
the definition.
- words: List[ehn.db.data.EhnDbWordData]¶
the attached words.
- definite: bool = False¶
whether is an instance of not.
- class ehn.db.data.EhnDbNode(tag=None, identifier=None, expanded=True, data=None)[source]¶
Bases:
treelib.node.Node
E-HowNet Database Node.
- data_class¶
alias of
ehn.db.data.EhnDbNodeData
ehn.graph package¶
Please refer the tutorial “Graph Builder”.
Submodules
ehn.graph.standard module¶
Please refer the tutorial “Graph Builder”.
- class ehn.graph.standard.EhnStandardGraph(nodes: dict, edges: list, functions: list, restrictions: list, root_id: int)[source]¶
Bases:
object
The standard E-HowNet graph.
ehn.graph.vis module¶
Please refer the tutorial “Graph Builder”.
- class ehn.graph.vis.EhnVisGraph(nodes: dict, edges: list)[source]¶
Bases:
object
The E-HowNet graph for vis.js.
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
- class ehn.parse.node.base.EhnParseEntityLike[source]¶
Bases:
ehn.parse.node.base.EhnParseNode
E-HowNet Parsing: Entity Like Node
- class ehn.parse.node.base.EhnParseEntityBase[source]¶
Bases:
ehn.parse.node.base.EhnParseEntityLike
E-HowNet Parsing: Base Entity Node
- class ehn.parse.node.base.EhnParseReferenceBase[source]¶
Bases:
ehn.parse.node.base.EhnParseEntityLike
E-HowNet Parsing: Base Reference Node
- class ehn.parse.node.base.EhnParsePlaceholderBase[source]¶
Bases:
ehn.parse.node.base.EhnParseEntityLike
E-HowNet Parsing: Base Placeholder Node
- class ehn.parse.node.base.EhnParseFeatureBase[source]¶
Bases:
ehn.parse.node.base.EhnParseNode
E-HowNet Parsing: Base Feature Node
- class ehn.parse.node.base.EhnParseFunctionBase[source]¶
Bases:
ehn.parse.node.base.EhnParseNode
E-HowNet Parsing: Base Function Node
- class ehn.parse.node.base.EhnParseSubjectBase[source]¶
Bases:
ehn.parse.node.base.EhnParseNode
E-HowNet Parsing: Base Subject Node
- class ehn.parse.node.base.EhnParseAnchor(head=None)[source]¶
Bases:
object
E-HowNet Parsing: Node Anchor
- class ehn.parse.node.base.EhnParseStrHead(head)[source]¶
Bases:
object
E-HowNet Parsing: Base Node with String Head
- class ehn.parse.node.base.EhnParseFunctionHead(function)[source]¶
Bases:
object
E-HowNet Parsing: Base Node with Function Head
- class ehn.parse.node.base.EhnParseValueBody(value)[source]¶
Bases:
object
E-HowNet Parsing: Base Node with Value
- class ehn.parse.node.base.EhnParseFeatureBody(*features)[source]¶
Bases:
collections.abc.Sequence
E-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, coindex=None, anchor=None)[source]¶
Bases:
ehn.parse.node.base.EhnParseEntityBase
,ehn.parse.node.base.EhnParseStrHead
,ehn.parse.node.base.EhnParseFeatureBody
,ehn.parse.node.base.EhnParseAnchorBody
E-HowNet Parsing: Normal Entity Node
- feature_type¶
- class ehn.parse.node.entity.EhnParseFunctionEntity(function, *features, coindex=None, anchor=None)[source]¶
Bases:
ehn.parse.node.base.EhnParseEntityBase
,ehn.parse.node.base.EhnParseFunctionHead
,ehn.parse.node.base.EhnParseFeatureBody
,ehn.parse.node.base.EhnParseAnchorBody
E-HowNet Parsing: Function Entity Node
- feature_type¶
- class ehn.parse.node.entity.EhnParseNameEntity(head)[source]¶
Bases:
ehn.parse.node.base.EhnParseEntityBase
,ehn.parse.node.base.EhnParseStrHead
E-HowNet Parsing: Name Entity Node
- class ehn.parse.node.entity.EhnParseNumberEntity(head)[source]¶
Bases:
ehn.parse.node.base.EhnParseEntityBase
,ehn.parse.node.base.EhnParseStrHead
E-HowNet Parsing: Number 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.EhnParseValueBody
E-HowNet Parsing: Normal Feature Node
- value_type¶
- class ehn.parse.node.feature.EhnParseFunctionFeature(function, value)[source]¶
Bases:
ehn.parse.node.base.EhnParseFeatureBase
,ehn.parse.node.base.EhnParseFunctionHead
,ehn.parse.node.base.EhnParseValueBody
E-HowNet Parsing: Function Feature Node
- value_type¶
ehn.parse.node.other module¶
Please refer the tutorial “Parse Nodes”.
- class ehn.parse.node.other.EhnParseSubject(*features)[source]¶
Bases:
ehn.parse.node.base.EhnParseSubjectBase
,ehn.parse.node.base.EhnParseFeatureBody
,ehn.parse.node.base.EhnParseAnchorBody
E-HowNet Parsing: Subject 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.EhnParseStrHead
E-HowNet Parsing: Function Node
- argument_type¶
ehn.parse.node.placeholder module¶
Please refer the tutorial “Parse Nodes”.
- class ehn.parse.node.placeholder.EhnParseRestrictionPlaceholder(value, *, coindex=None, anchor=None)[source]¶
Bases:
ehn.parse.node.base.EhnParsePlaceholderBase
,ehn.parse.node.base.EhnParseValueBody
,ehn.parse.node.base.EhnParseAnchorBody
E-HowNet Parsing: Restriction Placeholder Node
- value_type¶
- class ehn.parse.node.placeholder.EhnParseAnyPlaceholder[source]¶
Bases:
ehn.parse.node.base.EhnParsePlaceholderBase
E-HowNet Parsing: Any Placeholder Node
ehn.parse.node.reference module¶
Please refer the tutorial “Parse Nodes”.
- class ehn.parse.node.reference.EhnParseCoindexReference(head)[source]¶
Bases:
ehn.parse.node.base.EhnParseReferenceBase
,ehn.parse.node.base.EhnParseStrHead
E-HowNet Parsing: Coindex Reference Node
- class ehn.parse.node.reference.EhnParseSubjectReference[source]¶
Bases:
ehn.parse.node.base.EhnParseReferenceBase
E-HowNet Parsing: Subject Reference Node
- class ehn.parse.node.reference.EhnParseTildeReference[source]¶
Bases:
ehn.parse.node.base.EhnParseReferenceBase
E-HowNet Parsing: Tilde Reference Node
Deprecated since version 0.6.
Submodules
ehn.parse.parser module¶
Please refer the tutorial “Parser”.
- exception ehn.parse.parser.EhnSyntaxError(*args, pos=None)[source]¶
Bases:
SyntaxError
E-HowNet Syntax Error.