Python SDK

Skip to end of metadata
Go to start of metadata
Table of Contents

Installation Instructions

Pip3 installation

$ (sudo) pip3 install s4api

Direct Download

Source Code

API Keys

As described in the API documentation page, access to all S4 REST services require API key/secret pair, which can be generated from the S4 Management Console.

Workflow

The following examples demonstrates basic SDK usage.

Text-Analytics API

Text Annotation

Note: These examples are valid for each of the following S4 pipelines - News, SBTTwitIE

Step 1: Import the necessary modules from the package

from s4api.textanalytics_api import TextanalyticsApi
from s4api.swagger import ApiClient

import json

Step 2: Specify endpoint and credential details

api_key = "<your-credentials-here>"
key_secret = "<your-credentials-here>"

# The annotation service endpoint
endpoint = "http://text.s4.ontotext.com/v1/news"

Step 3: Initialize an ApiClient object with both your api_key and key_secret, as noted in the API Keys section

client = ApiClient(api_key=api_key, key_secret=key_secret, endpoint=endpoint)

Step 4: Initialize an accessor object through which you would make calls to the S4 Services

accessor = TextanalyticsApi(client)

Step 5: Call one of the methods with specified request options/headers/etc (if required). For further information please refer to the S4 Text Annotation page.

Step 5.1: Process JSON notation (simple inline document)

payload = {"document": "<your-text-here>", "documentType": "text/plain"}

annotation = json.loads(accessor.process("json", body=payload)))


# View text
print(annotation["text"])

# View sentiment score
print(annotation["sentimentScore"])

# View annotated entities
print(annotation["entities"])

Step 5.2: Process JSON notation (online URL)

payload = {"documentUrl": "<link-to-the-document-here>", "documentType": "text/html"}

annotation = json.loads(accessor.process("json", body=payload))


# View text
print(annotation["text"])

# View sentiment score
print(annotation["sentimentScore"])

# View annotated entities
print(annotation["entities"])

Step 5.3: Process JSON notation (image annotation)

payload = {"documentUrl": "<link-to-the-document-here>", "documentType": "text/html", "imageTagging": True, "imageCategorization": True}

annotation = json.loads(accessor.process("json", body=payload))


# View categorized images
print(annotation["images"])

Step 5.4: Process MS Word document (.docx)

# Multiple file types supported, refer to the link below the snippet
with open("<your-filename-here>", 'rb') as f:
    content = f.read()

doc = "<doctype_specifier>"  # *refer to the link below the snippet
metadata = json.dumps({"documentType": doc})
files = {"meta": ('', metadata, "application/json"),
         "data": ("<your-filename-here>", content, "application/octet-stream")}

print(accessor.process_multipart_request(files))

*<doctype_specifier> - It is different for different types of files, please refer to this table.

Text Classification

Note: These examples are only valid for the News-Classifier Pipeline

Step 1: Import the necessary modules from the package

from s4api.textanalytics_api import TextanalyticsApi
from s4api.swagger import ApiClient

import json

Step 2: Specify endpoint and credential details

api_key = "<your-credentials-here>"
key_secret = "<your-credentials-here>"

# The annotation service endpoint
endpoint = "http://text.s4.ontotext.com/v1/news-classifier"

Step 3: Initialize an ApiClient object with both your api_key and key_secret, as noted in the API Keys section

client = ApiClient(api_key=api_key, key_secret=key_secret, endpoint=endpoint)

Step 4: Initialize an accessor object through which you would make calls to the S4 Services

accessor = TextanalyticsApi(client)

Step 5: Call one of the methods with specified request options/headers/etc (if required). For further information please refer to the S4 Text Annotation page.

Step 5.1: Process JSON notation (simple inline document)

payload = {"document": "<your-text-here>", "documentType": "text/plain"}

classification = json.loads(accessor.process("json", body=payload))


# View the classification category
print(classification["category"])

# View the top 3 most probable categories
print(classification["allScores"])

Step 5.2: Process JSON notation (online URL)

payload = {"documentUrl": "<link-to-the-document-here>", "documentType": "text/html"}

classification = json.loads(accessor.process("json", body=payload))


# View the classification category
print(classification["category"])

# View the top 3 most probable categories
print(classification["allScores"])

GraphDB API

Step 1: Import the necessary modules from the package

from s4api.graphdb_api import GraphDBApi
from s4api.swagger import ApiClient

