2019-07-17 14:29:30 +00:00
|
|
|
#
|
|
|
|
# Copyright 2014 Grupo de Sistemas Inteligentes (GSI) DIT, UPM
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2015-02-23 01:13:31 +00:00
|
|
|
import random
|
2018-01-03 08:39:30 +00:00
|
|
|
from senpy import SentimentPlugin, Sentiment, Entry
|
2015-02-23 01:13:31 +00:00
|
|
|
|
|
|
|
|
2019-01-07 11:08:19 +00:00
|
|
|
class RandSent(SentimentPlugin):
|
2018-01-03 08:39:30 +00:00
|
|
|
'''A sample plugin that returns a random sentiment annotation'''
|
2019-01-07 11:08:19 +00:00
|
|
|
name = 'sentiment-random'
|
2018-01-03 08:39:30 +00:00
|
|
|
author = "@balkian"
|
|
|
|
version = '0.1'
|
|
|
|
url = "https://github.com/gsi-upm/senpy-plugins-community"
|
|
|
|
marl__maxPolarityValue = '1'
|
|
|
|
marl__minPolarityValue = "-1"
|
2015-02-23 01:13:31 +00:00
|
|
|
|
2019-01-07 11:08:19 +00:00
|
|
|
def analyse_entry(self, entry, activity):
|
2015-02-24 06:15:25 +00:00
|
|
|
polarity_value = max(-1, min(1, random.gauss(0.2, 0.2)))
|
2015-02-23 01:13:31 +00:00
|
|
|
polarity = "marl:Neutral"
|
|
|
|
if polarity_value > 0:
|
|
|
|
polarity = "marl:Positive"
|
|
|
|
elif polarity_value < 0:
|
|
|
|
polarity = "marl:Negative"
|
2018-01-03 08:39:30 +00:00
|
|
|
sentiment = Sentiment(marl__hasPolarity=polarity,
|
|
|
|
marl__polarityValue=polarity_value)
|
2019-01-07 11:08:19 +00:00
|
|
|
sentiment.prov(activity)
|
2016-02-19 18:24:09 +00:00
|
|
|
entry.sentiments.append(sentiment)
|
2017-02-27 10:37:43 +00:00
|
|
|
yield entry
|
2017-06-16 15:53:42 +00:00
|
|
|
|
|
|
|
def test(self):
|
2018-01-03 08:39:30 +00:00
|
|
|
'''Run several random analyses.'''
|
2017-06-16 15:53:42 +00:00
|
|
|
params = dict()
|
|
|
|
results = list()
|
2018-01-06 17:51:16 +00:00
|
|
|
for i in range(50):
|
2019-01-07 11:08:19 +00:00
|
|
|
activity = self.activity(params)
|
2018-01-03 08:39:30 +00:00
|
|
|
res = next(self.analyse_entry(Entry(nif__isString="Hello"),
|
2019-01-07 11:08:19 +00:00
|
|
|
activity))
|
2017-06-16 15:53:42 +00:00
|
|
|
res.validate()
|
|
|
|
results.append(res.sentiments[0]['marl:hasPolarity'])
|
|
|
|
assert 'marl:Positive' in results
|
|
|
|
assert 'marl:Negative' in results
|