示例#1
0
#!/usr/bin/python3
from terminusdb_client import WOQLQuery as WQ
from terminusdb_client import WOQLClient

server_url = "https://*****:*****@value': Word,
            '@language': 'en'
        }),
        WQ().triple("v:X", "ontolex:canonicalForm", "v:_Blank"),
        WQ().triple("v:X", "ontolex:sense", "v:Lemma"),
        WQ().triple("v:Lemma", "ontolex:isLexicalizedSenseOf", "v:PWN"),
        WQ().triple("v:PWN", "wn:definition", "v:_Blank2"),
        WQ().triple("v:PWN", "wn:partOfSpeech", "v:Part_Of_Speech"),
        WQ().triple("v:_Blank2", "rdf:value", "v:Definition"),
    ).execute(client)
    bindings = result['bindings']
    definitions = []
    for binding in bindings:
        definitions.append({
            'word': Word,
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
db = "schemaless_to_schema"
user = "******"
account = "admin"
key = "root"
repository = "local"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

try:
    client.delete_database(db)
except Exception as E:
    pass

client.create_database(db,
                       account,
                       label="Schemaless to Schema",
                       description="Two branch database one with schema",
                       include_schema=False)

# Add some data which meets the schema
WQ().woql_and(WQ().insert("doc:mike", "scm:BankAccount").property(
    "scm:owner", "mike").property("scm:balance",
                                  123)).execute(client, "Add mike")

# Create the schema graph
示例#3
0
                                    description="The Document object in SpaCy")
    token_dt = (WOQLQuery().doctype(
        "SpaCyToken",
        label="SpaCy Tokens").property("lemma", "string").property(
            "pos", "string").property("tag", "string").property(
                "dep", "string").property("shape", "string").property(
                    "is_alpha",
                    "boolean").property("is_stop", "boolean").property(
                        "head", "SpaCyToken").property("doc", "SpaCyDoc"))
    schema = spacy_doc + token_dt
    return schema.execute(client, "Create a schema for SpaCy Tokens")


if __name__ == "__main__":
    db_id = "nlp_spacy"
    client = WOQLClient(server_url="http://localhost:6363")
    client.connect(key="root", account="admin", user="******")
    existing = client.get_metadata(db_id, client.uid())
    if not existing:
        client.create_database(
            db_id,
            accountid="admin",
            label="Spacy Tokens",
            description="Storing tokenization result form SpaCy")
    else:
        client.delete_database(
            db_id)  # delete the original database if the sechema is updated
        client.create_database(
            db_id,
            accountid="admin",
            label="Spacy Tokens",
示例#4
0
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as Q

DB = WOQLClient("https://127.0.0.1:6363", insecure=True)
DB.connect(user="******",
           account="admin",
           key="root",
           db="TodoMVC",
           insecure=True)

from fastapi import FastAPI, HTTPException, Response, status
from fastapi.responses import JSONResponse

from typing import List, Dict
from pydantic import BaseModel

app = FastAPI()


class Todo(BaseModel):
    id: str
    title: str
    completed: bool
    links: Dict


class State(BaseModel):
    todos: List[Todo]
    links: Dict

示例#5
0
import os
import time
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ


server_url = "https://127.0.0.1:6363"
db = "places"
db_label = "Places"
db_comment = "All DBPedia places"
directory = 'geonames_100k'
user = "******"
account = "admin"
key = "root"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

try:
    client.delete_database(db)
except Exception as E:
    print(E)

client.create_database(db,account,label=f"{db_label}",
                       include_schema=False,
                       description=f"All DBPedia {db} data")

times = []
for f in os.listdir(directory):
    filename = f'{directory}/{f}'
    ttl_file = open(filename)
    commit_res = client.squash('This is a squash commit message!',"username")
    # reset to the squash commit 
    client.reset(commit_res, use_path=True)

    # Rebase the current branch onto the specified remote branch
    # more info: https://terminusdb.github.io/terminusdb-client-python/woqlClient.html#terminusdb_client.WOQLClient.rebase
    client.rebase("main")


if __name__ == "__main__":
    db_id = "Netflix"
    url = "netflix.csv"

    # TODO: change the team name 
    team = "<TEAM_NAME>"
    client = WOQLClient("https://cloud.terminusdb.com/"+team)
    
    try:
        client.connect(team=team, use_token=True)
        client.create_database(db_id, label = "Netflix Graph", description = "Create a graph with Netflix data")
    except Exception:
        client.connect(db=db_id, team=team, use_token=True)

    schema.commit(client, commit_msg = "Adding Netflix Schema")
    
    insert_content_data(client, url)

    contents = client.query_document({"@type"  : "Content"}, count=50)

    insert_user_data(list(contents))
# Now I want to change up item 1

new_item_1 = {
    "_id": "U1IT00001",
    "item_name": "Blender",
    "max_discount": "50%",
    "batch_number": "RR450020FRG",
    "price": 450,
    "category": "kitchen appliance"
}

# But before we update it in Mongo, I want to review the changes first

# Create a TerminusX client (see https://dashboard.terminusdb.com/profile)
tbd_endpoint = WOQLClient("http://localhost:6363/")

# Find the item back from database in case someone already changed it
item_1 = collection_name.find_one({"item_name": "Blender"})
patch = tbd_endpoint.diff(item_1, new_item_1)

pprint(patch.content)

# If we apprive, then proceed
collection_name.update_one(patch.before, {"$set": patch.update})

# Working on more complicated objects

expiry_date = '2021-07-15T00:00:00.000'
expiry = dt.datetime.fromisoformat(expiry_date)
new_item_3 = {
示例#8
0
                                    "Year", "v:year_clean").property(
                                        "Runtime", "v:runtime_clean").property(
                                            "Rating",
                                            "v:rating_clean").property(
                                                "Votes", "v:votes_clean"))

    data_prep = WOQLQuery().woql_and(read_csv, prepare_genre_obj,
                                     prepare_actor_obj, prepare_director_obj,
                                     wangles)

    return WOQLQuery().woql_and(data_prep, insert).execute(
        client, "Loading data for the movie graph")


db_id = "movie_graph"
client = WOQLClient("https://localhost:6363", insecure=True)
client.connect(key="root", account="admin", user="******")

existing = client.get_metadata(db_id, client.uid())
if not existing:
    client.create_database(db_id,
                           accountid="admin",
                           label="Graph of IMDB Movies Data",
                           description="Create a graph with IMDB movies data")
else:
    client.set_db(db_id)

create_schema(client)
result = loading_data(
    client,
    "https://raw.githack.com/terminusdb/terminus-tutorials/master/movies-data/IMDB-Movie-Data.csv"
示例#9
0
#!/usr/bin/python3
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ
import csv

# Place the snippet from TerminusX here:

# TerminusX
db = "stock_index"
client = WOQLClient(endpoint)
client.connect(account=team,user=user,key=key)

exists = client.get_database(db)

if not exists:
    client.create_database(db,
                           team,
                           "Stock Exchange Index Data",
                           "Data From Indexes",
                           { '@base' : "terminusdb:///stock_index/",
                             '@schema' : "terminusdb:///stock_index/schema#" },
                           True)
    schema = [
        { '@type' : 'Class',
          '@id' : 'IndexRecord',
          '@key' : { '@type' : 'ValueHash' },
          'index' : 'xsd:string',
          'date' : 'xsd:date',
          'open' : 'xsd:decimal',
          'high' : 'xsd:decimal',
          'low' : 'xsd:decimal',
示例#10
0
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
client = WOQLClient(server_url)
client.connect(user="******", account="admin", key="root", db="bike")

test1 = WQ().count("v:Count").using("_commits").woql_and(
    WQ().triple("v:CID", "ref:commit_timestamp", "v:Y"),
    WQ().triple("v:CID", "ref:commit_message", "v:Message")
).execute(client)
print(test1)
示例#11
0
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
db = "places"
db_label = "Places"
db_comment = "All DBPedia places (GeoNames)"
user = "******"
account = "admin"
key = "root"
filename = "geonames_links_en.ttl"
commit_comment = "Adding all geonames"

client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

try:
    client.delete_database(db)
except Exception as E:
    print(E)

client.create_database(db, account, label=db_label, description=db_comment)

ttl_file = open(filename)
contents = ttl_file.read()
ttl_file.close()

client.insert_triples("instance", "main", contents, commit_comment)
#!/usr/bin/python3
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
user = "******"
account = "admin"
key = "root"
dbid = "Bank_Balance_Example"
repository = "local"
label = "Bank Balance Example"
description = "An example database for playing with bank accounts"

client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=dbid)
from pprint import pprint
from terminusdb_client import WOQLClient, DocumentTemplate, WOQLSchema

# create a client endpoint

client = WOQLClient("http://localhost:6363/")

### Comparing a document object ###


class Person(DocumentTemplate):
    name: str
    age: int


jane = Person(name="Jane", age=18)
janine = Person(name="Janine", age=18)

result_patch = client.diff(jane, janine)

pprint(result_patch.content)

# apply result patch to get back final document

after_patch = client.patch(jane, result_patch)

pprint(after_patch)
assert after_patch == janine._obj_to_dict()

### Comapring document objects in json (dict) formats
示例#14
0
    index = 0
    for code in codes:
        matches.append(generateMatchClause(code, type, index))
        inserts.append(generateInsertClause(code, type, index))
        index = index + 1
    return WOQLQuery().when(WOQLQuery().woql_and(*matches),
                            WOQLQuery().woql_and(*inserts))


def load_data(client):
    """Load the Sample Data
       Parameters
       ==========
       client : a WOQLClient() connection
    """
    codes = ["DUB", "LHR", "ETC", "XXX"]
    q = generateMultiInsertQuery(codes, "Airport")
    #print(json.dumps(q.json(), indent=4))
    q.execute(client)


client = WOQLClient(server_url="https://127.0.0.1:6363")
client.connect(key="root", account="admin", user="******")
existing = client.get_metadata(dbId, client.uid())
if not existing:
    client.create_database(dbId, "admin", label="Airplane Graph")
else:
    client.db(dbId)
create_schema(client)
load_data(client)
示例#15
0
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
client = WOQLClient(server_url)
client.connect(user="******", account="admin", key="root", db="bike")

test3 = WQ().select("v:Time", "v:Message").using("_commits").woql_and(
    WQ().order_by("v:Time", "v:Message", order=["desc", "asc"]).woql_and(
        WQ().triple("v:A", "ref:commit_timestamp", "v:Time"),
        WQ().triple("v:A", "ref:commit_message", "v:Message")))

print(client.query(test3))
示例#16
0
import time
import math

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt
server_url = "https://127.0.0.1:6363"
db = "worldbank_ireland"
db_label = "WorldBank Ireland"
db_comment = "Data from the WorldBank on Ireland"
user = "******"
account = "admin"
key = "root"

client = WOQLClient(server_url, insecure=True)
client.connect(user=user, account=account, key=key, db=db, insecure=True)

try:
    client.delete_database(db)
except Exception as E:
    print(E)

client.create_database(db,
                       account,
                       label=db_label,
                       description=db_comment,
                       include_schema=False)

# Append
client.insert_csv(['WorldBank_Ireland.csv'],
示例#17
0
from terminusdb_client import WOQLQuery as WQ

# Assumes database already exists.

# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt
server_url = "https://127.0.0.1:6363"
#server_url = "https://195.201.12.87:6366"
db = "dbpedia"
db_label = "DBpedia"
db_comment = "Mapped object, property and types from DBpedia's core dataset"
user = "******"
account = "admin"
key = "root"
branches = ["literals", "types", "objects", "geo"]

client = WOQLClient(server_url, insecure=True)
client.connect(user=user, account=account, key=key, db=db, insecure=True)

try:
    client.delete_database(db)
except Exception as E:
    print(E)

client.create_database(db,
                       account,
                       label=db_label,
                       description=db_comment,
                       include_schema=False)

for branch in branches:
    # Load the perties branch
示例#18
0
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
db = "foo"
user = "******"
account = "admin"
key = "root"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

try:
    client.create_database(db,
                           account,
                           label="foo",
                           description="foo and bar, together again")
except Exception as E:
    print(E)

[x, y, z] = WQ().vars("x", "y", "z")
query = WQ().get(WQ().woql_as(x).woql_as(y).woql_as(z)).file(
    "/home/gavin/dev/terminus-server/terminus-schema/api.owl.ttl",
    {"type": "turtle"})
client.query(query)
示例#19
0
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
db = "schemaless_to_schema"
user = "******"
account = "admin"
key = "root"
repository = "local"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

try:
    client.delete_database(db)
except Exception as E:
    pass

client.create_database(db,
                       account,
                       label="Schemaless to Schema",
                       description="Two branch database one with schema",
                       include_schema=False)

# Create a schemaless branch
client.branch('schemaless')

# Switch back to main
client.checkout('main')
示例#20
0
import os
import sys
import time
import math

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt
server_url = "https://127.0.0.1:6363"
db = "woql"
user = "******"
account = "admin"
key = "root"

client = WOQLClient(server_url, insecure=True)
client.connect(user=user, account=account, key=key, db=db, insecure=True)

query = WQ().triple("v:X", "v:Y", "v:Z").to_dict()
print(query)

# Just stick the query in there...
WQ().update_object(query).execute(client)
print("Inserted query")

# Instead give the query a name to aid retrieval
query['@id'] = 'doc:my_triple_query'
WQ().update_object(query).execute(client)
print("Inserted named query")

# Get the object back
示例#21
0
#!/usr/bin/python3

import os
import time
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
db = "wordnet"
user = "******"
account = "admin"
key = "root"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)
#client.delete_database(db)

try:
    client.create_database(db,
                           account,
                           label="WordNet",
                           description="WordNet in TerminusDB",
                           include_schema=False)
except:
    pass

times = []
directory = 'wordnet_chunks'
for f in os.listdir(directory):
    filename = f'{directory}/{f}'
    ttl_file = open(filename)
    contents = ttl_file.read()
示例#22
0
#!/usr/bin/python3

import os
import time
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
db = "DBPedia_squash"
user = "******"
account = "admin"
key = "root"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

# client.branch("squash", empty=True)
client.checkout("squash")
query = WQ().select().woql_and(
    WQ().using(f"{account}/{db}").triple("v:X","v:Y","v:Z"),
    WQ().add_triple("v:X","v:Y","v:Z"))
client.query(query, "Squash commit")
示例#23
0
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery

server_url = "https://127.0.0.1:6363"
client = WOQLClient(server_url)
client.connect(user="******", account="admin", key="root", db="bike")

client.delete_database("bike")
client.create_database("bike","admin",label="Bikes", description="description")

query = WOQLQuery().get(
    WOQLQuery().woql_as("Start station","v:Start_Station")
        .woql_as("End station", "v:End_Station")
        .woql_as("Start date", "v:Start_Time")
        .woql_as("End date", "v:End_Time")
        .woql_as("Duration", "v:Duration")
        .woql_as("Start station number", "v:Start_ID")
        .woql_as("End station number", "v:End_ID")
        .woql_as("Bike number", "v:Bike")
        .woql_as("Member type", "v:Member_Type")
).post("bike_csv")
result = client.query(query,
                      "This is a commit string",
                      {'bike_csv' : '/home/gavin/tmp/bike_tutorial.csv'})
示例#24
0
#!/usr/bin/python3

import os
import time
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

# Assumes database already exists.

server_url = "https://127.0.0.1:6363"
db = "DBPedia"
user = "******"
account = "admin"
key = "root"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

client.branch('properties')
client.checkout('properties')
times = []
directory = 'properties_200k'  # 'properties_100k'
for f in os.listdir(directory):
    filename = f'{directory}/{f}'
    ttl_file = open(filename)
    contents = ttl_file.read()
    ttl_file.close()
    before = time.time()
    client.insert_triples("instance", "main", contents,
                          f"Adding properties in 100k chunk ({f})")
    after = time.time()
    total = (after - before)
示例#25
0
from terminusdb_client import WOQLClient
from terminusdb_client.woqlschema import WOQLSchema

# For Terminus X, use the following
# client = WOQLClient("https://cloud.terminusdb.com/<Your Team>/")
# client.connect(db="demo_workshop", team="<Your Team>", use_token=True)

client = WOQLClient("http://127.0.0.1:6363/")
client.connect(db="getting_started")

data_schema = WOQLSchema()
data_schema.from_db(client)

# Update a document

destiny_raw = client.get_document("Employee/001")
destiny = data_schema.import_objects(destiny_raw)

destiny.address.postcode = "PH12 3RP"
destiny.address.street = "Lairg Road"
destiny.address.street_num = 73
destiny.address.town = "Newbigging"

client.update_document(destiny, commit_msg="Update Destiny")

# Linking a new document to an old document

Employee = data_schema.object.get("Employee")
Address = data_schema.object.get("Address")
Team = data_schema.object.get("Team")
#!/usr/bin/python3

from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
user = "******"
account = "admin"
key = "root"
db = "roster"
repository = "local"
label = "Roster CSV Example"
description = "An example database for playing with bank accounts"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

#client.delete_database(db)
try:
    client.create_database(db,account,label=label,
                           description=description,
                           include_schema=None)
except Exception as E:
    pass

query = WQ().woql_and(
    WQ().get(WQ().woql_as("Name","v:Name")
                 .woql_as("Registration_Date", "v:Date")
                 .woql_as("Paid", "v:Paid")
            ).post('roster.csv'),
    WQ().idgen("doc:RosterRecord",["v:Name","v:Date","v:Paid"],"v:ID"),
    WQ().add_triple("v:ID","scm:name","v:Name"),
示例#27
0
from terminusdb_client import WOQLClient
from terminusdb_client.woqlschema import WOQLSchema

# For Terminus X, use the following
# client = WOQLClient("https://cloud.terminusdb.com/<Your Team>/")
# client.connect(db="demo_workshop", team="<Your Team>", use_token=True)

client = WOQLClient("http://127.0.0.1:6363/")
client.connect(db="getting_started", branch="contractors")

data_schema = WOQLSchema()
data_schema.from_db(client)

Employee = data_schema.object.get("Employee")
Address = data_schema.object.get("Address")
Team = data_schema.object.get("Team")

# Contractor 1

rhys_address = Address(postcode="DG4 2ZQ",
                       street="Helland Bridge",
                       street_num=1,
                       town="Ulzieside")

rhys = Employee(
    _id="Employee/006",
    name="Rhys Arnold",
    title="UX Designer",
    team=Team.it,
    contact_number="078 3951 7569",
    address=rhys_address,
#!/usr/bin/python3

import os
import time
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WQ

server_url = "https://127.0.0.1:6363"
db = "DBPedia_optimize"
db_label = "DBPedia speed test"
db_comment = "Testing new synchronous writes + optimize"
user = "******"
account = "admin"
key = "root"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)


def optimizer(client):
    client.optimize(f'{account}/{db}/_meta')
    client.optimize(f'{account}/{db}/local/_commits')


try:
    client.delete_database(db)
except Exception as E:
    print(E)

client.create_database(db,
                       account,
                       label=db_label,
#!/usr/bin/python3

import os
import time
import datetime
from terminusdb_client import WOQLClient
from terminusdb_client import WOQLQuery as WOQL

# Connect to the server
server_url = "https://127.0.0.1:6363"
db = "dbpedia_1"
user = "******"
account = "admin"
key = "root"
client = WOQLClient(server_url)
client.connect(user=user, account=account, key=key, db=db)

# Example of a single hop
query = WOQL().limit(
    1,
    WOQL.woql_and(
        WOQL().path("doc:Whitesnake", "scm:bandMember", "v:Member",
                    "v:Path"), ))
result = client.query(query)
print(f"Band member: {result['bindings'][0]['Member']}")

query = WOQL().limit(
    1,
    WOQL.woql_and(
        WOQL().path("doc:Whitesnake", "scm:bandMember,scm:birthPlace",
                    "v:Place", "v:Path"), ))
            street_num=street_num, street=street_name, town=town, postcode=row[3]
        )

with open("Employees.csv") as file:
    csv_file = csv.reader(file)
    next(csv_file)  # skiping header
    for row in csv_file:
        team = eval(f"Team.{row[3].lower()}")
        employees[row[0]] = Employee(
            _id="Employee/" + row[0],
            name=row[1],
            title=row[2],
            address=addresses[row[0]],
            contact_number=contact_numbers[row[0]],
            team = team
        )
        managers[row[0]] = row[4]

for emp_id, man_id in managers.items():
    if man_id:
        employees[emp_id].manager = employees[man_id]

# For Terminus X, use the following
# client = WOQLClient("https://cloud.terminusdb.com/<Your Team>/")
# client.connect(db="demo_workshop", team="<Your Team>", use_token=True)

client = WOQLClient("http://127.0.0.1:6363/")
client.connect(db="getting_started")

client.insert_document(list(employees.values()), commit_msg="Adding 4 Employees")