Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fix = [
]
sentiment = [
"textblob>=0.17.1",
"nltk>=3.10.3",
"vaderSentiment>=3.3.2",
"transformers>=4.21.0",
"torch>=1.12.0",
Expand Down
33 changes: 33 additions & 0 deletions tests/test_sentiment_textblob.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Optional TextBlob smoke coverage without downloaded corpora or network access."""

import socket

import pytest


@pytest.mark.parametrize(
("text", "direction"),
[("I love this excellent result.", 1), ("I hate this terrible result.", -1)],
)
def test_textblob_sentiment_without_network_or_corpora(monkeypatch, text, direction):
def forbid_network(*args, **kwargs):
raise AssertionError("Sentiment analysis must not access the network or download corpora")

monkeypatch.setattr(socket.socket, "connect", forbid_network)
monkeypatch.setattr(socket.socket, "connect_ex", forbid_network)
monkeypatch.setattr(socket, "create_connection", forbid_network)
monkeypatch.setattr(socket, "getaddrinfo", forbid_network)

nltk = pytest.importorskip("nltk")
monkeypatch.setattr(nltk.data, "path", [])
monkeypatch.setattr(nltk, "download", forbid_network)
pytest.importorskip("textblob")

from neural.analysis.sentiment import SentimentAnalyzer, SentimentEngine

score = SentimentAnalyzer(engine=SentimentEngine.TEXTBLOB).analyze_text(text)

assert "textblob" in score.metadata["engines_used"]
assert score.engine_used is SentimentEngine.TEXTBLOB
assert 0 < direction * score.overall_score <= 1
assert 0 <= score.subjectivity <= 1
18 changes: 15 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading