Le code est maintenant fait pour traiter du JSON venant du site rankedchoices.com
This commit is contained in:
parent
81e57251aa
commit
6d3d558606
5
.gitignore
vendored
5
.gitignore
vendored
@ -2,3 +2,8 @@
|
||||
.project
|
||||
.settings
|
||||
bin/
|
||||
|
||||
gradlew
|
||||
gradlew.bat
|
||||
.gradle
|
||||
gradle/
|
||||
|
||||
31
build.gradle
Normal file
31
build.gradle
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This generated file contains a sample Java Library project to get you started.
|
||||
* For more details take a look at the Java Libraries chapter in the Gradle
|
||||
* User Manual available at https://docs.gradle.org/6.3/userguide/java_library_plugin.html
|
||||
*/
|
||||
|
||||
plugins {
|
||||
// Apply the java-library plugin to add support for Java Library
|
||||
id 'java-library'
|
||||
}
|
||||
|
||||
repositories {
|
||||
// Use jcenter for resolving dependencies.
|
||||
// You can declare any Maven/Ivy/file repository here.
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// This dependency is exported to consumers, that is to say found on their compile classpath.
|
||||
api 'org.apache.commons:commons-math3:3.6.1'
|
||||
|
||||
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
|
||||
implementation 'com.google.guava:guava:28.2-jre'
|
||||
|
||||
// Use JUnit test framework
|
||||
testImplementation 'junit:junit:4.12'
|
||||
|
||||
compile "org.json:json:20200518"
|
||||
}
|
||||
10
settings.gradle
Normal file
10
settings.gradle
Normal file
@ -0,0 +1,10 @@
|
||||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* The settings file is used to specify which projects to include in your build.
|
||||
*
|
||||
* Detailed information about configuring a multi-project build in Gradle can be found
|
||||
* in the user manual at https://docs.gradle.org/6.3/userguide/multi_project_builds.html
|
||||
*/
|
||||
|
||||
rootProject.name = 'Scrutin'
|
||||
@ -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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user