Le code est maintenant fait pour traiter du JSON venant du site rankedchoices.com

This commit is contained in:
Mysaa
2021-05-24 19:09:14 +02:00
parent 81e57251aa
commit 6d3d558606
4 changed files with 89 additions and 7 deletions
@@ -0,0 +1,142 @@
package com.bernard.condorcet;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.json.JSONArray;
import org.json.JSONObject;
public class ScrutinConDOrcet {
public static void main2(String[] args) throws IOException {
Election e = voteReader(new File("/home/mysaa/Desktop/votes"));
List<String> results = condorc(e);
System.out.println(results);
}
public static void main(String[] args) throws IOException {
Election e = jsonVoteReader(new File("/home/mysaa/Documents/eclipse-workspace/Scrutin/manim2.json"));
List<String> results = condorc(e);
System.out.println(results);
}
public static final Election voteReader(File f) throws IOException{
BufferedReader bis = new BufferedReader(new FileReader(f));
List<Integer[]> votes = new ArrayList<Integer[]>();
List<String> candidats = new ArrayList<String>();
String line;
while((line = bis.readLine()) != null) {
int li = line.indexOf("#");
if(li!=-1)
line = line.substring(0, li);
String[] bulletin = line.split(",");
Integer[] bultindex = new Integer[bulletin.length];
for(int i = 0;i<bulletin.length;i++) {
if(!candidats.contains(bulletin[i]))
candidats.add(bulletin[i]);
bultindex[i] = candidats.indexOf(bulletin[i]);
}
votes.add(bultindex);
}
bis.close();
Integer[][] out = new Integer[votes.size()][];
votes.toArray(out);
String[] kdidats = new String[candidats.size()];
candidats.toArray(kdidats);
return new Election(out,kdidats);
}
public static final Election jsonVoteReader(File f) throws IOException{
List<Integer[]> votes = new ArrayList<Integer[]>();
List<String> candidats = new ArrayList<String>();
JSONArray obj = new JSONArray(new String(Files.readAllBytes(f.toPath())));
for(int i = 0;i<obj.length();i++) {
JSONObject tableLine = obj.getJSONObject(i);
String voteStr = tableLine.getString("vote");
JSONArray vote = new JSONArray(voteStr);
if(!tableLine.isNull("name"))
System.out.println(tableLine.getString("name")+" -> "+vote);
Integer[] voteArr = new Integer[vote.length()];
for (int j = 0; j < vote.length(); j++) {
int pos = candidats.indexOf(vote.getString(j));
if(pos==-1) {
// On n'a jamais vu ce candidat, on l'ajoute
candidats.add(vote.getString(j));
pos = candidats.indexOf(vote.getString(j));//Normalement candidats.size()
}
voteArr[j] = pos; // A voté !
}
votes.add(voteArr);
}
Integer[][] out = new Integer[votes.size()][];
votes.toArray(out);
String[] kdidats = candidats.stream()
.map(s -> s.startsWith("Proposition ")?s.substring("Proposition ".length()):s)
.toArray(i -> new String[i]);
return new Election(out,kdidats);
}
public static List<String> condorc(Election e) {
return condorc(e.candidats,e.votes);
}
public static List<String> condorc(String[] candidats,Integer[][] votes) {
System.out.println(votes.length+" votes enregistrés");
final int n = candidats.length;
int[] duels = new int[n];
Arrays.fill(duels, 0);
for(int i = 0;i<n;i++) {
for(int j = i+1;j<n;j++) {
// Duel entre candidat i et candidat j
final int i2 = i,j2 = j;
//Compte le nombre de votes pour (i plutot que j) puis (j plutot que i)
long votesi = Arrays.stream(votes).filter(a -> indexof(a, i2,n) < indexof(a, j2,n)).count();
long votesj = Arrays.stream(votes).filter(a -> indexof(a, j2,n) < indexof(a, i2,n)).count();
System.out.println(candidats[i]+">"+candidats[j]+": "+votesi+" yes, "+votesj+" no");
if(votesi>votesj)
duels[i]++;
else if(votesj>votesi)
duels[j]++;
}
}
return IntStream.range(0, candidats.length)
.boxed()
.sorted((i,j) -> Integer.compare(duels[i],duels[j]))
.map(i->Integer.toString(duels[i])+":"+candidats[i])
.collect(Collectors.toList());
}
public static final int indexof(Integer[] a, int i,int defaultValue) {
return IntStream.range(0, a.length).filter(j->a[j]==i).findAny().orElse(defaultValue);
}
static class Election{
Integer[][] votes;
String[] candidats;
public Election(Integer[][] out, String[] candidats) {
this.votes = out;
this.candidats = candidats;
}
}
}