Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'dev'
Conflicts:
	algorithms/addressal.py
	settings.py
  • Loading branch information
amrav committed Jan 25, 2012
2 parents 18b60d5 + 7e88d61 commit d938cbc
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 62 deletions.
36 changes: 20 additions & 16 deletions algorithms/addressal.py
Expand Up @@ -26,7 +26,7 @@ def address(self, username, address_add_weight):

def find_addressals(text):
users = []
matches = re.findall(r"(.*?):", text)
matches = re.findall(r"\W?(\w+)\W?", text)
for match in matches:
##print match
if match in statement.users:
Expand Down Expand Up @@ -69,29 +69,29 @@ def search_forward(statements, userscore, pos, end, recent_scope, power_weight):
ranges = []
steps = []

params += [10] #recent_scope
ranges += [(2,10)]
params += [7] #recent_scope
ranges += [(5,10)]
steps += [2]

params += [10] #forward_scope
ranges += [(2,10)]
params += [7] #forward_scope
ranges += [(5,10)]
steps += [2]

params += [1] #scope_minus
ranges += [(1,3)]
ranges += [(1,2)]
steps += [1]

params += [4] #address_add_weight
ranges += [(3,10)]
ranges += [(3,7)]
steps += [2]

params += [30] #lambda_threshold
params += [20] #lambda_threshold
ranges += [(1,50)]
steps += [5]
steps += [10]

params += [12] #power_weight
params += [20] #power_weight
ranges += [(10,40)]
steps += [5]
steps += [10]


def run(statements):
Expand Down Expand Up @@ -144,6 +144,7 @@ def run(statements):
if u not in userscore:
userscore[u] = 1
userscore[u] *= pow(userlist[username].recent[u], power_weight)
##print u, ':', userscore[u]
##print "Addressee's recently addressed scores: ", u, userscore[u]
#Do search for addressals forward of the unknown statement
if (i+forward_scope) > len(statements):
Expand All @@ -153,6 +154,7 @@ def run(statements):
search_forward(statements, userscore, i, end, recent_scope, power_weight)

#if directly addressed, set userscore to zero.
print addressals
for luser in addressals:
userscore[luser] = 0

Expand All @@ -171,6 +173,9 @@ def run(statements):
#finally assign these values as lambdas
for luser in userscore:
stat.alg_lambda[luser] = userscore[luser]

print stat.text_str
print stat.alg_lambda

else:
if len(addressals)!=0:
Expand All @@ -185,16 +190,15 @@ def run(statements):
userlist[u].update(scope_minus)

#finally if the algorithm assigns similar scores to the two top scores, then make no judgement.
maxes = sorted(stat.alg_lambda, key=lambda x: stat.alg_lambda[x], reverse=True)
if len(stat.alg_lambda) >= 2:
maxes = sorted(stat.alg_lambda, key=lambda x: stat.alg_lambda[x], reverse=True)[:2]
if stat.alg_lambda[maxes[0]] != 0:
if stat.alg_lambda[maxes[1]]/stat.alg_lambda[maxes[0]] > (lambda_threshold):
if stat.alg_lambda[maxes[1]]/stat.alg_lambda[maxes[0]] > (lambda_threshold):
stat.alg_guess = maxes[0]
stat.alg_lambda = {}



##print
##stat.print_details(online=False)
##print userscore
##for u in userlist:
##print u, ':', userlist[u].recent
##print stat.alg_lambda
Expand Down
21 changes: 17 additions & 4 deletions algorithms/line_context.py
Expand Up @@ -12,18 +12,22 @@
steps = []


params += [1] #issue_scope
params += [5] #issue_scope
ranges += [(1,10)]
steps += [1]

params += [9] #scope_weight
params += [2] #scope_weight
ranges += [(1,10)]
steps += [1]

params += [7] #scope_weight_minus
params += [1] #scope_weight_minus
ranges += [(0,10)]
steps += [1]

params += [50]
ranges += [(50,50)]
steps += [0]

class context_statement(parse_input.statement):
scope = {}
#for debugging:
Expand All @@ -43,6 +47,7 @@ def run_alg(statements):
issue_scope = params[0]
scope_weight = params[1]
scope_weight_minus = params[2]
lambda_threshold = params[3]/100
#function that runs the line context algorithm
for stat in statements:
#assign user_lambdas if required:
Expand All @@ -58,9 +63,17 @@ def run_alg(statements):

for user in context_statement.scope: #Normalise the scope scores by dividing by the average
if avg != 0:
stat.alg_lambda[user] = log(context_statement.scope[user])/avg
context_statement.scope[user] = log(context_statement.scope[user])/avg
stat.alg_lambda[user] = context_statement.scope[user]
##print stat.alg_lambda[user],
##print

#if scores don't cross threshold, don't assign any lambdas
maxes = sorted(context_statement.scope.values(), reverse = True)
if len(maxes)>1 and maxes[0] != 0:
if maxes[1]/maxes[0] > lambda_threshold:
stat.alg_lambda = {}

#Otherwise update scope
elif stat.issued_by not in context_statement.scope:
context_statement.scope[stat.issued_by] = scope_weight*issue_scope
Expand Down
8 changes: 4 additions & 4 deletions optimise.py
Expand Up @@ -50,9 +50,9 @@ def findmax(filename, alg, printmax = True, result_filename = ''):
if printmax != True:
print
print
print '----------'*4
print '----------'*5
print 'Run', count, ': Testing with parameters,', param_perm
print '----------'*4
print '----------'*5

alg.params = param_perm

Expand Down Expand Up @@ -145,8 +145,8 @@ def check(testfile_text, answerfile_text, alg):
return stats

if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage: python optimise.py filename [resultfilename]'
if len(sys.argv) != 3:
print 'Usage: python optimise.py filename resultfilename'
sys.exit(1)
if len(sys.argv) == 3:
findmax(sys.argv[1], opt_alg, printmax = True, result_filename = sys.argv[2])
Expand Down
4 changes: 2 additions & 2 deletions parse_input.py
Expand Up @@ -128,7 +128,7 @@ def __init__(self, time, total_time, issuing_user_name, statement_text, \
#subsequent statements


def print_details(self, full_text=True, online=True, current=True, total_time=True):
def print_details(self, full_text=True, online=False, current=True, total_time=True):
print self.issued_by + ' at ' + self.time
print
if full_text == False:
Expand All @@ -140,7 +140,7 @@ def print_details(self, full_text=True, online=True, current=True, total_time=Tr
print; print
if online==True:
print "Users online:", sorted(self.users_online)
print
print
if total_time==True:
print "Total time:", self.total_time
print
Expand Down
128 changes: 93 additions & 35 deletions run_algorithms.py
@@ -1,13 +1,16 @@
from __future__ import division
import sys
import parse_input
import settings

from settings import alg_list
from settings import next_display_scope
from settings import prev_display_scope

class final_prob_statement(parse_input.statement):
def init(self):
self.final_prob = {}

def run(filename, alg_list):
self.final_lambda = {}
self.alg_lambdas = {}

def run(logtext, answertext, alg_list):

'''Takes a list of algorithms and assigns final probabilities to each
statement in statements
Expand All @@ -18,51 +21,106 @@ def run(filename, alg_list):
For more information, refer to the Readme.'''

statements = parse_input.init(filename)
answers = answertext.split('\n')
deleted_nicks = 0
correct = 0
wrong = 0
unanswered = 0

print '-----------------'*4
print
print 'Test using', alg_list
print
print '-----------------'*4
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:
alg.run(statements)
for stat in statements:
if stat.issued_by == '$$$':
for user in stat.alg_lambda:
if user not in stat.final_prob:
stat.final_prob[user]=1
stat.final_prob[user] *= (stat.alg_lambda[user])
##print stat.alg_prob
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()

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
if len(stat.final_lambda)>0:
answer = sorted(stat.final_lambda, key=lambda x: stat.final_lambda[x], reverse=True)[0]
else:
answer = None
if answer != None and stat.final_lambda[answer]!=0:
print 'Replacing $$$ by', answer
else:
print 'Leaving unanswered.'
print
print 'Correct answer: ', answers[deleted_nicks-1]
if answer == None:
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
return statements


success = correct*100/(correct+wrong)
score = 3*correct - wrong
correct *= 100/deleted_nicks
wrong *= 100/deleted_nicks
unanswered *= 100/deleted_nicks

def print_decisions(statements):
for i in range(len(statements)):

if statements[i].issued_by == '$$$':
print_range = 5
print "--------------------------------------------------"
print "Context:"
for stat in statements[i-print_range:i+1]:
stat.print_details(full_text=True)
stat = statements[i]
for user in stat.final_prob:
if stat.final_prob[user] == max(stat.final_prob.values())\
and user!='$$$':
print "Replacing $$$ by", user
print stat.final_prob
print


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


if __name__ == '__main__':
if len(sys.argv)!=2:
print "Usage: python run_algorithms.py logfile"
print "Usage: python run_algorithms.py test_filename"
sys.exit(1)
#print type(sys.argv[1])
statements = run(sys.argv[1], settings.alg_list)
print_decisions(statements)
test_text = open(sys.argv[1]).read()
answer_text = open(sys.argv[1][:-8]+'answers.txt').read()
run(test_text, answer_text, alg_list)



3 changes: 2 additions & 1 deletion settings.py
Expand Up @@ -9,7 +9,8 @@
prev_display_scope = 4
next_display_scope = 4

alg_list = [line_context, bracket]
alg_list = [line_context, bracket, addressal]
test_alg = addressal
opt_alg = addressal
opt_runs = 3

0 comments on commit d938cbc

Please sign in to comment.