1
Fork 0
mirror of https://github.com/Steffo99/unimore-bda-6.git synced 2024-11-23 08:24:18 +00:00
bda-6-steffo/unimore_bda_6/analysis/nltk_sentiment.py

97 lines
3.5 KiB
Python
Raw Normal View History

2023-02-03 22:27:44 +00:00
import nltk
import nltk.classify
import nltk.sentiment
import nltk.sentiment.util
import logging
import typing as t
2023-02-12 04:11:58 +00:00
from ..database import TextReview, CachedDatasetFunc, TokenizedReview
2023-02-04 00:36:42 +00:00
from .base import BaseSentimentAnalyzer, AlreadyTrainedError, NotTrainedError
2023-02-03 22:27:44 +00:00
from ..tokenizer import BaseTokenizer
log = logging.getLogger(__name__)
TokenBag = list[str]
Features = dict[str, int]
class NLTKSentimentAnalyzer(BaseSentimentAnalyzer):
"""
A sentiment analyzer resembling the one implemented in structure the one implemented in the classroom, using the basic sentiment analyzer of NLTK.
"""
def __init__(self, *, tokenizer: BaseTokenizer) -> None:
2023-02-08 18:46:05 +00:00
super().__init__(tokenizer=tokenizer)
2023-02-03 22:27:44 +00:00
self.model: nltk.sentiment.SentimentAnalyzer = nltk.sentiment.SentimentAnalyzer()
self.trained: bool = False
2023-02-12 04:11:58 +00:00
def _add_feature_unigrams(self, dataset: t.Iterator[TokenizedReview]) -> None:
2023-02-03 22:27:44 +00:00
"""
Register the `nltk.sentiment.util.extract_unigram_feats` feature extrator on the model.
"""
# Ignore the category and only access the tokens
tokenbags = map(lambda r: r.tokens, dataset)
2023-02-03 22:27:44 +00:00
# Get all words in the documents
all_words = self.model.all_words(tokenbags, labeled=False)
# Create unigram `contains(*)` features from the previously gathered words
unigrams = self.model.unigram_word_feats(words=all_words, min_freq=4)
# Add the feature extractor to the model
self.model.add_feat_extractor(nltk.sentiment.util.extract_unigram_feats, unigrams=unigrams)
2023-02-12 04:11:58 +00:00
def _add_feature_extractors(self, dataset: t.Iterator[TextReview]):
2023-02-03 22:27:44 +00:00
"""
Register new feature extractors on the `.model`.
"""
# Tokenize the reviews and collect the iterator to avoid breaking NLTK
2023-02-12 04:11:58 +00:00
dataset: t.Iterator[TokenizedReview] = map(self.tokenizer.tokenize_review, dataset)
2023-02-03 22:27:44 +00:00
# Add the unigrams feature
self._add_feature_unigrams(dataset)
def __extract_features(self, review: TextReview) -> tuple[Features, str]:
2023-02-03 22:27:44 +00:00
"""
Convert a (TokenBag, Category) tuple to a (Features, Category) tuple.
Does not use `SentimentAnalyzer.apply_features` due to unexpected behaviour when using iterators.
"""
2023-02-12 04:11:58 +00:00
review: TokenizedReview = self.tokenizer.tokenize_review(review)
return self.model.extract_features(review.tokens), str(review.rating)
2023-02-03 22:27:44 +00:00
2023-02-08 18:46:05 +00:00
def train(self, training_dataset_func: CachedDatasetFunc, validation_dataset_func: CachedDatasetFunc) -> None:
2023-02-03 22:27:44 +00:00
# Forbid retraining the model
if self.trained:
raise AlreadyTrainedError()
# Add the feature extractors to the model
2023-02-12 04:11:58 +00:00
self._add_feature_extractors(training_dataset_func())
2023-02-03 22:27:44 +00:00
# Extract features from the dataset
featureset: t.Iterator[tuple[Features, str]] = map(self.__extract_features, training_dataset_func())
2023-02-03 22:27:44 +00:00
# Train the classifier with the extracted features and category
2023-02-12 04:11:58 +00:00
self.model.classifier = nltk.classify.NaiveBayesClassifier.train(featureset)
2023-02-03 22:27:44 +00:00
# Toggle the trained flag
self.trained = True
2023-02-12 04:11:58 +00:00
def use(self, text: str) -> float:
2023-02-03 22:27:44 +00:00
# Require the model to be trained
if not self.trained:
raise NotTrainedError()
# Tokenize the input
2023-02-12 04:11:58 +00:00
tokens = self.tokenizer.tokenize(text)
2023-02-03 22:27:44 +00:00
# Run the classification method
rating = self.model.classify(instance=tokens)
# Convert the class back into a float
rating = float(rating)
return rating
2023-02-03 22:27:44 +00:00
__all__ = (
"NLTKSentimentAnalyzer",
)