# Buggy code
def __init__(
    self, text: str = None, use_tokenizer: bool = False, labels: List[Label] = None
):
    super(Sentence, self).__init__()

    self.tokens: List[Token] = []

    self.labels: List[Label] = labels

    self._embeddings: Dict = {}

    # if text is passed, instantiate sentence with tokens (words)
    if text is not None:
        # tokenize the text first if option selected
        if use_tokenizer:
            # use segtok for tokenization
            tokens = []
            sentences = split_single(text)
            for sentence in sentences:
                contractions = split_contractions(word_tokenizer(sentence))
                tokens.extend(contractions)

            # determine offsets for whitespace_after field
            index = text.index
            running_offset = 0
            last_word_offset = -1
            last_token = None
            for word in tokens:
                token = Token(word)
                self.add_token(token)
                try:
                    word_offset = index(word, running_offset)
                except:
                    word_offset = last_word_offset + 1
                if word_offset - 1 == last_word_offset and last_token is not None:
                    last_token.whitespace_after = False
                word_len = len(word)
                running_offset = word_offset + word_len
                last_word_offset = running_offset - 1
                last_token = token

        # otherwise assumes whitespace tokenized text
        else:
            # add each word in tokenized string as Token object to Sentence
            for word in text.split(" "):
                token = Token(word)
                self.add_token(token)


# Traceback (ValueError)
Traceback (most recent call last):
File "predict.py", line 8, in <module>
tagger.predict(sentence)
File "/home/projects/ner/flair/flair/models/sequence_tagger_model.py", line 371, in predict
score, tag_seq = self._predict_scores_batch(batch)
File "/home/projects/ner/flair/flair/models/sequence_tagger_model.py", line 386, in _predict_scores_batch
all_feats, tags = self.forward(sentences)
File "/home/projects/ner/flair/flair/models/sequence_tagger_model.py", line 157, in forward
self.embeddings.embed(sentences)
File "/home/projects/ner/flair/flair/embeddings.py", line 115, in embed
embedding.embed(sentences)
File "/home/projects/ner/flair/flair/embeddings.py", line 48, in embed
self._add_embeddings_internal(sentences)
File "/home/projects/ner/flair/flair/embeddings.py", line 268, in _add_embeddings_internal
tokens_sorted_by_length = sorted(tokens_char_indices, key=lambda p: len(p), reverse=True)
File "/home/miniconda3/lib/python3.6/site-packages/torch/onnx/__init__.py", line 68, in wrapper
return fn(*args, **kwargs)
File "/home/miniconda3/lib/python3.6/site-packages/torch/nn/utils/rnn.py", line 141, in pack_padded_sequence
data, batch_sizes = PackPadded.apply(input, lengths, batch_first)
File "/home/miniconda3/lib/python3.6/site-packages/torch/nn/_functions/packing.py", line 12, in forward
raise ValueError("Length of all samples has to be greater than 0, "
ValueError: Length of all samples has to be greater than 0, but found an element in 'lengths' that is <= 0

# Fixed code
def __init__(
    self, text: str = None, use_tokenizer: bool = False, labels: List[Label] = None
):
    super(Sentence, self).__init__()

    self.tokens: List[Token] = []

    self.labels: List[Label] = labels

    self._embeddings: Dict = {}

    # if text is passed, instantiate sentence with tokens (words)
    if text is not None:
        # tokenize the text first if option selected
        if use_tokenizer:
            # use segtok for tokenization
            tokens = []
            sentences = split_single(text)
            for sentence in sentences:
                contractions = split_contractions(word_tokenizer(sentence))
                tokens.extend(contractions)

            # determine offsets for whitespace_after field
            index = text.index
            running_offset = 0
            last_word_offset = -1
            last_token = None
            for word in tokens:
                token = Token(word)
                self.add_token(token)
                try:
                    word_offset = index(word, running_offset)
                except:
                    word_offset = last_word_offset + 1
                if word_offset - 1 == last_word_offset and last_token is not None:
                    last_token.whitespace_after = False
                word_len = len(word)
                running_offset = word_offset + word_len
                last_word_offset = running_offset - 1
                last_token = token

        # otherwise assumes whitespace tokenized text
        else:
            # add each word in tokenized string as Token object to Sentence
            for word in text.split(" "):
                if word:
                    token = Token(word)
                    self.add_token(token)