Пример #1
0
def check(testfile_text, answerfile_text, alg):
    stats = {} #answered, unanswered, total
    stats['correct'] = stats['unanswered'] = stats['total'] = 0
    ##print testfile_text[:50]
    statements = parse_input.init(text = testfile_text)
    ##print statements[1:3]
    for stat in statements:
        stat.alg_lambda = {}
    alg.run(statements)
    answers = answerfile_text.split('\n')
    answer_count = -1
    
    for i in range(len(statements)):
        stat = statements[i]
        if stat.issued_by == '$$$':
            ##stat.print_details()
            answer_count += 1
            stats['total'] += 1
            if stat.alg_lambda == {}:
                stats['unanswered'] += 1
                continue
            alg_answer = sorted(stat.alg_lambda, key=stat.alg_lambda.get, reverse = True)[-1]
            ##print answer, alg_answer
            if answers[answer_count] == alg_answer: 
                stats['correct'] += 1
            ##stat.print_details()
            ##statements[i+1].print_details()
    return stats
Пример #2
0
def run(logtext, answertext, alg_list):
    
    '''Takes a list of algorithms and assigns final probabilities to each 
statement in statements

Each module in alg_list must contain a function called run(). 
This function takes the list of statements as an argument and should 
populate a probability dictionary for users.

For more information, refer to the Readme.''' 
    
    if answertext:
        answers = answertext.split('\n')
    deleted_nicks = 0
    correct = 0
    wrong = 0
    unanswered = 0
    answer_list = []

    print '-----------------'*12
    print
    print 'Test using', alg_list
    print
    print '-----------------'*12
    print 
    print 

    statements = parse_input.init(text=logtext)
    final_statements = []
    for stat in statements:
        stat.__class__ = final_prob_statement
        stat.init()
        final_statements.append(stat)
        
    
    for alg in alg_list:
        for stat in statements:
            stat.alg_lambda = {}
        alg.run(statements)
        for i in range(len(statements)):
            final_statements[i].alg_lambdas[str(alg)] = statements[i].alg_lambda
            

    for i in range(len(final_statements)):
        stat = final_statements[i]
        if stat.issued_by == '$$$':
            deleted_nicks += 1
            for st in final_statements[(i-prev_display_scope):i+next_display_scope+1]:
                st.print_details()
                print
            for alg in alg_list:
                print
                print alg
                print stat.alg_lambdas[str(alg)]
                print
                for user in stat.alg_lambdas[str(alg)]:
                    if user not in stat.final_lambda:
                        stat.final_lambda[user]=1
                    stat.final_lambda[user] *= (stat.alg_lambdas[str(alg)][user])
            print 'Final Decision:',
            print stat.final_lambda
            print
            if len(stat.final_lambda)>0:
                answer = sorted(stat.final_lambda, key=lambda x: stat.final_lambda[x], reverse=True)[0]
            else:
                answer = ''
            answer_list.append(answer)
            if answer != '' and stat.final_lambda[answer]!=0:
                print 'Replacing $$$ by', answer
            else:
                print 'Leaving unanswered.'
            print
            if answertext:
                print 'Correct answer: ', answers[deleted_nicks-1]
                if answer == '':
                    unanswered += 1
                elif answer == answers[deleted_nicks-1]:
                    correct += 1
                else:
                    wrong += 1
                print
                print 'So far:'
                print 'Correct: ', correct
                print 'Wrong:', wrong
                print 'Unanswered:', unanswered
                print

##print stat.alg_prob
                    #NEED TO ADD WEIGHTS. This function has to be looked
                    #carefully.
                    ##print user,final_prob
    if answertext:
        success = correct*100/(correct+wrong)
        score = 3*correct - wrong
        correct *= 100/deleted_nicks
        wrong *= 100/deleted_nicks
        unanswered *= 100/deleted_nicks


        print '----------------------'
        print 'RESULT:'
        print '----------------------'
        print
        print success, '% success,', correct, '% correct,', wrong, '% wrong,', unanswered, '% unanswered,', deleted_nicks, 'total.'
        print 'Score:', score
        print
    
    else:
        output = open("Output.txt", 'w')
        for ans in answer_list:
            print ans
            output.write(ans+',')
        output.close()
Пример #3
0
        #list of users fallling out of scope
        popped_users = []
        for user in context_statement.scope:
            context_statement.scope[user] -= scope_weight_minus
            if context_statement.scope[user]<=0:
                popped_users.append(user)
        for user in popped_users:
            context_statement.scope.pop(user)
        #for debugging
        stat.curr_users = context_statement.scope.items()
        
        
    

def run(statements):
    #Populates alg_lamda in each statement
    for stat in statements:
        stat.__class__ = context_statement
    run_alg(statements)
    

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print "Usage: python alg_line_context.py filename"
        sys.exit(1)
    statements = parse_input.init(sys.argv[1])
    run(statements)
    ##for stat in statements:
        ##if stat.issued_by == '$$$':
        ##stat.print_details()
