@@ -0,0 +1,2723 @@
+#!/usr/bin/env python%0A%0Avocabulary = dict()%0AoriginalVocabulary = dict()%0Aarticles = list()%0A%0Adef readVocabulary(input, default):%0A    print(input)%0A    with open(input) as fin:%0A        for line in fin:%0A            word = line.rstrip('%5Cn')%0A            if not word or word%5B0%5D == ';': continue%0A            vocabulary%5Bword%5D = default%0A%0Adef readArticle(input, skipColumn=0):%0A    print(input)%0A    article = %5B%5D%0A    with open(input, 'r') as fin:%0A        for line in fin:%0A            row = %5B%5D%0A            for triple in line.rstrip('%5Cn').split(' ')%5BskipColumn:%5D:%0A                splited = triple.split('/')%0A                if len(splited) == 3:%0A                    row.append(splited)%0A            article.append(row)%0A    articles.append(article)%0A%0Adef getLineSentiment(line):%0A    acum = 0%0A    sign = 1%0A    count = 0%0A    for triple in line:%0A        word, proto, label = triple%0A        if word in %5B'.', ',', '!', '?'%5D:%0A            sign = 1%0A        if word in %5B'not'%5D:%0A            sign *= -1%0A        if proto not in vocabulary:%0A            continue%0A        acum += vocabulary%5Bproto%5D * sign%0A        count += 1%0A    return acum / count if count else .0%0A%0Adef processArticle(article, diff):%0A    for line in article:%0A        ls = getLineSentiment(line)%0A        for triple in line:%0A            word, proto, label = triple%0A            if proto not in vocabulary:%0A                continue%0A            if proto not in diff:%0A                diff%5Bproto%5D = %7B 'sum': 0, 'cnt': 0 %7D%0A            diff%5Bproto%5D%5B'sum'%5D += ls%0A            diff%5Bproto%5D%5B'cnt'%5D += 1%0A%0Adef iterate():%0A    for times in range(1):%0A        print('iterate %25d' %25 times)%0A        diff = dict()%0A        for article in articles:%0A            processArticle(article, diff)%0A        for key in diff:%0A            if not diff%5Bkey%5D%5B'cnt'%5D:%0A                continue%0A            vocabulary%5Bkey%5D *= diff%5Bkey%5D%5B'sum'%5D / diff%5Bkey%5D%5B'cnt'%5D%0A%0Adef diff():%0A    diffList = %5B%5D%0A    for key in vocabulary:%0A        old = originalVocabulary%5Bkey%5D%0A        new = vocabulary%5Bkey%5D%0A        d = new-old%0A        if d:%0A            diffList.append((key, old, new, d))%0A    diffList.sort(key=lambda item: abs(item%5B3%5D), reverse=True)%0A    with open('outputs/brute_iterate.txt', 'w') as fout:%0A        fout.write('key%5Ctold%5Ctnew%5Ctdiff%5Cn')%0A        for d in diffList:%0A            fout.write('%25s%5Ct%25.6f%5Ct%25.6f%5Ct%25.6f%5Cn' %25 d)%0A%0A%0AreadVocabulary('inputs/positive-words.txt', +1.0)%0AreadVocabulary('inputs/negative-words.txt', -1.0)%0AoriginalVocabulary = vocabulary.copy()%0A%0AreadArticle('inputs/blender_sentences.txt', skipColumn=1)%0AreadArticle('inputs/drill_POS.txt')%0AreadArticle('inputs/fridge_ann.txt')%0AreadArticle('inputs/movies_ann.txt')%0AreadArticle('inputs/vacuum_ann.txt')%0AreadArticle('inputs/washer_sentences.txt', skipColumn=1)%0A%0Aiterate()%0Adiff()

brute iterate