code: def read_json(input_filename):
      """
      Read the json file and extract the NER labels
  
      Will not return lines which are not labeled
  
      Return format is a list of lines
      where each line is a tuple: (text, labels)
      labels is a list of maps, {'label':..., 'startOffset':..., 'endOffset':...}
      """
      docs = []
      blank = 0
      unlabeled = 0
      broken = 0
      with open(input_filename, encoding="utf-8") as fin:
          for line_idx, line in enumerate(fin):
              doc = json.loads(line)
              if sorted(doc.keys()) == ["source"]:
                  unlabeled += 1
                  continue
              if "source" not in doc:
                  blank += 1
                  continue
              source = doc["source"]
              entities = None
              for k in doc.keys():
                  if k == "source" or k.endswith("metadata"):
                      continue
                  if "annotations" not in doc[k]:
                      continue
                  annotations = doc[k]["annotations"]
                  if "entities" not in annotations:
                      continue
                  if "entities" in annotations:
                      if entities is not None:
                          raise ValueError(
                              "Found a map with multiple annotations at line %d"
                              % line_idx
                          )
                      entities = annotations["entities"]
                  # entities is now a map such as
                  # [{'label': 'Location', 'startOffset': 0, 'endOffset': 6},
                  #  {'label': 'Location', 'startOffset': 11, 'endOffset': 23},
                  #  {'label': 'NORP', 'startOffset': 66, 'endOffset': 74},
                  #  {'label': 'NORP', 'startOffset': 191, 'endOffset': 214}]
              if entities is None:
                  unlabeled += 1
                  continue
              is_broken = any(
                  any(
                      x not in entity
                      for x in ("label", "startOffset", "endOffset")
                  )
                  for entity in entities
              )
              if is_broken:
                  broken += 1
                  if broken == 1:
                      print(
                          "Found an entity which was missing either label, startOffset, or endOffset"
                      )
                      print(entities)
              docs.append((source, entities))
  
      print(
          "Found %d labeled lines.  %d lines were blank, %d lines were broken, and %d lines were unlabeled"
          % (len(docs), blank, broken, unlabeled)
      )
      return docs
  
file_path: stanza/stanza/utils/datasets/ner/convert_amt.py
imports: import argparse
  import copy
  import json
  from operator import itemgetter
  import sys
  from tqdm import tqdm
  import stanza
  from stanza.utils.datasets.ner.utils import write_sentences
  import stanza.utils.datasets.ner.prepare_ner_file
label_desc: Use image node_id 1 for calling a global function with example usage: read_json(input_filename) and returns: docs