Beispiel #1
0
def training(request):
    requestResults = dict()
    requestResults.update(csrf(request))
    print request.POST

    if "train1" in request.POST:
        hitList = list()
        misList = list()
        counter = 1
        trainLabel = "train%d" % counter
        while trainLabel in request.POST:
            if request.POST[trainLabel] == "pass":
                pass
            elif request.POST[trainLabel] == "hit":
                text = request.POST["text%d" % counter]
                hitList.append(text)
            elif request.POST[trainLabel] == "mis":
                text = request.POST["text%d" % counter]
                misList.append(text)

            counter = counter + 1
            trainLabel = "train%d" % counter

        bayesian.appendListToDefaultHitFileWithList(hitList)
        bayesian.appendListToDefaultMisFileWithList(misList)
        requestResults["message"] = "Training Completed"
        print "Training Completed"

#    client = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
#    access_token = request.COOKIES["access_token"]
#    expires_in = request.COOKIES["expires_in"]
#    client.set_access_token(access_token, expires_in)
#
#    publicTimeline = client.get.statuses__public_timeline(count=10)
    trainFileName = "trainFile.txt"
    testStringList = bayesian.readListFromFileWithFileName(trainFileName)
    statusesList = list()
    probabilityTable = bayesian.defaultTokensProbabilityTable()

    for item in testStringList:
        state = dict()
        state["text"] = item
        state["probability"] = bayesian.eventProbabilityFromStringAndTokensProbabilityTable(item,
            probabilityTable)
        statusesList.append(state)

    statusesList.sort(key=itemgetter("probability"), reverse=True)

    requestResults["statusesList"] = statusesList
    response = render_to_response("amour/training.html", requestResults)
    return response
Beispiel #2
0
def single(request):
    probabilityTable = bayesian.defaultTokensProbabilityTable()
    requestResults = dict()
    requestResults.update(csrf(request))
    if "string" in request.POST:
        testString = request.POST["string"].strip()
        probability = bayesian.eventProbabilityFromStringAndTokensProbabilityTable(testString, probabilityTable)
        requestResults["text"] = testString
        requestResults["probability"] = probability
    elif "train" in request.POST:
        text = request.POST["text"]
        train = request.POST["train"]
        tempList = [text]
        if train == "hit":
            bayesian.appendListToDefaultHitFileWithList(tempList)
        elif train == "mis":
            bayesian.appendListToDefaultMisFileWithList(tempList)
        requestResults["message"] = "Training Completed"

    response = render_to_response("amour/single.html", requestResults)
    return response