text: # Copyright (c) 2020 MeetKai Inc. All rights reserved.
  
  """Automatically generate a template for type-list. Manual Annotations may be required for WH_LABEL_DICT."""
  import os
  import json
  from glob import glob
  
  import typer
  
  WH_LABEL_DICT = {
      "text": ["What"],
      "genre": ["What"],
      "language": ["What"],
      "country": ["What"],
      "location": ["What"],
      "time": ["What"],
      "date": ["What"],
      "organization": ["What"],
      "image": ["What"],
      "number": ["What"],
      "dollar_value": ["How much"],
      "award": ["What"],
      "movie": ["What"],
      "person": ["Who"],
      "literary_work": ["What"],
      "television_series": ["What"],
      "race": ["What"],
      "count": ["How many"],
      "rating": ["What"],
      "url": ["What"],
      "id": ["What"],
      "thing": ["What"],
      "weight": ["How much"],
      "gender": ["What"],
      "height": ["How much"],
      "music": ["What"],
      "audio": ["What"],
  }
  
  
  def main(data_dir: str = "./data", prop_id: str = "*-props-preprocessed.json"):
      """Organizes types and automatically annotates them for the generation pipeline.
      Meta data includes:
          - start_domains: List of domains that the generation can start from
          - WH: Question prefix associated with each type
          - start_domain: Whether a type can be a start domain or not
  
      Args:
          data_dir: Data directory.
          prop_id: Glob identifier for preprocessed property data.
  
      Outputs:
          type-list-autogenerated.txt: Organized list of types containing metadata required for the pipeline.
      """
      fps = glob(os.path.join(data_dir, prop_id))
  
      prop_list = []
      for fp in fps:
          with open(fp, "r") as f:
              print(f"Reading {fp}")
              data = json.load(f)
              prop_list = prop_list + data
  
      domain_set = set()
      type_set = set()
      for prop in prop_list:
          _domain, _type = prop["type"].split("->")
          domain_set.add(_domain)
          type_set.add(_domain)
          type_set.add(_type)
  
      # start domain
      output = dict()
      output["start_domains"] = list(domain_set)
      output["types"] = dict()
      empty_wh_type_list = []
      for _type in type_set:
          output["types"][_type] = dict()
          output["types"][_type]["WH"] = WH_LABEL_DICT[_type] if _type in WH_LABEL_DICT.keys() else ""
          output["types"][_type]["start_domain"] = True if _type in domain_set else False
  
          if output["types"][_type]["WH"] == "":
              empty_wh_type_list.append(_type)
  
      if empty_wh_type_list:
          print("\nThe following types are missing WH labels (please add manually):")
          print(f"{empty_wh_type_list}\n")
  
      # write to file
      out_fp = os.path.join(data_dir, "type-list-autogenerated.json")
      with open(out_fp, "w") as f:
          json.dump(output, f, indent=4)
          print(f"Saved to {out_fp}")
  
  
  if __name__ == "__main__":
      typer.run(main)
  