rand.py 1.07 KB
Newer Older
1
2
3
import random

from senpy.plugins import SentimentPlugin
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
from senpy.models import Sentiment, Entry
5
6


J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
8
class RandPlugin(SentimentPlugin):
    def analyse_entry(self, entry, params):
9
10
        lang = params.get("language", "auto")

11
        polarity_value = max(-1, min(1, random.gauss(0.2, 0.2)))
12
13
14
15
16
        polarity = "marl:Neutral"
        if polarity_value > 0:
            polarity = "marl:Positive"
        elif polarity_value < 0:
            polarity = "marl:Negative"
J. Fernando Sánchez's avatar
YAPFed    
J. Fernando Sánchez committed
17
18
19
20
        sentiment = Sentiment({
            "marl:hasPolarity": polarity,
            "marl:polarityValue": polarity_value
        })
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
21
22
        sentiment["prov:wasGeneratedBy"] = self.id
        entry.sentiments.append(sentiment)
23
        entry.language = lang
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
24
        yield entry
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
25
26
27
28
29
30
31
32
33
34

    def test(self):
        params = dict()
        results = list()
        for i in range(100):
            res = next(self.analyse_entry(Entry(text="Hello"), params))
            res.validate()
            results.append(res.sentiments[0]['marl:hasPolarity'])
        assert 'marl:Positive' in results
        assert 'marl:Negative' in results