Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit f34b6bb

Browse files
committed
[launchweek demo] Add Sentiment class
1 parent 7d3c536 commit f34b6bb

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

codecov/sentiment.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from textblob import TextBlob
2+
3+
4+
class SentimentAnalyzer:
5+
def __init__(self):
6+
pass
7+
8+
def get_sentiment(self, sentence: str) -> str:
9+
polarity = self._get_polarity(sentence)
10+
if polarity > 0:
11+
return "positive"
12+
elif polarity < 0:
13+
return "negative"
14+
else:
15+
return "neutral"
16+
17+
def get_polarity_score(self, sentence: str) -> float:
18+
return self._get_polarity(sentence)
19+
20+
def get_subjectivity_score(self, sentence: str) -> float:
21+
return self._get_subjectivity(sentence)
22+
23+
def is_neutral(self, sentence: str) -> bool:
24+
return self.get_sentiment(sentence) == "neutral"
25+
26+
def is_positive(self, sentence: str) -> bool:
27+
return self.get_sentiment(sentence) == "positive"
28+
29+
def _get_polarity(self, sentence: str) -> float:
30+
analysis = TextBlob(sentence)
31+
return analysis.sentiment.polarity
32+
33+
def _get_subjectivity(self, sentence: str) -> float:
34+
analysis = TextBlob(sentence)
35+
return analysis.sentiment.subjectivity

requirements.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pre-commit
3434
psycopg2
3535
PyJWT
3636
pydantic
37+
nltk==3.8.1
38+
textblob==0.17.1
3739
pytest>=7.2.0
3840
pytest-cov
3941
pytest-django

requirements.txt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This file is autogenerated by pip-compile with Python 3.12
33
# by the following command:
44
#
5-
# pip-compile
5+
# pip-compile requirements.in
66
#
77
aiodataloader==0.4.0
88
# via -r requirements.in
@@ -83,6 +83,7 @@ click==8.1.7
8383
# click-didyoumean
8484
# click-plugins
8585
# click-repl
86+
# nltk
8687
click-didyoumean==0.3.0
8788
# via celery
8889
click-plugins==1.1.1
@@ -177,6 +178,7 @@ freezegun==1.1.0
177178
# via -r requirements.in
178179
google-api-core[grpc]==2.11.1
179180
# via
181+
# google-api-core
180182
# google-cloud-core
181183
# google-cloud-pubsub
182184
# google-cloud-storage
@@ -249,6 +251,8 @@ jmespath==0.10.0
249251
# via
250252
# boto3
251253
# botocore
254+
joblib==1.4.2
255+
# via nltk
252256
jsonschema==4.14.0
253257
# via drf-spectacular
254258
kombu==5.3.6
@@ -263,6 +267,10 @@ monotonic==1.5
263267
# via analytics-python
264268
multidict==4.7.6
265269
# via yarl
270+
nltk==3.8.1
271+
# via
272+
# -r requirements.in
273+
# textblob
266274
nodeenv==1.5.0
267275
# via pre-commit
268276
oauth2==1.9.0.post1
@@ -307,8 +315,6 @@ packaging==24.1
307315
# pytest
308316
pluggy==1.5.0
309317
# via pytest
310-
polars==1.12.0
311-
# via -r requirements.in
312318
pre-commit==2.11.1
313319
# via -r requirements.in
314320
prometheus-client==0.17.1
@@ -399,7 +405,9 @@ redis==4.4.4
399405
# python-redis-lock
400406
# shared
401407
regex==2023.12.25
402-
# via -r requirements.in
408+
# via
409+
# -r requirements.in
410+
# nltk
403411
requests==2.32.3
404412
# via
405413
# -r requirements.in
@@ -410,7 +418,9 @@ requests==2.32.3
410418
# shared
411419
# stripe
412420
rfc3986[idna2008]==1.4.0
413-
# via httpx
421+
# via
422+
# httpx
423+
# rfc3986
414424
rsa==4.7.2
415425
# via google-auth
416426
s3transfer==0.5.0
@@ -421,7 +431,7 @@ sentry-sdk[celery]==2.13.0
421431
# shared
422432
setproctitle==1.1.10
423433
# via -r requirements.in
424-
shared @ https://github.com/codecov/shared/archive/4db8f12c1a4ad43cedb1a965d2fa8cd8138fc2b3.tar.gz
434+
shared @ https://github.com/codecov/shared/archive/067b2e4ec72cdf1b3213306ea8aaaccbb374aecc.tar.gz
425435
# via -r requirements.in
426436
simplejson==3.17.2
427437
# via -r requirements.in
@@ -457,10 +467,14 @@ stripe==9.6.0
457467
# via -r requirements.in
458468
text-unidecode==1.3
459469
# via faker
470+
textblob==0.17.1
471+
# via -r requirements.in
460472
tlslite-ng==0.8.0b1
461473
# via shared
462474
toml==0.10.2
463475
# via pre-commit
476+
tqdm==4.66.6
477+
# via nltk
464478
typing==3.7.4.3
465479
# via shared
466480
typing-extensions==4.6.2
@@ -514,4 +528,4 @@ zipp==3.19.2
514528
# via importlib-metadata
515529

516530
# The following packages are considered to be unsafe in a requirements file:
517-
# setuptools
531+
# setuptools

0 commit comments

Comments
 (0)