# -*- coding: utf-8 -*-
import collections
import math
import q72

eta0 = 0.1
eta = 0.9

def sigmoid(z):
    return 1.0 / (1 + math.exp(-z))

def update(w, features, label, eta):
    z = sum([w[f] for f in features])
    predict = sigmoid(z)
    g = predict - label
    for f in features:
        w[f] = w[f] - (eta * g)

def calculation(d):
    tp = 0
    fp = 0
    fn = 0
    tn = 0
    for x in d.values():
        if (x[0] == x[1] and x[1] == '+1'):
            tp += 1
        elif (x[0] != x[1] and x[1] == '-1'):
            fp += 1
        elif (x[0] != x[1] and x[1] == '+1'):
            fn += 1
        else:
            tn += 1
    ac = float(tp + tn) / float(tp + fp + fn + tn)
    print 'Accuracy:{}'.format(ac)
    pr = float(tp) / float(tp + fp)
    print 'Precision:{}'.format(pr)
    re = float(tp) / float(tp + fn)
    print 'Recall:{}'.format(re)
    fm = float(2 * re * pr) / float(re + pr)
    print 'F-measure:{}'.format(fm)

def main():
    t = 0
    w = collections.defaultdict(float)
    fr = open('sentiment.tsv', 'r')
    for line in fr.readlines():
        features = line.strip('\n').split('\t')
        if features[0] > 0:
            label = 1
        else:
            label = 0
        update(w, features[1:], label, (eta0 * (eta**t)))
        t += 1

    # 学習結果を使って2値分類
    fr = open('q76.txt', 'r')
    d = {}
    for i, line in enumerate(fr.readlines()):
        line = line.rstrip('\n').split('\t')
        ans = line[0]
        words = line[1].split(' ')
        features = q72.get_stem(q72.rm_stopword(words))
        z = sum([w[f] for f in features])
        predict = sigmoid(z)
        if predict >= 0.5:
            # ans(正解ラベル),label(予測ラベル)の並びでリスト化
            d[i] = [ans, '+1']
        else:
            d[i] = [ans, '-1']

    # 正解率/適合率/再現率/F値を算出
    calculation(d)

if __name__ == '__main__':
    main()
