Hyperbase

repository·master·Indexed 20 days ago

https://github.com/hyperquest-hq/hyperbase

A foundational Python library for Semantic Hypergraphs (SH) that enables the representation of natural language as ordered, recursive hyperlinks. It provides abstractions for Atoms and Hyperedges, a plugin architecture for parsers (including alphabeta and generative plugins), and tools like PatternCounter for discovering frequent hyperedge structures.

Tokens
30.1K
Snippets
105
Records
131
Agent score
70%

What's inside hyperbase

  1. What is Hyperbase and Semantic Hypergraphs

    master

    Hyperbase is a Python library designed for working with Semantic Hypergraphs (SH). Semantic Hypergraphs allow for the structured representation of natural language sentences.

    For example, the sentence "Einstein first published the theory of relativity in 1905" can be represented in SH notation as a nested structure of semantic roles (e.g., M for modifiers, P.sox for predicates, C for concepts, B.ma for binding markers, and T for temporal markers):

    ((first/M published/P.sox)
        einstein/C
        (the/M
            (of/B.ma theory/C relativity/C))
        (in/T 1905))
  2. Match by type, subtype, and argrole

    master

    Patterns in Hyperbase support hierarchical type matching and flexible argument role matching:

    Type and Subtype Matching: If a pattern specifies a type but not a subtype (e.g., */C), it will match any subtype of that type (e.g., alice/Cp or chess/Cc).

    Argrole Matching:

    • Positional: Standard patterns like (plays/P.so * *) match specific roles at specific positions.
    • Unordered/Set Matching: Surrounding argroles with curly brackets {} allows matching a set of roles regardless of their position or the presence of other arguments. For example, (is/P.{sc} * */C) matches if both s and c roles are present anywhere.
    • Exclusion: Use a hyphen - after the argrole sequence to explicitly forbid certain roles. For example, (plays/P.{so}-x * *) matches only if the so role is present and the x role is NOT present.
    from hyperbase import hedge
    
    # Subtype matching: */C matches any concept subtype
    pattern_type = hedge("(plays/P.so */C */C)")
    edge_subtype = hedge("(plays/Pd.so alice/Cp chess/Cc)")
    print(edge_subtype.match(pattern_type))  # [{}]
    
    # Unordered argrole matching: {sc} matches roles regardless of position
    pattern_unordered = hedge("(is/P.{sc} * */C)")
    edge_unordered = hedge("(is/P.cs blue/C (the/M sky/C))")
    print(edge_unordered.match(pattern_unordered))  # [{}]
    
    # Forbidding roles: {so}-x forbids the 'x' role
    pattern_exclude = hedge("(plays/P.{so}-x * *)")
    edge_exclude = hedge("(plays/P.so alice/C chess/C)")
    print(edge_exclude.match(pattern_exclude))  # [{}]
  3. Understand Hyperedge and Atom abstractions

    master

    Hyperbase uses two main object classes:

    • Atom: The most basic unit, representing a single token with a root, role, and optional namespace.
    • Hyperedge: A collection of hyperedges. A non-atomic hyperedge is an ordered, recursive structure where the first element is the connector and the remaining elements are arguments.

    Hyperedge is derived from Python's tuple, allowing index-based access to arguments. Atom is a subclass of Hyperedge.

    Note: Do not instantiate these classes directly; always use hedge() to create them.

  4. How Hyperbase parsers work

    master

    Hyperbase uses a plugin architecture for parsers. The core hyperbase package does not include any parsers; it only defines the abstract parser interface. Parsers are separate Python packages that register themselves via entry points.

    Available parser packages include:

    • hyperbase-parser-ab: The alphabeta plugin (AlphaBeta parser using spaCy).
    • hyperbase-parser-gen: The generative plugin (Multilingual generative parser based on a transformer model).
  5. Understand Semantic Hypergraph (SH) Notation

    master

    Semantic Hypergraph (SH) notation is a structured way to represent natural language using atoms and hyperedges.

    Core Principles:

    • Atoms: The simplest structure, typically a single word with a type (e.g., sky/C).
    • Hyperedges: Combinations of atoms or other hyperedges (e.g., (blue/M sky/C)).
    • Connectors: The first element of a non-atomic hyperedge. Valid connectors include Predicates (P), Modifiers (M), Builders (B), Triggers (T), and Conjunctions (J).
    • Subtypes: Additional information appended to a type (e.g., maria/Cp where p is a subtype of concept C).
    • Namespaces: Used to distinguish identical atoms (e.g., paris/Cp/1 vs paris/Cp/2) or languages (e.g., sky/Cc/en).
    (is/P.so (the/M sky/C) blue/C)
  6. Represent knowledge and claims using hypergraphs

    master

    The Semantic Hypergraph serves as a knowledge model that allows for the representation of claims. Because hyperedges can be nested, you can represent facts about facts, which enables attributing assertions to specific sources.

    Instead of relying on a single notion of "ground truth," hyperbase models assertions as claims. A claim is structured as a hyperedge (the source) containing a hyperedge (the fact).

    Example: Representing that Mary claims Berlin is nice:

    (claims mary (is berlin nice))

    This structure is particularly useful for modeling controversial topics where multiple actors may have contradictory views on the same issue.

  7. How reader auto-detection and priority work

    master

    Hyperbase uses an accepts(source) mechanism to automatically select a reader for a given input.

    1. Acceptance: All registered readers are checked. If reader.accepts(source) returns True, that reader is a candidate.
    2. Priority (more_general): If multiple readers accept a source, Hyperbase uses the more_general attribute to pick the most specific one.
      • For example, a Wikipedia URL is accepted by both the url and wikipedia readers.
      • Because WikipediaReader declares more_general = ['url'], it takes priority over the generic url reader.
  8. Understand hyperedge types and inference

    master

    Every hyperedge has a type (t).

    • Atoms: The type is explicitly defined in the string.
    • Non-atomic hyperedges: The type is inferred from the connector. For example, a predicate (P) applied to arguments produces a relation (R).

    Use .ct to inspect the type of the connector specifically.

    edge = hedge("(likes/P.so mary/Cp chess/Cc)")
    print(edge.t)   # 'R' (inferred Relation)
    print(edge.ct) # 'P' (connector type Predicate)
  9. Understand the Semantic Hypergraph concept

    master

    The Semantic Hypergraph (SH) is the core data model of hyperbase. Unlike traditional graphs that use vertices and dyadic (two-way) edges, a hypergraph uses hyperedges to represent n-ary connections (relationships between any number of entities).

    Key properties of SH hyperedges include:

    • Ordering: The position of a vertex within a hyperedge matters (e.g., (a b c) is distinct from (c b a)).
    • Recursivity: Hyperedges can contain other hyperedges as vertices. This allows for representing relationships between relationships (higher-order relationships).

    Example of a recursive hyperedge:

    (a b c (d e f))

    In this example, the hyperedge connects atoms a, b, and c with another hyperedge (d e f).

  10. Use hyperedges as a syntactic language

    master

    In the hyperbase formalism, a hyperedge is the fundamental construct for carrying meaning. The syntax follows a universal rule: the first element is a connector, followed by one or more arguments (which can be atoms or other hyperedges).

    The connector specifies the semantic relationship between the arguments. Common roles for connectors include:

    • Predicate: Defines a proposition (e.g., (is berlin nice)).
    • Concept Combination: Defines a new concept from existing ones (e.g., (of capital germany)).
    • Concept Building: Combines concepts (e.g., (and meat potatoes)).
    • Concept Modification: Represents specific instances (e.g., (highest (in mountain brazil))).
    • Condition Specification: Defines conditions for a proposition (e.g., (when (is (the sky) blue))).

    Hyperedges are isomorphic to Lisp S-expressions, where the first item acts similarly to a function and the subsequent items act as arguments.

    (climbs mary (the (highest (in mountain brazil))))