Example usage for com.google.common.io LineProcessor LineProcessor

List of usage examples for com.google.common.io LineProcessor LineProcessor

Introduction

In this page you can find the example usage for com.google.common.io LineProcessor LineProcessor.

Prototype

LineProcessor

Source Link

Usage

From source file:com.axelor.web.tags.ScriptTag.java

private List<String> files(final String manifest) throws IOException {

    final List<String> files = new ArrayList<>();
    final URL resource = this.getResource(manifest);
    if (resource == null) {
        return files;
    }/*from w  ww.  j  a va2  s. c  om*/

    try (final InputStream is = resource.openStream()) {
        return CharStreams.readLines(new InputStreamReader(is), new LineProcessor<List<String>>() {
            @Override
            public boolean processLine(String line) throws IOException {
                if (line.startsWith("//= ")) {
                    line = line.substring(3).trim();
                    files.add(line);
                }
                return true;
            }

            @Override
            public List<String> getResult() {
                return files;
            }
        });
    }
}

From source file:org.gradle.configuration.DefaultImportsReader.java

public DefaultImportsReader() {
    try {/*w  w  w .  ja v a  2  s.c  o m*/
        URL url = getClass().getResource(RESOURCE);
        if (url == null) {
            throw new IllegalStateException("Could not load default imports resource: " + RESOURCE);
        }
        this.importPackages = Resources.asCharSource(url, Charsets.UTF_8)
                .readLines(new LineProcessor<String[]>() {
                    private final List<String> packages = Lists.newLinkedList();

                    @Override
                    public boolean processLine(@SuppressWarnings("NullableProblems") String line)
                            throws IOException {
                        packages.add(line.substring(7, line.length() - 2));
                        return true;
                    }

                    @Override
                    public String[] getResult() {
                        return packages.toArray(new String[packages.size()]);
                    }
                });
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:io.scigraph.lucene.SynonymMapSupplier.java

@Override
public SynonymMap get() {
    try {//w  ww  . java2  s.  c o  m
        return Resources.readLines(Resources.getResource("lemmatization.txt"), Charsets.UTF_8,
                new LineProcessor<SynonymMap>() {

                    SynonymMap.Builder builder = new SynonymMap.Builder(true);

                    @Override
                    public boolean processLine(String line) throws IOException {
                        List<String> synonyms = newArrayList(Splitter.on(',').trimResults().split(line));
                        for (String term : synonyms) {
                            for (String synonym : synonyms) {
                                if (!term.equals(synonym)) {
                                    builder.add(new CharsRef(term), new CharsRef(synonym), true);
                                }
                            }
                        }
                        return true;
                    }

                    @Override
                    public SynonymMap getResult() {
                        try {
                            return builder.build();
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                });
    } catch (Exception e) {
        logger.log(Level.WARNING, "Failed to build synonym map", e);
        return null;
    }
}

From source file:org.yomiassist.sourcedata.Edict.java

public Edict() {

    final File dictionaryFile = new File("dictionaries/edict/edict.txt");
    try {//w  ww  . j  a v a  2  s  . c o m
        Files.readLines(dictionaryFile, Charset.forName("EUC-JP"), new LineProcessor<Boolean>() {

            public boolean processLine(String line) throws IOException {
                addDictionaryTerm(line);
                return true;
            }

            public Boolean getResult() {
                return true;
            }
        });

    } catch (FileNotFoundException e) {
        throw new DictionaryException("Could not locate dictionary file " + dictionaryFile, e);
    } catch (IOException e) {
        throw new DictionaryException("Could not load dictionary file " + dictionaryFile, e);
    }
}

From source file:org.yomiassist.sourcedata.KnownWords.java

public KnownWords(Options options) throws IOException {
    File sourceFile = options.vocabularySourceFile;
    knownWords = Files.readLines(sourceFile, Charsets.UTF_8, new LineProcessor<Set<String>>() {

        Set<String> words = new HashSet<String>();

        public boolean processLine(String line) throws IOException {
            final boolean kanaOnly = line.indexOf('-') == 0;
            if (kanaOnly) {
                words.add(line.substring(1));
            } else {
                final String kanjiPortion = line.substring(0, line.indexOf('-'));
                String[] kanjiVariants = kanjiPortion.split("\\s");
                for (String variant : kanjiVariants) {
                    words.add(variant);/*from   w  w w  .j ava  2  s  .c  om*/
                }
            }
            return true;
        }

        public Set<String> getResult() {
            return words;
        }
    });
}

From source file:com.github.steveash.jg2p.seq.SeqInputReader.java

public List<AlignGroup> readInput(CharSource source) throws IOException {
    return source.readLines(new LineProcessor<List<AlignGroup>>() {

        final List<AlignGroup> groups = Lists.newArrayList();
        String lastGroup = "";
        List<Alignment> aligns = Lists.newArrayList();

        @Override//from  ww  w .j a v  a  2  s  .  co  m
        public boolean processLine(String line) throws IOException {
            List<String> fields = caretSplit.splitToList(line);
            if (!lastGroup.equals(fields.get(0))) {
                lastGroup = fields.get(0);
                if (!aligns.isEmpty()) {
                    groups.add(new AlignGroup(aligns));
                }
                aligns.clear();
            }
            Double score = Double.parseDouble(fields.get(1));
            Word input = Word.fromSpaceSeparated(fields.get(2));
            Iterable<String> graphs = pipeSplit.split(fields.get(3));
            Iterable<String> phones = pipeSplit.split(fields.get(4));

            aligns.add(new Alignment(input, Zipper.up(graphs, phones), score));
            return true;
        }

        @Override
        public List<AlignGroup> getResult() {
            if (!aligns.isEmpty()) {
                groups.add(new AlignGroup(aligns));
            }
            return groups;
        }
    });
}

From source file:com.davidbracewell.ml.utils.LibSVMFormat.java

@Override
public List<Instance> read(Resource location) throws IOException {
    Preconditions.checkNotNull(location);
    return location.read(new LineProcessor<List<Instance>>() {
        final List<Instance> instances = Lists.newArrayList();

        @Override//from ww  w . j av  a 2 s .c om
        public boolean processLine(String line) throws IOException {
            if (!Strings.isNullOrEmpty(line)) {
                String[] parts = line.split("\\s+");

                Instance inst = new SparseInstance(features);
                boolean cv = parts[0].equals("+1") || parts[0].equals("1");
                inst.setTargetValue(cv);

                for (int j = 1; j < parts.length; j++) {
                    String[] data = parts[j].split(":");
                    int fnum = Integer.parseInt(data[0]) - 1;
                    while (features.size() <= fnum) {
                        features.add(FeatureType.REAL.create("F" + features.size()));
                    }
                    double val = Double.parseDouble(data[1]);
                    inst.set(fnum, val);
                }

                instances.add(inst);
            }
            return true;
        }

        @Override
        public List<Instance> getResult() {
            return instances;
        }
    }

    );
}

From source file:com.android.idegen.ModuleIndexes.java

public void build() throws IOException {

    moduleNameToMakeFileMap = Maps.newHashMap();
    makeFileToModuleNamesMap = Maps.newHashMap();
    logger.info("Building index from " + indexFile.getCanonicalPath());
    Files.readLines(indexFile, Charset.forName("UTF-8"), new LineProcessor<Object>() {
        int count = 0;

        @Override/*from  www . j a va2  s  .c  o m*/
        public boolean processLine(String line) throws IOException {
            count++;
            String[] arr = line.split(":");
            if (arr.length < 2) {
                logger.log(Level.WARNING, "Ignoring index line " + count + ". Bad format: " + line);
            } else {
                String makeFile = arr[0];
                String moduleName = arr[1];
                moduleNameToMakeFileMap.put(moduleName, makeFile);
                append(makeFile, moduleName);
            }
            return true;
        }

        @Override
        public Object getResult() {
            return null;
        }
    });
}

From source file:io.druid.data.input.MapPopulator.java

/**
 * Read through the `source` line by line and populate `map` with the data returned from the `parser`
 *
 * @param source The ByteSource to read lines from
 * @param map    The map to populate// ww w .j ava  2s .c o m
 *
 * @return The number of entries parsed
 *
 * @throws IOException
 */
public long populate(final ByteSource source, final Map<K, V> map) throws IOException {
    return source.asCharSource(Charsets.UTF_8).readLines(new LineProcessor<Long>() {
        private long count = 0l;

        @Override
        public boolean processLine(String line) throws IOException {
            map.putAll(parser.parse(line));
            ++count;
            return true;
        }

        @Override
        public Long getResult() {
            return count;
        }
    });
}

From source file:edu.sdsc.solr.lemmatization.LemmatizationWriter.java

Multimap<String, String> buildSynonymMap() throws IOException {
    final Multimap<String, String> synonyms = HashMultimap.create();
    synonyms.putAll(spec.getExtraSynonyms());

    Files.readLines(uncompressedDictionary, Charsets.UTF_8, new LineProcessor<Void>() {

        @Override/*from www  .j a  v a  2s. c o m*/
        public boolean processLine(String line) throws IOException {
            List<String> cols = newArrayList(Splitter.on('\t').split(line));
            String language = cols.get(0);
            if (!spec.getLanguages().contains(language)) {
                return true;
            }
            Optional<String> synonym = DefinitionParser.getSynonym(cols.get(3), spec);
            if (synonym.isPresent()) {
                synonyms.put(synonym.get(), cols.get(1).trim());
            }
            return true;
        }

        @Override
        public Void getResult() {
            return null;
        }
    });
    return synonyms;
}