fr -> en code

This commit is contained in:
2025-08-20 16:36:09 +02:00
parent faedb4c80c
commit 9159d2a082
7 changed files with 84 additions and 84 deletions

View File

@@ -4,31 +4,31 @@ from majority_judgment import majority_judgment
verbose=False verbose=False
def collect_votes(votes,candidats,default_mention,warnings): def collect_votes(votes,candidates,default_mention,warnings):
collect={} collect={}
for vote in votes: for vote in votes:
# missing candidat in vote picks the worse one # missing candidate in vote picks the worse one
for candidat in candidats: for candidate in candidates:
if not candidat in vote: if not candidate in vote:
vote[candidat]=default_mention vote[candidate]=default_mention
for candidat,mention in vote.items(): for candidate,mention in vote.items():
if mention > default_mention or mention < 0: if mention > default_mention or mention < 0:
mention = default_mention 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") warnings.append("index de mention invalide, soit la liste des mentions est erronnée soit la mention dans le vote est erronée")
if candidat in collect: if candidate in collect:
if mention in collect[candidat]: if mention in collect[candidate]:
collect[candidat][mention]=collect[candidat][mention]+1 collect[candidate][mention]=collect[candidate][mention]+1
else: else:
collect[candidat][mention]=1 collect[candidate][mention]=1
else: else:
collect[candidat]={mention:1} collect[candidate]={mention:1}
return collect return collect
def arrange_votes_par_candidat(votes,candidats,default_mention): def arrange_votes_by_candidate(votes,candidates,default_mention):
vpc={} vpc={}
for candidat in candidats: for candidate in candidates:
vpc[candidat]=[vote[candidat] if candidat in vote else default_mention for vote in votes] vpc[candidate]=[vote[candidate] if candidate in vote else default_mention for vote in votes]
return vpc return vpc
def jugement_majoritaire(poll): def jugement_majoritaire(poll):
@@ -36,9 +36,9 @@ def jugement_majoritaire(poll):
verbose=False verbose=False
crosscheck=True crosscheck=True
votants=poll['votants']['decompte'] voters=poll['voters']['count']
candidats=poll['candidats'] candidates=poll['candidates']
nombre_candidats=len(candidats) nombre_candidates=len(candidates)
# l'ordre des mentions est de la meilleure à la pire (reject) # l'ordre des mentions est de la meilleure à la pire (reject)
nom_mentions=poll['mentions'] nom_mentions=poll['mentions']
@@ -47,35 +47,35 @@ def jugement_majoritaire(poll):
votes=poll["votation"]["votes"] votes=poll["votation"]["votes"]
if verbose: if verbose:
print('candidats:' + str(candidats)) print('candidates:' + str(candidates))
print(votes) print(votes)
warnings=[] warnings=[]
collect=collect_votes(votes,list(candidats),default_mention,warnings) collect=collect_votes(votes,list(candidates),default_mention,warnings)
if verbose: if verbose:
print(collect) print(collect)
votant_median_check= votants / 2 if votants % 2 == 0 else ((votants-1) / 2) +1 voter_median_check= voters / 2 if voters % 2 == 0 else ((voters-1) / 2) +1
votant_median=votants // 2 + votants % 2 voter_median=voters // 2 + voters % 2
if votant_median_check != votant_median: if voter_median_check != voter_median:
print('[ERROR] le nombre median de votants semble erroné. contactez le developpeur de ce code.' + str(votant_median_check)) print('[ERROR] le nombre median de voters semble erroné. contactez le developpeur de ce code.' + str(voter_median_check))
merite={} merit={}
mention_mediane={} mention_mediane={}
merite_pourcent={} merit_pourcent={}
votes_par_candidat=arrange_votes_par_candidat(votes,candidats,mentions-1) votes_by_candidate=arrange_votes_by_candidate(votes,candidates,mentions-1)
# cumul : du meilleur au pire # cumul : du meilleur au pire
# range_mentions = range(len(mentions)) # range_mentions = range(len(mentions))
# cumul du pire au meilleur # cumul du pire au meilleur
range_mentions = range(mentions-1,-1,-1) range_mentions = range(mentions-1,-1,-1)
for candidat in list(candidats): for candidate in list(candidates):
vote=collect[candidat] vote=collect[candidate]
cumul=0 cumul=0
m=[0 for i in range(mentions)] m=[0 for i in range(mentions)]
mention_m=None mention_m=None
@@ -83,26 +83,26 @@ def jugement_majoritaire(poll):
for mention in range_mentions: for mention in range_mentions:
if mention in vote: if mention in vote:
cumul=cumul+vote[mention] cumul=cumul+vote[mention]
pourcent[mention]= ( 100 * vote[mention] ) / votants pourcent[mention]= ( 100 * vote[mention] ) / voters
if cumul >= votant_median and mention_m is None: if cumul >= voter_median and mention_m is None:
mention_m=mention mention_m=mention
m[mention]=cumul m[mention]=cumul
if cumul < votants: if cumul < voters:
print('[ERROR] le cumul des votes doit correspondre au nombre de votants. contactez le developpeur de ce code.') print('[ERROR] cumulated votes should match voters. Contact this code developer.')
mention_mediane[candidat]=mention_m mention_mediane[candidate]=mention_m
merite[candidat]=m merit[candidate]=m
merite_pourcent[candidat]=pourcent merit_pourcent[candidate]=pourcent
if verbose: if verbose:
print(merite) print(merit)
print(mention_mediane) print(mention_mediane)
# les gagnants sont ceux qui ont la mention mediane minimale ( la meilleure ) # les gagnants sont ceux qui ont la mention mediane minimale ( la meilleure )
found=[] found=[]
for mention in range(0,mentions): for mention in range(0,mentions):
for candidat,merite_median in mention_mediane.items(): for candidate,merit_median in mention_mediane.items():
if merite_median == mention: if merit_median == mention:
found.append({candidat:candidats[candidat]}) found.append({candidate:candidates[candidate]})
if len(found) > 0: if len(found) > 0:
break break
@@ -110,15 +110,15 @@ def jugement_majoritaire(poll):
print(found) print(found)
print(nom_mentions[mention]) print(nom_mentions[mention])
# expanded=expand_collect(collect,list(candidats),mentions) # expanded=expand_collect(collect,list(candidates),mentions)
if verbose: if verbose:
print(votes_par_candidat) print(votes_by_candidatee)
result = {'resultat':{'mention':nom_mentions[mention],'candidats':found},'merite':merite,'profil':merite_pourcent,"warnings":warnings} result = {'result':{'mention':nom_mentions[mention],'candidates':found},'merit':merit,'profil':merit_pourcent,"warnings":warnings}
if crosscheck: if crosscheck:
# cross check with a well known implementation # cross check with a well known implementation
result["crosscheck"]=majority_judgment(votes_par_candidat, reverse=True) result["crosscheck"]=majority_judgment(votes_by_candidate, reverse=True)
return result return result

