1
0
mirror of https://github.com/gsi-upm/senpy synced 2024-11-25 01:22:28 +00:00

Fixed sentiment-basic plugin that only retrieved Neutral sentiment. This closes senpy/senpy-plugins-community#6. Also added nltk download for some plugins.

This commit is contained in:
militarpancho 2017-04-17 13:57:27 +02:00
parent 85db4db01d
commit e341cc82fa
3 changed files with 6 additions and 19 deletions

View File

@ -1,15 +1,9 @@
from gsiupm/senpy:0.6.1-python2.7 from gsiupm/senpy:0.8.7-python2.7
RUN mkdir -p /senpy-plugins
RUN pip install nltk
RUN python -m nltk.downloader stopwords RUN python -m nltk.downloader stopwords
RUN python -m nltk.downloader punkt RUN python -m nltk.downloader punkt
RUN python -m nltk.downloader maxent_treebank_pos_tagger RUN python -m nltk.downloader maxent_treebank_pos_tagger
RUN python -m nltk.downloader wordnet RUN python -m nltk.downloader wordnet
RUN pip install pytest
RUN pip install mock
ADD . /senpy-plugins ADD . /senpy-plugins
RUN senpy -f /senpy-plugins --only-install
WORKDIR /senpy-plugins/

View File

@ -9,7 +9,6 @@ import string
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from nltk.corpus import stopwords from nltk.corpus import stopwords
from nltk.corpus import WordNetCorpusReader from nltk.corpus import WordNetCorpusReader
from emotion import Emotion as Emo from emotion import Emotion as Emo
from pattern.en import parse from pattern.en import parse
from senpy.plugins import EmotionPlugin, SenpyPlugin, ShelfMixin from senpy.plugins import EmotionPlugin, SenpyPlugin, ShelfMixin
@ -51,6 +50,8 @@ class EmotionTextPlugin(EmotionPlugin, ShelfMixin):
Emo.emotions[name] = Emo(name, elem.get("isa")) Emo.emotions[name] = Emo(name, elem.get("isa"))
def activate(self, *args, **kwargs): def activate(self, *args, **kwargs):
nltk.download('stopwords')
self._stopwords = stopwords.words('english') self._stopwords = stopwords.words('english')
#local_path=os.path.dirname(os.path.abspath(__file__)) #local_path=os.path.dirname(os.path.abspath(__file__))
self._categories = {'anger': ['general-dislike',], self._categories = {'anger': ['general-dislike',],
@ -81,8 +82,6 @@ class EmotionTextPlugin(EmotionPlugin, ShelfMixin):
self._wn16 = self.sh['wn16'] self._wn16 = self.sh['wn16']
def deactivate(self, *args, **kwargs): def deactivate(self, *args, **kwargs):
self.save() self.save()

View File

@ -30,6 +30,7 @@ class SentiTextPlugin(SentimentPlugin):
return tagger return tagger
def activate(self, *args, **kwargs): def activate(self, *args, **kwargs):
nltk.download(['punkt','wordnet'])
self._swn = self._load_swn() self._swn = self._load_swn()
self._pos_tagger = self._load_pos_tagger() self._pos_tagger = self._load_pos_tagger()
@ -69,7 +70,6 @@ class SentiTextPlugin(SentimentPlugin):
def analyse_entry(self, entry, params): def analyse_entry(self, entry, params):
language = params.get("language") language = params.get("language")
text = entry.get("text", None) text = entry.get("text", None)
tokens = self._tokenize(text) tokens = self._tokenize(text)
@ -82,7 +82,6 @@ class SentiTextPlugin(SentimentPlugin):
if len(lemmas) == 0: if len(lemmas) == 0:
continue continue
tokens[i]['lemmas'][w[0]] = lemmas tokens[i]['lemmas'][w[0]] = lemmas
if language == "en": if language == "en":
trans = TextBlob(unicode(text)) trans = TextBlob(unicode(text))
else: else:
@ -96,11 +95,10 @@ class SentiTextPlugin(SentimentPlugin):
continue continue
eq_synset = self._compare_synsets(synsets, tokens, s_i) eq_synset = self._compare_synsets(synsets, tokens, s_i)
useful_synsets[s_i][t_w] = eq_synset useful_synsets[s_i][t_w] = eq_synset
scores = {} scores = {}
for i in tokens: for i in tokens:
scores[i] = {} scores[i] = {}
if useful_synsets is None: if useful_synsets != 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
@ -116,9 +114,7 @@ class SentiTextPlugin(SentimentPlugin):
score['score'] = f_score score['score'] = f_score
scores[i][word] = score scores[i][word] = score
break break
p = params.get("prefix", None) p = params.get("prefix", None)
for i in scores: for i in scores:
n_pos = 0.0 n_pos = 0.0
n_neg = 0.0 n_neg = 0.0
@ -127,7 +123,6 @@ class SentiTextPlugin(SentimentPlugin):
n_pos += 1.0 n_pos += 1.0
elif scores[i][w]['score'] == 'neg': elif scores[i][w]['score'] == 'neg':
n_neg += 1.0 n_neg += 1.0
inter = interp1d([-1.0, 1.0], [0.0, 1.0]) inter = interp1d([-1.0, 1.0], [0.0, 1.0])
try: try:
g_score = (n_pos - n_neg) / (n_pos + n_neg) g_score = (n_pos - n_neg) / (n_pos + n_neg)
@ -135,7 +130,6 @@ class SentiTextPlugin(SentimentPlugin):
except: except:
if n_pos == 0 and n_neg == 0: if n_pos == 0 and n_neg == 0:
g_score = 0.5 g_score = 0.5
polarity = 'marl:Neutral' polarity = 'marl:Neutral'
if g_score > 0.5: if g_score > 0.5:
polarity = 'marl:Positive' polarity = 'marl:Positive'
@ -149,4 +143,4 @@ class SentiTextPlugin(SentimentPlugin):
entry.sentiments.append(opinion) entry.sentiments.append(opinion)
yield entry yield entry