CKIP E-HowNet

CKIP E-HowNet Tools

Author

Requirements

Installation

pip install -U ehownet

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

License

CC BY-NC-SA 4.0

Copyright (c) 2018-2020 CKIP Lab under the CC BY-NC-SA 4.0 License.

E-HowNet Grammar

This section describes the grammar of the E-HowNet expression.

Text

  • 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 not mentioned subject)

Node

Entity

Feature

Function

  • EhnParseFunction

    • TEXT()

    • TEXT(RESTRICTION)

    • TEXT(ENTITY)

    • TEXT(ENTITY,ENTITY)

    • TEXT(ENTITY,ENTITY,...)

Restriction

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.

Inheritance diagram of ehn.parse.node.entity, ehn.parse.node.feature, ehn.parse.node.other

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 None if not exists)

get_function()

Get the function (or None if not exists)

get_anchor()

Get the anchor (or None if 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:

property features

A list of Features.

Feature

class EhnParseFeatureBase

The base class of E-HowNet parsing feature nodes.

Subclasses:

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

Restriction

class EhnParseRestrictionBase

The base class of E-HowNet parsing function nodes.

Subclasses:

property value

Must be an Entity.

Root

class EhnParseRootBase

The base class of E-HowNet parsing root nodes. Works similar to entities but is not an entity. Used only in feature-based expressions.

Subclasses:

property features

A list of Features.

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.

Subclasses:

property function

Must be a Function.

Anchor Body

class EhnParseAnchorBody

The base class of anchor nodes.

Subclasses:

property anchor

The Anchor.

Anchor

class EhnParseAnchor

The coindex target.

head: str

The coindex of this anchor.

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.EhnParseNode[source]

Bases: object

E-HowNet Parsing: Base Node

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 with save2file(), and json string with to_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_type parameter, 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 Node object. When this parameter is specified, the traversing will not continue to following children of node whose condition does not pass the filter.

  • key – the key param for sorting Node objects in the same level.

  • reverse – the reverse param for sorting Node objects 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.EhnParseNode

E-HowNet Parsing: Base Entity 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.EhnParseRestrictionBase[source]

Bases: ehn.parse.node.base.EhnParseNode

E-HowNet Parsing: Base Function Node

class ehn.parse.node.base.EhnParseRootBase[source]

Bases: ehn.parse.node.base.EhnParseNode

E-HowNet Parsing: Base Root 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: object

E-HowNet Parsing: Base Node with Feature

class ehn.parse.node.base.EhnParseArgumentBody(*arguments)[source]

Bases: object

E-HowNet Parsing: Base Node with Argument

class ehn.parse.node.base.EhnParseAnchorBody(anchor=None)[source]

Bases: object

E-HowNet Parsing: Base Node with Anchor

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.EhnParseAnchorBody

E-HowNet Parsing: Normal Entity Node

feature_type

alias of ehn.parse.node.base.EhnParseFeatureBase

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.EhnParseAnchorBody

E-HowNet Parsing: Function Entity Node

feature_type

alias of ehn.parse.node.base.EhnParseFeatureBase

class ehn.parse.node.entity.EhnParseAnyEntity[source]

Bases: ehn.parse.node.base.EhnParseEntityBase

E-HowNet Parsing: Any Entity Node

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

class ehn.parse.node.entity.EhnParseTildeEntity[source]

Bases: ehn.parse.node.base.EhnParseEntityBase

E-HowNet Parsing: Tilde Entity Node

class ehn.parse.node.entity.EhnParseCoindexEntity(head)[source]

Bases: ehn.parse.node.base.EhnParseEntityBase, ehn.parse.node.base.EhnParseStrHead

E-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.EhnParseValueBody

E-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.EhnParseValueBody

E-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.EhnParseFeatureBody

E-HowNet Parsing: Root Node

feature_type

alias of ehn.parse.node.base.EhnParseFeatureBase

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

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.EhnParseAnchorBody

E-HowNet Parsing: Restriction Node

value_type

alias of ehn.parse.node.base.EhnParseEntityBase

Submodules

ehn.parse.parser module

exception ehn.parse.parser.EhnSyntaxError(*args, pos=None)[source]

Bases: SyntaxError

E-HowNet Syntax Error.

show_pos(text)[source]

Show error position.

Parameters

text (str) – original input text

class ehn.parse.parser.EhnLexer(**kwargs)[source]

Bases: ehn.parse.parser._EhnLexer

E-HowNet Lexer.

__call__(data)

Run tokenization.

class ehn.parse.parser.EhnParser(lexer=None, **kwargs)[source]

Bases: ehn.parse.parser._EhnParser

E-HowNet Parser.

__call__(data)

Run parsing.

Index

Module Index