@Test(expected = IllegalArgumentException.class) public void readLabelsFromFileWrong1() throws Exception { Scorer.readLabelsFromFile(getFileFromResources("test-wrong1.txt")); }
public static Map<String, Integer> readLabelsFromFile(File file) throws IOException { FileInputStream inputStream = new FileInputStream(file); List<String> lines = IOUtils.readLines(inputStream, UTF8); IOUtils.closeQuietly(inputStream); Map<String, Integer> result = new TreeMap<>(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!line.trim().isEmpty() && !line.trim().startsWith("#")) { String[] split = line.split("\\s+"); if (split.length != 2) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected two whitespace-delimited entries but got '" + line + "' (file " + file.getAbsolutePath() + ")"); } String id = split[0].trim(); int value; try { value = Integer.valueOf(split[1]); } catch (NumberFormatException e) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected an integer but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } if (!(value == 0 || value == 1)) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected 0 or 1 but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } result.put(id, value); } } return result; }
Scorer { public static Map<String, Integer> readLabelsFromFile(File file) throws IOException { FileInputStream inputStream = new FileInputStream(file); List<String> lines = IOUtils.readLines(inputStream, UTF8); IOUtils.closeQuietly(inputStream); Map<String, Integer> result = new TreeMap<>(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!line.trim().isEmpty() && !line.trim().startsWith("#")) { String[] split = line.split("\\s+"); if (split.length != 2) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected two whitespace-delimited entries but got '" + line + "' (file " + file.getAbsolutePath() + ")"); } String id = split[0].trim(); int value; try { value = Integer.valueOf(split[1]); } catch (NumberFormatException e) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected an integer but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } if (!(value == 0 || value == 1)) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected 0 or 1 but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } result.put(id, value); } } return result; } }
Scorer { public static Map<String, Integer> readLabelsFromFile(File file) throws IOException { FileInputStream inputStream = new FileInputStream(file); List<String> lines = IOUtils.readLines(inputStream, UTF8); IOUtils.closeQuietly(inputStream); Map<String, Integer> result = new TreeMap<>(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!line.trim().isEmpty() && !line.trim().startsWith("#")) { String[] split = line.split("\\s+"); if (split.length != 2) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected two whitespace-delimited entries but got '" + line + "' (file " + file.getAbsolutePath() + ")"); } String id = split[0].trim(); int value; try { value = Integer.valueOf(split[1]); } catch (NumberFormatException e) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected an integer but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } if (!(value == 0 || value == 1)) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected 0 or 1 but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } result.put(id, value); } } return result; }  }
Scorer { public static Map<String, Integer> readLabelsFromFile(File file) throws IOException { FileInputStream inputStream = new FileInputStream(file); List<String> lines = IOUtils.readLines(inputStream, UTF8); IOUtils.closeQuietly(inputStream); Map<String, Integer> result = new TreeMap<>(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!line.trim().isEmpty() && !line.trim().startsWith("#")) { String[] split = line.split("\\s+"); if (split.length != 2) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected two whitespace-delimited entries but got '" + line + "' (file " + file.getAbsolutePath() + ")"); } String id = split[0].trim(); int value; try { value = Integer.valueOf(split[1]); } catch (NumberFormatException e) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected an integer but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } if (!(value == 0 || value == 1)) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected 0 or 1 but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } result.put(id, value); } } return result; }  static Map<String, Integer> readLabelsFromFile(File file); static double computeAccuracy(Map<String, Integer> gold,
            Map<String, Integer> predictions); static void main(String[] args); }
Scorer { public static Map<String, Integer> readLabelsFromFile(File file) throws IOException { FileInputStream inputStream = new FileInputStream(file); List<String> lines = IOUtils.readLines(inputStream, UTF8); IOUtils.closeQuietly(inputStream); Map<String, Integer> result = new TreeMap<>(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (!line.trim().isEmpty() && !line.trim().startsWith("#")) { String[] split = line.split("\\s+"); if (split.length != 2) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected two whitespace-delimited entries but got '" + line + "' (file " + file.getAbsolutePath() + ")"); } String id = split[0].trim(); int value; try { value = Integer.valueOf(split[1]); } catch (NumberFormatException e) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected an integer but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } if (!(value == 0 || value == 1)) { throw new IllegalArgumentException("Error on line " + (i + 1) + ", expected 0 or 1 but got '" + split[1] + "' (file " + file.getAbsolutePath() + ")"); } result.put(id, value); } } return result; }  static Map<String, Integer> readLabelsFromFile(File file); static double computeAccuracy(Map<String, Integer> gold,
            Map<String, Integer> predictions); static void main(String[] args);  }