'''
Created on Jul 9, 2013

@author: nicholas
'''
import sys
import re
import os
import subprocess

#an example of the file could be this
#[(8543, 0.99905097), (24296, 0.99758351), (36526, 0.99751455), (31288, 0.99637651), (24999, 0.9907158), (15899, 0.98741746)]
#the first number is the file name of the corpus, and the second is its score.  ignore its score for now and just get the list of 
#releveant docs
#@param path: should be the path to the file generated by SubTopicIdentifier
def parseSubTopicFile(path):
    topics = {}
    
    currTopic = 1
    content = open(path).readlines()
    for eachLine in content:
        eachLine = eachLine.replace("[", "")
        eachLine = eachLine.replace("]", "")
        eachLine = eachLine.replace(' ', '')
        eachLine = eachLine.replace("(","")
        eachLine = re.split("\),",eachLine)
        
        
        temp = []
        for eachTuple in eachLine:
            doc = re.split(",",eachTuple)[0]
            temp.append(doc)
        
        topics[currTopic] = temp
        currTopic +=1
        
    return topics

#@param corpus:  this is the absolute path to the dir containing the corpus files
#@param destination: this is the absolute path to the dir where we want to store to subtopics and thier individual corpi
#@param topics: a dictionary of the topic number and the documents associated with it
def copyCorpus(corpus, destination, topics):
    
    keys = topics.keys()
    
    for eachKey in keys:
        topicNum = str(eachKey)
        print "starting topic num " + topicNum
        
        if(not os.path.exists(destination + "/"+topicNum)):
            os.makedirs(destination + "/" + topicNum)
        
        docs = topics[eachKey]
        for eachDoc in docs:
            
            copy = subprocess.Popen("cp " + corpus +"/"+str(eachDoc) + " " + destination + "/"+ topicNum+ "/", shell=True)
            copy.wait()
        
    

#this file requires three parameters
#@param sys.argv[1]: the path to the file indicating which corpus docs go with which subtopic.  this file is generated by SubTopicIdentifier
#@param sys.argv[2]:  this is the absolute path to the dir containing the corpus files
#@param sys.argv[3]: this is the absolute path to the dir where we want to store to subtopics and thier individual corpi
if __name__ == '__main__':
    
    subTopicFilePath= sys.argv[1]
    corpusPath = sys.argv[2]
    outputDir = sys.argv[3]
    
    topics = parseSubTopicFile(subTopicFilePath)
    copyCorpus(corpusPath, outputDir, topics)
    print "All DONE!"
    
    
    
    
    