pysumo package

This section describes the backing pysumorary of pySUMO. It contains listings and descriptions of all the modules and classes in the pySUMO core. The pysumo conforms to PEP 008.

!include ./UML/pysumo/package_pysumo.iuml

Module contents

The pySUMO library package. This package contains the core functionality of pySUMO. It not only provides the GUI with a feature-rich API, but also validates input and keeps the Ontologies in a consistent state.

Submodules

pysumo.indexabstractor module

!include ./UML/pysumo/IndexAbstractor.iuml


This module abstracts all operations on the Ontology index. It is used for every read operation on the Ontology. It eases access to the Ontology and protects the data structures from direct user access.

This module contains:

  • IndexAbstractor: The interface to the Ontology index.
  • AbstractGraph: An object containing selected graph nodes and relations.
  • AbstractGraphNode: A node in a AbstractGraph
  • DotGraph: A Graphical representation of an AbstractGraph with DOT
class pysumo.indexabstractor.AbstractGraph(variant, major, minor, root, depth, info)[source]

Bases: builtins.object

An abstract representation of a subset of an Ontology as a collection of nodes and relations.

Variables:

  • nodes: The list of graph nodes.
  • relations: An adjacency matrix of all the paths in the graph.
class pysumo.indexabstractor.AbstractGraphNode(name)[source]

Bases: builtins.object

A node in an AbstractGraph. Contains information necessary to recreate an AbstractSyntaxTree from an AbstractGraph.

Varibles:

  • name: The name of the AbstractGraphNode.
class pysumo.indexabstractor.IndexAbstractor[source]

Bases: builtins.object

The IndexAbstractor provides a high-level index of the AbstractSyntaxTree. Each Ontology is represented by a hashmap, the list of these hashmaps is the index. Average access time is in O(n) where n is the number of loaded Ontologies.

Variables:

  • root: The root AbstractSyntaxTree node.
  • ontologies: The list of currently active Ontologies.
  • index: The index of all terms in the currently active Ontologies.
  • wordnet: A reference to the Object containing the SUMO-WordNet mapping.

Methods:

  • init_wordnet: Initializes the WordNet mapping.
  • update_index: Updates the index.
  • search: Searches for a term in the Ontology.
  • get_ontology_file: Return an in-memory file object for an Ontology.
  • get_completions: Return a list of possible completions for the current index.
  • get_graph: Creates an abstract graph containing a view of the Ontology.
  • wordnet_locate: Returns information about a term from WordNet.
get_completions()[source]

Returns a list of possible completions for the currently loaded ontologies.

get_graph(variant, major=2, minor=1, root=None, depth=None)[source]

Returns a hierarchical view of the Ontology.

Arguments:

  • variant: The list of terms against which the resulting AbstractGraph matches.
  • major: The position of the parent element.
  • minor: The position of the child element.
  • root: The root node to which all other nodes are related.
  • depth: The recursion depth.

Returns:

  • AbstractGraph

Raises:

  • KeyError
get_ontology_file(ontology)[source]

Returns an in-memory file object for the Kif representation of ontology.

init_wordnet()[source]

Initializes the SUMO mapping to WordNet.

search(term)[source]

Search for term in the in-memory Ontology. Returns all objects that match the search.

Returns:

  • {Ontology : (String, int)[]}
update_index(ast)[source]

Updates the index with all new AST nodes in ast.

wordnet_locate(term)[source]

Use the mapping from SUMO to WordNet to retrieve information about a term.

Arguments:

  • term: the term to locate

Returns:

  • String[]

Raises:

  • KeyError
pysumo.indexabstractor.normalize(term)[source]

Normalizes term to aid in searching.

pysumo.wordnet module

!include ./UML/pysumo/WordNet.iuml


The interface to WordNet.

This module contains:

  • WordNet: An interface to the WordNet online English lexical database.
class pysumo.wordnet.WordNet[source]

Bases: builtins.object

An interface to locate and search for items in WordNet. Searches WordNet for information about terms in the Ontology, to find colloquial variations of term names and to find terms that are synonyms of a word.

Methods:

  • locate_term: Locates a term in WordNet.
  • find_synonym: Finds possibly synonyms for a word.
find_synonym(word)[source]

Searches WordNet for possible synonyms for word.

Returns:

  • String[]
locate_term(term)[source]

Use the mapping from SUMO to WordNet to retrieve information about a term.

Kwargs:

  • term: the term to locate

Returns:

  • (name : String, type : SSType, gloss : String)[]

pysumo.parser module

!include ./UML/pysumo/Parser.iuml


The pySUMO parsing module. It contains functions to parse and serialize kif-files. It also handles the Ontology’s data structure and parses the mapping of SUMO terms to WordNet.

This module contains:

  • AbstractSyntaxTree: The in-memory representation of an Ontology.
  • Ontology: Contains basic information about an Ontology.
class pysumo.parser.AbstractSyntaxTree(ontology, parent=None, line=-1)[source]

Bases: builtins.object

The AbstractSyntaxTree is a node in the abstract syntax tree. The abstract syntax tree is defined by a root node and its children. The AbstractSyntaxTree is the in-memory representation of the loaded Ontologies for internal purposes only and should never be passed outside of the lib.

Variables:

  • parent: The parent node
  • children: A list of child nodes.
  • name: The name of the AbstractSyntaxTree object.
  • element_type: The type of the node element.
  • ontology: The Ontology object to which this node corresponds.
  • is_indexed: Whether or not this node is indexed.

