Example usage for org.apache.commons.csv CSVFormat DEFAULT

List of usage examples for org.apache.commons.csv CSVFormat DEFAULT

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat DEFAULT.

Prototype

CSVFormat DEFAULT

To view the source code for org.apache.commons.csv CSVFormat DEFAULT.

Click Source Link

Document

Standard comma separated format, as for #RFC4180 but allowing empty lines.

Usage

From source file:org.gitia.jdataanalysis.JDataAnalysis.java

private void obtainData() {
    try {/* w ww .j  ava  2  s  . c o  m*/
        CSVFormat csvf;
        if (this.isHeader) {
            csvf = CSVFormat.DEFAULT.withHeader();

            parser = new CSVParser(new FileReader(path), csvf);
            datos = IteratorUtils.toList(parser.iterator());
            data = new String[datos.size()][datos.get(0).size()];
            for (int i = 0; i < datos.size(); i++) {
                for (int j = 0; j < datos.get(0).size(); j++) {
                    data[i][j] = datos.get(i).get(j);
                }
            }

        } else {
            csvf = CSVFormat.DEFAULT.withIgnoreHeaderCase(isHeader);

            CsvMapper mapper = new CsvMapper();
            // important: we need "array wrapping" (see next section) here:
            mapper.enable(CsvParser.Feature.WRAP_AS_ARRAY);
            File csvFile = new File("src/main/resources/handwrittennumbers/mnist_train_in.csv"); // or from String, URL etc
            MappingIterator<double[]> it = mapper.readerFor(double[].class).readValues(csvFile);
            int a = 1;
            List<double[]> listData = it.readAll();
            double[][] data = new double[listData.size()][listData.get(0).length];
            for (int i = 0; i < listData.size(); i++) {
                data[i] = listData.get(i);
                System.out.println(a++ + ":\t");
            }
            SimpleMatrix A = new SimpleMatrix(data);
            A.print();

        }
        parser = new CSVParser(new FileReader(path), csvf);
        datos = IteratorUtils.toList(parser.iterator());
        data = new String[datos.size()][datos.get(0).size()];
        for (int i = 0; i < datos.size(); i++) {
            for (int j = 0; j < datos.get(0).size(); j++) {
                data[i][j] = datos.get(i).get(j);
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(JDataAnalysis.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.hawkular.datamining.forecast.CSVTimeSeriesReader.java

public static List<DataPoint> getData(String fileName) throws IOException {

    fileName = TestDirectory.pathPrefix + fileName;
    File fileToRead = new File(fileName);
    Reader in = new FileReader(fileToRead);

    Iterable<CSVRecord> records = CSVFormat.DEFAULT.withAllowMissingColumnNames(true).withHeader("").parse(in);

    List<DataPoint> dataPoints = new ArrayList<>();
    long counter = 0;
    for (CSVRecord record : records) {
        String value = record.get(0);

        Double doubleValue = null;
        try {// w  ww.  j  a  va2  s  . c  o m
            doubleValue = Double.parseDouble(value);
        } catch (NumberFormatException ex) {
            continue;
        }

        DataPoint dataPoint = new DataPoint(doubleValue, counter++);
        dataPoints.add(dataPoint);
    }

    return dataPoints;
}

From source file:org.hoteia.qalingo.translation.LoaderTranslationUtil.java

public static final void buildMessagesProperties(String currentPath, String project, String filePath,
        List<String> activedLanguages, String defaultLanguage, String inputEncoding, String outputEncoding) {
    try {/*from w  w  w .  ja v  a 2 s .c  o m*/
        String newPath = currentPath + project + LoaderTranslation.PROPERTIES_PATH;
        File folderProject = new File(newPath);
        if (!folderProject.exists()) {
            folderProject.mkdirs();
        }

        InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(filePath)),
                inputEncoding);
        LOG.info("File CSV encoding: " + inputEncoding);
        LOG.info("File properties encoding: " + outputEncoding);

        CSVFormat csvFormat = CSVFormat.DEFAULT;

        CSVParser readerCSV = new CSVParser(reader, csvFormat);
        List<CSVRecord> records = null;
        try {
            records = readerCSV.getRecords();
        } catch (Exception e) {
            LOG.error("Failed to load: " + filePath, e);
        }

        String prefixFileName = "";
        CSVRecord firstLineRecord = records.get(0);
        String[] prefixFileNameTemp = firstLineRecord.get(0).split("## Filename :");
        if (prefixFileNameTemp.length > 1) {
            prefixFileName = prefixFileNameTemp[1].trim();
        }

        String prefixKey = "";
        CSVRecord secondLineRecord = records.get(1);
        String[] prefixKeyTemp = secondLineRecord.get(0).split("## PrefixKey :");
        if (prefixKeyTemp.length > 1) {
            prefixKey = prefixKeyTemp[1].trim();
        }

        Map<String, Integer> availableLanguages = new HashMap<String, Integer>();
        for (CSVRecord record : records) {
            String firstCell = record.get(0);
            if (firstCell.contains("Prefix") && record.size() > 1) {
                String secondCell = record.get(1);
                if (secondCell.contains("Key")) {
                    for (int i = 2; i < record.size(); i++) {
                        String languageCode = record.get(i);
                        availableLanguages.put(languageCode, new Integer(i));
                    }
                    break;
                }
            }
        }

        // BUILD DEFAULT FILE
        String fileFullPath = newPath + "/" + prefixFileName + ".properties";
        Integer defaultLanguagePosition = availableLanguages.get(defaultLanguage);
        buildMessagesProperties(records, fileFullPath, prefixKey, defaultLanguage, defaultLanguagePosition,
                outputEncoding, true);

        for (Iterator<String> iterator = availableLanguages.keySet().iterator(); iterator.hasNext();) {
            String languageCode = (String) iterator.next();
            if (activedLanguages.contains(languageCode)) {
                Integer languagePosition = availableLanguages.get(languageCode);
                String languegFileFullPath = newPath + "/" + prefixFileName + "_" + languageCode
                        + ".properties";
                buildMessagesProperties(records, languegFileFullPath, prefixKey, languageCode,
                        languagePosition.intValue(), outputEncoding, false);
            }
        }
        LOG.info(newPath + "/" + prefixFileName + ".properties");

    } catch (Exception e) {
        LOG.info("Exception", e);
    }
}

From source file:org.italiangrid.storm.webdav.authz.vomap.MapfileVOMembershipSource.java

private CSVParser getParser() {

    try {//from   ww w .  jav  a 2s .  c  o m
        return new CSVParser(new FileReader(mapFile), CSVFormat.DEFAULT);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:org.jvalue.ods.processor.adapter.CsvSourceAdapter.java

@Inject
CsvSourceAdapter(@Assisted DataSource source,
        @Assisted(SourceAdapterFactory.ARGUMENT_SOURCE_URL) String sourceUrl,
        @Assisted(SourceAdapterFactory.ARGUMENT_CSV_FORMAT) String csvFormatString, MetricRegistry registry) {

    super(source, sourceUrl, registry);
    switch (csvFormatString) {
    case "DEFAULT":
        csvFormat = CSVFormat.DEFAULT;
        break;//www .ja  v  a  2 s . c  o m

    case "EXCEL":
        csvFormat = CSVFormat.EXCEL;
        break;

    case "MYSQL":
        csvFormat = CSVFormat.MYSQL;
        break;

    case "RFC4180":
        csvFormat = CSVFormat.RFC4180;
        break;

    case "TDF":
        csvFormat = CSVFormat.TDF;
        break;

    default:
        throw new IllegalArgumentException("unknown csv format \"" + csvFormatString + "\"");
    }
}

From source file:org.languagetool.rules.spelling.morfologik.suggestions_ordering.SuggestionsOrdererTest.java

public static void main(String[] args) throws IOException {
    Map<String, JLanguageTool> ltMap = new HashMap<>();
    Map<String, Rule> rules = new HashMap<>();
    Map<String, SuggestionsOrderer> ordererMap = new HashMap<>();
    final AtomicInteger numOriginalCorrect = new AtomicInteger(0), numReorderedCorrect = new AtomicInteger(0),
            numOtherCorrect = new AtomicInteger(0), numBothCorrect = new AtomicInteger(0),
            numTotalReorderings = new AtomicInteger(0), numMatches = new AtomicInteger(0);
    AtomicLong totalReorderingComputationTime = new AtomicLong(0),
            totalHunspellComputationTime = new AtomicLong(0);
    Runtime.getRuntime()//ww w .j av  a  2s  . co m
            .addShutdownHook(new Thread(() -> System.out.printf(
                    "%n**** Correct Suggestions ****%nBoth: %d / Original: %d / Reordered: %d / Other: %d%n"
                            + "Average time per reordering: %fms / Average time in match(): %fms%n",
                    numBothCorrect.intValue(), numOriginalCorrect.intValue(), numReorderedCorrect.intValue(),
                    numOtherCorrect.intValue(),
                    (double) totalReorderingComputationTime.get() / numTotalReorderings.get(),
                    (double) totalHunspellComputationTime.get() / numMatches.get())));
    SuggestionsOrdererConfig.setNgramsPath(args[1]);
    try (CSVParser parser = new CSVParser(new FileReader(args[0]),
            CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
        for (CSVRecord record : parser) {
            String lang = record.get("language");
            String covered = record.get("covered");
            String replacement = record.get("replacement");
            String sentenceStr = record.get("sentence");

            if (lang.equals("auto") || !(lang.equals("en-US") || lang.equals("de-DE"))) { // TODO: debugging only
                continue; // TODO do language detection?
            }
            Language language = Languages.getLanguageForShortCode(lang);
            JLanguageTool lt = ltMap.computeIfAbsent(lang, langCode -> new JLanguageTool(language));
            Rule spellerRule = rules.computeIfAbsent(lang, langCode -> lt.getAllRules().stream()
                    .filter(Rule::isDictionaryBasedSpellingRule).findFirst().orElse(null));
            if (spellerRule == null) {
                continue;
            }
            SuggestionsOrderer orderer = null;
            try {
                orderer = ordererMap.computeIfAbsent(lang,
                        langCode -> new SuggestionsOrdererGSoC(language, null, spellerRule.getId()));
            } catch (RuntimeException ignored) {
            }
            if (orderer == null) {
                continue;
            }
            numMatches.incrementAndGet();
            AnalyzedSentence sentence = lt.getAnalyzedSentence(sentenceStr);
            long startTime = System.currentTimeMillis();
            RuleMatch[] matches = spellerRule.match(sentence);
            totalHunspellComputationTime.addAndGet(System.currentTimeMillis() - startTime);
            for (RuleMatch match : matches) {
                String matchedWord = sentence.getText().substring(match.getFromPos(), match.getToPos());
                if (!matchedWord.equals(covered)) {
                    //System.out.println("Other spelling error detected, ignoring: " + matchedWord + " / " + covered);
                    continue;
                }
                List<String> original = match.getSuggestedReplacements();
                SuggestionsOrdererConfig.setMLSuggestionsOrderingEnabled(true);
                numTotalReorderings.incrementAndGet();
                startTime = System.currentTimeMillis();
                List<String> reordered = orderer.orderSuggestionsUsingModel(original, matchedWord, sentence,
                        match.getFromPos());
                totalReorderingComputationTime.addAndGet(System.currentTimeMillis() - startTime);
                SuggestionsOrdererConfig.setMLSuggestionsOrderingEnabled(false);
                if (original.isEmpty() || reordered.isEmpty()) {
                    continue;
                }
                String firstOriginal = original.get(0);
                String firstReordered = reordered.get(0);
                if (firstOriginal.equals(firstReordered)) {
                    if (firstOriginal.equals(replacement)) {
                        numBothCorrect.incrementAndGet();
                    } else {
                        numOtherCorrect.incrementAndGet();
                    }
                    //System.out.println("No change for match: " + matchedWord);
                } else {
                    System.out.println("Ordering changed for match " + matchedWord + ", before: "
                            + firstOriginal + ", after: " + firstReordered + ", choosen: " + replacement);
                    if (firstOriginal.equals(replacement)) {
                        numOriginalCorrect.incrementAndGet();
                    } else if (firstReordered.equals(replacement)) {
                        numReorderedCorrect.incrementAndGet();
                    } else {
                        numOtherCorrect.incrementAndGet();
                    }
                }
            }
        }
    }
}

From source file:org.languagetool.rules.spelling.suggestions.SuggestionChangesTest.java

public void testChanges() throws IOException, InterruptedException {

    File configFile = new File(System.getProperty("config", "SuggestionChangesTestConfig.json"));
    ObjectMapper mapper = new ObjectMapper(new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS));
    SuggestionChangesTestConfig config = mapper.readValue(configFile, SuggestionChangesTestConfig.class);

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
    String timestamp = dateFormat.format(new Date());
    Path loggingFile = Paths.get(config.logDir, String.format("suggestionChangesExperiment_%s.log", timestamp));
    Path datasetFile = Paths.get(config.logDir, String.format("suggestionChangesExperiment_%s.csv", timestamp));

    BufferedWriter writer = Files.newBufferedWriter(loggingFile);
    CSVPrinter datasetWriter = new CSVPrinter(Files.newBufferedWriter(datasetFile),
            CSVFormat.DEFAULT.withEscape('\\'));
    List<String> datasetHeader = new ArrayList<>(
            Arrays.asList("sentence", "correction", "covered", "replacement", "dataset_id"));

    SuggestionsChanges.init(config, writer);
    writer.write("Evaluation configuration: \n");
    String configContent = String.join("\n", Files.readAllLines(configFile.toPath()));
    writer.write(configContent);/* w w  w. ja va  2  s.c  om*/
    writer.write("\nRunning experiments: \n");
    int experimentId = 0;
    for (SuggestionChangesExperiment experiment : SuggestionsChanges.getInstance().getExperiments()) {
        experimentId++;
        writer.write(String.format("#%d: %s%n", experimentId, experiment));
        datasetHeader.add(String.format("experiment_%d_suggestions", experimentId));
        datasetHeader.add(String.format("experiment_%d_metadata", experimentId));
        datasetHeader.add(String.format("experiment_%d_suggestions_metadata", experimentId));
    }
    writer.newLine();
    datasetWriter.printRecord(datasetHeader);

    BlockingQueue<SuggestionTestData> tasks = new LinkedBlockingQueue<>(1000);
    ConcurrentLinkedQueue<Pair<SuggestionTestResultData, String>> results = new ConcurrentLinkedQueue<>();
    List<SuggestionTestThread> threads = new ArrayList<>();
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
        SuggestionTestThread worker = new SuggestionTestThread(tasks, results);
        worker.start();
        threads.add(worker);
    }

    // Thread for writing results from worker threads into CSV
    Thread logger = new Thread(() -> {
        try {
            long messages = 0;
            //noinspection InfiniteLoopStatement
            while (true) {
                Pair<SuggestionTestResultData, String> message = results.poll();
                if (message != null) {
                    writer.write(message.getRight());

                    SuggestionTestResultData result = message.getLeft();
                    int datasetId = 1 + config.datasets.indexOf(result.getInput().getDataset());
                    if (result != null && result.getSuggestions() != null && !result.getSuggestions().isEmpty()
                            && result.getSuggestions().stream()
                                    .noneMatch(m -> m.getSuggestedReplacements() == null
                                            || m.getSuggestedReplacements().isEmpty())) {

                        List<Object> record = new ArrayList<>(Arrays.asList(result.getInput().getSentence(),
                                result.getInput().getCorrection(), result.getInput().getCovered(),
                                result.getInput().getReplacement(), datasetId));
                        for (RuleMatch match : result.getSuggestions()) {
                            List<String> suggestions = match.getSuggestedReplacements();
                            record.add(mapper.writeValueAsString(suggestions));
                            // features extracted by SuggestionsOrdererFeatureExtractor
                            record.add(mapper.writeValueAsString(match.getFeatures()));
                            List<SortedMap<String, Float>> suggestionsMetadata = new ArrayList<>();
                            for (SuggestedReplacement replacement : match.getSuggestedReplacementObjects()) {
                                suggestionsMetadata.add(replacement.getFeatures());
                            }
                            record.add(mapper.writeValueAsString(suggestionsMetadata));
                        }
                        datasetWriter.printRecord(record);
                    }

                    if (++messages % 1000 == 0) {
                        writer.flush();
                        System.out.printf("Evaluated %d corrections.%n", messages);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    logger.setDaemon(true);
    logger.start();

    // format straight from database dump
    String[] header = { "id", "sentence", "correction", "language", "rule_id", "suggestion_pos",
            "accept_language", "country", "region", "created_at", "updated_at", "covered", "replacement",
            "text_session_id", "client" };

    int datasetId = 0;
    // read data, send to worker threads via queue
    for (SuggestionChangesDataset dataset : config.datasets) {

        writer.write(String.format("Evaluating dataset #%d: %s.%n", ++datasetId, dataset));

        CSVFormat format = CSVFormat.DEFAULT;
        if (dataset.type.equals("dump")) {
            format = format.withEscape('\\').withNullString("\\N").withHeader(header);
        } else if (dataset.type.equals("artificial")) {
            format = format.withEscape('\\').withFirstRecordAsHeader();
        }
        try (CSVParser parser = new CSVParser(new FileReader(dataset.path), format)) {
            for (CSVRecord record : parser) {

                String lang = record.get("language");
                String rule = dataset.type.equals("dump") ? record.get("rule_id") : "";
                String covered = record.get("covered");
                String replacement = record.get("replacement");
                String sentence = record.get("sentence");
                String correction = record.isSet("correction") ? record.get("correction") : "";
                String acceptLanguage = dataset.type.equals("dump") ? record.get("accept_language") : "";

                if (sentence == null || sentence.trim().isEmpty()) {
                    continue;
                }

                if (!config.language.equals(lang)) {
                    continue; // TODO handle auto maybe?
                }
                if (dataset.type.equals("dump") && !config.rule.equals(rule)) {
                    continue;
                }

                // correction column missing in export from doccano; workaround
                if (dataset.enforceCorrect && !record.isSet("correction")) {
                    throw new IllegalStateException("enforceCorrect in dataset configuration enabled,"
                            + " but column 'correction' is not set for entry " + record);
                }

                if (dataset.type.equals("dump") && dataset.enforceAcceptLanguage) {
                    if (acceptLanguage != null) {
                        String[] entries = acceptLanguage.split(",", 2);
                        if (entries.length == 2) {
                            String userLanguage = entries[0]; // TODO: what to do with e.g. de-AT,de-DE;...
                            if (!config.language.equals(userLanguage)) {
                                continue;
                            }
                        }
                    }
                }

                tasks.put(new SuggestionTestData(lang, sentence, covered, replacement, correction, dataset));
            }
        }

    }

    for (Thread t : threads) {
        t.join();
    }
    logger.join(10000L);
    logger.interrupt();
    datasetWriter.close();
}

From source file:org.languagetool.rules.spelling.SuggestionsChangesTest.java

/***
 * TODO: document// ww  w .j av a  2s.c  om
 * @throws IOException
 */
@Test
public void testChanges() throws IOException {

    String correctionsFileLocation = System.getProperty("correctionsFileLocation");
    assertNotEquals("needs corrections data", null, correctionsFileLocation);

    String testMode = System.getProperty("suggestionsTestMode");
    assertThat(testMode, is(anyOf(equalTo("A"), equalTo("B"), equalTo("AB"))));

    if (testMode.equals("A") || testMode.equals("B")) {
        String modeValue = testMode.equals("A") ? "0" : "1";
        System.setProperty("SuggestionsChangesTestAlternativeEnabled", modeValue);
    }

    String languagesValue = System.getProperty("languages");
    Set<Language> languages = new HashSet<>();
    if (languagesValue == null) { // default -> all languages
        languages.addAll(Languages.get());
    } else {
        for (String langCode : languagesValue.split(",")) {
            languages.add(Languages.getLanguageForShortCode(langCode));
        }
    }

    Random sampler = new Random(0);
    final float SAMPLE_RATE = 1f;

    Map<String, JLanguageTool> ltMap = new HashMap<>();
    Map<String, Rule> rules = new HashMap<>();
    final AtomicInteger numOriginalCorrect = new AtomicInteger(0), numReorderedCorrect = new AtomicInteger(0),
            numOtherCorrect = new AtomicInteger(0), numBothCorrect = new AtomicInteger(0),
            numMatches = new AtomicInteger(0), numCorrectSuggestion = new AtomicInteger(0),
            numTotal = new AtomicInteger(0);
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        if (testMode.equals("AB")) {
            System.out.printf(
                    "%n**** Correct Suggestions ****%nBoth: %d / Original: %d / Reordered: %d / Other: %d%n",
                    numBothCorrect.intValue(), numOriginalCorrect.intValue(), numReorderedCorrect.intValue(),
                    numOtherCorrect.intValue());
            int total = numOriginalCorrect.intValue() + numReorderedCorrect.intValue()
                    + numOtherCorrect.intValue() + numBothCorrect.intValue();
            float accuracyA = (float) (numBothCorrect.intValue() + numOriginalCorrect.intValue()) / total;
            float accuracyB = (float) (numBothCorrect.intValue() + numReorderedCorrect.intValue()) / total;
            System.out.printf("**** Accuracy ****%nA: %f / B: %f%n", accuracyA, accuracyB);
        } else {
            String name = testMode.equals("A") ? "Original" : "Alternative";
            int correct = numCorrectSuggestion.intValue();
            int total = numTotal.intValue();
            float percentage = 100f * ((float) correct / total);
            System.out.printf("%n**** Correct Suggestions ****%n %s: %d / %d (%f%%)%n", name, correct, total,
                    percentage);
        }
    }));
    try (CSVParser parser = new CSVParser(new FileReader(correctionsFileLocation),
            CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
        for (CSVRecord record : parser) {

            if (sampler.nextFloat() > SAMPLE_RATE) {
                continue;
            }

            String lang = record.get("language");
            String covered = record.get("covered");
            String replacement = record.get("replacement");
            //String sentenceStr = record.get("sentence");

            if (lang.equals("auto")) {
                continue; // TODO do language detection?
            }
            Language language = Languages.getLanguageForShortCode(lang);

            if (!languages.contains(language)) {
                continue;
            }

            JLanguageTool lt = ltMap.computeIfAbsent(lang, langCode -> {
                try {
                    JLanguageTool tool = new JLanguageTool(language);
                    tool.activateLanguageModelRules(new File("ngrams/"));
                    return tool;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
            Rule spellerRule = rules.computeIfAbsent(lang, langCode -> lt.getAllRules().stream()
                    .filter(Rule::isDictionaryBasedSpellingRule).findFirst().orElse(null));
            if (spellerRule == null) {
                continue;
            }
            numMatches.incrementAndGet();
            //AnalyzedSentence sentence = lt.getAnalyzedSentence(sentenceStr);
            AnalyzedSentence sentence = lt.getAnalyzedSentence(covered);

            if (testMode.equals("AB")) {
                System.setProperty("SuggestionsChangesTestAlternativeEnabled", "0");
                RuleMatch[] originalMatches = spellerRule.match(sentence);
                System.setProperty("SuggestionsChangesTestAlternativeEnabled", "1");
                RuleMatch[] alternativeMatches = spellerRule.match(sentence);
                assertEquals(originalMatches.length, alternativeMatches.length);

                for (int i = 0; i < originalMatches.length; i++) {
                    RuleMatch original = originalMatches[i];
                    RuleMatch alternative = alternativeMatches[i];

                    String matchedWord = sentence.getText().substring(original.getFromPos(),
                            original.getToPos());
                    String matchedWord2 = sentence.getText().substring(alternative.getFromPos(),
                            alternative.getToPos());
                    assertEquals(matchedWord, matchedWord2);
                    if (!matchedWord.equals(covered)) {
                        //System.out.println("Other spelling error detected, ignoring: " + matchedWord + " / " + covered);
                        continue;
                    }
                    List<String> originalSuggestions = original.getSuggestedReplacements();
                    List<String> alternativeSuggestions = alternative.getSuggestedReplacements();
                    if (originalSuggestions.size() == 0 || alternativeSuggestions.size() == 0) {
                        continue;
                    }
                    String firstOriginal = originalSuggestions.get(0);
                    String firstAlternative = alternativeSuggestions.get(0);
                    if (firstOriginal.equals(firstAlternative)) {
                        if (firstOriginal.equals(replacement)) {
                            numBothCorrect.incrementAndGet();
                        } else {
                            numOtherCorrect.incrementAndGet();
                        }
                        System.out.println("No change for match: " + matchedWord);
                    } else {
                        String correct;
                        if (firstOriginal.equals(replacement)) {
                            numOriginalCorrect.incrementAndGet();
                            correct = "A";
                        } else if (firstAlternative.equals(replacement)) {
                            numReorderedCorrect.incrementAndGet();
                            correct = "B";
                        } else {
                            numOtherCorrect.incrementAndGet();
                            correct = "other";
                        }
                        System.out.printf(
                                "Ordering changed for match %s, before: %s, after: %s, choosen: %s, correct: %s%n",
                                matchedWord, firstOriginal, firstAlternative, replacement, correct);
                    }
                }

            } else {
                RuleMatch[] matches = spellerRule.match(sentence);

                for (RuleMatch match : matches) {
                    String matchedWord = sentence.getText().substring(match.getFromPos(), match.getToPos());
                    if (!matchedWord.equals(covered)) {
                        //System.out.println("Other spelling error detected, ignoring: " + matchedWord + " / " + covered);
                        continue;
                    }
                    List<String> suggestions = match.getSuggestedReplacements();
                    if (suggestions.size() == 0) {
                        continue;
                    }
                    String first = suggestions.get(0);
                    numTotal.incrementAndGet();
                    System.out.printf("Correction for %s: %s %s / chosen: %s -> position %d%n", covered, first,
                            suggestions.subList(1, Math.min(suggestions.size(), 5)), replacement,
                            suggestions.indexOf(replacement));
                    if (first.equals(replacement)) {
                        numCorrectSuggestion.incrementAndGet();
                    }
                }
            }
        }
    }
}

From source file:org.logstash.dependencies.Dependency.java

/**
 * Reads dependencies from the specified stream using the Ruby dependency report format
 * and adds them to the supplied set./*w w  w .j av a  2s .  co  m*/
 */
static void addDependenciesFromRubyReport(InputStream stream, SortedSet<Dependency> dependencies)
        throws IOException {
    Reader in = new InputStreamReader(stream);
    for (CSVRecord record : CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in)) {
        dependencies.add(Dependency.fromRubyCsvRecord(record));
    }
}

From source file:org.logstash.dependencies.Dependency.java

/**
 * Reads dependencies from the specified stream using the Java dependency report format
 * and adds them to the supplied set.//from w ww. ja  v a 2s .c o  m
 */
static void addDependenciesFromJavaReport(InputStream stream, SortedSet<Dependency> dependencies)
        throws IOException {
    Reader in = new InputStreamReader(stream);
    for (CSVRecord record : CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in)) {
        dependencies.add(Dependency.fromJavaCsvRecord(record));
    }
}