import pandas as pd
from sklearn import preprocessing


le = preprocessing.LabelEncoder()

from config import *

def splitPartFile():
    chunks = pd.read_csv(AllFile,iterator = True)
    chunk = chunks.get_chunk(1000000)
    chunk.to_csv(TrainFile,index=0)




def anayApi(df):
    for index, data in df.iterrows():
        apiName = data['api']
        if apiName not in apiMap:
            apiMap[apiName] =  dict((x,0) for x in range(0,6))
        label  = data['label']
        apiMap[apiName][label]+=1
    for key in apiMap:
        print(key,apiMap[key])


def seqCollect(df):
    le.fit(df['api'])
    df['encodedApi'] = le.transform(df['api'])
    outf = open(outtestFile,'w+')
    outf.write('file_id,tid,label,seq\n')
    for x in df.groupby(['file_id','tid']):
        file_id,tid = x[0]    
        apiList = []            
        for index, data in x[1].iterrows():            
            apiList.append(str(data['encodedApi']))
        apiResult = ';'.join(apiList)
        outf.write('%d,%d,%d,%s\n'%(file_id,tid,apiResult))
        print(file_id)
    outf.close()
        




if __name__=='__main__':
    data = pd.read_csv(testFile)
    seqCollect(data)
    # anayApi(data)

