code: def conll_to_brat(
      conll_input_filepath,
      conll_output_filepath,
      brat_original_folder,
      brat_output_folder,
      overwrite=False,
  ):
      """
      convert conll file in conll-filepath to brat annotations and output to brat_output_folder,
      with reference to the existing text files in brat_original_folder
      if brat_original_folder does not exist or contain any text file, then the text files are generated from conll files,
      and conll file is updated with filenames and token offsets accordingly.
  
      conll_input_filepath: path to conll file to convert to brat annotations
      conll_output_filepath: path to output conll file with filename and offsets that are compatible with brat annotations
      brat_original_folder: folder that contains the original .txt (and .ann) files that are formatted according to brat.
                            .txt files are used to check if the token offsets match and generate the annotation from conll.
      brat_output_folder: folder to output the text and brat annotations
                          .txt files are copied from brat_original_folder to brat_output_folder
      """
      verbose = False
      dataset_type = utils.get_basename_without_extension(
          conll_input_filepath
      )
      print(
          "Formatting {0} set from CONLL to BRAT... ".format(
              dataset_type
          ),
          end="",
      )
  
      # if brat_original_folder does not exist or have any text file
      if (
          not os.path.exists(brat_original_folder)
          or len(glob.glob(os.path.join(brat_original_folder, "*.txt")))
          == 0
      ):
          assert conll_input_filepath != conll_output_filepath
          generate_reference_text_file_for_conll(
              conll_input_filepath,
              conll_output_filepath,
              brat_original_folder,
          )
  
      utils.create_folder_if_not_exists(brat_output_folder)
      conll_file = codecs.open(conll_output_filepath, "r", "UTF-8")
  
      previous_token_label = "O"
      previous_filename = ""
      text_filepath = ""
      text = ""
      entity_id = 1
      entities = []
      entity = {}
      for line in conll_file:
          line = line.strip().split(" ")
          # New sentence
          if (
              len(line) == 0
              or len(line[0]) == 0
              or "-DOCSTART-" in line[0]
          ):
              # Add the last entity
              if entity != {}:
                  if verbose:
                      print("entity: {0}".format(entity))
                  entities.append(entity)
                  entity_id += 1
                  entity = {}
              previous_token_label = "O"
              continue
  
          filename = str(line[1])
          # New file
          if filename != previous_filename:
              output_entities(
                  brat_output_folder,
                  previous_filename,
                  entities,
                  text_filepath,
                  text,
                  overwrite=overwrite,
              )
              text_filepath = os.path.join(
                  brat_original_folder, "{0}.txt".format(filename)
              )
              with codecs.open(text_filepath, "r", "UTF-8") as f:
                  text = f.read()
              previous_token_label = "O"
              previous_filename = filename
              entity_id = 1
              entities = []
              entity = {}
  
          label = str(line[-1]).replace("_", "-")  # For LOCATION-OTHER
          if label == "O":
              # Previous entity ended
              if previous_token_label != "O":
                  if verbose:
                      print("entity: {0}".format(entity))
                  entities.append(entity)
                  entity_id += 1
                  entity = {}
              previous_token_label = "O"
              continue
  
          token = {}
          token["text"] = str(line[0])
          token["start"] = int(line[2])
          token["end"] = int(line[3])
          # check that the token text matches the original
          if token["text"] != text[
              token["start"] : token["end"]
          ].replace(" ", "-"):
              print("Warning: conll and brat text do not match.")
              print("\tCONLL: {0}".format(token["text"]))
              print(
                  "\tBRAT : {0}".format(
                      text[token["start"] : token["end"]]
                  )
              )
          token["label"] = label[2:]
  
          if label[:2] == "B-":
              if previous_token_label != "O":
                  # End the previous entity
                  if verbose:
                      print("entity: {0}".format(entity))
                  entities.append(entity)
                  entity_id += 1
              # Start a new entity
              entity = token
          elif label[:2] == "I-":
              # Entity continued
              if previous_token_label == token["label"]:
                  # if there is no newline between the entity and the token
                  if "\n" not in text[entity["end"] : token["start"]]:
                      # Update entity
                      entity["text"] = (
                          entity["text"] + " " + token["text"]
                      )
                      entity["end"] = token["end"]
                  else:  # newline between the entity and the token
                      # End the previous entity
                      if verbose:
                          print("entity: {0}".format(entity))
                      entities.append(entity)
                      entity_id += 1
                      # Start a new entity
                      entity = token
              elif previous_token_label != "O":
                  # TODO: count BI or II incompatibility
                  # End the previous entity
                  if verbose:
                      print("entity: {0}".format(entity))
                  entities.append(entity)
                  entity_id += 1
                  # Start new entity
                  entity = token
              else:  # previous_token_label == 'O'
                  # TODO: count  OI incompatibility
                  # Start new entity
                  entity = token
          previous_token_label = token["label"]
      output_entities(
          brat_output_folder,
          previous_filename,
          entities,
          text_filepath,
          text,
          overwrite=overwrite,
      )
      conll_file.close()
      print("Done.")
  
file_path: NeuroNER/neuroner/conll_to_brat.py
imports: import codecs
  import glob
  import os
  import shutil
  from neuroner import utils
  from neuroner import utils_nlp
label_desc: Use image node_id 4 for calling a global function with example usage: conll_to_brat(conll_input_filepath, conll_output_filepath, brat_original_folder, brat_output_folder, overwrite) without return types