#!/usr/bin/env python

import os.path as path
import sys
import json

root = path.join(path.dirname(path.realpath(__file__)), "..")
sys.path.append(root)

config_dict = json.load(open(path.join(root, 'config', 'server', 'config_py3.json')))
config = config_dict["natural"]

filename = " ".join(sys.argv[1:])
output_filename = filename.split(".")[0] + ".py"

with open(filename) as f:
  lines = f.readlines()

from components.standalone_parser import StandaloneParser
parser = StandaloneParser(parser_name=config['parser'],
                          model_path=path.join(root, config['model_path']),
                          example_processor_name=config['example_processor'],
                          beam_size=config['beam_size'],
                          reranker_path=path.join(root, config['reranker_path']),
                          cuda=False)

output = []
for line in lines:
    hypotheses = parser.parse(line, debug=False)
    print("hypotheses[0]", hypotheses[0])
    output.append(hypotheses[0].code)

with open(output_filename, 'w') as f:
    f.write("\n".join(output))

print("Generated", output_filename)