Module odinson.ruleutils

Expand source code
try:
    from .info import info
    from .queryparser import parse_odinson_query
    from .oracle import *
    from .random import *

    __version__ = info.version

    __all__ = [
        "parse_odinson_query",
        "path_from_root",
        "all_paths_from_root",
        "make_transition_table",
        "random_surface",
        "random_traversal",
        "random_hybrid",
        "random_query",
    ]

except Exception as e:
    print("Failed to import info")
    print(e)

Sub-modules

odinson.ruleutils.config
odinson.ruleutils.info
odinson.ruleutils.oracle
odinson.ruleutils.queryast
odinson.ruleutils.queryparser
odinson.ruleutils.random

Functions

def all_paths_from_root(target: AstNode, vocabularies: Optional[Dict[str, List[str]]] = None) ‑> List[List[AstNode]]

Returns all episodes that render an equivalent rule to target.

Expand source code
def all_paths_from_root(
    target: AstNode, vocabularies: Optional[Vocabularies] = None
) -> List[List[AstNode]]:
    """Returns all episodes that render an equivalent rule to target."""
    results = []
    for p in target.permutations():
        results.append(path_from_root(p, vocabularies))
    return results
def make_transition_table(paths)

Gets a list of paths and returns a transition table.

Expand source code
def make_transition_table(paths):
    """Gets a list of paths and returns a transition table."""
    trans = defaultdict(set)
    for path in paths:
        for i in range(len(path) - 1):
            trans[path[i]].add(path[i + 1])
    return {k: list(v) for k, v in trans.items()}
def parse_odinson_query(pattern: str) ‑> AstNode

Gets a string and returns the corresponding AST.

Expand source code
def parse_odinson_query(pattern: Text) -> AstNode:
    """Gets a string and returns the corresponding AST."""
    return top.parseString(pattern)[0]
def path_from_root(target: AstNode, vocabularies: Optional[Dict[str, List[str]]] = None) ‑> List[AstNode]

Returns the sequence of transitions from the root of the search tree to the specified AstNode.

Expand source code
def path_from_root(
    target: AstNode, vocabularies: Optional[Vocabularies] = None
) -> List[AstNode]:
    """
    Returns the sequence of transitions from the root of the search tree
    to the specified AstNode.
    """
    if isinstance(target, Query):
        root = HoleQuery()
    elif isinstance(target, Traversal):
        root = HoleTraversal()
    elif isinstance(target, Surface):
        root = HoleSurface()
    elif isinstance(target, Constraint):
        root = HoleConstraint()
    else:
        raise ValueError(f"unsupported target type '{type(target)}'")
    if vocabularies is None:
        # If no vocabularies were provided then construct
        # the minimal vocabularies required to reach the target.
        vocabularies = make_minimal_vocabularies(target)
    oracle = Oracle(root, target, vocabularies)
    return list(oracle.traversal())
def random_hybrid(vocabularies: Dict[str, List[str]], n_iters: int = 1, **kwargs) ‑> HybridQuery
Expand source code
def random_hybrid(
    vocabularies: Vocabularies, n_iters: int = 1, **kwargs
) -> HybridQuery:
    return HybridQuery(
        random_surface(vocabularies, n_iters, **kwargs),
        random_traversal(vocabularies, n_iters, **kwargs),
        random_query(vocabularies, n_iters, **kwargs),
    )
def random_query(vocabularies: Dict[str, List[str]], n_iters: int = 1, **kwargs) ‑> AstNode
Expand source code
def random_query(vocabularies: Vocabularies, n_iters: int = 1, **kwargs) -> AstNode:
    if random.random() < 0.5:
        return random_surface(vocabularies, n_iters, **kwargs)
    else:
        return random_hybrid(vocabularies, n_iters, **kwargs)
def random_surface(vocabularies: Dict[str, List[str]], n_iters: int = 1, **kwargs) ‑> Surface
Expand source code
def random_surface(vocabularies: Vocabularies, n_iters: int = 1, **kwargs) -> Surface:
    if "allow_wildcards" in kwargs:
        kwargs["allow_surface_wildcards"] = kwargs["allow_wildcards"]
    if "allow_mentions" in kwargs:
        kwargs["allow_surface_mentions"] = kwargs["allow_mentions"]
    if "allow_alternations" in kwargs:
        kwargs["allow_surface_alternations"] = kwargs["allow_alternations"]
    if "allow_concatenations" in kwargs:
        kwargs["allow_surface_concatenations"] = kwargs["allow_concatenations"]
    if "allow_repetitions" in kwargs:
        kwargs["allow_surface_repetitions"] = kwargs["allow_repetitions"]
    tree = random_tree(HoleSurface(), vocabularies, n_iters, **kwargs)
    # hack: pass tree through parser to make it right-heavy
    tree = parse_odinson_query(str(tree))
    return tree
def random_traversal(vocabularies: Dict[str, List[str]], n_iters: int = 1, **kwargs) ‑> Traversal
Expand source code
def random_traversal(
    vocabularies: Vocabularies, n_iters: int = 1, **kwargs
) -> Traversal:
    if "allow_wildcards" in kwargs:
        kwargs["allow_traversal_wildcards"] = kwargs["allow_wildcards"]
    if "allow_alternations" in kwargs:
        kwargs["allow_traversal_alternations"] = kwargs["allow_alternations"]
    if "allow_concatenations" in kwargs:
        kwargs["allow_traversal_concatenations"] = kwargs["allow_concatenations"]
    if "allow_repetitions" in kwargs:
        kwargs["allow_traversal_repetitions"] = kwargs["allow_repetitions"]
    tree = random_tree(HoleTraversal(), vocabularies, n_iters, **kwargs)
    # hack: pass tree through parser to make it right-heavy
    tree = parse_traversal(str(tree))
    return tree