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.davidbracewell.text.morphology.EnglishLemmatizer.java

/**
 * Instantiates a new English lemmatizer.
 *///from  ww w .  java  2s  .  com
protected EnglishLemmatizer() {
    rules.put(POS.NOUN, new DetachmentRule("s", ""));
    rules.put(POS.NOUN, new DetachmentRule("ses", "s"));
    rules.put(POS.NOUN, new DetachmentRule("xes", "x"));
    rules.put(POS.NOUN, new DetachmentRule("zes", "z"));
    rules.put(POS.NOUN, new DetachmentRule("ies", "y"));
    rules.put(POS.NOUN, new DetachmentRule("shes", "sh"));
    rules.put(POS.NOUN, new DetachmentRule("ches", "ch"));
    rules.put(POS.NOUN, new DetachmentRule("men", "man"));
    loadException(POS.NOUN);

    rules.put(POS.VERB, new DetachmentRule("s", ""));
    rules.put(POS.VERB, new DetachmentRule("ies", "y"));
    rules.put(POS.VERB, new DetachmentRule("es", "s"));
    rules.put(POS.VERB, new DetachmentRule("es", ""));
    rules.put(POS.VERB, new DetachmentRule("ed", "e"));
    rules.put(POS.VERB, new DetachmentRule("ed", ""));
    rules.put(POS.VERB, new DetachmentRule("ing", "e"));
    rules.put(POS.VERB, new DetachmentRule("ing", ""));
    loadException(POS.VERB);

    rules.put(POS.ADJECTIVE, new DetachmentRule("er", ""));
    rules.put(POS.ADJECTIVE, new DetachmentRule("est", ""));
    rules.put(POS.ADJECTIVE, new DetachmentRule("er", "e"));
    rules.put(POS.ADJECTIVE, new DetachmentRule("est", "e"));
    loadException(POS.ADJECTIVE);

    loadException(POS.ADVERB);

    try {
        this.lemmas = Config.get(EnglishLemmatizer.class, "dictionary").asResource()
                .read(new LineProcessor<PatriciaTrie<Set<POS>>>() {
                    private PatriciaTrie<Set<POS>> lemmas = new PatriciaTrie<>();

                    @Override
                    public boolean processLine(String line) throws IOException {
                        if (!Strings.isNullOrEmpty(line) && !line.trim().startsWith("#")) {
                            String[] parts = line.trim().split("\t+");
                            String lemma = parts[0].toLowerCase();
                            POS pos = POS.fromString(parts[1].toUpperCase());
                            if (!lemmas.containsKey(lemma)) {
                                lemmas.put(lemma, new CompactHashSet<POS>());
                            }
                            lemmas.get(lemma).add(pos);
                        }
                        return true;
                    }

                    @Override
                    public PatriciaTrie<Set<POS>> getResult() {
                        return lemmas;
                    }
                });
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}

From source file:cpw.mods.fml.common.asm.transformers.AccessTransformer.java

private void readMapFile(String rulesFile) throws IOException {
    File file = new File(rulesFile);
    URL rulesResource;/*from ww  w.j  av a 2 s  . c o  m*/
    if (file.exists()) {
        rulesResource = file.toURI().toURL();
    } else {
        rulesResource = Resources.getResource(rulesFile);
    }
    Resources.readLines(rulesResource, Charsets.UTF_8, new LineProcessor<Void>() {
        @Override
        public Void getResult() {
            return null;
        }

        @Override
        public boolean processLine(String input) throws IOException {
            String line = Iterables.getFirst(Splitter.on('#').limit(2).split(input), "").trim();
            if (line.length() == 0) {
                return true;
            }
            List<String> parts = Lists.newArrayList(Splitter.on(" ").trimResults().split(line));
            if (parts.size() > 3) {
                throw new RuntimeException("Invalid config file line " + input);
            }
            Modifier m = new Modifier();
            m.setTargetAccess(parts.get(0));

            if (parts.size() == 2) {
                m.modifyClassVisibility = true;
            } else {
                String nameReference = parts.get(2);
                int parenIdx = nameReference.indexOf('(');
                if (parenIdx > 0) {
                    m.desc = nameReference.substring(parenIdx);
                    m.name = nameReference.substring(0, parenIdx);
                } else {
                    m.name = nameReference;
                }
            }
            String className = parts.get(1).replace('/', '.');
            modifiers.put(className, m);
            if (DEBUG)
                System.out.printf("AT RULE: %s %s %s (type %s)\n", toBinary(m.targetAccess), m.name, m.desc,
                        className);
            return true;
        }
    });
    FMLRelaunchLog.fine("Loaded %d rules from AccessTransformer config file %s\n", modifiers.size(), rulesFile);
}

From source file:org.neptunepowered.vanilla.launch.transformers.DeobfuscationTransformer.java

public DeobfuscationTransformer() throws Exception {
    URL mappings = (URL) Launch.blackboard.get("vanilla.mappings");

    final ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder();
    final ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder();
    final ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder();

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

        @Override/*ww w  .j  a va 2 s  . c o m*/
        public boolean processLine(String line) throws IOException {
            if ((line = line.trim()).isEmpty()) {
                return true;
            }

            String[] parts = StringUtils.split(line, ' ');
            if (parts.length < 3) {
                NeptuneServerTweaker.getLogger().warn("Invalid deobfuscation mapping line: {}", line);
                return true;
            }

            MappingType type = MappingType.of(parts[0]);
            if (type == null) {
                NeptuneServerTweaker.getLogger().warn("Invalid deobfuscation mapping type: {}", line);
                return true;
            }

            String[] source;
            String[] dest;
            switch (type) {
            case CLASS:
                classes.put(parts[1], parts[2]);
                break;
            case FIELD:
                source = getSignature(parts[1]);
                dest = getSignature(parts[2]);
                String fieldType = getFieldType(source[0], source[1]);
                fields.put(source[0], source[1] + ':' + fieldType, dest[1]);
                if (fieldType != null) {
                    fields.put(source[0], source[1] + ":null", dest[1]);
                }
                break;
            case METHOD:
                source = getSignature(parts[1]);
                dest = getSignature(parts[3]);
                methods.put(source[0], source[1] + parts[2], dest[1]);
                break;
            default:
            }

            return true;
        }

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

    this.classes = classes.build();
    this.rawFields = fields.build();
    this.rawMethods = methods.build();

    this.fields = Maps.newHashMapWithExpectedSize(this.rawFields.size());
    this.methods = Maps.newHashMapWithExpectedSize(this.rawMethods.size());
}

From source file:org.spongepowered.server.launch.transformer.DeobfuscationTransformer.java

public DeobfuscationTransformer() throws Exception {
    URL mappings = (URL) Launch.blackboard.get("vanilla.mappings");

    final ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder();
    final ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder();
    final ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder();

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

        @Override//  www  . j a  v a 2s . c  om
        public boolean processLine(String line) throws IOException {
            if ((line = line.trim()).isEmpty()) {
                return true;
            }

            String[] parts = StringUtils.split(line, ' ');
            if (parts.length < 3) {
                VanillaServerTweaker.getLogger().warn("Invalid deobfuscation mapping line: {}", line);
                return true;
            }

            MappingType type = MappingType.of(parts[0]);
            if (type == null) {
                VanillaServerTweaker.getLogger().warn("Invalid deobfuscation mapping type: {}", line);
                return true;
            }

            String[] source;
            String[] dest;
            switch (type) {
            case CLASS:
                classes.put(parts[1], parts[2]);
                break;
            case FIELD:
                source = getSignature(parts[1]);
                dest = getSignature(parts[2]);
                String fieldType = getFieldType(source[0], source[1]);
                fields.put(source[0], source[1] + ':' + fieldType, dest[1]);
                if (fieldType != null) {
                    fields.put(source[0], source[1] + ":null", dest[1]);
                }
                break;
            case METHOD:
                source = getSignature(parts[1]);
                dest = getSignature(parts[3]);
                methods.put(source[0], source[1] + parts[2], dest[1]);
                break;
            default:
            }

            return true;
        }

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

    this.classes = classes.build();
    this.rawFields = fields.build();
    this.rawMethods = methods.build();

    this.fields = Maps.newHashMapWithExpectedSize(this.rawFields.size());
    this.methods = Maps.newHashMapWithExpectedSize(this.rawMethods.size());
}

From source file:org.neptunepowered.vanilla.launch.transformer.DeobfuscationTransformer.java

public DeobfuscationTransformer() throws Exception {
    URL mappings = (URL) Launch.blackboard.get("vanilla.mappings");

    final ImmutableBiMap.Builder<String, String> classes = ImmutableBiMap.builder();
    final ImmutableTable.Builder<String, String, String> fields = ImmutableTable.builder();
    final ImmutableTable.Builder<String, String, String> methods = ImmutableTable.builder();

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

        @Override//from w  ww  .j a v a 2s  .co m
        public boolean processLine(String line) throws IOException {
            if ((line = line.trim()).isEmpty()) {
                return true;
            }

            String[] parts = StringUtils.split(line, ' ');
            if (parts.length < 3) {
                NeptuneServerTweaker.log.warn("Invalid deobfuscation mapping line: {}", line);
                return true;
            }

            MappingType type = MappingType.of(parts[0]);
            if (type == null) {
                NeptuneServerTweaker.log.warn("Invalid deobfuscation mapping type: {}", line);
                return true;
            }

            String[] source;
            String[] dest;
            switch (type) {
            case CLASS:
                classes.put(parts[1], parts[2]);
                break;
            case FIELD:
                source = getSignature(parts[1]);
                dest = getSignature(parts[2]);
                String fieldType = getFieldType(source[0], source[1]);
                fields.put(source[0], source[1] + ':' + fieldType, dest[1]);
                if (fieldType != null) {
                    fields.put(source[0], source[1] + ":null", dest[1]);
                }
                break;
            case METHOD:
                source = getSignature(parts[1]);
                dest = getSignature(parts[3]);
                methods.put(source[0], source[1] + parts[2], dest[1]);
                break;
            default:
            }

            return true;
        }

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

    this.classes = classes.build();
    this.rawFields = fields.build();
    this.rawMethods = methods.build();

    this.fields = Maps.newHashMapWithExpectedSize(this.rawFields.size());
    this.methods = Maps.newHashMapWithExpectedSize(this.rawMethods.size());
}

From source file:net.minecraftforge.gradle.util.mcp.ReobfExceptor.java

private Map<String, String> readCSVs() throws IOException {
    final Map<String, String> csvData = Maps.newHashMap();
    File[] csvs = new File[] { fieldCSV == null ? null : fieldCSV, methodCSV == null ? null : methodCSV };

    for (File f : csvs) {
        if (f == null)
            continue;

        Files.readLines(f, Charset.defaultCharset(), new LineProcessor<Object>() {
            @Override/* ww  w.j  a  v  a 2s .c  o  m*/
            public boolean processLine(String line) throws IOException {
                String[] s = line.split(",");
                csvData.put(s[0], s[1]);
                return true;
            }

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

    return csvData;
}

From source file:net.minecraftforge.fml.common.asm.transformers.AccessTransformer.java

protected void processATFile(CharSource rulesResource) throws IOException {
    rulesResource.readLines(new LineProcessor<Void>() {
        @Override//  w ww.j  a va 2s.  c  o m
        public Void getResult() {
            return null;
        }

        @Override
        public boolean processLine(String input) throws IOException {
            String line = Iterables.getFirst(Splitter.on('#').limit(2).split(input), "").trim();
            if (line.length() == 0) {
                return true;
            }
            List<String> parts = Lists.newArrayList(Splitter.on(" ").trimResults().split(line));
            if (parts.size() > 3) {
                throw new RuntimeException("Invalid config file line " + input);
            }
            Modifier m = new Modifier();
            m.setTargetAccess(parts.get(0));

            if (parts.size() == 2) {
                m.modifyClassVisibility = true;
            } else {
                String nameReference = parts.get(2);
                int parenIdx = nameReference.indexOf('(');
                if (parenIdx > 0) {
                    m.desc = nameReference.substring(parenIdx);
                    m.name = nameReference.substring(0, parenIdx);
                } else {
                    m.name = nameReference;
                }
            }
            String className = parts.get(1).replace('/', '.');
            modifiers.put(className, m);
            if (DEBUG)
                System.out.printf("AT RULE: %s %s %s (type %s)\n", toBinary(m.targetAccess), m.name, m.desc,
                        className);
            return true;
        }
    });
}

From source file:net.minecraftforge.gradle.patcher.TaskGenBinPatches.java

private void loadMappings() throws Exception {
    Files.readLines(getSrg(), Charset.defaultCharset(), new LineProcessor<String>() {

        Splitter splitter = Splitter.on(CharMatcher.anyOf(": ")).omitEmptyStrings().trimResults();

        @Override//from  ww  w  . j a v a 2  s .co  m
        public boolean processLine(String line) throws IOException {
            if (!line.startsWith("CL")) {
                return true;
            }

            String[] parts = Iterables.toArray(splitter.split(line), String.class);
            obfMapping.put(parts[1], parts[2]);
            String srgName = parts[2].substring(parts[2].lastIndexOf('/') + 1);
            srgMapping.put(srgName, parts[1]);
            int innerDollar = srgName.lastIndexOf('$');
            if (innerDollar > 0) {
                String outer = srgName.substring(0, innerDollar);
                innerClasses.put(outer, srgName);
            }
            return true;
        }

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

From source file:org.apache.mahout.knn.tools.Vectorize20NewsGroups.java

static Multiset<String> parse(File f) throws IOException {
    return Files.readLines(f, Charsets.UTF_8, new LineProcessor<Multiset<String>>() {
        private boolean readingHeaders = true;
        private Splitter header = Splitter.on(":").limit(2);
        private Splitter words = Splitter.on(CharMatcher.forPredicate(new Predicate<Character>() {
            @Override//from  w  w  w. j  a v  a 2 s  . c o  m
            public boolean apply(Character ch) {
                return !Character.isLetterOrDigit(ch) && ch != '.' && ch != '/' && ch != ':';
            }
        })).omitEmptyStrings().trimResults();

        private Pattern quotedLine = Pattern.compile("(^In article .*)|(^> .*)|(.*writes:$)|(^\\|>)");

        private Multiset<String> counts = HashMultiset.create();

        @Override
        public boolean processLine(String line) throws IOException {
            if (readingHeaders && line.length() == 0) {
                readingHeaders = false;
            }

            if (readingHeaders) {
                Iterator<String> i = header.split(line).iterator();
                String head = i.next().toLowerCase();
                if (legalHeaders.contains(head)) {
                    addText(counts, i.next());
                }
            } else {
                boolean quote = quotedLine.matcher(line).matches();
                if (includeQuotes || !quote) {
                    addText(counts, line);
                }
            }
            return true;
        }

        @Override
        public Multiset<String> getResult() {
            return counts;
        }

        private void addText(Multiset<String> v, String line) {
            for (String word : words.split(line)) {
                v.add(word.toLowerCase());
            }
        }
    });
}

From source file:com.mgmtp.perfload.perfalyzer.util.PerfAlyzerUtils.java

/**
 * Reads a semicolon-delimited CSV file into a map of lists series values. Values for each
 * column are return as list of lists in the map, the key being the column header.
 * /*from   w  w w .  j  a va2s. c o m*/
 * @param file
 *            the file
 * @param charset
 *            the character set to read the file
 * @param numberFormat
 *            the number format for formatting the column values
 * @param columnNames
 *            the columns to consider
 * 
 * @return an immutable map of lists of series values
 */
public static Map<String, List<SeriesPoint>> readDataFile(final File file, final Charset charset,
        final NumberFormat numberFormat, final Set<String> columnNames) throws IOException {
    final StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
    tokenizer.setDelimiterChar(';');

    return readLines(file, charset, new LineProcessor<Map<String, List<SeriesPoint>>>() {
        private String[] headers;
        private final Map<String, List<SeriesPoint>> result = newHashMapWithExpectedSize(4);
        private int colCount;

        @Override
        public boolean processLine(final String line) throws IOException {
            try {
                tokenizer.reset(line);
                String[] tokens = tokenizer.getTokenArray();

                if (headers == null) {
                    headers = tokens;
                    colCount = tokens.length;
                } else {
                    Integer counter = Integer.valueOf(tokens[0]);
                    for (int i = 1; i < colCount; ++i) {
                        String header = headers[i];
                        if (columnNames.contains(header)) {
                            List<SeriesPoint> colValues = result.get(header);
                            if (colValues == null) {
                                colValues = newArrayListWithExpectedSize(50);
                                result.put(header, colValues);
                            }
                            colValues.add(new SeriesPoint(counter, numberFormat.parse(tokens[i])));
                        }
                    }
                }
                return true;
            } catch (ParseException ex) {
                throw new IOException("Error parsing number in file: " + file, ex);
            }
        }

        @Override
        public Map<String, List<SeriesPoint>> getResult() {
            return ImmutableMap.copyOf(result);
        }
    });
}