#!/usr/bin/env python3
import os
import argparse
import json
import subprocess
import shutil
print("Searching...")

parser = argparse.ArgumentParser()
parser.add_argument("--json", type=json.loads, required=True, help="the json input")
args, unknown = parser.parse_known_args()
topics=args.json["topic"]["path"]
topics= args.json["topic"]["path"]
mod = args.json["opts"]["model"]

try: 
    threshold='-t '+args.json["opts"]["threshold"]
except:
    threshold=''

try: 
    nfields='-n '+args.json["opts"]["nfields"]
except:
    nfields=''

try: 
    lambdas='-l '+ args.json["opts"]["lambdas"]
except:
    lambdas=''
###################

os.chdir("//elr_files//EntityLinkingRetrieval-ELR-master")
sb1=subprocess.Popen(['python query_combine.py {} '.format(args.json["topic"]["path"])],shell=True)
sb1.wait()
#print(os.listdir('//elr_files//EntityLinkingRetrieval-ELR-master//data//'))

files = os.listdir('data')
for file in files:
    shutil.copy(os.path.join('data', file), '/output')
    
query_json_file='//elr_files//EntityLinkingRetrieval-ELR-master//data//extracted_queries.json'
annotaion_file="//elr_files//EntityLinkingRetrieval-ELR-master//data//extracted_annotations.json"

sp3 = subprocess.Popen(["python -m nordlys.elr.retrieval_elr -q {} -a {} {}  {} {}  {}".format(query_json_file,annotaion_file,mod,threshold,nfields,lambdas)],shell=True)
sp3.wait()

files = os.listdir('my_runs')
for file in files:
    shutil.copy(os.path.join('my_runs', file), '/output')

files = os.listdir('runs')
for file in files:
    shutil.copy(os.path.join('runs', file), '/output')


