code: def write_ner_results(filename, batch, preds, predict_tagset):
      if len(batch.tags) != len(preds):
          raise ValueError(
              "Unexpected batch vs pred lengths: %d vs %d"
              % (len(batch.tags), len(preds))
          )
  
      with open(filename, "w", encoding="utf-8") as fout:
          tag_idx = 0
          for b in batch:
              # b[0] is words, b[5] is orig_idx
              # a namedtuple would make this cleaner without being much slower
              text = utils.unsort(b[0], b[5])
              for sentence in text:
                  # TODO: if we change the predict_tagset mechanism, will have to change this
                  sentence_gold = [
                      x[predict_tagset] for x in batch.tags[tag_idx]
                  ]
                  sentence_pred = preds[tag_idx]
                  tag_idx += 1
                  for word, gold, pred in zip(
                      sentence, sentence_gold, sentence_pred
                  ):
                      fout.write("%s\t%s\t%s\n" % (word, gold, pred))
                  fout.write("\n")
  
imports: import sys
  import os
  import time
  from datetime import datetime
  import argparse
  import logging
  import numpy
  import random
  import re
  import json
  import torch
  from torch import nn, optim
  from stanza.models.ner.data import DataLoader
  from stanza.models.ner.trainer import Trainer
  from stanza.models.ner import scorer
  from stanza.models.common import utils
  from stanza.models.common.pretrain import Pretrain
  from stanza.utils.conll import CoNLL
  from stanza.models.common.doc import *
  from stanza.models import _training_logging
  from stanza.utils.confusion import confusion_to_weighted_f1, format_confusion
label_desc: Use image node_id 9 for calling a global function with example usage: write_ner_results(filename, batch, preds, predict_tagset) without return types