mirror of
				https://github.com/gsi-upm/senpy
				synced 2025-10-31 15:38:17 +00:00 
			
		
		
		
	Move Taiger to a separate repository
This commit is contained in:
		| @@ -1 +0,0 @@ | ||||
| from gsiupm/senpy:0.11.4 | ||||
| @@ -1,67 +0,0 @@ | ||||
| # Senpy Plugin Taiger | ||||
|  | ||||
| Proxy for two of Taiger's sentiment analysis services for social media posts: | ||||
|  | ||||
| * taiger-plugin: proxy for a service that normalizes the text, and gives both a polarity and a polarity value for the text. It works for Spanish and English text. | ||||
| * taiger3c-plugin: it uses a simpler service that only returns a polarity (positive, negative or none). It only works for Spanish. | ||||
|  | ||||
|  | ||||
| ## Usage | ||||
|  | ||||
| To use this plugin, you should use a GET Requests with the following possible params: | ||||
| Params:	 | ||||
| - Input: text to analyse.(required) | ||||
|  | ||||
| ## Example of Usage | ||||
|  | ||||
| Example request:  | ||||
| ``` | ||||
| curl http://senpy.cluster.gsi.dit.upm.es/api/?algo=sentiment-taiger&inputText=This%20is%20amazing | ||||
|  | ||||
| #Or, for the taiger3c plugin: | ||||
| curl http://senpy.cluster.gsi.dit.upm.es/api/?algo=sentiment-taiger3c&inputText=Me%20encanta | ||||
| ``` | ||||
|  | ||||
| This plugin follows the senpy schema and vocabularies, please visit [senpy documentation](http://senpy.readthedocs.io). Specifically, the NIF API section.  | ||||
| It should look like this: | ||||
|  | ||||
| ``` | ||||
| { | ||||
|     "@context": "http://localhost:5005/api/contexts/Results.jsonld", | ||||
|     "@id": "_:Results_1532449339.5887764", | ||||
|     "@type": "results", | ||||
|     "analysis": [ | ||||
|         "endpoint:plugins/sentiment-taiger_0.1" | ||||
|     ], | ||||
|     "entries": [ | ||||
|         { | ||||
|             "@id": "#", | ||||
|             "@type": "entry", | ||||
|             "emotions": [], | ||||
|             "entities": [], | ||||
|             "nif:isString": "This is amazing", | ||||
|             "sentiments": [ | ||||
|                 { | ||||
|                     "@id": "Opinion0", | ||||
|                     "@type": "sentiment", | ||||
|                     "marl:hasPolarity": "marl:Positive", | ||||
|                     "marl:polarityValue": -1.4646806570973374, | ||||
|                     "normalizedText": "This is amazing", | ||||
|                     "prov:wasGeneratedBy": "endpoint:plugins/sentiment-taiger_0.1" | ||||
|                 } | ||||
|             ], | ||||
|             "suggestions": [], | ||||
|             "topics": [] | ||||
|         } | ||||
|     ] | ||||
| } | ||||
| ``` | ||||
|  | ||||
| As can be seen, this plugin analyzes sentiment giving three categories or tags: `marl:Positive`, `marl:Neutral` or `marl:Negative`, that will be held in the `marl:hasPolarity` field. | ||||
| Moreover, the plugin retrieves a `marl:polarityValue` (a value between -1 and 1). | ||||
| This plugin supports **python2.7** and **python3**. | ||||
|  | ||||
| ![alt GSI Logo][logoGSI] | ||||
|  | ||||
| [logoGSI]: http://www.gsi.dit.upm.es/images/stories/logos/gsi.png "GSI Logo" | ||||
|  | ||||
| @@ -1,11 +0,0 @@ | ||||
| version: '3' | ||||
| services: | ||||
|     dev: | ||||
|         image: "gsiupm/senpy-taiger:${VERSION:-dev}" | ||||
|         working_dir: "/senpy-plugins" | ||||
|         ports: | ||||
|             - "127.0.0.1:5005:5000" | ||||
|         volumes: | ||||
|             - ".:/senpy-plugins" | ||||
|         environment: | ||||
|             TAIGER_ENDPOINT: "${TAIGER_ENDPOINT:-http://somedi-taiger.hopto.org:5406/es_sentiment_analyzer_3classes}" | ||||
| @@ -1,164 +0,0 @@ | ||||
| # -*- coding: utf-8 -*- | ||||
|  | ||||
| import time | ||||
| import requests | ||||
| import json | ||||
| import string | ||||
| import os | ||||
| from os import path | ||||
| import time | ||||
| from senpy.plugins import SentimentPlugin | ||||
| from senpy.models import Results, Entry, Entity, Topic, Sentiment, Error | ||||
|  | ||||
|  | ||||
| TAIGER_ENDPOINT = os.environ.get("TAIGER3C_ENDPOINT", 'http://somedi-taiger.hopto.org:5406/es_sentiment_analyzer_3classes') | ||||
|  | ||||
|  | ||||
| class TaigerPlugin3cats(SentimentPlugin): | ||||
|     ''' | ||||
|     Service that analyzes sentiments from social posts written in Spanish or English. | ||||
|  | ||||
|     Example request: | ||||
|  | ||||
|     http://senpy.cluster.gsi.dit.upm.es/api/?algo=sentiment-taiger3c&inputText=This%20is%20amazing | ||||
|     ''' | ||||
|     name = 'sentiment-taiger3c' | ||||
|     author = 'GSI UPM' | ||||
|     version = "0.1" | ||||
|     maxPolarityValue = -1 | ||||
|     minPolarityValue = 1 | ||||
|  | ||||
|     def _polarity(self, value): | ||||
|  | ||||
|         if 'NONE' == value: | ||||
|             polarity = 'marl:Neutral' | ||||
|             value = 0 | ||||
|         elif 'N' == value: | ||||
|             polarity = 'marl:Negative' | ||||
|             value = -1 | ||||
|         elif 'P' == value: | ||||
|             polarity = 'marl:Positive' | ||||
|             value = 1 | ||||
|         else: | ||||
|             raise ValueError('unknown polarity: {}'.format(value)) | ||||
|         return polarity, value | ||||
|  | ||||
|     def analyse_entry(self, entry, activity): | ||||
|         params = activity.params | ||||
|  | ||||
|         txt = entry['nif:isString'] | ||||
|         api = TAIGER_ENDPOINT | ||||
|         parameters = { | ||||
|             'text': txt | ||||
|         } | ||||
|         try: | ||||
|             r = requests.get( | ||||
|                 api, params=parameters, timeout=3) | ||||
|             agg_polarity, value = self._polarity(r.text.strip()) | ||||
|         except requests.exceptions.Timeout: | ||||
|             raise Error("No response from the API") | ||||
|         except Exception as ex: | ||||
|             raise Error("There was a problem with the endpoint: {}".format(ex)) | ||||
|         if not agg_polarity: | ||||
|             raise Error('No category in response: {}'.format(ar.text)) | ||||
|         self.log.debug(agg_polarity) | ||||
|         agg_opinion = Sentiment( | ||||
|             id="Opinion0", | ||||
|             marl__hasPolarity=agg_polarity, | ||||
|             marl__polarityValue=value, | ||||
|             ) | ||||
|         agg_opinion.prov(self) | ||||
|         entry.sentiments.append(agg_opinion) | ||||
|  | ||||
|         yield entry | ||||
|  | ||||
|     test_cases = [ | ||||
|         { | ||||
|             'params': { | ||||
|                 'algo': 'sentiment-taiger', | ||||
|                 'intype': 'direct', | ||||
|                 'expanded-jsonld': 0, | ||||
|                 'informat': 'text', | ||||
|                 'prefix': '', | ||||
|                 'plugin_type': 'analysisPlugin', | ||||
|                 'urischeme': 'RFC5147String', | ||||
|                 'outformat': 'json-ld', | ||||
|                 'conversion': 'full', | ||||
|                 'language': 'en', | ||||
|                 'apikey': '00000', | ||||
|                 'algorithm': 'sentiment-taiger' | ||||
|             }, | ||||
|             'input': 'I hate to say this', | ||||
|             'expected': { | ||||
|                 'marl:hasOpinion': [ | ||||
|                     {'marl:hasPolarity': 'marl:Negative'}], | ||||
|             }, | ||||
|             'responses': [ | ||||
|                 { | ||||
|                     'url': TAIGER_ENDPOINT, | ||||
|                     'body': 'N', | ||||
|                 } | ||||
|             ] | ||||
|         }, | ||||
|         { | ||||
|             'params': { | ||||
|                 'algo': 'sentiment-taiger', | ||||
|                 'intype': 'direct', | ||||
|                 'expanded-jsonld': 0, | ||||
|                 'informat': 'text', | ||||
|                 'prefix': '', | ||||
|                 'plugin_type': 'analysisPlugin', | ||||
|                 'urischeme': 'RFC5147String', | ||||
|                 'outformat': 'json-ld', | ||||
|                 'conversion': 'full', | ||||
|                 'language': 'en', | ||||
|                 'apikey': '00000', | ||||
|                 'algorithm': 'sentiment-taiger' | ||||
|             }, | ||||
|             'input': 'This is amazing', | ||||
|             'expected': { | ||||
|                 'marl:hasOpinion': [ | ||||
|                     {'marl:hasPolarity': 'marl:Positive'}], | ||||
|             }, | ||||
|             'responses': [ | ||||
|                 { | ||||
|                     'url': TAIGER_ENDPOINT, | ||||
|                     'body': 'P', | ||||
|                 } | ||||
|             ] | ||||
|         }, | ||||
|         { | ||||
|             'params': { | ||||
|                 'algo': 'sentiment-taiger', | ||||
|                 'intype': 'direct', | ||||
|                 'expanded-jsonld': 0, | ||||
|                 'informat': 'text', | ||||
|                 'prefix': '', | ||||
|                 'plugin_type': 'analysisPlugin', | ||||
|                 'urischeme': 'RFC5147String', | ||||
|                 'outformat': 'json-ld', | ||||
|                 'conversion': 'full', | ||||
|                 'language': 'en', | ||||
|                 'apikey': '00000', | ||||
|                 'algorithm': 'sentiment-taiger' | ||||
|             }, | ||||
|             'input': 'The pillow is in the wardrobe', | ||||
|             'expected': { | ||||
|                 'marl:hasOpinion': [ | ||||
|                     {'marl:hasPolarity': 'marl:Neutral'}], | ||||
|             }, | ||||
|             'responses': [ | ||||
|                 { | ||||
|                     'url': TAIGER_ENDPOINT, | ||||
|                     'body': 'NONE', | ||||
|                 } | ||||
|             ] | ||||
|         } | ||||
|  | ||||
|  | ||||
|     ] | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     from senpy import easy_test | ||||
|     easy_test(debug=False) | ||||
| @@ -1,174 +0,0 @@ | ||||
| # -*- coding: utf-8 -*- | ||||
|  | ||||
| import time | ||||
| import requests | ||||
| import json | ||||
| import string | ||||
| import os | ||||
| from os import path | ||||
| import time | ||||
| from senpy.plugins import SentimentPlugin | ||||
| from senpy.models import Results, Entry, Entity, Topic, Sentiment, Error | ||||
|  | ||||
|  | ||||
| TAIGER_ENDPOINT = os.environ.get("TAIGER_ENDPOINT", 'http://34.244.91.7:8080/sentiment/classifyPositivity') | ||||
|  | ||||
|  | ||||
| class TaigerPlugin(SentimentPlugin): | ||||
|     ''' | ||||
|     Service that analyzes sentiments from social posts written in Spanish or English. | ||||
|  | ||||
|     Example request: | ||||
|  | ||||
|     http://senpy.cluster.gsi.dit.upm.es/api/?algo=sentiment-taiger&inputText=This%20is%20amazing | ||||
|     ''' | ||||
|     name = 'sentiment-taiger' | ||||
|     author = 'GSI UPM' | ||||
|     version = "0.1" | ||||
|     maxPolarityValue = -1 | ||||
|     minPolarityValue = 1 | ||||
|  | ||||
|     def _polarity(self, value): | ||||
|  | ||||
|         if 'neu' in value: | ||||
|             polarity = 'marl:Neutral' | ||||
|         elif 'neg' in value: | ||||
|             polarity = 'marl:Negative' | ||||
|         elif 'pos' in value: | ||||
|             polarity = 'marl:Positive' | ||||
|         return polarity | ||||
|  | ||||
|     def analyse_entry(self, entry, activity): | ||||
|         params = activity.params | ||||
|  | ||||
|         txt = entry['nif:isString'] | ||||
|         api = TAIGER_ENDPOINT | ||||
|         parameters = { | ||||
|             'inputText': txt | ||||
|         } | ||||
|         try: | ||||
|             r = requests.get( | ||||
|                 api, params=parameters, timeout=3) | ||||
|             api_response = r.json() | ||||
|         except requests.exceptions.Timeout: | ||||
|             raise Error("No response from the API") | ||||
|         except Exception as ex: | ||||
|             raise Error("There was a problem with the endpoint: {}".format(ex)) | ||||
|         if not api_response.get('positivityCategory'): | ||||
|             raise Error('No positive category in response: {}'.format(r.json())) | ||||
|         self.log.debug(api_response) | ||||
|         agg_polarity = self._polarity(api_response.get('positivityCategory')) | ||||
|         normalized_text = api_response.get('normalizedText', None) | ||||
|         agg_opinion = Sentiment( | ||||
|             id="Opinion0", | ||||
|             marl__hasPolarity=agg_polarity, | ||||
|             marl__polarityValue=api_response['positivityScore'] | ||||
|             ) | ||||
|         agg_opinion["normalizedText"] = api_response['normalizedText'] | ||||
|         agg_opinion.prov(self) | ||||
|         entry.sentiments.append(agg_opinion) | ||||
|  | ||||
|         yield entry | ||||
|  | ||||
|     test_cases = [ | ||||
|         { | ||||
|             'params': { | ||||
|                 'algo': 'sentiment-taiger', | ||||
|                 'intype': 'direct', | ||||
|                 'expanded-jsonld': 0, | ||||
|                 'informat': 'text', | ||||
|                 'prefix': '', | ||||
|                 'urischeme': 'RFC5147String', | ||||
|                 'outformat': 'json-ld', | ||||
|                 'conversion': 'full', | ||||
|                 'language': 'en', | ||||
|                 'apikey': '00000', | ||||
|                 'algorithm': 'sentiment-taiger' | ||||
|             }, | ||||
|             'input': 'I hate to say this', | ||||
|             'expected': { | ||||
|                 'marl:hasOpinion': [ | ||||
|                     {'marl:hasPolarity': 'marl:Negative'}], | ||||
|             }, | ||||
|             'responses': [ | ||||
|                 { | ||||
|                     'url': TAIGER_ENDPOINT, | ||||
|                     'json': { | ||||
|                       "inputText": "I hate to say this", | ||||
|                       "normalizedText": "I hate to say this", | ||||
|                       "positivityScore": -1.8951251587831475, | ||||
|                       "positivityCategory": "neg" | ||||
|                     } | ||||
|                 } | ||||
|             ] | ||||
|         }, | ||||
|         { | ||||
|             'params': { | ||||
|                 'algo': 'sentiment-taiger', | ||||
|                 'intype': 'direct', | ||||
|                 'expanded-jsonld': 0, | ||||
|                 'informat': 'text', | ||||
|                 'prefix': '', | ||||
|                 'urischeme': 'RFC5147String', | ||||
|                 'outformat': 'json-ld', | ||||
|                 'conversion': 'full', | ||||
|                 'language': 'en', | ||||
|                 'apikey': '00000', | ||||
|                 'algorithm': 'sentiment-taiger' | ||||
|             }, | ||||
|             'input': 'This is amazing', | ||||
|             'expected': { | ||||
|                 'marl:hasOpinion': [ | ||||
|                     {'marl:hasPolarity': 'marl:Positive'}], | ||||
|             }, | ||||
|             'responses': [ | ||||
|                 { | ||||
|                     'url': TAIGER_ENDPOINT, | ||||
|                     'json': { | ||||
|                       "inputText": "This is amazing ", | ||||
|                       "normalizedText": "This is amazing", | ||||
|                       "positivityScore": -1.4646806570973374, | ||||
|                       "positivityCategory": "pos" | ||||
|                     } | ||||
|                 } | ||||
|             ] | ||||
|         }, | ||||
|         { | ||||
|             'params': { | ||||
|                 'algo': 'sentiment-taiger', | ||||
|                 'intype': 'direct', | ||||
|                 'expanded-jsonld': 0, | ||||
|                 'informat': 'text', | ||||
|                 'prefix': '', | ||||
|                 'urischeme': 'RFC5147String', | ||||
|                 'outformat': 'json-ld', | ||||
|                 'conversion': 'full', | ||||
|                 'language': 'en', | ||||
|                 'apikey': '00000', | ||||
|                 'algorithm': 'sentiment-taiger' | ||||
|             }, | ||||
|             'input': 'The pillow is in the wardrobe', | ||||
|             'expected': { | ||||
|                 'marl:hasOpinion': [ | ||||
|                     {'marl:hasPolarity': 'marl:Neutral'}], | ||||
|             }, | ||||
|             'responses': [ | ||||
|                 { | ||||
|                     'url': TAIGER_ENDPOINT, | ||||
|                     'json': { | ||||
|                       "inputText": "The pillow is in the wardrobe", | ||||
|                       "normalizedText": "The pillow is in the wardrobe", | ||||
|                       "positivityScore": -2.723999097522657, | ||||
|                       "positivityCategory": "neu" | ||||
|                     } | ||||
|                 } | ||||
|             ] | ||||
|         } | ||||
|  | ||||
|  | ||||
|     ] | ||||
|  | ||||
|  | ||||
| if __name__ == '__main__': | ||||
|     from senpy import easy_test | ||||
|     easy_test() | ||||
		Reference in New Issue
	
	Block a user