#!/usr/bin/env python
import sys, os.path
from sys import argv
from os import system
from string import *

if len(argv) < 3:
	print("Usage: %s rcv1.topics.txt rcv1-v2.topics.qrels " % (argv[0]))
	sys.exit(1)

def strip(s):
	return s.split('\n')[0]

file1 = argv[1]
assert os.path.exists(file1),"rcv1.topics.txt not found."
labels = [x.strip() for x in open(file1,"r")]

file2 = argv[2]
assert os.path.exists(file2),"rcv1-v2.topics.qrels not found."

label_lists = []
data = [x.split() for x in open(file2,"r")]
prv_id = ""
idx = -1
for line in data:
	label = line[0]
	id = line[1]
	if (prv_id != id):
		label_lists.append(id)
		prv_id = id
		idx = idx + 1
	label_lists[idx] = str(label_lists[idx]) + " " + str(labels.index(label))

out_file = open("id_label","w")
for el in label_lists:
	out_file.write("%s\n" % el)

out_file.close()

