1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-22 16:12:29 +00:00

Compatibility with senpy 0.5

This commit is contained in:
NachoCP 2016-02-24 17:41:22 +01:00
parent 17976d85b1
commit 0e9db7081c
2 changed files with 67 additions and 26 deletions

View File

@ -11,8 +11,7 @@ from scipy.interpolate import interp1d
from os import path from os import path
from senpy.plugins import SentimentPlugin, SenpyPlugin from senpy.plugins import SentimentPlugin, SenpyPlugin
from senpy.models import Response, Opinion, Entry from senpy.models import Results, Entry, Sentiment
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -113,27 +112,28 @@ class SentiTextPlugin(SentimentPlugin):
scores = {} scores = {}
for i in tokens: for i in tokens:
scores[i] = {} scores[i] = {}
for word in useful_synsets[i]: if useful_synsets is None:
if useful_synsets[i][word] is None: for word in useful_synsets[i]:
continue if useful_synsets[i][word] is None:
temp_scores = self._swn.get_score(useful_synsets[i][word].name().split('.')[0].replace(' ',' ')) continue
for score in temp_scores: temp_scores = self._swn.get_score(useful_synsets[i][word].name().split('.')[0].replace(' ',' '))
if score['synset'] == useful_synsets[i][word]: for score in temp_scores:
t_score = score['pos'] - score['neg'] if score['synset'] == useful_synsets[i][word]:
f_score = 'neu' t_score = score['pos'] - score['neg']
if t_score > 0: f_score = 'neu'
f_score = 'pos' if t_score > 0:
elif t_score < 0: f_score = 'pos'
f_score = 'neg' elif t_score < 0:
score['score'] = f_score f_score = 'neg'
scores[i][word] = score score['score'] = f_score
break scores[i][word] = score
break
logger.debug("All scores (some not used): {}".format(scores)) logger.debug("All scores (some not used): {}".format(scores))
lang = params.get("language", "auto") lang = params.get("language", "auto")
p = params.get("prefix", None) p = params.get("prefix", None)
response = Response(prefix=p) response = Results()
for i in scores: for i in scores:
n_pos = 0.0 n_pos = 0.0
@ -159,17 +159,16 @@ class SentiTextPlugin(SentimentPlugin):
polarity = 'marl:Negative' polarity = 'marl:Negative'
entry = Entry(id="Entry"+str(i), entry = Entry(id="Entry"+str(i),
text=tokens[i]['sentence'], nif_isString=tokens[i]['sentence'])
prefix=p)
polarity
opinion = Opinion(id="Opinion0"+'_'+str(i),
prefix=p,
hasPolarity=polarity,
polarityValue=float("{0:.2f}".format(g_score)))
opinion = Sentiment(id="Opinion0"+'_'+str(i),
marl__hasPolarity=polarity,
marL__polarityValue=float("{0:.2f}".format(g_score)))
opinion["prov:wasGeneratedBy"] = self.id opinion["prov:wasGeneratedBy"] = self.id
entry.opinions.append(opinion)
entry.sentiments = []
entry.sentiments.append(opinion)
entry.language = lang entry.language = lang
response.entries.append(entry) response.entries.append(entry)
return response return response

42
sentiText/test.py Normal file
View File

@ -0,0 +1,42 @@
import os
import logging
logging.basicConfig()
try:
import unittest.mock as mock
except ImportError:
import mock
from senpy.extensions import Senpy
from flask import Flask
import unittest
class SentiTextTest(unittest.TestCase):
def setUp(self):
self.app = Flask("test_plugin")
self.dir = os.path.join(os.path.dirname(__file__))
self.senpy = Senpy(plugin_folder=self.dir, default_plugins=False)
self.senpy.init_app(self.app)
def tearDown(self):
self.senpy.deactivate_plugin("SentiText", sync=True)
def test_analyse(self):
plugin = self.senpy.plugins["SentiText"]
plugin.activate()
texts = {'Odio ir al cine' : 'marl:Neutral',
'El cielo esta nublado' : 'marl:Positive',
'Esta tarta esta muy buena' : 'marl:Neutral'}
for text in texts:
response = plugin.analyse(input=text)
sentimentSet = response.entries[0].sentiments[0]
print sentimentSet
expected = texts[text]
assert sentimentSet['marl:hasPolarity'] == expected
plugin.deactivate()
if __name__ == '__main__':
unittest.main()