From 2e311f78767fbb94751daef46bba1c826ea00f82 Mon Sep 17 00:00:00 2001 From: Mani Sarkar Date: Sat, 3 Oct 2020 07:57:39 +0100 Subject: [PATCH] NaN checks: use math.isnan() to verify/test for NaN values --- nlp_profiler/grammar_quality_check.py | 8 +++++--- nlp_profiler/sentiment_subjectivity.py | 3 ++- nlp_profiler/spelling_quality_check.py | 3 ++- tests/high_level/test_sentiment_polarity.py | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/nlp_profiler/grammar_quality_check.py b/nlp_profiler/grammar_quality_check.py index 56d2b10..a0d85a5 100644 --- a/nlp_profiler/grammar_quality_check.py +++ b/nlp_profiler/grammar_quality_check.py @@ -1,6 +1,8 @@ import language_tool_python + language_tool = language_tool_python.LanguageTool('en-GB') import pandas as pd +import math from nlp_profiler.constants import NOT_APPLICABLE, NaN, DEFAULT_PARALLEL_METHOD, \ GRAMMAR_CHECK_SCORE_COL, GRAMMAR_CHECK_COL @@ -31,13 +33,13 @@ def grammar_check_score(text: str) -> int: return len(matches) -def grammar_quality(score: float) -> str: - if score is NaN: +def grammar_quality(score: int) -> str: + if math.isnan(score): return NOT_APPLICABLE if score == 1: return "1 issue" elif score > 1: - return f"{score} issues" + return f"{int(score)} issues" return "No issues" diff --git a/nlp_profiler/sentiment_subjectivity.py b/nlp_profiler/sentiment_subjectivity.py index 166a403..c6cf69b 100644 --- a/nlp_profiler/sentiment_subjectivity.py +++ b/nlp_profiler/sentiment_subjectivity.py @@ -1,6 +1,7 @@ from textblob import TextBlob from nlp_profiler.constants import NOT_APPLICABLE, NaN +import math ### Sentiment Subjectivity @@ -31,7 +32,7 @@ def sentiment_subjectivity_summarised(subjectivity: str) -> str: def sentiment_subjectivity(score: float) -> str: - if score is NaN: + if math.isnan(score): return NOT_APPLICABLE score = float(score) * 100 diff --git a/nlp_profiler/spelling_quality_check.py b/nlp_profiler/spelling_quality_check.py index ff64669..6fa72c3 100644 --- a/nlp_profiler/spelling_quality_check.py +++ b/nlp_profiler/spelling_quality_check.py @@ -4,6 +4,7 @@ from joblib import Memory from nltk.tokenize import word_tokenize from textblob import Word +import math from nlp_profiler.constants import \ DEFAULT_PARALLEL_METHOD @@ -91,7 +92,7 @@ def actual_spell_check(each_word: str) -> str: # pragma: no cover def spelling_quality(score: float) -> str: - if score is NaN: + if math.isnan(score): return NOT_APPLICABLE score = float(score) * 100 diff --git a/tests/high_level/test_sentiment_polarity.py b/tests/high_level/test_sentiment_polarity.py index 384d296..2da62fd 100644 --- a/tests/high_level/test_sentiment_polarity.py +++ b/tests/high_level/test_sentiment_polarity.py @@ -40,7 +40,7 @@ def test_given_a_text_when_sentiment_analysis_is_applied_then_sentiment_analysis # given, when actual_score = sentiment_polarity_score(text) # then - if expected_polarity_score is NaN: + if math.isnan(expected_polarity_score): assert actual_score is expected_polarity_score else: assert math.isclose(expected_polarity_score, actual_score,