mirror of
https://github.com/gsi-upm/senpy
synced 2024-11-22 16:12:29 +00:00
Add aggregate sentiment. This closes senpy/senpy-plugins-community#10
This commit is contained in:
parent
1b8a24c530
commit
86fdd8678a
@ -1,4 +1,5 @@
|
|||||||
import time
|
import time
|
||||||
|
import logging
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import string
|
import string
|
||||||
@ -8,8 +9,23 @@ import time
|
|||||||
from senpy.plugins import SentimentPlugin, SenpyPlugin
|
from senpy.plugins import SentimentPlugin, SenpyPlugin
|
||||||
from senpy.models import Results, Entry, Sentiment,Error
|
from senpy.models import Results, Entry, Sentiment,Error
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
class DaedalusPlugin(SentimentPlugin):
|
class DaedalusPlugin(SentimentPlugin):
|
||||||
|
|
||||||
|
|
||||||
|
def _polarity(self, value):
|
||||||
|
|
||||||
|
polarityValue = 0
|
||||||
|
polarity = 'marl:Neutral'
|
||||||
|
if 'N' in value:
|
||||||
|
polarity = 'marl:Negative'
|
||||||
|
polarityValue = -1
|
||||||
|
elif 'P' in value:
|
||||||
|
polarity = 'marl:Positive'
|
||||||
|
polarityValue = 1
|
||||||
|
return polarity, polarityValue
|
||||||
|
|
||||||
def analyse_entry(self, entry, params):
|
def analyse_entry(self, entry, params):
|
||||||
|
|
||||||
txt = entry.get("text",None)
|
txt = entry.get("text",None)
|
||||||
@ -28,22 +44,32 @@ class DaedalusPlugin(SentimentPlugin):
|
|||||||
r = requests.post(api, params=parameters, timeout=3)
|
r = requests.post(api, params=parameters, timeout=3)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
raise Error("Meaning Cloud API does not response")
|
raise Error("Meaning Cloud API does not response")
|
||||||
value = r.json().get('score_tag', None)
|
api_response = r.json()
|
||||||
if not value:
|
if not api_response.get('score_tag'):
|
||||||
raise Error(r.json())
|
raise Error(r.json())
|
||||||
|
|
||||||
#Senpy Response
|
|
||||||
response = Results()
|
response = Results()
|
||||||
polarityValue = 0
|
agg_polarity, agg_polarityValue = self._polarity(api_response.get('score_tag', None))
|
||||||
polarity = 'marl:Neutral'
|
agg_opinion = Sentiment(id="Opinion0",
|
||||||
if 'N' in value:
|
marl__hasPolarity=agg_polarity,
|
||||||
polarity = 'marl:Negative'
|
marl__polarityValue = agg_polarityValue,
|
||||||
polarityValue = -1
|
marl__opinionCount = len(api_response['sentence_list']))
|
||||||
elif 'P' in value:
|
entry.sentiments.append(agg_opinion)
|
||||||
polarity = 'marl:Positive'
|
logger.info(api_response['sentence_list'])
|
||||||
polarityValue = 1
|
count = 1
|
||||||
|
for sentence in api_response['sentence_list']:
|
||||||
|
for nopinion in sentence['segment_list']:
|
||||||
|
logger.info(nopinion)
|
||||||
|
polarity, polarityValue = self._polarity(nopinion.get('score_tag', None))
|
||||||
|
opinion = Sentiment(id="Opinion{}".format(count),
|
||||||
|
marl__hasPolarity=polarity,
|
||||||
|
marl__polarityValue=polarityValue,
|
||||||
|
marl__aggregatesOpinion=agg_opinion.get('id'),
|
||||||
|
nif__anchorOf=nopinion.get('text', None),
|
||||||
|
nif__beginIndex=nopinion.get('inip', None),
|
||||||
|
nif__endIndex=nopinion.get('endp', None)
|
||||||
|
)
|
||||||
|
|
||||||
opinion = Sentiment(id="Opinion0",marl__hasPolarity=polarity,marl__polarityValue = polarityValue)
|
count += 1
|
||||||
entry.sentiments.append(opinion)
|
entry.sentiments.append(opinion)
|
||||||
|
|
||||||
yield entry
|
yield entry
|
Loading…
Reference in New Issue
Block a user