Knowledge Graph Language (KGL) parser.
Project description
Knowledge Graph Language (KGL)
Knowledge Graph Language is a query language for interacting with graphs. It accepts semantic triples (i.e. ("James", "Enjoys", "Coffee")
), indexes them, and makes them available for querying.
You can use this language to:
- Find all attributes associated with a node in a graph.
- Return all nodes that are connected to a node.
- Return all nodes that are connected to a node and meet a specified condition.
- Find how two nodes connect in a graph.
This language is a work in progress.
Ingesting Information
This project allows you to index triples of data like:
("James", "Enjoys", "Coffee")
("James", "Hobbies", "Making coffee")
("James", "WorksFor", "Roboflow")
("Roboflow", "Makes", "Computer Vision")
("Roboflow", "EntityType", "Company")
A graph is then constructed from the triples that you can then query.
Syntax
Query a Single Item
You can query a single item:
{ James }
This will return all items associated with the James
entry:
{'Birthday': ['March 20th, 2024'], 'WorksFor': ['Roboflow', 'PersonalWeb', 'IndieWeb'], 'Enjoys': ['Coffee'], 'Hobbies': ['Making coffee']}
Sequential Queries
The Knowledge Graph Language flows from left to right. You can make a statement, then use an arrow (->
) to query an attribute related to the result:
Consider the following query:
{ James -> WorksFor -> Makes }
This query gets the James
item, retrieves for whom James works, then reports the Makes
attribute for the employer.
The query returns:
['Computer vision software.']
Filter Queries
You can filter queries so that the flow of data is constrained to only work with results that match a condition.
Consider this query:
{ Roboflow ("EntityType" = "Company") -> WorksFor ("Enjoys" = "Coffee") -> Hobbies }
This query gets the instance of Roboflow
that has the EntityType
property Company
. This could be used for disambiguation.
Then, the query gets everyone who works at Roboflow who enjoys coffee. The query then finds who everyone works for, and returns their hobbies.
This returns:
['Making coffee']
You can filter by the number of items connected to a node in the graph, too.
Consider these triples:
("CLIP", "isA", "Paper")
("CLIP", "Authors", "Person 1")
("CLIP", "Authors", "Person 2")
("Person 1", "Citations", "Paper 42")
("Person 2", "Citations", "Paper 1")
("Person 2", "Citations", "Paper 2")
("Person 2", "Citations", "Paper 3")
("Person 2", "Citations", "Paper 4")
Suppose you want to find all authors of the CLIP paper in a research graph, but you only want to retrieve authors whose work has been cited at least three times. You can do this with the following query:
{ CLIP -> Authors ("Citations" > "3") }
This query returns:
['Person 2']
This is because only Person 2
has greater than three citations to their works.
Describe Relationships
Suppose you want to know how James
and Roboflow
relate. For this, you can use the interrelation query operator (<->
).
Consider this query:
{ Roboflow <-> James }
This returns:
['Roboflow', ('James', 'WorksFor')]
Serialized into Knowledge Graph Language, this response is represented as:
Roboflow -> WorksFor
If we execute that query in introspection mode, we can see all information about James:
{ Roboflow -> WorksFor }!
This returns:
[{'James': {'Birthday': ['March 20th, 2024'], 'WorksFor': ['Roboflow', 'PersonalWeb', 'IndieWeb'], 'Enjoys': ['Coffee'], 'Hobbies': ['Coffee']}}, {'Lenny': {'WorksFor': ['MetaAI', 'Roboflow']}}]
Introspection
By default, all Sequential Queries return single values. For example, this query returns the names of everyone who works at Roboflow:
{ Roboflow -> WorksFor }
The response is:
['James', 'Lenny']
We can enable introspection mode to learn more about each of these responses. To enable introspection mode, append a !
to the end of your query:
{ Roboflow -> WorksFor }!
This returns all attributes related, within one degree, to James and Lenny, who both work at Roboflow:
[{'James': {'Birthday': ['March 20th, 2024'], 'WorksFor': ['Roboflow', 'PersonalWeb', 'IndieWeb'], 'Enjoys': ['Coffee'], 'Hobbies': ['Coffee']}}, {'Lenny': {'WorksFor': ['MetaAI', 'Roboflow']}}]
Description Operators
By default, Knowledge Graph Language returns the value associated with your query. You can add operators to the end of your query to change the output.
You can use:
?
to return True if your query returns a response and False if your query returns no response.#
to count the number of responses!
to return an introspection response.
Python API
First, install KGL:
pip install kgl
Create a Knowledge Graph
from kgl import KnowledgeGraph
kg = KnowledgeGraph()
Ingest Items
You can ingest triples of strings:
kg.add_node(("Roboflow", "Owned", "Lenny"))
You can also ingest triples whose third item is a list:
kg.add_node(("Alex", "Citations", ["MetaAI", "GoogleAI", "Coffee", "Teacup", "Roboflow"]))
Evaluate a Query
result = kg.evaluate("{ James }")
print(result)
Responses are valid Python objects, whose type varies depending on your query.
By default, KGL returns a list.
But:
!
queries return dictionaries.#
queries return integers.?
queries return booleans.
Tests
To run the project test suite, run:
pytest test
License
This project is licensed under an MIT license.
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
Built Distribution
File details
Details for the file kgl-0.1.2.tar.gz
.
File metadata
- Download URL: kgl-0.1.2.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2b8c91d15cdd8065f4ec8b1b0d93a6d4f2505a81d6f31a542b1f5f963522edc |
|
MD5 | 9c2f42b59d6218760dabd5eabea4f0c7 |
|
BLAKE2b-256 | 02b0bf6bed3f6ff05977f9a33605f57c79daea3cdab12f4d9563377eed69a1ca |
File details
Details for the file kgl-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: kgl-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f73d7f03003fed443d2c0186958d8540dcc27d440328246d3566c26da6053c73 |
|
MD5 | 3bfbd53f23c5d268bacfc1306aae2934 |
|
BLAKE2b-256 | 76fd0183a000f6a29c147d6ca468b230c0a5d6f716ba069e61d19ef1f26388e8 |