Skip to content

Commit

Permalink
Fix flairNLP#565 by raising an Exception for an empty String (flairNL…
Browse files Browse the repository at this point in the history
…P#566)

* Fix flairNLP#565 by raising ValueError for empty string

* Added unit test for empty string case in PR flairNLP#566
  • Loading branch information
bharatr21 authored and kashif committed Mar 4, 2019
1 parent 797c958 commit c2c2b3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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

0 comments on commit c2c2b3f

Please sign in to comment.