code: def brat_to_conll(input_folder, output_filepath, tokenizer, language):
      """
      Assumes '.txt' and '.ann' files are in the input_folder.
      Checks for the compatibility between .txt and .ann at the same time.
      """
      if tokenizer == "spacy":
          spacy_nlp = spacy.load(language)
      elif tokenizer == "stanford":
          core_nlp = StanfordCoreNLP(
              "http://localhost:{0}".format(9000)
          )
      else:
          raise ValueError(
              "tokenizer should be either 'spacy' or 'stanford'."
          )
      verbose = False
      dataset_type = os.path.basename(input_folder)
      print(
          "Formatting {0} set from BRAT to CONLL... ".format(
              dataset_type
          ),
          end="",
      )
      text_filepaths = sorted(
          glob.glob(os.path.join(input_folder, "*.txt"))
      )
      output_file = codecs.open(output_filepath, "w", "utf-8")
      for text_filepath in text_filepaths:
          base_filename = os.path.splitext(
              os.path.basename(text_filepath)
          )[0]
          annotation_filepath = os.path.join(
              os.path.dirname(text_filepath), base_filename + ".ann"
          )
          # create annotation file if it does not exist
          if not os.path.exists(annotation_filepath):
              codecs.open(annotation_filepath, "w", "UTF-8").close()
  
          text, entities = get_entities_from_brat(
              text_filepath, annotation_filepath
          )
          entities = sorted(
              entities, key=lambda entity: entity["start"]
          )
  
          if tokenizer == "spacy":
              sentences = get_sentences_and_tokens_from_spacy(
                  text, spacy_nlp
              )
          elif tokenizer == "stanford":
              sentences = get_sentences_and_tokens_from_stanford(
                  text, core_nlp
              )
  
          for sentence in sentences:
              inside = False
              previous_token_label = "O"
              for token in sentence:
                  token["label"] = "O"
                  for entity in entities:
                      if (
                          entity["start"]
                          <= token["start"]
                          < entity["end"]
                          or entity["start"]
                          < token["end"]
                          <= entity["end"]
                          or token["start"]
                          < entity["start"]
                          < entity["end"]
                          < token["end"]
                      ):
                          token["label"] = entity["type"].replace(
                              "-", "_"
                          )  # Because the ANN doesn't support tag with '-' in it
  
                          break
                      elif token["end"] < entity["start"]:
                          break
  
                  if len(entities) == 0:
                      entity = {"end": 0}
                  if token["label"] == "O":
                      gold_label = "O"
                      inside = False
                  elif (
                      inside and token["label"] == previous_token_label
                  ):
                      gold_label = "I-{0}".format(token["label"])
                  else:
                      inside = True
                      gold_label = "B-{0}".format(token["label"])
                  if token["end"] == entity["end"]:
                      inside = False
                  previous_token_label = token["label"]
                  if verbose:
                      print(
                          "{0} {1} {2} {3} {4}\n".format(
                              token["text"],
                              base_filename,
                              token["start"],
                              token["end"],
                              gold_label,
                          )
                      )
                  output_file.write(
                      "{0} {1} {2} {3} {4}\n".format(
                          token["text"],
                          base_filename,
                          token["start"],
                          token["end"],
                          gold_label,
                      )
                  )
              if verbose:
                  print("\n")
              output_file.write("\n")
  
      output_file.close()
      print("Done.")
      if tokenizer == "spacy":
          del spacy_nlp
      elif tokenizer == "stanford":
          del core_nlp
  
file_path: NeuroNER/neuroner/brat_to_conll.py
imports: import codecs
  import glob
  import json
  import os
  from pycorenlp import StanfordCoreNLP
  import spacy
  from neuroner import utils_nlp
label_desc: Use image node_id 7 for calling a global function with example usage: brat_to_conll(input_folder, output_filepath, tokenizer, language) without return types