index
module components.common_modules.tree
Node
NodeInsertionError
TarsqiTree
Contains the TarsqiTree class.
class Node
Inherits from: object
This class is used to build a temporary hierarchical structure from instances
of docmodel.source_parser.Tag and to turn that structure into a TarsqiTree
instance with Sentence, NounChunk, VerbChunk, AdjectiveToken, Token,
TimexTag and EventTag elements in the hierarchy. Nodes can be considered
proto TarsqiTree elements or an intermediary between Tag and the classes
like Sentence and NounChunk. Nodes know how to insert a tag into themselves
(insert method) and how to add themselves to a TarsqiTree (add_to_tree
method).
Instance variables:
name - the name, taken from the Tag that the Node is created from
begin - the beginning offset from the Tag
end - the ending offset from the Tag
parent - the parent of the Node: None or another Node
position - the position in the parent's dtrs list
dtrs - a list of Nodes
event_dtr - None or the Node from dtrs that is an event
tag - the Tag that the Node is created from
tree - the TarsqiTree that the Node will be inserted into as an element
Public Functions
__init__(self, tag, parent, tree)
Initialize using a Tag object, a parent Node which can be None for the top
node, and the TarsqiTree that the node is for.
__str__(self)
add_to_tree(self, tree_element)
Add all daughters in self.dtrs as tree_elements to the initially
empty list in tree_element.dtrs. Add them as instances of Sentence,
NounChunk, VerbChunk, EventTag, TimexTag, Token or AdjectiveToken. The
tree_element argument can be one of those seven elements or it can be a
TarsqiTree.
as_tree_element(self)
Create from the node an instance of Sentence, NounChunk, VerbChunk,
EventTag, TimexTag, Token or AdjectiveToken. Copy information from the
Node as needed.
insert(self, tag)
Insert a Tag in the node. This could be insertion in one of the node's
daughters, or insertion in the node's daughters list. Log a warning if
the tag cannot be inserted.
pp(self, indent=0)
set_event_markers(self)
Set the self.event_dtrs variable if one of the dtrs is an event. Assumes that
at most one daughter is an event.
set_positions(self)
For each daughter, set its position variable to its index in the
self.dtrs list, the recurse for the daughter. These positions will later
be handed in to the TarsqiTree elements (Sentence, VerbChunk, EventTag,
Token etcetera).
Private Functions
_find_dtr_idx(self, tag)
Return the idex of the daughter node that can include the tag, return
None is there is no such node.
_find_gap_idx(self, tag)
Return the index in the daughters list where the tag can be inserted,
meaning that tag.begin is after the end of the previous element and that
tag.end is before the begin of the next element. Return None if there is
no point where the tag can be inserted.
_find_span_idx(self, tag)
Returns a list of indices of the dtrs that fit inside the tag. Returns an
empty list if there are no dtrs that fit. Also returns an empty list if
the begin or end of the tag is inside a dtr (this indicates a crossing
tag, the case where the tag is inside one dtr was already dealt
with).
_insert_tag_into_dtr(self, tag, idx)
Insert the tag into the dtr at self.dtrs[idx]. But take care of the
situation where the dtr and the tag have the same extent, in which case
we need to check the specified order and perhaps replace the dtr with
the tag and insert the dtr into the tag.
_replace_span_with_tag(self, tag, span)
Replace the span of dtrs with the tag and add the span as dtrs to the
tag.
class NodeInsertionError
Inherits from: Exception
An exception used to trap cases where you insert a node in the tree and there
is no place where it can go.
class TarsqiTree
Inherits from: object
Implements the shallow tree that is input to some of the Tarsqi components.
Instance variables
tarsqidoc - the TarsqiDocument instance that the tree is part of
docelement - the Tag with name=docelement that the tree was made for
parent - the parent of the tree, which is always None
dtrs - a list with daughters
events - a dictionary with events found by Evita
alinks - a list of AlinkTags, filled in by Slinket
slinks - a list of SlinkTags, filled in by Slinket
tlinks - a list of TlinkTags
orphans - a list of tags that could not be inserted
The events dictionary is used by Slinket and stores events from the tree
indexed on event eids.
Public Functions
__getitem__(self, index)
Indexing occurs on the dtrs variable.
__init__(self, tarsqidoc, docelement_tag)
Initialize all dictionaries, list and counters and set the file
name.
__len__(self)
Length is determined by the length of the dtrs list.
addEvent(self, event)
Takes an instance of evita.event.Event and adds it to the
TagRepository on the TarsqiDocument. Does not add it if there is already
an event at the same location.
addLink(self, linkAttrs, linkType)
Add a link of type linkType with its attributes to the tree by appending
them to self.alink_list, self.slink_list or self.tlink_list. This allows
other code, for example the main function of Slinket, to easily access
newly created links in the TarsqiTree. The linkType argument is'ALINK',
'SLINK' or 'TLINK' and linkAttrs is a dictionary of attributes.
get_nodes(self)
Returns a list of all nodes in the tree.
get_sentences(self)
Returns a list of all sentences in the tree.
hasEventWithAttribute(self, eid, att)
Returns the attribute value if the events dictionary has an event
with the given id that has a value for the given attribute, returns
False otherwise. Arguments:
eid - a string indicating the eid of the event
att - a string indicating the attribute
initialize_alinks(self, alinks)
initialize_slinks(self, slinks)
initialize_tlinks(self, tlinks)
pp(self)
Short form of pretty_print()
pretty_print(self)
Pretty printer that prints all instance variables and a neat representation
of the sentences.
pretty_print_links(self, links)
pretty_print_sentences(self)
pretty_print_tagged_events_dict(self)
storeEventValues(self, pairs)
Store attributes associated with an event (that is, they live on an event or
makeinstance tag) in the events dictionary. The pairs argument is a
dcitionary of attributes
module functions
create_tarsqi_tree(tarsqidoc, element, links=False)
Return an instance of TarsqiTree, using the tags in tarsqidoc included in
an element, which is an Tag instance with name=docelement. Include links that
fall within the boundaries of the elements if the optional links parameter
is set to True.