#!/usr/bin/python
import json
import sys
import os
import argparse

##  ARGPARSE USAGE  
 ##  <https://docs.python.org/2/howto/argparse.html>  
parser = argparse.ArgumentParser(description="Visualize AlchemyAPI Taxonomy")  
group = parser.add_mutually_exclusive_group()  
group.add_argument("-v", "--verbose", action="store_true")  
group.add_argument("-q", "--quiet", action="store_true")  
parser.add_argument("input", help="The input path to an AlchemyAPI Taxonomy TSV Output file")  
parser.add_argument("output", help="The output path for writing (exporting).")  
args = parser.parse_args()

infile = args.input
outfile = "{0}.d".format(args.output)

print("*Input Path is {0}".format(infile))
print("*Output Path is {0}".format(outfile))

file = open(infile)
graph = open(outfile, "w+")
graph.write("digraph temp {\n")

dict = {}

lines = [line.rstrip('\n') for line in file]
for line in lines:
	
	taxonomy = line.split("\t")[1]
	taxonomy = taxonomy.replace(", ", "_")
	taxonomy = taxonomy.replace(" and ", "_")
	taxonomy = taxonomy.replace(" ", "_")

	score = line.split("\t")[0]

	for entity in taxonomy[1:].split("/") : 
		if entity in dict :
			dict[entity] = float(dict[entity]) + float(score)
		else:
			dict[entity] = score

	graph.write("\t{0} -> {1}\n".format("thing", taxonomy[1:].split("/")[0]))
	graph.write("\t{0}\n".format(taxonomy[1:].replace("/", " -> ")))


for entity in dict :
	score = str(dict[entity])
	pos = score.find("e")
	if pos != -1 : 
		score = score[:pos]
	graph.write("\t{0} [shape=ellipse, height={1}];\n".format(entity, score))

graph.write("}")

graph.close
file.close

@@ -562,16 +562,119 @@
 g).%22)  %0A
+parser.add_argument(%22threshold%22, help=%22The minimum threshold for a value to be useful.%22, type=float)  %0A
 args = p
@@ -1187,50 +1187,189 @@
 %5B0%5D%0A
-%0A%09for entity in taxonomy%5B1:%5D.split(%22/%22) : 
+%09if float(score) %3C args.threshold : %0A%09%09continue%0A%0A%09for entity in taxonomy%5B1:%5D.split(%22/%22) : %0A%09%09print(%22*Score meets threshold %7B0%7D %3E= %7B1%7D for %7B2%7D%22.format(score, args.threshold, entity))
 %0A%09%09i
@@ -1726,16 +1726,55 @@
 e%5B:pos%5D%0A
+%09if float(score) %3E= args.threshold : %0A%09
 %09graph.w

add thresholding