Step 2: Specify endpoint and credential details

api_key = "<your-credentials-here>"
key_secret = "<your-credentials-here>"

# The Database service endpoint
# Refer to the screenshot below for information on
# where to copy the full database service endpoint from

endpoint = "Please copy this from the UI"
repo_name = "my_repo"


Step 3: Initialize an ApiClient object with both your api_key and key_secret, as noted in the API Keys section

client = ApiClient(api_key=api_key, key_secret=key_secret, endpoint=endpoint)

Step 4: Initialize an accessor object through which you would make calls to the S4 Services

accessor = GraphDBApi(client)

Step 5: Call one of the methods with specified request options/headers/etc (if required). For further information please refer to the Fully-Managed GraphDB in the Cloud page.

Step 5.1: Create a repository

For a list of all available rulesets, please look at this table.

payload = {
    "repositoryID": repo_name,
    "label": "Description of your database",
    "ruleset": "owl-horst-optimized"
}
accessor.create_repository(body=payload)

Step 5.2: Get repository cache percentage configuration

print (accessor.get_repo_config())

Step 5.3: Update repository cache percentage configuration

Warning: This method may be used only if you have more than one repository in your database.
Note: Repository percentage sum MUST be equal to 100

payload3 = {
    "proportions": [
        {"repositoryID": repo_name,
         "percentage": 45},
        {"repositoryID": repo_name2,
         "percentage": 55}
    ]
}
accessor.update_repo_config(body=payload3)

# Note: After updating the cache percentage, you need
# to wait for a couple of seconds for the changes to take effect
time.sleep(15)

print (accessor.get_repo_config())

Step 5.4: Upload data from .rdf file

accessor.upload_data_file("/full/path/to/your/file.rdf", repo_name=repo_name))

Step 5.5: Execute SPARQL SELECT query

query = """PREFIX fb: <http://rdf.freebase.com/ns/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbp-prop: <http://dbpedia.org/property/>
PREFIX dbp-ont: <http://dbpedia.org/ontology/>

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX om: <http://www.ontotext.com/owlim/>
SELECT ?name ?numberOfStudents ?staff ?established
WHERE {
    dbpedia:University_of_Leipzig  rdfs:label ?name;
    dbp-prop:students ?numberOfStudents;
    dbp-prop:staff ?staff;
    dbp-prop:established ?established.
}"""


payload_query = {"query": query}
print(accessor.sparql_select(body=payload_query, repo_name=repo_name))

Step 5.6: Execute SPARQL UPDATE query

update = """INSERT DATA {
<http://dbpedia.org/resource/University_of_Leipzig>
<http://dbpedia.org/property/students>
"12345678"
}"""

query = {"update": update}
accessor.sparql_update(body=query, repo_name=repo_name)

Step 5.7: Delete your repository

accessor.delete_repository(repo_name=repo_name)

FactForge API

Step 1: Import the necessary modules from the package

from s4api.fact_forge_api import FactForgeApi
from s4api.swagger import ApiClient

Step 2: Specify endpoint and credential details

api_key = "<your-credentials-here>"
key_secret = "<your-credentials-here>"

# The SPARQL Service endpoint
endpoint = "https://lod.s4.ontotext.com/v1/FactForge/sparql"

Step 3: Initialize an ApiClient object with both your api_key and key_secret, as noted in the API Keys section

client = ApiClient(api_key=api_key, key_secret=key_secret, endpoint=endpoint)

Step 4: Initialize an accessor object through which you would make calls to the S4 Services

accessor = FactForgeApi(client)

Step 5: Create a SPARQL 1.1 Select Query

# Any syntactically valid SPARQL 1.1 Select query would work
query = "SELECT * WHERE { ?S ?P ?O } LIMIT 10"

Step 6: Call one of the methods with specified request options/headers/etc (if required). For further information please refer to the Knowledge Graphs page.

Step 6.1 SPARQL Select query (GET)

Note: This is the recommended method

print (accessor.sparql_get(
    query=query,
    accept="application/sparql-results+json"))

Step 6.2 SPARQL Select query (POST) 

Note: This method should be used in cases where the length of the query exceeds practicable limits of proxies, servers, etc.

print (accessor.sparql_post(
    query=query,
    accept="application/sparql-results+json",
    content_type="application/x-www-form-urlencoded"))

Sphinx autodoc

  • The Sphinx autodoc is available here (or you may use the Python3 built-in help() method).
Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.