Le code est maintenant fait pour traiter du JSON venant du site rankedchoices.com
This commit is contained in:
+43
-7
@@ -4,20 +4,31 @@ 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 main(String[] args) throws IOException {
|
||||
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[]>();
|
||||
@@ -50,12 +61,32 @@ public class ScrutinConDOrcet {
|
||||
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 = new String[candidats.size()];
|
||||
candidats.toArray(kdidats);
|
||||
String[] kdidats = candidats.stream()
|
||||
.map(s -> s.startsWith("Proposition ")?s.substring("Proposition ".length()):s)
|
||||
.toArray(i -> new String[i]);
|
||||
return new Election(out,kdidats);
|
||||
|
||||
|
||||
@@ -65,6 +96,8 @@ public class ScrutinConDOrcet {
|
||||
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);
|
||||
@@ -77,7 +110,7 @@ public class ScrutinConDOrcet {
|
||||
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+" sont pour et "+votesj+" sont contre");
|
||||
System.out.println(candidats[i]+">"+candidats[j]+": "+votesi+" yes, "+votesj+" no");
|
||||
|
||||
if(votesi>votesj)
|
||||
duels[i]++;
|
||||
@@ -85,10 +118,13 @@ public class ScrutinConDOrcet {
|
||||
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());
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in New Issue
Block a user