alex.components.nlg.tectotpl.core package

Submodules

alex.components.nlg.tectotpl.core.block module

class alex.components.nlg.tectotpl.core.block.Block(scenario, args)[source]

Bases: object

A common ancestor to all Treex processing blocks.

load()[source]

Load required files / models, to be overridden by child blocks.

process_bundle(bundle)[source]

Process a bundle. Default behavior is to process the zone according to the current language and selector.

process_document(doc)[source]

Process a document. Default behavior is to look for methods that process a bundle/zone/tree/node. If none is found, raise a NotImplementedError.

process_zone(zone)[source]

Process a zone. Default behavior is to try if there is a process_Xtree or process_Xnode method and run this method, otherwise raise an error.

alex.components.nlg.tectotpl.core.document module

class alex.components.nlg.tectotpl.core.document.Bundle(document, data=None, b_ord=None)[source]

Bases: object

Represents a bundle, i.e. a list of zones pertaining to the same sentence (in different variations).

create_zone(language, selector)[source]

Creates a zone at the given language and selector. Will overwrite any existing zones.

document

The document this bundle belongs to.

get_all_zones()[source]

Return all zones contained in this bundle.

get_or_create_zone(language, selector)[source]

Returns the zone for a language and selector; if it does not exist, creates an empty zone.

get_zone(language, selector)[source]

Returns the corresponding zone for a language and selector; raises an exception if the zone does not exist.

has_zone(language, selector)[source]

Returns True if the bundle has a zone for the given language and selector.

ord

The order of this bundle in the document, as given by constructor

class alex.components.nlg.tectotpl.core.document.Document(filename=None, data=None)[source]

Bases: object

This represents a Treex document, i.e. a sequence of bundles. It contains an index of node IDs.

create_bundle(data=None)[source]

Append a new bundle and return it.

get_node_by_id(node_id)[source]
index_backref(attr_name, source_id, target_ids)[source]

Keep track of a backward reference (source, target node IDs are in the direction of the original reference)

index_node(node)[source]

Index a node by its id. Also index the node’s references in the backwards reference index.

remove_backref(attr_name, source_id, target_ids)[source]

Remove references from the backwards index.

remove_node(node_id)[source]

Remove a node from all indexes.

class alex.components.nlg.tectotpl.core.document.Zone(data=None, language=None, selector=None, bundle=None)[source]

Bases: object

Represents a zone, i.e. a sentence and corresponding trees.

atree

Direct access to a-tree (will raise an exception if the tree does not exist).

bundle

The bundle in which this zone is located

create_atree()[source]

Create a tree on the a-layer

create_ntree()[source]

Create a tree on the n-layer

create_ptree()[source]

Create a tree on the p-layer

create_tree(layer, data=None)[source]

Create a tree on the given layer, filling it with the given data (if applicable).

create_ttree()[source]

Create a tree on the t-layer

document

The document in which this zone is located

get_tree(layer)[source]

Return a tree this node has on the given layer or raise an exception if the tree does not exist.

has_atree()[source]

Return true if this zone has an a-tree.

has_ntree()[source]

Return true if this zone has an n-tree.

has_ptree()[source]

Return true if this zone has a p-tree.

has_tree(layer)[source]

Return True if this zone has a tree on the given layer, False otherwise.

has_ttree()[source]

Return true if this zone has a t-tree.

language_and_selector

Return string concatenation of the zone’s language and selector.

ntree

Direct access to n-tree (will raise an exception if the tree does not exist).

ptree

Direct access to p-tree (will raise an exception if the tree does not exist).

ttree

Direct access to t-tree (will raise an exception if the tree does not exist).

alex.components.nlg.tectotpl.core.exception module

exception alex.components.nlg.tectotpl.core.exception.DataException(path)[source]

Bases: alex.components.nlg.tectotpl.core.exception.TreexException

Data file not found exception

exception alex.components.nlg.tectotpl.core.exception.LoadingException(text)[source]

Bases: alex.components.nlg.tectotpl.core.exception.TreexException

Block loading exception

exception alex.components.nlg.tectotpl.core.exception.RuntimeException(text)[source]

Bases: alex.components.nlg.tectotpl.core.exception.TreexException

Block runtime exception

exception alex.components.nlg.tectotpl.core.exception.ScenarioException(text)[source]

Bases: alex.components.nlg.tectotpl.core.exception.TreexException

Scenario-related exception.

exception alex.components.nlg.tectotpl.core.exception.TreexException(message)[source]

Bases: exceptions.Exception

Common ancestor for Treex exception

alex.components.nlg.tectotpl.core.log module

alex.components.nlg.tectotpl.core.log.log_info(message)[source]

Print an information message

alex.components.nlg.tectotpl.core.log.log_warn(message)[source]

Print a warning message

alex.components.nlg.tectotpl.core.node module

