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

    self.tokens: List[Token] = []

    self.labels: List[Label] = []
    if labels is not None:
        self.add_labels(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:
                try:
                    word_offset = index(word, running_offset)
                    start_position = word_offset
                except:
                    word_offset = last_word_offset + 1
                    start_position = (
                        running_offset + 1 if running_offset > 0 else running_offset
                    )

                token = Token(word, start_position=start_position)
                self.add_token(token)

                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
            word = ""
            for index, char in enumerate(text):
                if char == " ":
                    if len(word) > 0:
                        token = Token(word, start_position=index - len(word))
                        self.add_token(token)

                    word = ""
                else:
                    word += char
            # increment for last token in sentence if not followed by whtespace
            index += 1
            if len(word) > 0:
                token = Token(word, start_position=index - len(word))
                self.add_token(token)


# Traceback (UnboundLocalError)
from flair.data import Sentence
s = ''
Sentence(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\XYZ\AppData\Local\Programs\Python\Python36\lib\site-packages\flair\data.py", line 338, in __init__
index += 1
UnboundLocalError: local variable 'index' referenced before assignment

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

    self.tokens: List[Token] = []

    self.labels: List[Label] = []
    if labels is not None:
        self.add_labels(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:
                try:
                    word_offset = index(word, running_offset)
                    start_position = word_offset
                except:
                    word_offset = last_word_offset + 1
                    start_position = (
                        running_offset + 1 if running_offset > 0 else running_offset
                    )

                token = Token(word, start_position=start_position)
                self.add_token(token)

                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:
            # 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):
                if char == " ":
                    if len(word) > 0:
                        token = Token(word, start_position=index - len(word))
                        self.add_token(token)

                    word = ""
                else:
                    word += char
            # increment for last token in sentence if not followed by whtespace
            index += 1
            if len(word) > 0:
                token = Token(word, start_position=index - len(word))
                self.add_token(token)