Skip to content

Commit

Permalink
Italian sentiment analysis (thanks Fabio Marfia, Politecnico di Milano)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom De Smedt committed Feb 5, 2016
1 parent a34c683 commit 5dbff7b
Show file tree
Hide file tree
Showing 2 changed files with 3,155 additions and 1 deletion.
35 changes: 34 additions & 1 deletion pattern/text/it/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
)
# Import sentiment analysis base classes.
from pattern.text import (
Sentiment, NOUN, VERB, ADJECTIVE, ADVERB
Sentiment as _Sentiment,
NOUN, VERB, ADJECTIVE, ADVERB,
MOOD, IRONY
)
# Import spelling base class.
from pattern.text import (
Expand Down Expand Up @@ -146,6 +148,11 @@ def find_tags(self, tokens, **kwargs):
kwargs.setdefault("map", lambda token, tag: penntreebank2universal(token, tag))
return _Parser.find_tags(self, tokens, **kwargs)

class Sentiment(_Sentiment):

def load(self, path=None):
_Sentiment.load(self, path)

parser = Parser(
lexicon = os.path.join(MODULE, "it-lexicon.txt"),
frequency = os.path.join(MODULE, "it-frequency.txt"),
Expand All @@ -157,6 +164,16 @@ def find_tags(self, tokens, **kwargs):

lexicon = parser.lexicon # Expose lexicon.

sentiment = Sentiment(
path = os.path.join(MODULE, "it-sentiment.xml"),
synset = None,
negations = ("mai", "no", "non"),
modifiers = ("RB",),
modifier = lambda w: w.endswith(("mente")),
tokenizer = parser.find_tokens,
language = "it"
)

spelling = Spelling(
path = os.path.join(MODULE, "it-spelling.txt")
)
Expand Down Expand Up @@ -204,6 +221,22 @@ def suggest(w):
"""
return spelling.suggest(w)


def polarity(s, **kwargs):
""" Returns the sentence polarity (positive/negative) between -1.0 and 1.0.
"""
return sentiment(s, **kwargs)[0]

def subjectivity(s, **kwargs):
""" Returns the sentence subjectivity (objective/subjective) between 0.0 and 1.0.
"""
return sentiment(s, **kwargs)[1]

def positive(s, threshold=0.1, **kwargs):
""" Returns True if the given sentence has a positive sentiment (polarity >= threshold).
"""
return polarity(s, **kwargs) >= threshold

split = tree # Backwards compatibility.

#---------------------------------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 5dbff7b

Please sign in to comment.