Skip to main content

Semantic network implementation

Project description

semantic_network Package

SemanticNetwork

The class is designed for encoding graph structures using universal triplets:

entity_1 relation entity_2
entity_2 relation entity_3
  • Triplet parts are separated by spaces
  • First and third parts are objects
  • Second part is a directed relationship between objects

1. Creating and Extending the Graph:

from semantic_network import SemanticNetwork

script = """
    Circle hasPart Circle.radius
    Circle hasPart Circle.center
"""

sn = SemanticNetwork.from_script(script)

script_2 = """
    c1 fromProto Circle
    c1 hasPart c1.radius
    c2 fromProto Circle
    c2 hasPart c2.radius
    c2 hasPart c2.center
    c2.radius fromProto Circle.radius
"""

sn.append_script(script_2)

This creates a graph with objects and relationships:

print(sn)
----------------------------------------
|- c1
|- Circle
|- c1 fromProto Circle
|- c1.radius
|- c1 hasPart c1.radius
|- c2
|- c2 fromProto Circle
|- c2.radius
|- c2 hasPart c2.radius
|- c2.center
|- c2 hasPart c2.center
|- Circle.radius
|- c2.radius fromProto Circle.radius
----------------------------------------

2. Graph Search

Search queries are specified using a special query format. Elements starting with * are treated as variables, while other elements are treated as constants.

q = """
    *o1 hasPart *o2
    *o1 fromProto Circle
"""

for objs, rels in sn.query(q):
    print(objs, rels)

Returns dictionaries with found graph fragments:

{'o1': 'c2', 'Circle': 'Circle', 'o2': 'c2.radius'} {'fromProto': 'fromProto', 'hasPart': 'hasPart'}
{'o1': 'c2', 'Circle': 'Circle', 'o2': 'c2.center'} {'fromProto': 'fromProto', 'hasPart': 'hasPart'}
{'o1': 'c1', 'Circle': 'Circle', 'o2': 'c1.radius'} {'fromProto': 'fromProto', 'hasPart': 'hasPart'}

During search, the system also verifies that the query graph is connected and acyclic.

3. Utility Methods

# Extend the graph
sn.append_script(new_script)

# Print graph description (number of unique objects and relationships)
sn.describe()

# Create a copy
sn.copy()

# Serialization to dictionary and creation from dictionary
sn.to_dict()
sn = SemanticNetwork.from_dict(dict_data)

# Write to file with additional custom variables dictionary, and read from file
sm.dump(path, variables)
sn = SemanticNetwork.load(path)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

semantic_network-0.1.1.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

semantic_network-0.1.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file semantic_network-0.1.1.tar.gz.

File metadata

  • Download URL: semantic_network-0.1.1.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for semantic_network-0.1.1.tar.gz
Algorithm Hash digest
SHA256 df9f7854b48523718c86990f4e1e1970ef227b8ddc898d0631207aa366e612da
MD5 9cb7943afe375488396c16b1066f35fd
BLAKE2b-256 aa7fb07c506d3ad9603246f8ba637485301c022c92be354ef33d5c8f45aeb00f

See more details on using hashes here.

File details

Details for the file semantic_network-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for semantic_network-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ed98b9910b1bffb34451c71ee20ccc1f578a13ca689f3fab7ee5957c84f256b0
MD5 18ef3b2a17b33c4a2f05264bccb737e4
BLAKE2b-256 3e498235d21f28411d1d4bddfe9015fe8f68f808eaebb9b4e5913d55f1447f01

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page