Skip to content

Commit 8a79147

Browse files
add pytest
1 parent fd76e3f commit 8a79147

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from textblob import TextBlob
2+
3+
def extract_sentiment(text: str):
4+
'''Extract sentiment using textblob.
5+
Polarity is within range [-1, 1]'''
6+
7+
text = TextBlob(text)
8+
9+
return text.sentiment.polarity
10+
11+
def test_extract_sentiment():
12+
13+
text = "I think today will be a great day"
14+
15+
sentiment = extract_sentiment(text)
16+
17+
assert sentiment > 0
18+
19+
def test_extract_sentiment_negative():
20+
21+
text = "I do not think this will turn out well"
22+
23+
sentiment = extract_sentiment(text)
24+
25+
assert sentiment < 0
26+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
def text_contain_word(word: str, text: str):
3+
'''Find whether the text contains a particular word'''
4+
5+
return word in text
6+
7+
testdata = [
8+
('There is a duck in this text',True),
9+
('There is nothing here', False)
10+
]
11+
12+
@pytest.mark.parametrize('sample, expected_output', testdata)
13+
def test_text_contain_word(sample, expected_output):
14+
15+
word = 'duck'
16+
17+
assert text_contain_word(word, sample) == expected_output
18+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest
2+
from textblob import TextBlob
3+
4+
5+
def extract_sentiment(text: str):
6+
'''Extract sentiment using textblob.
7+
Polarity is within range [-1, 1]'''
8+
9+
text = TextBlob(text)
10+
11+
return text.sentiment.polarity
12+
13+
def text_contain_word(word: str, text: str):
14+
'''Find whether the text contains a particular word'''
15+
16+
return word in text
17+
18+
@pytest.fixture
19+
def example_data():
20+
return 'Today I found a duck and I am happy'
21+
22+
23+
def test_extract_sentiment(example_data):
24+
25+
sentiment = extract_sentiment(example_data)
26+
27+
assert sentiment > 0
28+
29+
def test_text_contain_word(example_data):
30+
31+
word = 'duck'
32+
33+
assert text_contain_word(word, example_data) == True
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from textblob import TextBlob
2+
import pytest
3+
4+
def extract_sentiment(text: str):
5+
'''Extract sentiment using textblob.
6+
Polarity is within range [-1, 1]'''
7+
8+
text = TextBlob(text)
9+
10+
return text.sentiment.polarity
11+
12+
testdata = ["I think today will be a great day","I do not think this will turn out well"]
13+
14+
@pytest.mark.parametrize('sample', testdata)
15+
def test_extract_sentiment(sample):
16+
17+
sentiment = extract_sentiment(sample)
18+
19+
assert sentiment > 0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from textblob import TextBlob
2+
3+
def extract_sentiment(text: str):
4+
'''Extract sentiment using textblob.
5+
Polarity is within range [-1, 1]'''
6+
7+
text = TextBlob(text)
8+
9+
return text.sentiment.polarity
10+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
import os.path
3+
sys.path.append(
4+
os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
5+
from src.process import extract_sentiment
6+
import pytest
7+
8+
9+
def test_extract_sentiment():
10+
11+
text = 'Today I found a duck and I am happy'
12+
13+
sentiment = extract_sentiment(text)
14+
15+
assert sentiment > 0

0 commit comments

Comments
 (0)