#!/usr/bin/python
import sys, operator

def kill_stops(filename):
	print "Processing file %s ..." % (filename,)
	words = map(lambda a:a.lower().strip(), open(filename,'r').readlines())
	iwords = filter(lambda (a,b):operator.truth(a), map(lambda a:(indexterms.get(a,0),a), words))
	nfile = open('/Users/titusvdm/indexterme_docs/'+filename.split('/')[-1][:-5]+'iterm','w')
	for ln in iwords:
		nfile.write(ln[1]+'\n')
	nfile.close()

if __name__=="__main__":
	print "Reading filenames..."
	files = filter(operator.truth,
				   map(lambda a:a.strip(), sys.stdin.readlines()))
	print "Reading indexterms..."
	indexlist = map(lambda a:a.strip(), open('freqlist','r').readlines())
	indexterms={}
	for t in indexlist:
		indexterms[t]=1 
	print "Start processing..."
	for f in files:
		kill_stops(f)
