graphANNIS Python API (graphannis package)¶
graphannis.cs module¶
-
class
graphannis.cs.
CorpusStorageManager
(db_dir='data/', use_parallel=True)¶ Bases:
object
-
apply_update
(corpus_name: str, update)¶ Atomically apply update (add/delete nodes, edges and labels) to the database
>>> from graphannis.cs import CorpusStorageManager >>> from graphannis.graph import GraphUpdate >>> with CorpusStorageManager() as cs: ... with GraphUpdate() as g: ... g.add_node('n1') ... cs.apply_update('test', g)
-
count
(corpora, query, query_language=<QueryLanguage.AQL: 0>)¶ Count the number of results for a query.
-
delete_corpus
(corpus_name: str)¶ Delete a corpus from the database
>>> from graphannis.cs import CorpusStorageManager >>> from graphannis.graph import GraphUpdate >>> with CorpusStorageManager() as cs: ... # create a corpus named "test" ... with GraphUpdate() as g: ... g.add_node('anynode') ... cs.apply_update('test', g) ... # delete it ... cs.delete_corpus('test') True
-
find
(corpora, query, query_language=<QueryLanguage.AQL: 0>, offset=0, limit=10, order=<ResultOrder.Normal: 0>)¶ Find all results for a query and return the match ID for each result. The query is paginated and an offset and limit can be specified. Returns a list of match IDs, where each match ID consists of the matched node annotation identifiers separated by spaces.
-
import_from_fs
(path, fmt: graphannis.cs.ImportFormat = <ImportFormat.RelANNIS: 0>, corpus_name: str = None)¶ Import corpus from the file system into the database
>>> from graphannis.cs import CorpusStorageManager >>> from graphannis.graph import GraphUpdate >>> with CorpusStorageManager() as cs: ... # import relANNIS corpus with automatic name ... cs.import_from_fs("relannis/GUM") ... # import with a different name ... cs.import_from_fs("relannis/GUM", ImportFormat.RelANNIS, "GUM_version_unknown")
-
list
()¶ List all available corpora in the corpus storage.
-
subcorpus_graph
(corpus_name: str, document_ids)¶ Return the copy of a subgraph which includes all nodes that belong to any of the given list of sub-corpus/document identifiers.
-
subgraph
(corpus_name: str, node_ids, ctx_left=0, ctx_right=0)¶ Return the copy of a subgraph which includes the given list of node annotation identifiers, the nodes that cover the same token as the given nodes and all nodes that cover the token which are part of the defined context.
-
graphannis.graph module¶
-
class
graphannis.graph.
GraphUpdate
¶ Bases:
object
-
add_edge
(source_node, target_node, layer, component_type, component_name)¶ Add an edge between two existing nodes.
>>> from graphannis.graph import GraphUpdate >>> with GraphUpdate() as g: ... g.add_node('n1') ... g.add_node('n2') ... g.add_edge('n1', 'n2', 'mylayer', 'Pointing', 'dep') ...
-
add_edge_label
(source_node, target_node, layer, component_type, component_name, anno_ns, anno_name, anno_value)¶ Add a label to an existing edge
>>> from graphannis.graph import GraphUpdate >>> with GraphUpdate() as g: ... g.add_node('n1') ... g.add_node('n2') ... g.add_edge('n1', 'n2', 'mylayer', 'Pointing', 'dep') ... g.add_edge_label('n1', 'n2', 'mylayer', 'Pointing', 'dep', ... 'myns', 'myanno', 'annoval') ...
-
add_node
(node_name, node_type='node')¶ Add a named node to the graph
>>> from graphannis.graph import GraphUpdate >>> with GraphUpdate() as g: ... g.add_node('n1') ...
-
add_node_label
(node_name, anno_ns, anno_name, anno_value)¶ Add a label to an existing node to the graph
>>> from graphannis.graph import GraphUpdate >>> with GraphUpdate() as g: ... g.add_node('n1') ... g.add_node_label('n1', 'mynamespace', 'myname', 'myvalue') ...
-
delete_edge
(source_node, target_node, layer, component_type, component_name)¶ Delete an existingedge between two nodes.
>>> from graphannis.graph import GraphUpdate >>> with GraphUpdate() as g: ... g.add_node('n1') ... g.add_node('n2') ... g.add_edge('n1', 'n2', 'mylayer', 'Pointing', 'dep') ... g.delete_edge('n1', 'n2', 'mylayer', 'Pointing', 'dep') ...
-
delete_edge_label
(source_node, target_node, layer, component_type, component_name, anno_ns, anno_name)¶ Delete a label from an edge
>>> from graphannis.graph import GraphUpdate >>> with GraphUpdate() as g: ... g.add_node('n1') ... g.add_node('n2') ... g.add_edge('n1', 'n2', 'mylayer', 'Pointing', 'dep') ... g.add_edge_label('n1', 'n2', 'mylayer', 'Pointing', 'dep', ... 'myns', 'myanno', 'annoval') ... g.delete_edge_label('n1', 'n2', 'mylayer', 'Pointing', 'dep', ... 'myns', 'myanno') ...
-
delete_node_label
(node_name, anno_ns, anno_name)¶ Delete an existing label from an existing node
>>> from graphannis.graph import GraphUpdate >>> with GraphUpdate() as g: ... g.add_node('n1') ... g.add_node_label('n1', 'mynamespace', 'myname', 'myvalue') ... g.delete_node_label('n1', 'mynamespace', 'myname') ...
-
graphannis.util module¶
-
graphannis.util.
node_name_from_match
(match)¶ Takes a match identifier (which includes the matched annotation name) and returns the node name. This can take a single string or a list of strings as argument.
>>> m = node_name_from_match("tiger::cat::topcorpus/subcorpus/doc1#n2) >>> m == "topcorpus/subcorpus/doc1#n2"
graphannis.errors module¶
-
exception
graphannis.errors.
AQLSemanticError
(msg: str, cause: graphannis.errors.GraphANNISException = None)¶
-
exception
graphannis.errors.
AQLSyntaxError
(msg: str, cause: graphannis.errors.GraphANNISException = None)¶
-
exception
graphannis.errors.
GraphANNISException
(msg: str, cause: Exception = None)¶ Bases:
Exception
-
exception
graphannis.errors.
NoSuchCorpus
(msg: str, cause: graphannis.errors.GraphANNISException = None)¶
-
exception
graphannis.errors.
SetLoggerError
(msg: str, cause: graphannis.errors.GraphANNISException = None)¶
-
graphannis.errors.
consume_errors
(err)¶ Processes the error list from the C-API and raises an exception if they contain an error. It also deletes the memory if the vector has been filled.