def test(intercept, featureWeight):
    testFeatures = getFeatures(0)
    testFeatures = getAverage(testFeatures)
    testFeatures = normalize(testFeatures)

    region = testFeatures.region
    import json
    b = json.dumps(testFeatures.region)
    f = open("teststruct_region.json",'wb')
    f.write(b)
    f.close()

    score = []
    for i in range(len(region)):
        score.append({})
    fOut = open("../txt/result.txt",'w')
    for i in range(len(region)):
        for query in region[i]:
            score[i][query] = {}
            for url in region[i][query]:
                score[i][query][url] = \
                dot_product(region[i][query][url],featureWeight) + intercept
            url_sorted_by_value = OrderedDict(sorted(score[i][query].items(), key=lambda x: x[1]))
            url_sorted_by_value_r = list(reversed(url_sorted_by_value))
            out = str(query) + " " + str(i) + " "
            for u in url_sorted_by_value_r:
                out = out + str(u) + " "
            out = out+ "\n"
            fOut.write(out)
import time
import commands
from getFeatures import *
from featureProcess import getAverage
from featureProcess import normalize
from rank import *
print '##################################'
print '#          Program start         #'
print '##################################'
print '-Stage 1- extract features..'
trainStruct = getFeatures(1)
print '-Stage 2- processing features..'
trainStruct = getAverage(trainStruct)
print '-Stage 3- normalizing features..'
trainStruct = normalize(trainStruct)

print '-Stage 4- generating training set for Weka..'
arffGen(trainStruct.region)
time.sleep(1)

print '-Stage 5- getting trained model parameters..'
beta = commands.getoutput("java WekaTester trainClickThrough.arff").split('\n')
intercept = float(beta[0])
beta = beta[1:len(beta)-1]
for i,item in enumerate(beta):
    beta[i] = float(item)
    print beta[i]

print '-Stage 6- get test set..'
test(intercept,beta)