def gen_level(): print("Initializing DAG...", file=sys.stderr) dag = Dag.loads_from(ID_HIERARCHY_FILE) print("Generating level.txt ...", file=sys.stderr) fout = open(LEVEL_FILE, 'w') level_list = dag.level_list() for i in range(len(level_list)): fout.write("{0}\t{1}".format(i, level_list[i]) + "\n") fout.close()
def category2ancestor(): print("Initializing DAG...", file=sys.stderr) dag = Dag.loads_from(ID_HIERARCHY_FILE) print("Generating entity2ancestor.txt ...", file=sys.stderr) fin = open(ENTITY2CATEGORY_FILE) fout = open(ENTITY2ANCESTOR_FILE, 'w') count = 0 for line in fin: if count % 1000 == 0: print("{0}\r".format(count), file=sys.stderr, end="") info = line.split('\t') entity = info[0] clist = [int(c) for c in info[1:]] stats = dag.ancestor_path(clist) fout.write("\t".join([entity] + ["{0}:{1}".format(c[0], c[1]) for c in stats]) + "\n"); count += 1 fout.close() fin.close()
def category2ancestor(): print("Initializing DAG...", file=sys.stderr) dag = Dag.loads_from(ID_HIERARCHY_FILE) print("Generating entity2ancestor.txt ...", file=sys.stderr) fin = open(ENTITY2CATEGORY_FILE) fout = open(ENTITY2ANCESTOR_FILE, 'w') count = 0 for line in fin: if count % 1000 == 0: print("{0}\r".format(count), file=sys.stderr, end="") info = line.split('\t') entity = info[0] clist = [int(c) for c in info[1:]] stats = dag.ancestor_path(clist) fout.write("\t".join([entity] + ["{0}:{1}".format(c[0], c[1]) for c in stats]) + "\n") count += 1 fout.close() fin.close()