{ "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": 3, "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_1554391718.7649393"\n",
" }\n",
" ],\n",
" "nif:isString": "Senpy is awesome",\n",
" "onyx:hasEmotionSet": []\n",
" }\n",
" ]\n",
"}\n",
"
{\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",
" "@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",
" "@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",
" "@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",
" "@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",
"
@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",
"