Skip to content

Instantly share code, notes, and snippets.

@serenasensini
Last active January 24, 2022 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serenasensini/c8e8d603c9a2271d75043c89750eb301 to your computer and use it in GitHub Desktop.
Save serenasensini/c8e8d603c9a2271d75043c89750eb301 to your computer and use it in GitHub Desktop.
Sentiment Analysis in italiano con Naive-Bayes e NLTK
# pip install nltk
# nltk.download("punkt")
from nltk.corpus import movie_reviews
from nltk.classify import NaiveBayesClassifier
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.corpus import subjectivity
from nltk.sentiment import SentimentAnalyzer
import nltk.sentiment.util
# ITALIANO - Naïve Bayes
train2 = [("E' stata una bella esperienza", "positive"),
("Male recitato, male interpretato, pessimo", "negative"),
("Il miglior film che ho mai visto, incredibile", "positive"),
("Una perdita di tempo", "negative"),
("Ben diretto, ben recitato, mi è piaciuto, bellissimo", "positive"),
("Mi è piaciuta la fotografia e gli attori sono fantastici", "positive"),
("Davvero deluso di questo film", "negative"),
("Tutti i personaggi sono belli e ben interpretati", "positive"),
("Purtroppo faceva schifo", "negative"),
("Orribile e mediocre, sono orripilato", "negative")]
dictionary = set(word.lower() for passage in train2 for word in word_tokenize(passage[0]))
t = [({word: (word in word_tokenize(x[0])) for word in dictionary}, x[1]) for x in train2]
classifier = nltk.NaiveBayesClassifier.train(t)
test_data = "Questo libro era terribile, sono orripilato"
test_data_features = {word.lower(): (word in word_tokenize(test_data.lower())) for word in dictionary}
print(classifier.classify(test_data_features))
test_data2 = "Bellissimo, è stato incredibile"
test_data_features2 = {word.lower(): (word in word_tokenize(test_data2.lower())) for word in dictionary}
print(classifier.classify(test_data_features2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment