From 709c3f67cbb1c0e6ff92ddcecedea997da560e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Fernando=20S=C3=A1nchez?= Date: Wed, 11 Jan 2017 13:15:49 +0100 Subject: [PATCH] --- Accessing Sentiment Analysis services.ipynb | 309 ++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 Accessing Sentiment Analysis services.ipynb diff --git a/Accessing Sentiment Analysis services.ipynb b/Accessing Sentiment Analysis services.ipynb new file mode 100644 index 0000000..d46e6cd --- /dev/null +++ b/Accessing Sentiment Analysis services.ipynb @@ -0,0 +1,309 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "List of services:\n", + "* [Sentiment140](http://www.sentiment140.com/)\n", + "* AlchemyAPI\n", + "* [Free Natural Language Processing Service](https://www.mashape.com/loudelement/free-natural-language-processing-service)\n", + "* [Daedalus' service]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import requests\n", + "import json" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "from secret import *" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Free Natural Language Processing Service\n", + "========================================\n", + "\n", + "* https://loudelement-free-natural-language-processing-service.p.mashape.com/nlp-text/\n", + "* Method: POST\n", + "* Parameters:\n", + " * text: Text to be analysed\n", + "* Headers:\n", + " * X-Mashape-Key: Your Mashape Key\n", + "\n", + "Example output:\n", + "\n", + "```json\n", + "{\n", + " \"api-author\": \"Taewook Kang (taewook.kang@gmail.com)\",\n", + " \"api-usage\": \"completely free as long as you give me credit somewhere on your website.\",\n", + " \"status\": \"OK\",\n", + " \"language\": \"english\",\n", + " \"sentiment-text\": \"negative\",\n", + " \"sentiment-score\": \"-0.171441\"\n", + "}\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "SENTIMENT140\n", + "=============\n", + "\n", + "* http://www.sentiment140.com/api/bulkClassifyJson\n", + "* Method: POST\n", + "* Parameters: JSON Object (that is copied to the result)\n", + " * text\n", + " * query\n", + " * language\n", + " * topic\n", + "\n", + "* Example response:\n", + "```json\n", + "{\"data\": [{\"text\": \"I love Titanic.\", \"id\":1234, \"polarity\": 4}, \n", + " {\"text\": \"I hate Titanic.\", \"id\":4567, \"polarity\": 0}]}\n", + "```\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'data': [{'meta': {'language': 'en'},\n", + " 'polarity': 4,\n", + " 'text': 'I love Titanic'}]}" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res = requests.post(\"http://www.sentiment140.com/api/bulkClassifyJson\", json.dumps({\"data\": [{\"text\": \"I love Titanic\"}]}))\n", + "res.json()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Free Natural Language Processing\n", + "================================\n", + "* https://loudelement-free-natural-language-processing-service.p.mashape.com/nlp-text/\n", + "* Method: GET\n", + "* Parameters\n", + " * text: Text to be analysed\n", + "* Headers\n", + " * X-Mashape-Key: Mashape key\n", + " \n", + "* Example response:\n", + "```json\n", + "{\n", + " \"api-author\": \"Taewook Kang (taewook.kang@gmail.com)\",\n", + " \"api-usage\": \"completely free as long as you give me credit somewhere on your website.\",\n", + " \"status\": \"OK\",\n", + " \"language\": \"english\",\n", + " \"sentiment-text\": \"negative\",\n", + " \"sentiment-score\": \"-0.171441\"\n", + "}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'message': 'Invalid Mashape Key'}" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "res = requests.get(\"https://loudelement-free-natural-language-processing-service.p.mashape.com/nlp-text/\", headers={\"x-mashape-key\": MASHAPE_KEY}, params={\"text\": \"I hate Titanic\"})\n", + "res.json()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Alchemy API\n", + "===========\n", + "\n", + "* http://access.alchemyapi.com/calls/text/TextGetTextSentiment\n", + "* Parameters\n", + " * apikey: your alchemy API key\n", + " * outputMode: xml (default), json, rdf\n", + " * showSourceText: 0 (default)/ 1 (include the original text in the response)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "{'docSentiment': {'score': '-0.871736', 'type': 'negative'},\n", + " 'language': 'english',\n", + " 'status': 'OK',\n", + " 'text': 'I hate Titanic',\n", + " 'totalTransactions': '1',\n", + " 'usage': 'By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html'}" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ALCHEMY_TOKEN = ALCHEMY_TOKEN\n", + "res = requests.get(\"http://access.alchemyapi.com/calls/text/TextGetTextSentiment\", params={\"text\": \"I hate Titanic\", \"apikey\": ALCHEMY_TOKEN, \"outputMode\": \"json\", \"showSourceText\": 1})\n", + "res.json()" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": false + }, + "source": [ + "# Daedalus" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "%%HTML\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\"status\":{\"code\":\"0\",\"msg\":\"OK\",\"credits\":\"1\",\"remaining_credits\":\"39999\"},\"model\":\"general_en\",\"score_tag\":\"N\",\"agreement\":\"AGREEMENT\",\"subjectivity\":\"SUBJECTIVE\",\"confidence\":\"100\",\"irony\":\"NONIRONIC\",\"sentence_list\":[{\"text\":\"I hate Titanic\",\"inip\":\"0\",\"endp\":\"13\",\"bop\":\"y\",\"confidence\":\"100\",\"score_tag\":\"N\",\"agreement\":\"AGREEMENT\",\"segment_list\":[{\"text\":\"I hate Titanic\",\"segment_type\":\"main\",\"inip\":\"0\",\"endp\":\"13\",\"confidence\":\"100\",\"score_tag\":\"N\",\"agreement\":\"AGREEMENT\",\"polarity_term_list\":[{\"text\":\"hate\",\"inip\":\"2\",\"endp\":\"5\",\"confidence\":\"100\",\"score_tag\":\"N\",\"sentimented_entity_list\":[{\"form\":\"Titanic\",\"id\":\"04d28e668d\",\"variant\":\"Titanic\",\"inip\":\"7\",\"endp\":\"13\",\"type\":\"Top>Product>Machine>Vehicle>Ship\",\"score_tag\":\"N\"}]}]}],\"sentimented_entity_list\":[{\"form\":\"Titanic\",\"id\":\"04d28e668d\",\"type\":\"Top>Product>Machine>Vehicle>Ship\",\"score_tag\":\"N\"}],\"sentimented_concept_list\":[]}],\"sentimented_entity_list\":[{\"form\":\"Titanic\",\"id\":\"04d28e668d\",\"type\":\"Top>Product>Machine>Vehicle>Ship\",\"score_tag\":\"N\"}],\"sentimented_concept_list\":[]}\n" + ] + }, + { + "data": { + "text/plain": [ + "'N'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import time\n", + "import ratelim\n", + "\n", + "from ipywidgets import FloatProgress\n", + "from IPython.display import display\n", + "\n", + "@ratelim.patient(2,1)\n", + "def get_one_sentiment_daedalus(txt):\n", + " model = 'general' # general_es / general_es / general_fr\n", + " api = 'http://api.meaningcloud.com/sentiment-2.1'\n", + " parameters = {'key': DAEDALUS_KEY,\n", + " 'model': model,\n", + " 'lang': 'en',\n", + " 'of': 'json',\n", + " 'txt': txt,\n", + " 'src': 'its-not-a-real-python-sdk'}\n", + " r = requests.post(api, params=parameters)\n", + " print(r.text)\n", + " return r.json().get('score_tag', None)\n", + "\n", + "get_one_sentiment_daedalus('I hate Titanic')" + ] + } + ], + "metadata": { + "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.5.2" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}