class alex.components.nlg.tectotpl.core.node.A(data=None, parent=None, zone=None)[source]

Bases: alex.components.nlg.tectotpl.core.node.Node, alex.components.nlg.tectotpl.core.node.Ordered, alex.components.nlg.tectotpl.core.node.EffectiveRelations, alex.components.nlg.tectotpl.core.node.InClause

Representing an a-node

attrib = [(u'form', <type 'unicode'>), (u'lemma', <type 'unicode'>), (u'tag', <type 'unicode'>), (u'afun', <type 'unicode'>), (u'no_space_after', <type 'bool'>), (u'morphcat', <type 'dict'>), (u'is_parenthesis_root', <type 'bool'>), (u'edge_to_collapse', <type 'bool'>), (u'is_auxiliary', <type 'bool'>), (u'p_terminal.rf', <type 'unicode'>)]
is_coap_root()[source]
morphcat_case
morphcat_gender
morphcat_grade
morphcat_members = [u'pos', u'subpos', u'gender', u'number', u'case', u'person', u'tense', u'negation', u'voice', u'grade', u'mood', u'possnumber', u'possgender']
morphcat_mood
morphcat_negation
morphcat_number
morphcat_person
morphcat_pos
morphcat_possgender
morphcat_possnumber
morphcat_subpos
morphcat_tense
morphcat_voice
ref_attrib = [u'p_terminal.rf']
reset_morphcat()[source]

Reset the morphcat structure members to ‘.’

class alex.components.nlg.tectotpl.core.node.EffectiveRelations[source]

Bases: object

Representing a node with effective relations

attrib = [(u'is_member', <type 'bool'>)]
get_coap_members()[source]

Return the members of the coordination, if the node is a coap root. Otherwise return the node itself.

get_echildren(or_topological=False, add_self=False, ordered=False, preceding_only=False, following_only=False)[source]

Return the effective children of the current node.

get_eparents(or_topological=False, add_self=False, ordered=False, preceding_only=False, following_only=False)[source]

Return the effective parents of the current node.

is_coap_root()[source]

Testing whether the node is a coordination/apposition root. Must be implemented in descendants.

ref_attrib = []
class alex.components.nlg.tectotpl.core.node.InClause[source]

Bases: object

Represents nodes that are organized in clauses

attrib = [(u'clause_number', <type 'int'>), (u'is_clause_head', <type 'bool'>)]
get_clause_root()[source]

Return the root of the clause the current node resides in.

ref_attrib = []
class alex.components.nlg.tectotpl.core.node.N(data=None, parent=None, zone=None)[source]

Bases: alex.components.nlg.tectotpl.core.node.Node

Representing an n-node

attrib = [(u'ne_type', <type 'unicode'>), (u'normalized_name', <type 'unicode'>), (u'a.rf', <type 'list'>)]
ref_attrib = [u'a.rf']
class alex.components.nlg.tectotpl.core.node.Node(data=None, parent=None, zone=None)[source]

Bases: object

Representing a node in a tree (recursively)

attrib = [(u'alignment', <type 'list'>), (u'wild', <type 'dict'>)]
create_child(id=None, data=None)[source]

Create a child of the current node

document

The document this node is a member of.

get_attr(name)[source]

Return the value of the given attribute. Allows for dictionary nesting, e.g. ‘morphcat/gender’

get_attr_list(include_types=False, safe=False)[source]

Get attributes of the current class (gathering all attributes of base classes)

get_children(add_self=False, ordered=False, preceding_only=False, following_only=False)[source]

Return all children of the node

get_depth()[source]

Return the depth, i.e. the distance to the root.

get_deref_attr(name)[source]

This assumes the given attribute holds node id(s) and returns the corresponding node(s)

get_descendants(add_self=False, ordered=False, preceding_only=False, following_only=False)[source]

Return all topological descendants of this node.

get_ref_attr_list(split_nested=False)[source]

Return a list of the attributes of the current class that contain references (splitting nested ones, if needed)

get_referenced_ids()[source]

Return all ids referenced by this node, keyed under their reference types in a hash.

id

The unique id of the node within the document.

is_root

Return true if this node is a root

parent

The parent of the current node. None for roots.

ref_attrib = []
remove()[source]

Remove the node from the tree.

remove_reference(ref_type, refd_id)[source]

Remove the reference of the given type to the given node.

root

The root of the tree this node is in.

set_attr(name, value)[source]

Set the value of the given attribute. Allows for dictionary nesting, e.g. ‘morphcat/gender’

set_deref_attr(name, value)[source]

This assumes the value is a node/list of nodes and sets its id/their ids as the value of the given attribute.

zone

The zone this node belongs to.

class alex.components.nlg.tectotpl.core.node.Ordered[source]

Bases: object

Representing an ordered node (has an attribute called ord), defines sorting.

