# Structures¶

elit.structure.to_gold(key: str) → str[source]
elit.structure.to_out(key: str) → str[source]
class elit.structure.Sentence(d=None, **kwargs)[source]

Bases: dict

__init__(d=None, **kwargs)[source]
Parameters: d (dict) – a dictionary, if not None, all of whose fields are added to this sentence. kwargs – additional fields to be added; if keys already exist; the values are overwritten with these.
tokens
Returns: the list of tokens in the sentence. list of str
part_of_speech_tags
Returns: the list of part-of-speech tags corresponding to the tokens in this sentence. list of str
class elit.structure.Document(d=None, **kwargs)[source]

Bases: dict

__init__(d=None, **kwargs)[source]
Parameters: d (dict) – a dictionary, if not None, all of whose fields are added to this document. kwargs – additional fields to be added; if keys already exist; the values are overwritten with these.
sentences
Returns: the list of sentences in the document. list of Sentence
tokens
Returns: list of tokens across all sentences. list of str
add_sentence(sentence: elit.structure.Sentence)[source]
add_sentences(sentences: Sequence[elit.structure.Sentence])[source]
class elit.structure.BILOU[source]

Bases: object

B = 'B'
I = 'I'
L = 'L'
O = 'O'
U = 'U'
classmethod to_chunks(tags: List[str], fix: bool = False) → List[Tuple[int, int, str]][source]
Parameters: tags – a list of tags encoded by BILOU. fix – if True, fixes potential mismatches in BILOU (see heuristic_fix()). a list of tuples where each tuple contains (begin index (inclusive), end index (exclusive), label) of the chunk.
classmethod heuristic_fix(tags)[source]

Use heuristics to fix potential mismatches in BILOU. :param tags: a list of tags encoded by BLIOU.

class elit.structure.Node[source]

Bases: object

Node is used for constructing the NLP structure.

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

parent
Returns: parent of current node Node
left_sibling
Returns: left libling of current node Node
right_sibling
Returns: right libling of current node Node
children

the list of children

Returns: the list of children list
index_of(child)[source]

the index of the child

Parameters: child (Node) – a child node the index of the child node. return None if the given node is not a child int or None
child(index)[source]
Parameters: index (int) – index of a child node the index’th child of this node if exists; otherwise, None Node or None
first_child()[source]
Returns: the first child of this node if exists; otherwise, None Node or None
last_child()[source]
Returns: the last child of this node if exists; otherwise, None Node or None
find_child_by(matcher=<function Node.<lambda>>, index=0)[source]
Parameters: matcher (lambda) – the condition index (int) – displacement (0: 1st, 1: 2nd, etc.) the order’th child matching the specific condition of this node if exists; otherwise, None Node or None
add_child(node, index=None)[source]

Adds a node as the index’th child of this node if it is not already a child of this node.

Parameters: node (Node) – node is added to children index (int) – add child to this position if the node is added successfully, retun True; otherwise, False bool
set_child(node, index)[source]

Sets a node as the index’th child of this node if it is not already a child of this node.

Parameters: node (Node) – node is set at the index position index (int) – set a child to this position the previously index’th node if added; otherwise, None Node or None
remove_child(node)[source]

Removes a child from this node.

Parameters: node (Node) – remove node True if the node exist; otherwise, False bool
replace_child_from(old_node, new_node)[source]

Replaces the child from old_node to new_node

Parameters: old_node (Node) – old child new_node (Node) – new child True if the child is replaced successfully; otherwise, False bool
remove()[source]

Removes this node from its parent. If this is the only child, removeToken its parent from its grandparent, and applies this logic recursively to the ancestors.

has_child()[source]

Whether current node has any child or not

Returns: True if this node has any child; otherwise, False bool
is_child_of(node)[source]

Whether current node is a child of the node or not

Parameters: node – Node node – Node True if this node is a child of the specific node; otherwise, False bool
contains_child(matcher=<function Node.<lambda>>)[source]

Whether current node contains a child with the matcher or not

Parameters: matcher (lambda) – The matcher of condition True if this node contains a child of the matcher; otherwise, False bool
num_of_children()[source]
Returns: numbers of children int
find_children_in(first_child_index=0, last_child_index=None)[source]

