summaryrefslogtreecommitdiff
path: root/inversion/categories_to_json.py
blob: 4931fdc32633654e97733bf20167ead88cc3713d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
import sys
import json

if len(sys.argv) < 2:
  sys.exit('usage: categories_to_json.py infile.txt')

in_fn = sys.argv[1]

with open(in_fn, "r") as f:
  groups = f.read().lower().split("\n\n")
data = {}
for g in groups:
  lines = g.split("\n")
  title = lines[0].lower()
  group = {}
  for line in lines[1:]:
    partz = line.split(") ")
    if len(partz) < 2:
      continue
    klass = int(partz[0])
    label = partz[1].strip()
    if len(partz) > 2:
      print(line)
    group[label] = klass
  data[title] = group

name, ext = os.path.splitext(in_fn)
out_fn = name + '.json'
with open(out_fn, 'w') as outfile:
  json.dump(data, outfile, sort_keys=True, indent=2)