e-bot7 Sentiment Analysis

The e-bot7 Sentiment Analysis Service is provided as a simple RESTful interface.

The only dependencies needed to call it are an HTTP client and json for encoding/decoding the payload.


In [10]:
from http.client import HTTPSConnection
import json

Example: Direct call to API

In this first example we demonstrate a single call to the interface.


In [40]:
conn = HTTPSConnection('sentiment.e-bot7.de')

headers = { 'Content-Type': 'application/json' }

data = {
    "documents": [
        {
            "id": 1,
             "text": "Ich finde das super."
        },
        {
            "id": 2,
            "text": "Es ist sehr schön, aber hat mir überhaupt nicht geholfen."
        }
    ]
}

try:
    conn.request("POST", "/sentiment", json.dumps(data), headers)
    result = json.loads(conn.getresponse().read().decode('ascii'))
    print(result)
    conn.close()
except Exception as e:
    print(e)


{'documents': [{'id': '1', 'score': 0.920766}, {'id': '2', 'score': 0.287722}]}

Example: Convenience function for calling the API

As another example we define a simple function that can be used to obtain the sentiment prediction for a list of texts.


In [18]:
def get_sentiment(texts):
    conn = HTTPSConnection('sentiment.e-bot7.de')

    headers = { 'Content-Type': 'application/json' }

    docs = [dict(id=i,text=text) for i,text in enumerate(texts)]
    data = { "documents": docs }

    conn.request("POST", "/sentiment", json.dumps(data), headers)
    result = json.loads(conn.getresponse().read().decode('ascii'))
    conn.close()
    
    for doc in result["documents"]:
        i = int(doc["id"])
        docs[i]["score"] = doc["score"]
    
    return docs

Here is that function in action.


In [57]:
docs = [
    "Sehr hilfreich und macht sogar Spaß!",
    "Der Prozess ist langsam und kostet einen viele Nerven."
]

for doc in get_sentiment(docs):
    print('Text: {}'.format(doc["text"]))
    print('Score: {:.3f}'.format(doc["score"]))
    print()


Text: Sehr hilfreich und macht sogar Spaß!
Score: 0.913

Text: Der Prozess ist langsam und kostet einen viele Nerven.
Score: 0.266