The sublist begins at the specific position and extends to the end.

Parameters: first_child_index (int) – the ID of the first child (inclusive) last_child_index (int) – the ID of the last child (exclusive) an immutable list of sub-children list
find_children_by(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition the list of children matching the specific condition. list
grand_children()[source]
Returns: the list of grand-children list
find_first_descendant_by(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition the first descendant matching the specific condition Node or None
find_first_lowest_chained_descendant_by(matcher)[source]
Parameters: matcher (lambda) – the condition the first lowest descendant whose intermediate ancestors to this node all match the specific condition Node or None
descendants()[source]
Returns: the list of all descendents (excluding this node). list
find_descendants_in(depth)[source]
Parameters: depth (int) – the level of the descendents to be retrieved (1: children, 2: childreNLPNode + grand-children, etc.). the list of descendents (excluding this node). list
find_single_chained_by(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition a node in this node’s subtree (including self) matching the specific condition if it is single-chained. Node or None
adapt_descendants_from(node)[source]
Parameters: node (Node) –
is_descendants_of(node)[source]
Parameters: node (Node) – the node True if the node is a descendant of the specific node bool
grand_parent()[source]
Returns: the grandparent of this node if exists; otherwise, None Node or None
ancestor(height)[source]
Parameters: height (int) – height of the ancestor from this node (1: parent, 2: grandparent, etc.). the lowest ancestor matching the specific condition Node
find_lowest_ancestor_by(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition the lowest ancestor matching the specific condition Node
find_highest_ancestor_by(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition the highest ancestor where all the intermediate ancestors match the specific condition Node
lowest_common_ancestor(node)[source]
Parameters: node (Node) – the node the lowest common ancestor of this node and the specified node Node or None
ancestor_set()[source]
Returns: the setToken of all ancestors of this node set
is_parent_of(node)[source]
Parameters: node (Node) – the node True if this node is the parent of the specific node; otherwise, False bool
is_ancestor_of(node)[source]
Parameters: node (Node) – the node True if the node is a descendant of the specific node; otherwise, False bool
has_parent(matcher=<function Node.<lambda>>)[source]
Parameters: matcher – the condition True if this node has a parent; otherwise, False bool
has_grand_parent()[source]
Returns: True if this node has a grand parent; otherwise, False bool
siblings()[source]
Returns: the list of all siblings list
left_nearest_sibling(order=0)[source]
Parameters: order (int) – displacement (0: left-nearest, 1: 2nd left-nearest, etc.) the order’th left-nearest sibling of this node if exists; otherwise, None Node or None
find_left_nearest_sibling_by(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition the left nearest sibling that matches the specific condition Node
right_nearest_sibling(order=0)[source]
Parameters: order (int) – displacement (0: left-nearest, 1: 2nd left-nearest, etc.) the order’th right-nearest sibling of this node if exists; otherwise, None Node or None
find_right_nearest_sibling_by(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition the right nearest sibling that matches the specific condition Node
has_left_sibling(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition Trur if this node has a left sibling; otherwise, False.
has_right_sibling(matcher=<function Node.<lambda>>)[source]
Parameters: matcher (lambda) – the condition Trur if this node has a right sibling; otherwise, False.
is_sibling_of(node)[source]
Parameters: node (Node) – the node True if this node is a sibling of the specific node. bool
is_left_sibling_of(node)[source]
Parameters: node (Node) – True if this node is a left sibling of the specific node bool
is_right_sibling_of(node)[source]
Parameters: node (Node) – True if this node is a right sibling of the specific node bool
find_node_in(order, getter)[source]
Parameters: order (int) – 0: self, 1: nearest, 2: second nearest, etc. getter (str) – takes a node and returns a node. the order’th node with respect to the getter. Node
find_nearest_node_by(getter, matcher=<function Node.<lambda>>)[source]
Parameters: getter (str) – takes a node and returns a node. matcher (lambda) – takes a node and the supplement, and returns true if its field matches to the specific predicate. the first node matching the specific condition Node or None
isolate()[source]

Isolates this node from its parent, children, and siblings

second_order(getter)[source]
Parameters: getter (str) – takes a node and returns a list of nodes. the list of second order elements according to the getter. list