From 0e9db7081c7daf8e12c2e743c52939a621c3dac7 Mon Sep 17 00:00:00 2001 From: NachoCP Date: Wed, 24 Feb 2016 17:41:22 +0100 Subject: [PATCH] Compatibility with senpy 0.5 --- sentiText/sentitext.py | 51 +++++++++++++++++++++--------------------- sentiText/test.py | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 sentiText/test.py diff --git a/sentiText/sentitext.py b/sentiText/sentitext.py index 04dc325..903dbeb 100644 --- a/sentiText/sentitext.py +++ b/sentiText/sentitext.py @@ -11,8 +11,7 @@ from scipy.interpolate import interp1d from os import path from senpy.plugins import SentimentPlugin, SenpyPlugin -from senpy.models import Response, Opinion, Entry - +from senpy.models import Results, Entry, Sentiment logger = logging.getLogger(__name__) @@ -113,27 +112,28 @@ class SentiTextPlugin(SentimentPlugin): scores = {} for i in tokens: scores[i] = {} - for word in useful_synsets[i]: - if useful_synsets[i][word] is None: - continue - temp_scores = self._swn.get_score(useful_synsets[i][word].name().split('.')[0].replace(' ',' ')) - for score in temp_scores: - if score['synset'] == useful_synsets[i][word]: - t_score = score['pos'] - score['neg'] - f_score = 'neu' - if t_score > 0: - f_score = 'pos' - elif t_score < 0: - f_score = 'neg' - score['score'] = f_score - scores[i][word] = score - break + if useful_synsets is None: + for word in useful_synsets[i]: + if useful_synsets[i][word] is None: + continue + temp_scores = self._swn.get_score(useful_synsets[i][word].name().split('.')[0].replace(' ',' ')) + for score in temp_scores: + if score['synset'] == useful_synsets[i][word]: + t_score = score['pos'] - score['neg'] + f_score = 'neu' + if t_score > 0: + f_score = 'pos' + elif t_score < 0: + f_score = 'neg' + score['score'] = f_score + scores[i][word] = score + break logger.debug("All scores (some not used): {}".format(scores)) lang = params.get("language", "auto") p = params.get("prefix", None) - response = Response(prefix=p) + response = Results() for i in scores: n_pos = 0.0 @@ -159,17 +159,16 @@ class SentiTextPlugin(SentimentPlugin): polarity = 'marl:Negative' entry = Entry(id="Entry"+str(i), - text=tokens[i]['sentence'], - prefix=p) - polarity - opinion = Opinion(id="Opinion0"+'_'+str(i), - prefix=p, - hasPolarity=polarity, - polarityValue=float("{0:.2f}".format(g_score))) + nif_isString=tokens[i]['sentence']) + opinion = Sentiment(id="Opinion0"+'_'+str(i), + marl__hasPolarity=polarity, + marL__polarityValue=float("{0:.2f}".format(g_score))) opinion["prov:wasGeneratedBy"] = self.id - entry.opinions.append(opinion) + + entry.sentiments = [] + entry.sentiments.append(opinion) entry.language = lang response.entries.append(entry) return response diff --git a/sentiText/test.py b/sentiText/test.py new file mode 100644 index 0000000..1bb0c75 --- /dev/null +++ b/sentiText/test.py @@ -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() \ No newline at end of file