attrib = [(u'ord', <type 'int'>)]
get_next_node()[source]

Get the following node in the ordering.

get_prev_node()[source]

Get the preceding node in the ordering.

is_first_node()[source]

Return True if this node is the first node in the tree, i.e. has no previous nodes.

is_last_node()[source]

Return True if this node is the last node in the tree, i.e. has no following nodes.

is_right_child

Return True if this node has a greater ord than its parent. Returns None for a root.

ref_attrib = []
shift_after_node(other, without_children=False)[source]

Shift one node after another in the ordering.

shift_after_subtree(other, without_children=False)[source]

Shift one node after the whole subtree of another node in the ordering.

shift_before_node(other, without_children=False)[source]

Shift one node before another in the ordering.

shift_before_subtree(other, without_children=False)[source]

Shift one node before the whole subtree of another node in the ordering.

class alex.components.nlg.tectotpl.core.node.P(data=None, parent=None, zone=None)[source]

Bases: alex.components.nlg.tectotpl.core.node.Node

Representing a p-node

attrib = [(u'is_head', <type 'bool'>), (u'index', <type 'unicode'>), (u'coindex', <type 'unicode'>), (u'edgelabel', <type 'unicode'>), (u'form', <type 'unicode'>), (u'lemma', <type 'unicode'>), (u'tag', <type 'unicode'>), (u'phrase', <type 'unicode'>), (u'functions', <type 'unicode'>)]
ref_attrib = []
class alex.components.nlg.tectotpl.core.node.T(data=None, parent=None, zone=None)[source]

Bases: alex.components.nlg.tectotpl.core.node.Node, alex.components.nlg.tectotpl.core.node.Ordered, alex.components.nlg.tectotpl.core.node.EffectiveRelations, alex.components.nlg.tectotpl.core.node.InClause

Representing a t-node

add_aux_anodes(new_anodes)[source]

Add an auxiliary a-node/a-nodes to the list.

anodes

Return all anodes of a t-node

attrib = [(u'functor', <type 'unicode'>), (u'formeme', <type 'unicode'>), (u't_lemma', <type 'unicode'>), (u'nodetype', <type 'unicode'>), (u'subfunctor', <type 'unicode'>), (u'tfa', <type 'unicode'>), (u'is_dsp_root', <type 'bool'>), (u'gram', <type 'dict'>), (u'a', <type 'dict'>), (u'compl.rf', <type 'list'>), (u'coref_gram.rf', <type 'list'>), (u'coref_text.rf', <type 'list'>), (u'sentmod', <type 'unicode'>), (u'is_parenthesis', <type 'bool'>), (u'is_passive', <type 'bool'>), (u'is_generated', <type 'bool'>), (u'is_relclause_head', <type 'bool'>), (u'is_name_of_person', <type 'bool'>), (u'voice', <type 'unicode'>), (u'mlayer_pos', <type 'unicode'>), (u't_lemma_origin', <type 'unicode'>), (u'formeme_origin', <type 'unicode'>), (u'is_infin', <type 'bool'>), (u'is_reflexive', <type 'bool'>)]
aux_anodes
compl_nodes
coref_gram_nodes
coref_text_nodes
gram_aspect
gram_degcmp
gram_deontmod
gram_diathesis
gram_dispmod
gram_gender
gram_indeftype
gram_iterativeness
gram_negation
gram_number
gram_numertype
gram_person
gram_politeness
gram_resultative
gram_sempos
gram_tense
gram_verbmod
is_coap_root()[source]
lex_anode
ref_attrib = [u'a/lex.rf', u'a/aux.rf', u'compl.rf', u'coref_gram.rf', u'coref_text.rf']
remove_aux_anodes(to_remove)[source]

Remove an auxiliary a-node from the list

alex.components.nlg.tectotpl.core.run module

class alex.components.nlg.tectotpl.core.run.Scenario(config)[source]

Bases: object

This represents a scenario, i.e. a sequence of blocks to be run on the data

apply_to(string, language=None, selector=None)[source]

Apply the whole scenario to a string (which should be readable by the first block of the scenario), return the sentence(s) of the given target language and selector.

load_blocks()[source]

Load all blocks into memory, finding and creating class objects.

alex.components.nlg.tectotpl.core.util module

alex.components.nlg.tectotpl.core.util.as_list(value)[source]

Cast anything to a list (just copy a list or a tuple, or put an atomic item to as a single element to a list).

alex.components.nlg.tectotpl.core.util.file_stream(filename, mode=u'r', encoding=u'UTF-8')[source]

Given a file stream or a file name, return the corresponding stream, handling GZip. Depending on mode, open an input or output stream.

alex.components.nlg.tectotpl.core.util.first(condition_function, sequence, default=None)[source]

Return first item in sequence where condition_function(item) == True, or None if no such item exists.

Module contents