# Buggy code
def _expand_sentence_with_context(self, sentence):
    # remember original sentence
    original_sentence = sentence

    # get left context
    left_context = ""
    while True:
        sentence = sentence.previous_sentence()
        if sentence is None:
            break
        if sentence.is_document_boundary:
            break

        left_context = sentence.to_tokenized_string() + " " + left_context
        left_context = left_context.strip()
        if len(left_context.split(" ")) > self.context_length:
            left_context = " ".join(left_context.split(" ")[-self.context_length :])
            break
    context_length = len(left_context.split(" "))
    original_sentence.left_context = left_context

    # get right context
    sentence = original_sentence
    right_context = ""
    while True:
        sentence = sentence.next_sentence()
        if sentence is None:
            break
        if sentence.is_document_boundary:
            break

        right_context += " " + sentence.to_tokenized_string()
        right_context = right_context.strip()
        if len(right_context.split(" ")) > self.context_length:
            right_context = " ".join(right_context.split(" ")[: self.context_length])
            break
    original_sentence.right_context = right_context

    # make expanded sentence
    expanded_sentence = Sentence()
    expanded_sentence.tokens = [
        Token(token)
        for token in left_context.split(" ")
        + original_sentence.to_tokenized_string().split(" ")
        + right_context.split(" ")
    ]
    return expanded_sentence, context_length


# Traceback (OSError)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-5-2f413379aedd> in <module>
6
7 # load the NER tagger
----> 8 tagger = SequenceTagger.load('best-model.pt')
9
10

c:\users\nicod\miniconda3\lib\site-packages\flair\nn.py in load(cls, model)
86             # see https://github.com/zalandoresearch/flair/issues/351
87             f = file_utils.load_big_file(str(model_file))
---> 88             state = torch.load(f, map_location='cpu')
89
90         model = cls._init_model_with_state_dict(state)

c:\users\nicod\miniconda3\lib\site-packages\torch\serialization.py in load(f, map_location, pickle_module, **pickle_load_args)
527             with _open_zipfile_reader(f) as opened_zipfile:
528                 return _load(opened_zipfile, map_location, pickle_module, **pickle_load_args)
--> 529         return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
530
531

c:\users\nicod\miniconda3\lib\site-packages\torch\serialization.py in _legacy_load(f, map_location, pickle_module, **pickle_load_args)
700     unpickler = pickle_module.Unpickler(f, **pickle_load_args)
701     unpickler.persistent_load = persistent_load
--> 702     result = unpickler.load()
703
704     deserialized_storage_keys = pickle_module.load(f, **pickle_load_args)

c:\users\nicod\miniconda3\lib\site-packages\transformers\tokenization_camembert.py in __setstate__(self, d)
259             raise
260         self.sp_model = spm.SentencePieceProcessor()
--> 261         self.sp_model.Load(self.vocab_file)
262
263     def convert_tokens_to_string(self, tokens):

c:\users\nicod\miniconda3\lib\site-packages\sentencepiece.py in Load(self, model_file, model_proto)
365       if model_proto:
366         return self.LoadFromSerializedProto(model_proto)
--> 367       return self.LoadFromFile(model_file)
368
369

c:\users\nicod\miniconda3\lib\site-packages\sentencepiece.py in LoadFromFile(self, arg)
175
176     def LoadFromFile(self, arg):
--> 177         return _sentencepiece.SentencePieceProcessor_LoadFromFile(self, arg)
178
179     def Init(self,

OSError: Not found: "/home/ubuntu/.cache/torch/transformers/3715e3a4a2de48834619b2a6f48979e13ddff5cabfb1f3409db689f9ce3bb98f.28d30f926f545047fc59da64289371eef0fbdc0764ce9ec56f808a646fcfec59": No such file or directory Error #2

# Fixed code
def _expand_sentence_with_context(self, sentence):
    # remember original sentence
    original_sentence = sentence

    # get left context
    left_context = ""
    while True:
        sentence = sentence.previous_sentence()
        if sentence is None:
            break
        if self.respect_document_boundaries and sentence.is_document_boundary:
            break

        left_context = sentence.to_tokenized_string() + " " + left_context
        left_context = left_context.strip()
        if len(left_context.split(" ")) > self.context_length:
            left_context = " ".join(left_context.split(" ")[-self.context_length :])
            break
    context_length = len(left_context.split(" "))
    original_sentence.left_context = left_context

    # get right context
    sentence = original_sentence
    right_context = ""
    while True:
        sentence = sentence.next_sentence()
        if sentence is None:
            break
        if self.respect_document_boundaries and sentence.is_document_boundary:
            break

        right_context += " " + sentence.to_tokenized_string()
        right_context = right_context.strip()
        if len(right_context.split(" ")) > self.context_length:
            right_context = " ".join(right_context.split(" ")[: self.context_length])
            break
    original_sentence.right_context = right_context

    # make expanded sentence
    expanded_sentence = Sentence()
    expanded_sentence.tokens = [
        Token(token)
        for token in left_context.split(" ")
        + original_sentence.to_tokenized_string().split(" ")
        + right_context.split(" ")
    ]
    return expanded_sentence, context_length