From c2c2b3fef786cc74d6a68cec1bf93408aeed5431 Mon Sep 17 00:00:00 2001 From: Bharat Raghunathan Date: Mon, 4 Mar 2019 15:56:15 +0530 Subject: [PATCH] Fix #565 by raising an Exception for an empty String (#566) * Fix #565 by raising ValueError for empty string * Added unit test for empty string case in PR #566 --- flair/data.py | 3 +++ tests/test_data.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/flair/data.py b/flair/data.py index 2e5d91e054..ec944407c6 100644 --- a/flair/data.py +++ b/flair/data.py @@ -323,6 +323,9 @@ def __init__(self, text: str = None, use_tokenizer: bool = False, labels: Union[ # otherwise assumes whitespace tokenized text else: + # catch the empty string case + if not text: + raise ValueError("Cannot convert empty string to a Sentence object.") # add each word in tokenized string as Token object to Sentence word = '' for index, char in enumerate(text): diff --git a/tests/test_data.py b/tests/test_data.py index b9152ab4cc..34e6b49d72 100644 --- a/tests/test_data.py +++ b/tests/test_data.py @@ -22,6 +22,13 @@ def test_get_head(): assert (token1 == token2.get_head()) assert (None == token1.get_head()) +def test_create_sentence_on_empty_string(): + + with pytest.raises(ValueError) as e: + sentence: Sentence = Sentence('') + + assert (e.type is ValueError) + assert (e.value.args[0] == "Cannot convert empty string to a Sentence object.") def test_create_sentence_without_tokenizer(): sentence: Sentence = Sentence('I love Berlin.')