List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
From source file:org.hoteia.qalingo.translation.LoaderTranslationUtil.java
private static void processLineWithValue(DataOutputStream writer, String prefixKey, CSVRecord line, int languagePosition, int linePosition, String outputEncoding) throws UnsupportedEncodingException, IOException { if (line.size() > 1) { String key = prefixKey + "."; String firstCell = line.get(0); if (StringUtils.isNotEmpty(firstCell)) { key = key + I18nKeyUtil.handleKey(firstCell).trim() + "."; }/*ww w.j a va 2s. c o m*/ String secondCell = line.get(1); if (StringUtils.isNotEmpty(secondCell)) { key = key + I18nKeyUtil.handleKey(secondCell).trim(); if (StringUtils.isNotEmpty(secondCell) && line.size() > languagePosition) { String value = line.get(languagePosition); if (value.contains("\\\"")) { LOG.warn("Some properties values contain double quote twice: " + value); value = value.replace("\\\"", "\""); } writer.write(((String) key + "=" + value).getBytes(outputEncoding)); } } if (linePosition != 1) { writer.write(buildCarriageReturn(outputEncoding)); } } }
From source file:org.italiangrid.storm.webdav.authz.vomap.MapfileVOMembershipSource.java
private boolean isValidCSVRecord(CSVRecord r) { if (r.size() > 3) { logger.debug("Invalid CSVRecord: {}. Illegal size: {}", r, r.size()); return false; }/*from w w w. j a v a 2 s . c o m*/ if (!r.get(0).startsWith("/")) { logger.debug("Invalid CSVRecord: {}. Subject does not start with / : {}", r, r.get(0)); return false; } return true; }
From source file:org.italiangrid.storm.webdav.authz.vomap.MapfileVOMembershipSource.java
@Override public Set<String> getVOMembers() { long startTime = System.currentTimeMillis(); Set<String> subjects = new HashSet<String>(); CSVParser parser = getParser();//from w ww .j a v a2s . c om try { List<CSVRecord> records = parser.getRecords(); for (CSVRecord r : records) { if (logger.isDebugEnabled()) { logger.debug("Parsed record: {} for VO {}", r, voName); } if (!isValidCSVRecord(r)) { break; } String subject = r.get(0); if (logger.isDebugEnabled()) { logger.debug("Parsed subject {} as member of VO {}", subject, voName); } @SuppressWarnings("deprecation") String rfcSubject = OpensslNameUtils.opensslToRfc2253(subject); if (logger.isDebugEnabled()) { logger.debug("Converted subject {} to rfc format {}", subject, rfcSubject); } subjects.add(rfcSubject); } } catch (IOException e) { throw new RuntimeException(e); } long totalTime = System.currentTimeMillis() - startTime; logger.debug("Parsing VO {} members from {} took {} msecs.", voName, mapFile, totalTime); return subjects; }
From source file:org.kisoonlineapp.kisoonlineapp.csv.DefaultOutputTemplate.java
@Override protected void outputData(CSVRecord csvRecord) { for (int i = 0; i < csvRecord.size(); i++) { System.out.print("\'" + csvRecord.get(i) + "\', "); }/* ww w. ja v a 2 s . c om*/ }
From source file:org.kisoonlineapp.kisoonlineapp.csv.KisoVeranstalterOutputTemplate.java
@Override protected void outputData(CSVRecord csvRecord) { final int len = fieldInfos.length; for (int i = 0; i < len; i++) { final FieldInfo fi = fieldInfos[i]; final String fieldName = fi.name; final String value = csvRecord.get(fieldName); final String formattedValue = fi.formatValue(value); pw.print(formattedValue);/* ww w.ja v a2s.c o m*/ outputOptionalComma(i, len); } }
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()/*from w w w . j a v a 2 s. c o 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.SuggestionsChangesTest.java
/*** * TODO: document/* w w w . j a va 2 s . c o m*/ * @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
private static Dependency fromRubyCsvRecord(CSVRecord record) { Dependency d = new Dependency(); // name, version, url, license d.name = record.get(0); d.version = record.get(1);// w ww.ja v a 2s . c o m return d; }
From source file:org.logstash.dependencies.Dependency.java
private static Dependency fromJavaCsvRecord(CSVRecord record) { Dependency d = new Dependency(); // artifact,moduleUrl,moduleLicense,moduleLicenseUrl String nameAndVersion = record.get(0); int colonIndex = nameAndVersion.indexOf(':'); if (colonIndex == -1) { String err = String.format("Could not parse java artifact name and version from '%s'", nameAndVersion); throw new IllegalStateException(err); }//from www. j a v a 2 s . c om colonIndex = nameAndVersion.indexOf(':', colonIndex + 1); if (colonIndex == -1) { String err = String.format("Could not parse java artifact name and version from '%s'", nameAndVersion); throw new IllegalStateException(err); } d.name = nameAndVersion.substring(0, colonIndex); d.version = nameAndVersion.substring(colonIndex + 1); // We DON'T read the license info out of this CSV because it is not reliable, we want humans // to use the overrides to ensure our license info is accurate return d; }
From source file:org.logstash.dependencies.ReportGenerator.java
private void readAcceptableLicenses(InputStream stream, List<String> acceptableLicenses) throws IOException { Reader in = new InputStreamReader(stream); for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) { String acceptableLicenseId = record.get(0); if (acceptableLicenseId != null && !acceptableLicenseId.equals("")) { acceptableLicenses.add(acceptableLicenseId); }// www . ja va 2 s. c o m } }