Skip to content

Instantly share code, notes, and snippets.

@linuskohl
Last active June 4, 2019 15:04
Show Gist options
  • Save linuskohl/fb6461eff2a33461ceade2de8460620f to your computer and use it in GitHub Desktop.
Save linuskohl/fb6461eff2a33461ceade2de8460620f to your computer and use it in GitHub Desktop.
Sample code for medium article
# import client and input model
from ambiverseclient.clients import AmbiverseNLU, KnowledgeGraph
from ambiverseclient.models import AnalyzeInput
# specify the API endpoints
a_endpoint = AmbiverseNLU_ENDPOINT
kg_endpoint = KnowledgeGraph_ENDPOINT
# setup the clients
a_client = AmbiverseNLU(a_endpoint, port=8080)
kg_client = KnowledgeGraph(kg_endpoint, port=8080)
# generate request
request_doc = AnalyzeInput(docId="test", language="en")
request_doc.text = """Brexit: UBS to move London jobs to Europe as
lack of transition deal forces 'significant
changes' Swiss banking giant expects to merge
UK entity with its German-headquartered
European ..."""
# send request
result = a_client.analyze(request_doc)
# result:
# <AnalyzeOutput(docId='test', language='en',
# matches=[
# <Match(charLength=3, charOffset=8, text='UBS')>,
# <Match(charLength=6, charOffset=20, text='London')>,
# <Match(charLength=6, charOffset=35, text='Europe')>,
# <Match(charLength=2, charOffset=157, text='UK')>
# ],
# entities=[
# <OutputEntity(id='http://www.wikidata.org/entity/Q84',
# salience=0.5508285629605183,
# type='LOCATION',
# name='London')>,
# <OutputEntity(id='http://www.wikidata.org/entity/Q193199',
# salience=0.7585665045660286,
# type='ORGANIZATION',
# name='UBS')>,
# <OutputEntity(id='http://www.wikidata.org/entity/Q46',
# salience=0.5606302485874791,
# type='LOCATION',
# name='Europe')>,
# <OutputEntity(id='http://www.wikidata.org/entity/Q145',
# salience=0.1973288954864318,
# type='LOCATION',
# name='United Kingdom')>
# ])>
entity_ids = [entity.id for entity in res.entities]
entities = kg.entities(entity_ids)
# entities:
# {'entities': {
# 'http://www.wikidata.org/entity/Q84': <Entity(
# id='http://www.wikidata.org/entity/Q84',
# type=None,
# names={'en': <Label(language='en', value='London')>,
# 'de': <Label(language='de', value='London')>})>,
# 'http://www.wikidata.org/entity/Q193199': <Entity(
# id='http://www.wikidata.org/entity/Q193199',
# type=None, names={'en': <Label(language='en', value='UBS')>,
# 'de': <Label(language='de', value='UBS')>})>,
# 'http://www.wikidata.org/entity/Q46': <Entity(
# id='http://www.wikidata.org/entity/Q46',
# type='LOCATION', names={'en': <Label(language='en', value='Europe')>,
# 'de': <Label(language='de', value='Europa')>})>,
# 'http://www.wikidata.org/entity/Q145': <Entity(
# id='http://www.wikidata.org/entity/Q145',
# type=None, names={'en': <Label(language='en', value='United Kingdom')>,
# 'de': <Label(language='de', value='Vereinigtes Königreich')>})>
# }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment