rename into english
This commit is contained in:
124
code/majority_judgment_method/__init__.py
Normal file
124
code/majority_judgment_method/__init__.py
Normal file
@@ -0,0 +1,124 @@
|
||||
import json
|
||||
import sys
|
||||
from majority_judgment import majority_judgment
|
||||
|
||||
verbose=False
|
||||
|
||||
def collect_votes(votes,candidates,default_mention,warnings):
|
||||
collect={}
|
||||
for vote in votes:
|
||||
# missing candidate in vote picks the worse one
|
||||
for candidate in candidates:
|
||||
if not candidate in vote:
|
||||
vote[candidate]=default_mention
|
||||
|
||||
for candidate,mention in vote.items():
|
||||
if mention > default_mention or mention < 0:
|
||||
mention = default_mention
|
||||
warnings.append("index de mention invalide, soit la liste des mentions est erronnée soit la mention dans le vote est erronée")
|
||||
if candidate in collect:
|
||||
if mention in collect[candidate]:
|
||||
collect[candidate][mention]=collect[candidate][mention]+1
|
||||
else:
|
||||
collect[candidate][mention]=1
|
||||
else:
|
||||
collect[candidate]={mention:1}
|
||||
return collect
|
||||
|
||||
def arrange_votes_by_candidate(votes,candidates,default_mention):
|
||||
vpc={}
|
||||
for candidate in candidates:
|
||||
vpc[candidate]=[vote[candidate] if candidate in vote else default_mention for vote in votes]
|
||||
return vpc
|
||||
|
||||
def majority_judgment_run(poll):
|
||||
|
||||
verbose=False
|
||||
crosscheck=True
|
||||
|
||||
voters=poll['voters']['count']
|
||||
candidates=poll['candidates']
|
||||
nombre_candidates=len(candidates)
|
||||
|
||||
# l'ordre des mentions est de la meilleure à la pire (reject)
|
||||
nom_mentions=poll['mentions']
|
||||
mentions=len(nom_mentions)
|
||||
default_mention=mentions-1
|
||||
|
||||
votes=poll["votation"]["votes"]
|
||||
if verbose:
|
||||
print('candidates:' + str(candidates))
|
||||
print(votes)
|
||||
|
||||
warnings=[]
|
||||
|
||||
collect=collect_votes(votes,list(candidates),default_mention,warnings)
|
||||
if verbose:
|
||||
print(collect)
|
||||
|
||||
voter_median_check= voters / 2 if voters % 2 == 0 else ((voters-1) / 2) +1
|
||||
|
||||
voter_median=voters // 2 + voters % 2
|
||||
|
||||
if voter_median_check != voter_median:
|
||||
print('[ERROR] le nombre median de voters semble erroné. contactez le developpeur de ce code.' + str(voter_median_check))
|
||||
|
||||
merit={}
|
||||
mention_mediane={}
|
||||
merit_pourcent={}
|
||||
|
||||
votes_by_candidate=arrange_votes_by_candidate(votes,candidates,mentions-1)
|
||||
|
||||
# cumul : du meilleur au pire
|
||||
# range_mentions = range(len(mentions))
|
||||
# cumul du pire au meilleur
|
||||
range_mentions = range(mentions-1,-1,-1)
|
||||
|
||||
for candidate in list(candidates):
|
||||
vote=collect[candidate]
|
||||
cumul=0
|
||||
m=[0 for i in range(mentions)]
|
||||
mention_m=None
|
||||
pourcent=[0 for i in range(mentions)]
|
||||
for mention in range_mentions:
|
||||
if mention in vote:
|
||||
cumul=cumul+vote[mention]
|
||||
pourcent[mention]= ( 100 * vote[mention] ) / voters
|
||||
if cumul >= voter_median and mention_m is None:
|
||||
mention_m=mention
|
||||
m[mention]=cumul
|
||||
if cumul < voters:
|
||||
print('[ERROR] cumulated votes should match voters. Contact this code developer.')
|
||||
mention_mediane[candidate]=mention_m
|
||||
merit[candidate]=m
|
||||
merit_pourcent[candidate]=pourcent
|
||||
|
||||
if verbose:
|
||||
print(merit)
|
||||
print(mention_mediane)
|
||||
|
||||
# les gagnants sont ceux qui ont la mention mediane minimale ( la meilleure )
|
||||
found=[]
|
||||
for mention in range(0,mentions):
|
||||
for candidate,merit_median in mention_mediane.items():
|
||||
if merit_median == mention:
|
||||
found.append({candidate:candidates[candidate]})
|
||||
if len(found) > 0:
|
||||
break
|
||||
|
||||
if verbose:
|
||||
print(found)
|
||||
print(nom_mentions[mention])
|
||||
|
||||
# expanded=expand_collect(collect,list(candidates),mentions)
|
||||
if verbose:
|
||||
print(votes_by_candidatee)
|
||||
|
||||
result = {'result':{'mention':nom_mentions[mention],'candidates':found},'merit':merit,'profil':merit_pourcent,"warnings":warnings}
|
||||
|
||||
if crosscheck:
|
||||
# cross check with a well known implementation
|
||||
result["crosscheck"]=majority_judgment(votes_by_candidate, reverse=True)
|
||||
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user