Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #565 by raising an Exception for an empty String #566

Merged
merged 4 commits into from
Mar 4, 2019
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
3 changes: 3 additions & 0 deletions flair/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down