mirror of
				https://github.com/gsi-upm/senpy
				synced 2025-10-30 15:08:19 +00:00 
			
		
		
		
	Compatibility with senpy 0.5
This commit is contained in:
		| @@ -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,6 +112,7 @@ class SentiTextPlugin(SentimentPlugin): | |||||||
|         scores = {} |         scores = {} | ||||||
|         for i in tokens: |         for i in tokens: | ||||||
|             scores[i] = {} |             scores[i] = {} | ||||||
|  |             if useful_synsets is None:    | ||||||
|                 for word in useful_synsets[i]: |                 for word in useful_synsets[i]: | ||||||
|                     if useful_synsets[i][word] is None: |                     if useful_synsets[i][word] is None: | ||||||
|                         continue |                         continue | ||||||
| @@ -133,7 +133,7 @@ class SentiTextPlugin(SentimentPlugin): | |||||||
|  |  | ||||||
|         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
									
								
							
							
						
						
									
										42
									
								
								sentiText/test.py
									
									
									
									
									
										Normal 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() | ||||||
		Reference in New Issue
	
	Block a user