View File

@@ -21,9 +21,9 @@ if ($verbose) {
var_dump($poll); var_dump($poll);
} }
$votants=$poll['votants']['decompte']; $voters=$poll['voters']['count'];
$candidats=$poll['candidats']; $candidates=$poll['candidates'];
$nombre_candidats=count($candidats); $nombre_candidates=count($candidates);
# l'ordre des mentions est de la meilleure à la pire (reject) # l'ordre des mentions est de la meilleure à la pire (reject)
$nom_mentions=$poll['mentions']; $nom_mentions=$poll['mentions'];
@@ -33,22 +33,22 @@ $default_mention=$mentions-1;
$votes=$poll["votation"]["votes"]; $votes=$poll["votation"]["votes"];
if ($verbose) { if ($verbose) {
print("candidats\n"); print("candidates\n");
var_dump($candidats); var_dump($candidates);
print("votes:\n"); print("votes:\n");
var_dump($votes); var_dump($votes);
} }
$warnings=[]; $warnings=[];
$default_vote=[]; $default_vote=[];
foreach ($candidats as $candidat => $nom_candidat ) { foreach ($candidates as $candidat => $nom_candidat ) {
$default_vote[$candidat]=$default_mention; $default_vote[$candidat]=$default_mention;
} }
$collect=[]; $collect=[];
foreach ($votes as $vote) { foreach ($votes as $vote) {
# missing candidat in vote is the worts one # missing candidat in vote is the worts one
foreach ($candidats as $candidat => $nom_candidat ) { foreach ($candidates as $candidat => $nom_candidat ) {
if (! array_key_exists($candidat,$vote)) { if (! array_key_exists($candidat,$vote)) {
$vote[$candidat]=$default_mention; $vote[$candidat]=$default_mention;
} }
@@ -78,24 +78,24 @@ if ($verbose) {
print(json_encode($collect)); print(json_encode($collect));
} }
$votant_median_check= ($votants % 2 == 0) ? intdiv($votants,2) : intdiv(($votants-1),2) + 1; $voter_median_check= ($voters % 2 == 0) ? intdiv($voters,2) : intdiv(($voters-1),2) + 1;
$votant_median=intdiv($votants, 2) + $votants % 2; $voter_median=intdiv($voters, 2) + $voters % 2;
if ( $votant_median_check != $votant_median ) { if ( $voter_median_check != $voter_median ) {
print('[ERROR] le nombre median de votants (' . $votant_median_check . '/' . $votant_median . ') semble erroné. contactez le developpeur de ce code.'); print('[ERROR] le nombre median de voters (' . $voter_median_check . '/' . $voter_median . ') semble erroné. contactez le developpeur de ce code.');
} }
$merite=[]; $merit=[];
$mention_mediane=[]; $mention_mediane=[];
$merite_pourcent=[]; $merit_pourcent=[];
# cumul : du meilleur au pire # cumul : du meilleur au pire
# range_mentions = range(count(mentions)) # range_mentions = range(count(mentions))
# cumul du pire au meilleur # cumul du pire au meilleur
$range_mentions = $mentions; $range_mentions = $mentions;
foreach ($candidats as $candidat => $nom_candidat) { foreach ($candidates as $candidat => $nom_candidat) {
$vote=$collect[$candidat]; $vote=$collect[$candidat];
$cumul=0; $cumul=0;
for ($i=0; $i < $range_mentions; $i ++) { for ($i=0; $i < $range_mentions; $i ++) {
@@ -106,24 +106,24 @@ foreach ($candidats as $candidat => $nom_candidat) {
for ($mention=$range_mentions -1; $mention >=0; $mention --) { for ($mention=$range_mentions -1; $mention >=0; $mention --) {
if ( array_key_exists($mention,$vote) ) { if ( array_key_exists($mention,$vote) ) {
$cumul=$cumul+$vote[$mention]; $cumul=$cumul+$vote[$mention];
$pourcent[$mention]= ( 100 * $vote[$mention] ) / $votants; $pourcent[$mention]= ( 100 * $vote[$mention] ) / $voters;
} }
if ($cumul >= $votant_median and $mention_m == null ) { if ($cumul >= $voter_median and $mention_m == null ) {
$mention_m=$mention; $mention_m=$mention;
} }
$m[$mention]=$cumul; $m[$mention]=$cumul;
} }
if ($cumul < $votants) { if ($cumul < $voters) {
print("[ERROR] le cumul des votes " . $cumul . " doit correspondre au nombre de votants. contactez le developpeur de ce code."); print("[ERROR] votes cumulated " . $cumul . " should match voters. Contact this code developer.");
} }
$mention_mediane[$candidat]=$mention_m; $mention_mediane[$candidat]=$mention_m;
$merite[$candidat]=$m; $merit[$candidat]=$m;
$merite_pourcent[$candidat]=$pourcent; $merit_pourcent[$candidat]=$pourcent;
} }
if ($verbose) { if ($verbose) {
print("\nmerite:\n"); print("\nmerit:\n");
print(json_encode($merite)); print(json_encode($merit));
print("\nmention_mediane:\n"); print("\nmention_mediane:\n");
print(json_encode($mention_mediane)); print(json_encode($mention_mediane));
} }
@@ -132,10 +132,10 @@ if ($verbose) {
$found=[]; $found=[];
for ($mention = 0; $mention < $mentions; $mention ++) { for ($mention = 0; $mention < $mentions; $mention ++) {
foreach ($mention_mediane as $candidat => $merite_median) foreach ($mention_mediane as $candidat => $merit_median)
if ($merite_median == $mention) { if ($merit_median == $mention) {
$item=[]; $item=[];
$item[$candidat]=$candidats[$candidat]; $item[$candidat]=$candidates[$candidat];
array_push($found,$item); array_push($found,$item);
} }
if (count($found) > 0) { if (count($found) > 0) {
@@ -148,7 +148,7 @@ if ($verbose) {
print($nom_mentions[$mention]); print($nom_mentions[$mention]);
} }
$result = ['resultat'=> ['mention'=>$nom_mentions[$mention],'candidats' => $found],'merite' => $merite,'profil' => $merite_pourcent,"warnings" => $warnings]; $result = ['result'=> ['mention'=>$nom_mentions[$mention],'candidates' => $found],'merit' => $merit,'profil' => $merit_pourcent,"warnings" => $warnings];
print(json_encode($result)); print(json_encode($result));

View File

@@ -1,5 +1,5 @@
{ {
"candidats":{ "candidates":{
"A":"albert", "A":"albert",
"B":"Beatrice", "B":"Beatrice",
"C":"Chloé", "C":"Chloé",
@@ -9,8 +9,8 @@
"G":"Gisèle", "G":"Gisèle",
"H":"Hugo" "H":"Hugo"
}, },
"votants":{ "voters":{
"decompte":10 "count":10
}, },
"mentions":[ "mentions":[
"Très Bien", "Très Bien",

View File

@@ -1,5 +1,5 @@
{ {
"candidats":{ "candidates":{
"A":"albert", "A":"albert",
"B":"Beatrice", "B":"Beatrice",
"C":"Chloé", "C":"Chloé",
@@ -9,8 +9,8 @@
"G":"Gisèle", "G":"Gisèle",
"H":"Hugo" "H":"Hugo"
}, },
"votants":{ "voters":{
"decompte":10 "count":10
}, },
"mentions":[ "mentions":[
"Très Bien", "Très Bien",

View File

@@ -1,5 +1,5 @@
{ {
"candidats":{ "candidates":{
"A":"albert", "A":"albert",
"B":"Beatrice", "B":"Beatrice",
"C":"Chloé", "C":"Chloé",
@@ -9,8 +9,8 @@
"G":"Gisèle", "G":"Gisèle",
"H":"Hugo" "H":"Hugo"
}, },
"votants":{ "voters":{
"decompte":11 "count":11
}, },
"mentions":[ "mentions":[
"Très Bien", "Très Bien",

View File

@@ -1,5 +1,5 @@
{ {
"candidats":{ "candidates":{
"A":"albert", "A":"albert",
"B":"Beatrice", "B":"Beatrice",
"C":"Chloé", "C":"Chloé",
@@ -9,8 +9,8 @@
"G":"Gisèle", "G":"Gisèle",
"H":"Hugo" "H":"Hugo"
}, },
"votants":{ "voters":{
"decompte":11 "count":11
}, },
"mentions":[ "mentions":[
"Très Bien", "Très Bien",

View File

@@ -1,5 +1,5 @@
{ {
"candidats":{ "candidates":{
"A":"albert", "A":"albert",
"B":"Beatrice", "B":"Beatrice",
"C":"Chloé", "C":"Chloé",
@@ -9,8 +9,8 @@
"G":"Gisèle", "G":"Gisèle",
"H":"Hugo" "H":"Hugo"
}, },
"votants":{ "voters":{
"decompte":11 "count":11
}, },
"mentions":[ "mentions":[
"Très Bien", "Très Bien",