GIG Python Library

Umayanga Gunawardhana
3 min readAug 25, 2021

In my previous article, I briefly introduced GIG and its architecture. As explained there, GIG is written in Golang with a focus on optimizing the performance of the core system. To make things much easier for the developers, we have also developed a python library that enables Python developers to integrate GIG API into their code seamlessly.

You can use the following oneliner to install the gig-python package to your python project.

Installation

pip install gig-python

Prerequisites

The Python package is only a wrapper to the GIG Server which is originally built using Golang. You require a running GIG server to interface with the Python library. Check https://github.com/LDFLK/GIG for more information on how to initiate a GIG server locally. Also having some data populated using the crawlers might come in handy when testing the functions we are explaining below.

Initialization

Once you have the GIG server up and running use the following code snippet to import and initiate the server connection. Change the server URL accordingly.

from gig-python import Server as GIG_Server
from gig-python import Entity

server = GIG_Server("http://localhost:9000/")

API Functions

Next will see how we can use the functionality of our Python package. The library provides a rich set of functions to interface with the GIG server easily.

Let's say you need to get all the details available regarding an entity from the server. The following code snippet shows how to retrieve an entity using the library. Please note the entity name must be an exact match.

entity = server.get("Sri Lanka")

As said above the entity name to get the details should be an exact match. To find the existing entities from the server, you can use the search function to retrieve matching entity results using the code sample given below. The search keyword doesn’t have to be an exact match. our search algorithm is capable of finding the most relevant results from the database.

entities = server.search("sri lanka", attributes_list=["title"])

Also, if you decide to write your own crawlers to populate the GIG server database, you can tap into the normalization service to get the proper entity names if they exist. GIG API always rechecks for normalized title names before saving them into the server. Therefore it is optional for you to check before sending an entity to be saved in the server.

results = server.normalize_name("sri lanka")

Similarly, we have a separate endpoint to Normalize location names.

results = server.normalize_location("colombo")

Having all the details from an entity might not be enough. Let's say Person A is connected to Organization B. So Organization B will be linked to Person A. To get any entities linked to Person A, you can use the following code snippet. This will return a list of entities linked to the given entity.

entities = server.get_links("Person A")

Similarly, you can get the reverse of the links using the following function. To get a list of other entities which refer to a given entity, use the following code.

entities = server.get_relations("Sri Lanka")

You can use add and add_all functions to add new entities to the server. This function will look if an entity with the same name already exists and modify the entity with new data. Otherwise, it creates a new entity.

entity = Entity()
entity.title = "LDF Test Entity"
result = server.add(entity)
entity = Entity()
entity.title = "LDF Batch"
result = server.add_all([entity])

Use the following code snippet to notify the system that a certain entity ceases to exist from a specific data. The source date is considered the end date of the entity.

entity = Entity()
entity.title = "LDF Test Entity"
entity.source_date = "2021-01-02T15:04:05-07:00"
entity.source = "python-lib"
result = server.terminate(entity)

References

  1. https://github.com/LDFLK/gig-python
  2. https://pypi.org/project/gig-python/

--

--