Пример #4
0
def run(logtext, answertext, alg_list):
    '''Takes a list of algorithms and assigns final probabilities to each 
statement in statements

Each module in alg_list must contain a function called run(). 
This function takes the list of statements as an argument and should 
populate a probability dictionary for users.

For more information, refer to the Readme.'''

    if answertext:
        answers = answertext.split('\n')
    deleted_nicks = 0
    correct = 0
    wrong = 0
    unanswered = 0
    answer_list = []

    print '-----------------' * 12
    print
    print 'Test using', alg_list
    print
    print '-----------------' * 12
    print

    statements = parse_input.init(text=logtext)
    final_statements = []
    for stat in statements:
        stat.__class__ = final_prob_statement
        stat.init()
        final_statements.append(stat)

    for alg in alg_list:
        for stat in statements:
            stat.alg_lambda = {}
        alg.run(statements)
        for i in range(len(statements)):
            final_statements[i].alg_lambdas[str(
                alg)] = statements[i].alg_lambda

    for i in range(len(final_statements)):
        stat = final_statements[i]
        if stat.issued_by == '$$$':
            deleted_nicks += 1
            for st in final_statements[(i - prev_display_scope):i +
                                       next_display_scope + 1]:
                st.print_details()
                print
            for alg in alg_list:
                print
                print alg
                print stat.alg_lambdas[str(alg)]
                print
                for user in stat.alg_lambdas[str(alg)]:
                    if user not in stat.final_lambda:
                        stat.final_lambda[user] = 1
                    stat.final_lambda[user] *= (
                        stat.alg_lambdas[str(alg)][user])
            print 'Final Decision:',
            print stat.final_lambda
            print
            if len(stat.final_lambda) > 0:
                answer = sorted(stat.final_lambda,
                                key=lambda x: stat.final_lambda[x],
                                reverse=True)[0]
            else:
                answer = ''
            answer_list.append(answer)
            if answer != '' and stat.final_lambda[answer] != 0:
                print 'Replacing $$$ by', answer
            else:
                print 'Leaving unanswered.'
            print
            if answertext:
                print 'Correct answer: ', answers[deleted_nicks - 1]
                if answer == '':
                    unanswered += 1
                elif answer == answers[deleted_nicks - 1]:
                    correct += 1
                else:
                    wrong += 1
                print
                print 'So far:'
                print 'Correct: ', correct
                print 'Wrong:', wrong
                print 'Unanswered:', unanswered
                print


##print stat.alg_prob
#NEED TO ADD WEIGHTS. This function has to be looked
#carefully.
##print user,final_prob
    if answertext:
        success = correct * 100 / (correct + wrong)
        score = 3 * correct - wrong
        correct *= 100 / deleted_nicks
        wrong *= 100 / deleted_nicks
        unanswered *= 100 / deleted_nicks

        print '----------------------'
        print 'RESULT:'
        print '----------------------'
        print
        print success, '% success,', correct, '% correct,', wrong, '% wrong,', unanswered, '% unanswered,', deleted_nicks, 'total.'
        print 'Score:', score
        print

    else:
        output = open("Output.txt", 'w')
        for ans in answer_list:
            print ans
            output.write(ans + ',')
        output.close()
Пример #5
0
    if len(sys.argv) > 2:
        for option in sys.argv[1:-1]:
            if option == '--online':
                show_online = True
            elif option == '--help':
                print help_text
                sys.exit(0)
            else:
                print "Invalid option."
                print help_text
                sys.exit(1)

    from settings import test_alg

    statements = parse_input.init(filename=sys.argv[-1])
    answers_file = open(sys.argv[-1][:-8] + 'answers.txt',
                        'r')  #opening the corresponding _answers.txt file
    deleted_nicks = 0  #counts how many $$$s were there
    correct_answers = 0  #counts how many the algorithm got correct
    unanswered = 0  #counts how many are unanswered
    test_alg.run(statements)
    for i in range(len(statements)):
        if statements[i].issued_by == '$$$':
            deleted_nicks += 1
            print '-------------------------' * 3
            print test_alg
            print '-------------------------' * 3
            print
            for stat in statements[i - prev_display_scope:i + 1 +
                                   next_display_scope]:
Пример #6
0
    if len(sys.argv)>2:
        for option in sys.argv[1:-1]:
            if option == '--online':
                show_online = True
            elif option == '--help':
                print help_text
                sys.exit(0)
            else:
                print "Invalid option."
                print help_text
                sys.exit(1)
    
    from settings import test_alg
    
    statements = parse_input.init(filename = sys.argv[-1])
    answers_file=open(sys.argv[-1][:-8]+'answers.txt','r') #opening the corresponding _answers.txt file 
    deleted_nicks=0 #counts how many $$$s were there
    correct_answers=0 #counts how many the algorithm got correct
    unanswered=0 #counts how many are unanswered
    test_alg.run(statements)
    for i in range(len(statements)):
        if statements[i].issued_by == '$$$':
            deleted_nicks+=1
            print '-------------------------'*3
            print test_alg 
            print '-------------------------'*3
            print
            for stat in statements[i-prev_display_scope:i+1+next_display_scope]:
		##print stat.__class__
                stat.__class__ = parse_input.statement