Methods:

  • add_child: Adds a child node.
  • remove_child: Removes a child node.
add_child(entry)[source]

Adds entry as a child to self.

parse(tokens)[source]
remove_child(entry)[source]

Removes entry from the node’s children.

exception pysumo.parser.ParseError(line, linenumber)[source]

Bases: builtins.Exception

class pysumo.parser.Pos[source]

Bases: enum.Enum

class pysumo.parser.SSType[source]

Bases: enum.Enum

class pysumo.parser.SUMOConceptWordNetItem(sumo_concept, suffix, synset_offset, lex_filenum, ss_type, synset, ptr_list, frames, gloss)[source]

Bases: builtins.object

The object returned from _wtokenize containing info on the SUMO-WordNet mapping.

pysumo.parser.astmerge(trees)[source]

Merge two Abstract Syntax Trees

Args:

  • trees: a tuple of 2 AST objects

Returns:

  • AbstractSyntaxTree
pysumo.parser.kifparse(infile, ontology, ast=None)[source]

Parse an ontology and return an AbstractSyntaxTree.

Args:

  • ontology: the ontology to parse
  • graph: a modified graph of this ontology
  • ast: the AST of ontologies which are needed from this ontology
  • infile: the file object to parse

Returns:

  • AbstractSyntaxTree
pysumo.parser.kifserialize(ast, ontology, out)[source]

Writes ontology to disk as kif. Parses ast and writes out all nodes that belong to ontology.

Args:

  • ast: the Abstract Syntax Tree
  • ontology: The specific ontology with is written to disk
  • f: The file object witch written in

Raises:

  • OSError
pysumo.parser.wparse(datafiles)[source]

Parses the file containing the SUMO-WordNet mapping.

Args:

  • path: The path to the SUMO-WordNet mapping files

Returns:

  • Dictionary

pysumo.syntaxcontroller module

!include ./UML/pysumo/SyntaxController.iuml


Handles all write accesses to the Ontologies and kif files. The SyntaxController’s main purpose is to act as an intermediary between the user and the Ontology.

This module contains:

  • SyntaxController: The interface to the parser/serializer.
class pysumo.syntaxcontroller.Ontology(path, name=None, url=None, lpath=None)[source]

Bases: builtins.object

Contains basic information about a KIF file. This class is used to maintain separation between different Ontology-files so the user can choose which are active and where each Ontology should be saved.

Variables:

  • name: The name of the Ontology.
  • path: The location of the Ontology in the filesystem.
  • url: The URL from which the Ontology can be updated.
  • active: Whether or not the Ontology is currently loaded.
save()[source]

Saves all pending changes in self to self.path.

class pysumo.syntaxcontroller.SyntaxController(index)[source]

Bases: builtins.object

The high-level class containing the interface to all parsing/serialization operations. All operations that can modify the Ontology or kif-file are passed through the SyntaxController. The SyntaxController acts as a moderator between user-side widgets and the low-level API ensuring that all requests are correct, that higher objects never gain direct access to internal objects and that all changes to the Ontology are atomic.

Methods:

  • parse_partial: Checks a code block for syntax errors.
  • parse_patch: Checks a code for correctness and adds it to the Ontology.
  • add_ontology: Adds an Ontology to the in-memory Ontology.
  • remove_ontology: Removes an Ontology from the in-memory Ontology.
  • undo: Undoes the most recent change to the ontology.
  • redo: Redoes the most recent change to the ontology.
add_ontology(ontology, newversion=None)[source]

Adds ontology to the current in-memory Ontology.

Arguments:

  • ontology: the ontology that will be added
  • newversion: a string witch represent the new verison of the ontology

Raises:

  • ParseError
parse_partial(code_block, ontology=None)[source]

Tells self.parser to check code_block for syntactical correctness.

Arguments:

  • code_block: the partial code block that will be checked

Raises:

  • ParseError
parse_patch(ontology, patch)[source]

Apply a patch to the last version of the ontology and parse this new version

Arguments:

  • ontology: the ontlogy which is patched
  • patch: the patch to add to the ontology

Raises:

  • ParseError
redo(ontology)[source]

Redoes the last action in ontology

remove_ontology(ontology)[source]

Removes ontology from the current in-memory Ontology.

Arguments:

  • ontology: the ontology that will be removed

Raises:

  • NoSuchOntologyError
undo(ontology)[source]

Undoes the last action in ontology

pysumo.syntaxcontroller.get_ontologies(lpath=None)[source]

Returns a set of all ontologies provided by pysumo as well as local ontologies.

pysumo.updater module

!include ./UML/pysumo/Updater.iuml


Module which handles updating of Ontologies. It use PycURL to check if any updates are avalible. The URLs for the ontlogies are safe in the ontology so the user can easy add an URL for a new ontlogy.

pysumo.updater.check_for_updates(ontology, function=<function <lambda> at 0x7effb7414f28>)[source]

Check if there are updates for ontology. If an update is available, executes function. Function receives the new ontology file as the first positional argument.

Returns:

  • Boolean

Raises:

  • HTTPError
pysumo.updater.update(ontology, function=<function <lambda> at 0x7effb71db378>)[source]

Checks for updates to ontology and if available downloads them. If the update succeeds, executes function after the download completes. Function receives the path to the downloaded ontology as the first positional argument, and ontology as the second.

Raises:

  • HTTPError