{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Senpy in 10 minutes\n", "\n", "This tutorial assumes you have completed the [1 minute tutorial](Quickstart-1minute.ipynb) and you know how to access the API.\n", "\n", "It covers:\n", "\n", "* Annotating entiment with a sentiment analysis service\n", "* Annotating emotions\n", "* Interoperability of services\n", "* Using other semantic formats (RDF)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Requirements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once again, we will use the demo server at http://senpy.gsi.upm.es.\n", "This time, we will use the requests library.\n", "\n", "We will use a variable for our endpoint, so you can try this examples on a different senpy instance:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "endpoint = 'http://senpy.gsi.upm.es/api'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will also add a function to get syntax highlighting for the JSON-LD/Turtle results:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "try:\n", " from IPython.display import Code\n", " def pretty(txt, language='json-ld'):\n", " return Code(txt, language=language)\n", "except ImportError:\n", " def pretty(txt, **kwargs):\n", " print(txt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sentiment Analysis of Text" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To start, let us analyse the sentiment in the following sentence: *senpy is awesome*.\n", "\n", "For now, we will use the [sentiment140](http://www.sentiment140.com/) service, through the sentiment140 plugin.\n", "We will later cover how to use a different service.\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "  "@context": "http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lIw%3D%3D",\n",
       "  "@type": "Results",\n",
       "  "entries": [\n",
       "    {\n",
       "      "@id": "prefix:",\n",
       "      "@type": "Entry",\n",
       "      "marl:hasOpinion": [\n",
       "        {\n",
       "          "@type": "Sentiment",\n",
       "          "marl:hasPolarity": "marl:Positive",\n",
       "          "prov:wasGeneratedBy": "prefix:Analysis_1554396807.629645"\n",
       "        }\n",
       "      ],\n",
       "      "nif:isString": "Senpy is awesome",\n",
       "      "onyx:hasEmotionSet": []\n",
       "    }\n",
       "  ]\n",
       "}\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@context\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lIw\\PYZpc{}3D\\PYZpc{}3D\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Results\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}entries\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@id\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Entry\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasOpinion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Sentiment\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasPolarity\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}marl:Positive\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}prov:wasGeneratedBy\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:Analysis\\PYZus{}1554396807.629645\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}nif:isString\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Senpy is awesome\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionSet\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\\PY{p}{]}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", "\\PY{p}{\\PYZcb{}}\n", "\\end{Verbatim}\n" ], "text/plain": [ "{\n", " \"@context\": \"http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lIw%3D%3D\",\n", " \"@type\": \"Results\",\n", " \"entries\": [\n", " {\n", " \"@id\": \"prefix:\",\n", " \"@type\": \"Entry\",\n", " \"marl:hasOpinion\": [\n", " {\n", " \"@type\": \"Sentiment\",\n", " \"marl:hasPolarity\": \"marl:Positive\",\n", " \"prov:wasGeneratedBy\": \"prefix:Analysis_1554396807.629645\"\n", " }\n", " ],\n", " \"nif:isString\": \"Senpy is awesome\",\n", " \"onyx:hasEmotionSet\": []\n", " }\n", " ]\n", "}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "res = requests.get(f'{endpoint}/sentiment140',\n", " params={\"input\": \"Senpy is awesome\",})\n", "pretty(res.text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Senpy services always return an object of type `senpy:Results`, with a list of entries.\n", "You can think of an entry as a self-contained textual context (`nif:Context` and `senpy:Entry`).\n", "Entries can be as short as a sentence, or as long as a news article.\n", "\n", "Each entry has a `nif:isString` property that contains the original text of the entry, and several other properties that are provided by the plugins.\n", "\n", "For instance, sentiment annotations are provided through `marl:hasOpinion`.\n", "\n", "The annotations are semantic.\n", "We can ask Senpy for the expanded JSON-LD output to reveal the full URIs of each property and entity:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "  "@context": "http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lJmV4cGFuZGVkPVRydWUj",\n",
       "  "@type": [\n",
       "    "http://www.gsi.upm.es/onto/senpy/ns#Results"\n",
       "  ],\n",
       "  "http://www.w3.org/ns/prov#used": [\n",
       "    {\n",
       "      "@id": "http://senpy.invalid/",\n",
       "      "@type": [\n",
       "        "http://www.gsi.upm.es/onto/senpy/ns#Entry"\n",
       "      ],\n",
       "      "http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#isString": [\n",
       "        {\n",
       "          "@value": "Senpy is awesome"\n",
       "        }\n",
       "      ],\n",
       "      "http://www.gsi.dit.upm.es/ontologies/marl/ns#hasOpinion": [\n",
       "        {\n",
       "          "@type": [\n",
       "            "http://www.gsi.upm.es/onto/senpy/ns#Sentiment"\n",
       "          ],\n",
       "          "http://www.gsi.dit.upm.es/ontologies/marl/ns#hasPolarity": [\n",
       "            {\n",
       "              "@value": "marl:Positive"\n",
       "            }\n",
       "          ],\n",
       "          "http://www.w3.org/ns/prov#wasGeneratedBy": [\n",
       "            {\n",
       "              "@id": "http://senpy.invalid/Analysis_1554389672.269198"\n",
       "            }\n",
       "          ]\n",
       "        }\n",
       "      ],\n",
       "      "http://www.gsi.dit.upm.es/ontologies/onyx/ns#hasEmotionSet": []\n",
       "    }\n",
       "  ]\n",
       "}\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@context\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lJmV4cGFuZGVkPVRydWUj\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{l+s+s2}{\\PYZdq{}http://www.gsi.upm.es/onto/senpy/ns\\PYZsh{}Results\\PYZdq{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}http://www.w3.org/ns/prov\\PYZsh{}used\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@id\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.invalid/\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{l+s+s2}{\\PYZdq{}http://www.gsi.upm.es/onto/senpy/ns\\PYZsh{}Entry\\PYZdq{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}http://persistence.uni\\PYZhy{}leipzig.org/nlp2rdf/ontologies/nif\\PYZhy{}core\\PYZsh{}isString\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@value\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Senpy is awesome\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}http://www.gsi.dit.upm.es/ontologies/marl/ns\\PYZsh{}hasOpinion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{l+s+s2}{\\PYZdq{}http://www.gsi.upm.es/onto/senpy/ns\\PYZsh{}Sentiment\\PYZdq{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}http://www.gsi.dit.upm.es/ontologies/marl/ns\\PYZsh{}hasPolarity\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@value\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}marl:Positive\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}http://www.w3.org/ns/prov\\PYZsh{}wasGeneratedBy\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@id\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.invalid/Analysis\\PYZus{}1554389672.269198\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}http://www.gsi.dit.upm.es/ontologies/onyx/ns\\PYZsh{}hasEmotionSet\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\\PY{p}{]}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", "\\PY{p}{\\PYZcb{}}\n", "\\end{Verbatim}\n" ], "text/plain": [ "{\n", " \"@context\": \"http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lJmV4cGFuZGVkPVRydWUj\",\n", " \"@type\": [\n", " \"http://www.gsi.upm.es/onto/senpy/ns#Results\"\n", " ],\n", " \"http://www.w3.org/ns/prov#used\": [\n", " {\n", " \"@id\": \"http://senpy.invalid/\",\n", " \"@type\": [\n", " \"http://www.gsi.upm.es/onto/senpy/ns#Entry\"\n", " ],\n", " \"http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#isString\": [\n", " {\n", " \"@value\": \"Senpy is awesome\"\n", " }\n", " ],\n", " \"http://www.gsi.dit.upm.es/ontologies/marl/ns#hasOpinion\": [\n", " {\n", " \"@type\": [\n", " \"http://www.gsi.upm.es/onto/senpy/ns#Sentiment\"\n", " ],\n", " \"http://www.gsi.dit.upm.es/ontologies/marl/ns#hasPolarity\": [\n", " {\n", " \"@value\": \"marl:Positive\"\n", " }\n", " ],\n", " \"http://www.w3.org/ns/prov#wasGeneratedBy\": [\n", " {\n", " \"@id\": \"http://senpy.invalid/Analysis_1554389672.269198\"\n", " }\n", " ]\n", " }\n", " ],\n", " \"http://www.gsi.dit.upm.es/ontologies/onyx/ns#hasEmotionSet\": []\n", " }\n", " ]\n", "}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "res = requests.get(f'{endpoint}/sentiment140',\n", " params={\"input\": \"Senpy is awesome\",\n", " \"expanded\": True})\n", "pretty(res.text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Service interoperability" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All senpy plugins use the same API, which makes moving from one service to another a breeze.\n", "\n", "Let us go back to our simple example, which uses the `sentiment140` service." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "  "@context": "http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lIw%3D%3D",\n",
       "  "@type": "Results",\n",
       "  "entries": [\n",
       "    {\n",
       "      "@id": "prefix:",\n",
       "      "@type": "Entry",\n",
       "      "marl:hasOpinion": [\n",
       "        {\n",
       "          "@type": "Sentiment",\n",
       "          "marl:hasPolarity": "marl:Positive",\n",
       "          "prov:wasGeneratedBy": "prefix:Analysis_1554389675.2303865"\n",
       "        }\n",
       "      ],\n",
       "      "nif:isString": "Senpy is awesome",\n",
       "      "onyx:hasEmotionSet": []\n",
       "    }\n",
       "  ]\n",
       "}\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@context\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lIw\\PYZpc{}3D\\PYZpc{}3D\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Results\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}entries\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@id\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Entry\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasOpinion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Sentiment\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasPolarity\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}marl:Positive\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}prov:wasGeneratedBy\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:Analysis\\PYZus{}1554389675.2303865\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}nif:isString\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Senpy is awesome\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionSet\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\\PY{p}{]}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", "\\PY{p}{\\PYZcb{}}\n", "\\end{Verbatim}\n" ], "text/plain": [ "{\n", " \"@context\": \"http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudDE0MD9pbnB1dD1TZW5weStpcythd2Vzb21lIw%3D%3D\",\n", " \"@type\": \"Results\",\n", " \"entries\": [\n", " {\n", " \"@id\": \"prefix:\",\n", " \"@type\": \"Entry\",\n", " \"marl:hasOpinion\": [\n", " {\n", " \"@type\": \"Sentiment\",\n", " \"marl:hasPolarity\": \"marl:Positive\",\n", " \"prov:wasGeneratedBy\": \"prefix:Analysis_1554389675.2303865\"\n", " }\n", " ],\n", " \"nif:isString\": \"Senpy is awesome\",\n", " \"onyx:hasEmotionSet\": []\n", " }\n", " ]\n", "}" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "res = requests.get(f'{endpoint}/sentiment140',\n", " params={\"input\": \"Senpy is awesome\"})\n", "pretty(res.text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we wanted to use a different service (e.g. the `sentiment-basic` plugin), we can do it just by changing the URL of the service:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
{\n",
       "  "@context": "http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudC1iYXNpYz9pbnB1dD1TZW5weStpcythd2Vzb21lIw%3D%3D",\n",
       "  "@type": "Results",\n",
       "  "entries": [\n",
       "    {\n",
       "      "@id": "prefix:",\n",
       "      "@type": "Entry",\n",
       "      "marl:hasOpinion": [\n",
       "        {\n",
       "          "@type": "Sentiment",\n",
       "          "marl:hasPolarity": "marl:Neutral",\n",
       "          "prov:wasGeneratedBy": "prefix:Analysis_1554389675.9911203"\n",
       "        }\n",
       "      ],\n",
       "      "nif:isString": "Senpy is awesome",\n",
       "      "onyx:hasEmotionSet": []\n",
       "    }\n",
       "  ]\n",
       "}\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@context\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudC1iYXNpYz9pbnB1dD1TZW5weStpcythd2Vzb21lIw\\PYZpc{}3D\\PYZpc{}3D\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Results\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}entries\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@id\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Entry\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasOpinion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Sentiment\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasPolarity\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}marl:Neutral\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}prov:wasGeneratedBy\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:Analysis\\PYZus{}1554389675.9911203\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}nif:isString\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Senpy is awesome\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionSet\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\\PY{p}{]}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", "\\PY{p}{\\PYZcb{}}\n", "\\end{Verbatim}\n" ], "text/plain": [ "{\n", " \"@context\": \"http://senpy.gsi.upm.es/api/contexts/YXBpL3NlbnRpbWVudC1iYXNpYz9pbnB1dD1TZW5weStpcythd2Vzb21lIw%3D%3D\",\n", " \"@type\": \"Results\",\n", " \"entries\": [\n", " {\n", " \"@id\": \"prefix:\",\n", " \"@type\": \"Entry\",\n", " \"marl:hasOpinion\": [\n", " {\n", " \"@type\": \"Sentiment\",\n", " \"marl:hasPolarity\": \"marl:Neutral\",\n", " \"prov:wasGeneratedBy\": \"prefix:Analysis_1554389675.9911203\"\n", " }\n", " ],\n", " \"nif:isString\": \"Senpy is awesome\",\n", " \"onyx:hasEmotionSet\": []\n", " }\n", " ]\n", "}" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import requests\n", "res = requests.get(f'{endpoint}/sentiment-basic',\n", " params={\"input\": \"Senpy is awesome\"})\n", "pretty(res.text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, the structure and annotation schema of the response is the same.\n", "This makes it very easy to compare and migrate to different services.\n", "\n", "Service interoperability is not only useful for users.\n", "It is also key for other features such as [automated evaluation](#Evaluation).\n", "This is a compelling reason to adapt existing services to use the Senpy API.\n", "In fact, the `sentiment140` senpy service is proxy to the public [Sentiment 140 service](http://www.sentiment140.com/)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Emotion analysis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "Senpy uses the `onyx` vocabulary to represent emotions, which incorporates the notion of `EmotionSet`'s, an emotion that is composed of several emotions.\n", "In a nutshell, an `Entry` is linked to one or more `EmotionSet`, which in turn is made up of one or more `Emotion`.\n", "\n", "Let's illustrate it with an example, using the `emotion-depechemood` plugin." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "  "@context": "http://senpy.gsi.upm.es/api/contexts/YXBpL2Vtb3Rpb24tZGVwZWNoZW1vb2Q_aW5wdXQ9U2VucHkraXMrYSt3b25kZXJmdWwrdGhhdCtzZXJ2aWNlIw%3D%3D",\n",
       "  "@type": "Results",\n",
       "  "entries": [\n",
       "    {\n",
       "      "@id": "prefix:",\n",
       "      "@type": "Entry",\n",
       "      "marl:hasOpinion": [],\n",
       "      "nif:isString": "Senpy is a wonderful that service",\n",
       "      "onyx:hasEmotionSet": [\n",
       "        {\n",
       "          "@type": "EmotionSet",\n",
       "          "onyx:hasEmotion": [\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:negative-fear",\n",
       "              "onyx:hasEmotionIntensity": 0.06258366271018097\n",
       "            },\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:amusement",\n",
       "              "onyx:hasEmotionIntensity": 0.15784834034155437\n",
       "            },\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:anger",\n",
       "              "onyx:hasEmotionIntensity": 0.08728815135373413\n",
       "            },\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:annoyance",\n",
       "              "onyx:hasEmotionIntensity": 0.12184635680460143\n",
       "            },\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:indifference",\n",
       "              "onyx:hasEmotionIntensity": 0.1374081151031531\n",
       "            },\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:joy",\n",
       "              "onyx:hasEmotionIntensity": 0.12267040802346799\n",
       "            },\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:awe",\n",
       "              "onyx:hasEmotionIntensity": 0.21085262130713067\n",
       "            },\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:sadness",\n",
       "              "onyx:hasEmotionIntensity": 0.09950234435617733\n",
       "            }\n",
       "          ],\n",
       "          "prov:wasGeneratedBy": "prefix:Analysis_1554389679.0535374"\n",
       "        }\n",
       "      ]\n",
       "    }\n",
       "  ]\n",
       "}\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@context\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.gsi.upm.es/api/contexts/YXBpL2Vtb3Rpb24tZGVwZWNoZW1vb2Q\\PYZus{}aW5wdXQ9U2VucHkraXMrYSt3b25kZXJmdWwrdGhhdCtzZXJ2aWNlIw\\PYZpc{}3D\\PYZpc{}3D\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Results\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}entries\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@id\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Entry\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasOpinion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}nif:isString\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Senpy is a wonderful that service\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionSet\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}EmotionSet\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:negative\\PYZhy{}fear\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.06258366271018097}\n", " \\PY{p}{\\PYZcb{}}\\PY{p}{,}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:amusement\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.15784834034155437}\n", " \\PY{p}{\\PYZcb{}}\\PY{p}{,}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:anger\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.08728815135373413}\n", " \\PY{p}{\\PYZcb{}}\\PY{p}{,}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:annoyance\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.12184635680460143}\n", " \\PY{p}{\\PYZcb{}}\\PY{p}{,}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:indifference\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.1374081151031531}\n", " \\PY{p}{\\PYZcb{}}\\PY{p}{,}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:joy\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.12267040802346799}\n", " \\PY{p}{\\PYZcb{}}\\PY{p}{,}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:awe\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.21085262130713067}\n", " \\PY{p}{\\PYZcb{}}\\PY{p}{,}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:sadness\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.09950234435617733}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}prov:wasGeneratedBy\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:Analysis\\PYZus{}1554389679.0535374\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", "\\PY{p}{\\PYZcb{}}\n", "\\end{Verbatim}\n" ], "text/plain": [ "{\n", " \"@context\": \"http://senpy.gsi.upm.es/api/contexts/YXBpL2Vtb3Rpb24tZGVwZWNoZW1vb2Q_aW5wdXQ9U2VucHkraXMrYSt3b25kZXJmdWwrdGhhdCtzZXJ2aWNlIw%3D%3D\",\n", " \"@type\": \"Results\",\n", " \"entries\": [\n", " {\n", " \"@id\": \"prefix:\",\n", " \"@type\": \"Entry\",\n", " \"marl:hasOpinion\": [],\n", " \"nif:isString\": \"Senpy is a wonderful that service\",\n", " \"onyx:hasEmotionSet\": [\n", " {\n", " \"@type\": \"EmotionSet\",\n", " \"onyx:hasEmotion\": [\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:negative-fear\",\n", " \"onyx:hasEmotionIntensity\": 0.06258366271018097\n", " },\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:amusement\",\n", " \"onyx:hasEmotionIntensity\": 0.15784834034155437\n", " },\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:anger\",\n", " \"onyx:hasEmotionIntensity\": 0.08728815135373413\n", " },\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:annoyance\",\n", " \"onyx:hasEmotionIntensity\": 0.12184635680460143\n", " },\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:indifference\",\n", " \"onyx:hasEmotionIntensity\": 0.1374081151031531\n", " },\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:joy\",\n", " \"onyx:hasEmotionIntensity\": 0.12267040802346799\n", " },\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:awe\",\n", " \"onyx:hasEmotionIntensity\": 0.21085262130713067\n", " },\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:sadness\",\n", " \"onyx:hasEmotionIntensity\": 0.09950234435617733\n", " }\n", " ],\n", " \"prov:wasGeneratedBy\": \"prefix:Analysis_1554389679.0535374\"\n", " }\n", " ]\n", " }\n", " ]\n", "}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = requests.get(f'{endpoint}/emotion-depechemood',\n", " params={\"input\": \"Senpy is a wonderful that service\"})\n", "pretty(res.text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you have probably noticed, there are several emotions in this result, each with a different intensity.\n", "\n", "We can also tell senpy to only return the emotion with the maximum intensity using the `maxemotion` parameter:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
{\n",
       "  "@context": "http://senpy.gsi.upm.es/api/contexts/YXBpL2Vtb3Rpb24tZGVwZWNoZW1vb2Q_aW5wdXQ9U2VucHkraXMrYSt3b25kZXJmdWwrc2VydmljZSZtYXhlbW90aW9uPVRydWUj",\n",
       "  "@type": "Results",\n",
       "  "entries": [\n",
       "    {\n",
       "      "@id": "prefix:",\n",
       "      "@type": "Entry",\n",
       "      "marl:hasOpinion": [],\n",
       "      "nif:isString": "Senpy is a wonderful service",\n",
       "      "onyx:hasEmotionSet": [\n",
       "        {\n",
       "          "@type": "EmotionSet",\n",
       "          "onyx:hasEmotion": [\n",
       "            {\n",
       "              "@type": "Emotion",\n",
       "              "onyx:hasEmotionCategory": "wna:awe",\n",
       "              "onyx:hasEmotionIntensity": 0.21085262130713067\n",
       "            }\n",
       "          ],\n",
       "          "prov:wasGeneratedBy": "prefix:Analysis_1554389681.1941268"\n",
       "        }\n",
       "      ]\n",
       "    }\n",
       "  ]\n",
       "}\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@context\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}http://senpy.gsi.upm.es/api/contexts/YXBpL2Vtb3Rpb24tZGVwZWNoZW1vb2Q\\PYZus{}aW5wdXQ9U2VucHkraXMrYSt3b25kZXJmdWwrc2VydmljZSZtYXhlbW90aW9uPVRydWUj\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Results\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}entries\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@id\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Entry\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}marl:hasOpinion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}nif:isString\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Senpy is a wonderful service\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionSet\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}EmotionSet\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotion\\PYZdq{}}\\PY{p}{:} \\PY{p}{[}\n", " \\PY{p}{\\PYZob{}}\n", " \\PY{n+nd}{\\PYZdq{}@type\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}Emotion\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionCategory\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}wna:awe\\PYZdq{}}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}onyx:hasEmotionIntensity\\PYZdq{}}\\PY{p}{:} \\PY{l+m+mf}{0.21085262130713067}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\\PY{p}{,}\n", " \\PY{n+nt}{\\PYZdq{}prov:wasGeneratedBy\\PYZdq{}}\\PY{p}{:} \\PY{l+s+s2}{\\PYZdq{}prefix:Analysis\\PYZus{}1554389681.1941268\\PYZdq{}}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", " \\PY{p}{\\PYZcb{}}\n", " \\PY{p}{]}\n", "\\PY{p}{\\PYZcb{}}\n", "\\end{Verbatim}\n" ], "text/plain": [ "{\n", " \"@context\": \"http://senpy.gsi.upm.es/api/contexts/YXBpL2Vtb3Rpb24tZGVwZWNoZW1vb2Q_aW5wdXQ9U2VucHkraXMrYSt3b25kZXJmdWwrc2VydmljZSZtYXhlbW90aW9uPVRydWUj\",\n", " \"@type\": \"Results\",\n", " \"entries\": [\n", " {\n", " \"@id\": \"prefix:\",\n", " \"@type\": \"Entry\",\n", " \"marl:hasOpinion\": [],\n", " \"nif:isString\": \"Senpy is a wonderful service\",\n", " \"onyx:hasEmotionSet\": [\n", " {\n", " \"@type\": \"EmotionSet\",\n", " \"onyx:hasEmotion\": [\n", " {\n", " \"@type\": \"Emotion\",\n", " \"onyx:hasEmotionCategory\": \"wna:awe\",\n", " \"onyx:hasEmotionIntensity\": 0.21085262130713067\n", " }\n", " ],\n", " \"prov:wasGeneratedBy\": \"prefix:Analysis_1554389681.1941268\"\n", " }\n", " ]\n", " }\n", " ]\n", "}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = requests.get(f'{endpoint}/emotion-depechemood',\n", " params={\"input\": \"Senpy is a wonderful service\",\n", " \"maxemotion\": True})\n", "pretty(res.text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Other output formats" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Senpy supports several semantic formats, like turtle and xml-RDF.\n", "You can select the format of the output with the `outformat` parameter:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
@prefix : <http://www.gsi.upm.es/onto/senpy/ns#> .\n",
       "@prefix dc: <http://dublincore.org/2012/06/14/dcelements#> .\n",
       "@prefix emoml: <http://www.gsi.dit.upm.es/ontologies/onyx/vocabularies/emotionml/ns#> .\n",
       "@prefix endpoint: <http://senpy.gsi.upm.es/api/> .\n",
       "@prefix fam: <http://vocab.fusepool.info/fam#> .\n",
       "@prefix marl: <http://www.gsi.dit.upm.es/ontologies/marl/ns#> .\n",
       "@prefix nif: <http://persistence.uni-leipzig.org/nlp2rdf/ontologies/nif-core#> .\n",
       "@prefix onyx: <http://www.gsi.dit.upm.es/ontologies/onyx/ns#> .\n",
       "@prefix prefix: <http://senpy.invalid/> .\n",
       "@prefix prov: <http://www.w3.org/ns/prov#> .\n",
       "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n",
       "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n",
       "@prefix senpy: <http://www.gsi.upm.es/onto/senpy/ns#> .\n",
       "@prefix wna: <http://www.gsi.dit.upm.es/ontologies/wnaffect/ns#> .\n",
       "@prefix xml: <http://www.w3.org/XML/1998/namespace> .\n",
       "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n",
       "\n",
       "prefix: a senpy:Entry ;\n",
       "    nif:isString "Senpy is the best framework for semantic sentiment analysis, and very easy to use" ;\n",
       "    marl:hasOpinion [ a senpy:Sentiment ;\n",
       "            marl:hasPolarity "marl:Positive" ;\n",
       "            prov:wasGeneratedBy prefix:Analysis_1554389682.350598 ] .\n",
       "\n",
       "[] a senpy:Results ;\n",
       "    prov:used prefix: .\n",
       "
\n" ], "text/latex": [ "\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.gsi.upm.es/onto/senpy/ns\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{dc:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://dublincore.org/2012/06/14/dcelements\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{emoml:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.gsi.dit.upm.es/ontologies/onyx/vocabularies/emotionml/ns\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{endpoint:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://senpy.gsi.upm.es/api/\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{fam:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://vocab.fusepool.info/fam\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{marl:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.gsi.dit.upm.es/ontologies/marl/ns\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{nif:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://persistence.uni\\PYZhy{}leipzig.org/nlp2rdf/ontologies/nif\\PYZhy{}core\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{onyx:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.gsi.dit.upm.es/ontologies/onyx/ns\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{prefix:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://senpy.invalid/\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{prov:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.w3.org/ns/prov\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{rdf:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.w3.org/1999/02/22\\PYZhy{}rdf\\PYZhy{}syntax\\PYZhy{}ns\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{rdfs:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.w3.org/2000/01/rdf\\PYZhy{}schema\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{senpy:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.gsi.upm.es/onto/senpy/ns\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{wna:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.gsi.dit.upm.es/ontologies/wnaffect/ns\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{xml:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.w3.org/XML/1998/namespace\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\\PY{k}{@prefix}\\PY{+w}{ }\\PY{n+nn}{xsd:}\\PY{+w}{ }\\PY{n+nv}{\\PYZlt{}http://www.w3.org/2001/XMLSchema\\PYZsh{}\\PYZgt{}}\\PY{+w}{ }\\PY{p}{.}\n", "\n", "\\PY{err}{p}\\PY{err}{r}\\PY{err}{e}\\PY{err}{f}\\PY{err}{i}\\PY{err}{x}\\PY{p}{:}\\PY{+w}{ }\\PY{k+kt}{a}\\PY{+w}{ }\\PY{n+nn}{senpy:}\\PY{n+nt}{Entry}\\PY{+w}{ }\\PY{p}{;}\n", "\\PY{+w}{ }\\PY{n+nn}{nif:}\\PY{n+nt}{isString}\\PY{+w}{ }\\PY{l+s}{\\PYZdq{}}\\PY{l+s}{Senpy is the best framework for semantic sentiment analysis, and very easy to use}\\PY{l+s}{\\PYZdq{}}\\PY{+w}{ }\\PY{p}{;}\n", "\\PY{+w}{ }\\PY{n+nn}{marl:}\\PY{n+nt}{hasOpinion}\\PY{+w}{ }\\PY{p}{[}\\PY{+w}{ }\\PY{k+kt}{a}\\PY{+w}{ }\\PY{n+nn}{senpy:}\\PY{n+nt}{Sentiment}\\PY{+w}{ }\\PY{p}{;}\n", "\\PY{+w}{ }\\PY{n+nn}{marl:}\\PY{n+nt}{hasPolarity}\\PY{+w}{ }\\PY{l+s}{\\PYZdq{}}\\PY{l+s}{marl:Positive}\\PY{l+s}{\\PYZdq{}}\\PY{+w}{ }\\PY{p}{;}\n", "\\PY{+w}{ }\\PY{n+nn}{prov:}\\PY{n+nt}{wasGeneratedBy}\\PY{+w}{ }\\PY{n+nn}{prefix:}\\PY{n+nt}{Analysis\\PYZus{}1554389682}\\PY{l+m+mf}{.350598}\\PY{+w}{ }\\PY{p}{]}\\PY{+w}{ }\\PY{p}{.}\n", "\n", "\\PY{p}{[}\\PY{p}{]}\\PY{+w}{ }\\PY{k+kt}{a}\\PY{+w}{ }\\PY{n+nn}{senpy:}\\PY{n+nt}{Results}\\PY{+w}{ }\\PY{p}{;}\n", "\\PY{+w}{ }\\PY{n+nn}{prov:}\\PY{n+nt}{used}\\PY{+w}{ }\\PY{err}{p}\\PY{err}{r}\\PY{err}{e}\\PY{err}{f}\\PY{err}{i}\\PY{err}{x}\\PY{p}{:}\\PY{+w}{ }\\PY{p}{.}\n", "\\end{Verbatim}\n" ], "text/plain": [ "@prefix : .\n", "@prefix dc: .\n", "@prefix emoml: .\n", "@prefix endpoint: .\n", "@prefix fam: .\n", "@prefix marl: .\n", "@prefix nif: .\n", "@prefix onyx: .\n", "@prefix prefix: .\n", "@prefix prov: .\n", "@prefix rdf: .\n", "@prefix rdfs: .\n", "@prefix senpy: .\n", "@prefix wna: .\n", "@prefix xml: .\n", "@prefix xsd: .\n", "\n", "prefix: a senpy:Entry ;\n", " nif:isString \"Senpy is the best framework for semantic sentiment analysis, and very easy to use\" ;\n", " marl:hasOpinion [ a senpy:Sentiment ;\n", " marl:hasPolarity \"marl:Positive\" ;\n", " prov:wasGeneratedBy prefix:Analysis_1554389682.350598 ] .\n", "\n", "[] a senpy:Results ;\n", " prov:used prefix: .\n" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = requests.get(f'{endpoint}/sentiment140',\n", " params={\"input\": \"Senpy is the best framework for semantic sentiment analysis, and very easy to use\",\n", " \"outformat\": \"turtle\"})\n", "pretty(res.text, language='turtle')" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" }, "toc": { "colors": { "hover_highlight": "#DAA520", "running_highlight": "#FF0000", "selected_highlight": "#FFD700" }, "moveMenuLeft": true, "nav_menu": { "height": "68px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }