Example usage for com.google.common.io Resources readLines

List of usage examples for com.google.common.io Resources readLines

Introduction

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

Prototype

public static List<String> readLines(URL url, Charset charset) throws IOException 

Source Link

Document

Reads all of the lines from a URL.

Usage

From source file:com.mapr.synth.samplers.NameSampler.java

public NameSampler() {
    try {//from  w ww. j  av  a 2s  .c om
        if (first.compareAndSet(null, new Multinomial<String>())) {
            Preconditions.checkState(last.getAndSet(new Multinomial<String>()) == null);

            Splitter onTab = Splitter.on(CharMatcher.WHITESPACE).omitEmptyStrings().trimResults();
            for (String resourceName : ImmutableList.of("dist.male.first", "dist.female.first")) {
                for (String line : Resources.readLines(Resources.getResource(resourceName), Charsets.UTF_8)) {
                    if (!line.startsWith("#")) {
                        Iterator<String> parts = onTab.split(line).iterator();
                        String name = initialCap(parts.next());
                        double weight = Double.parseDouble(parts.next());
                        if (first.get().getWeight(name) == 0) {
                            first.get().add(name, weight);
                        } else {
                            // do this instead of add because some first names may appear more than once
                            first.get().set(name, first.get().getWeight(name) + weight);
                        }
                    }
                }
            }

            for (String line : Resources.readLines(Resources.getResource("dist.all.last"), Charsets.UTF_8)) {
                if (!line.startsWith("#")) {
                    Iterator<String> parts = onTab.split(line).iterator();
                    String name = initialCap(parts.next());
                    double weight = Double.parseDouble(parts.next());
                    last.get().add(name, weight);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Couldn't read built-in resource file", e);
    }
}

From source file:com.mapr.synth.samplers.SsnSampler.java

public SsnSampler() {
    Splitter onComma = Splitter.on(",").trimResults();
    try {//from   w ww .  ja  v  a2 s .  co  m
        names = null;
        for (String line : Resources.readLines(Resources.getResource("ssn-seeds"), Charsets.UTF_8)) {
            if (line.startsWith("#")) {
                // last comment line contains actual field names
                names = Lists.newArrayList(onComma.split(line.substring(1)));
            } else {
                Preconditions.checkState(names != null);
                assert names != null;

                List<String> fields = Lists.newArrayList(onComma.split(line));
                for (int i = Integer.parseInt(fields.get(1)); i <= Integer.parseInt(fields.get(1)); i++) {
                    String key = String.format("%03d", i);
                    values.put(key, fields.subList(2, fields.size()));
                    codes.add(key);
                }

            }
        }
        assert names != null;
        names = names.subList(2, names.size());
    } catch (IOException e) {
        throw new RuntimeException("Couldn't read built-in resource", e);
    }
}

From source file:org.incodehq.amberg.vshcolab.webapp.DomainApplication.java

private static String readLines(final Class<?> contextClass, final String resourceName) {
    try {//from  w w w.j  ava 2 s .  com
        List<String> readLines = Resources.readLines(Resources.getResource(contextClass, resourceName),
                Charset.defaultCharset());
        final String aboutText = Joiner.on("\n").join(readLines);
        return aboutText;
    } catch (IOException e) {
        return "This is the VSH Colab exploration of the workflow domain";
    }
}

From source file:edu.cmu.lti.oaqa.baseqa.answer.yesno.scorers.TokenOverlapYesNoScorer.java

@Override
public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
        throws ResourceInitializationException {
    super.initialize(aSpecifier, aAdditionalParams);
    String stoplistPath = String.class.cast(getParameterValue("stoplist-path"));
    try {/*from   ww  w.j av  a  2s  .  c  o  m*/
        stoplist = Resources.readLines(getClass().getResource(stoplistPath), UTF_8).stream().map(String::trim)
                .collect(toSet());
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
    viewNamePrefix = String.class.cast(getParameterValue("view-name-prefix"));
    return true;
}

From source file:app.MyLaMoradaStartApplication.java

private static String readLines(final String resourceName) {
    try {/*from   ww  w.jav  a 2  s .c om*/
        List<String> readLines = Resources.readLines(
                Resources.getResource(MyLaMoradaStartApplication.class, resourceName),
                Charset.defaultCharset());
        final String aboutText = Joiner.on("\n").join(readLines);
        return aboutText;
    } catch (IOException e) {
        return "La Morada Petit Hotel";
    }
}

From source file:edu.cmu.lti.oaqa.baseqa.learning_base.CVPredictLoader.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);
    String candidateProviderName = UimaContextHelper.getConfigParameterStringValue(context,
            "candidate-provider");
    candidateProvider = ProviderCache.getProvider(candidateProviderName, CandidateProvider.class);
    // load cv/* w  w  w .j a  v  a2  s . co  m*/
    String cvPredictFile = UimaContextHelper.getConfigParameterStringValue(context, "cv-predict-file");
    List<String> lines;
    try {
        lines = Resources.readLines(getClass().getResource(cvPredictFile), Charsets.UTF_8);
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
    qid2uri2score = HashBasedTable.create();
    lines.stream().map(line -> line.split("\t"))
            .forEach(segs -> qid2uri2score.put(segs[0], segs[1], Double.parseDouble(segs[2])));
}

From source file:edu.cmu.lti.oaqa.baseqa.answer.yesno.scorers.SentimentYesNoScorer.java

@Override
public boolean initialize(ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
        throws ResourceInitializationException {
    super.initialize(aSpecifier, aAdditionalParams);
    String positiveWordlistPath = (String) getParameterValue("positive-wordlist-path");
    String negativeWordlistPath = (String) getParameterValue("negative-wordlist-path");
    try {/*from  w ww  . j  a  v  a  2 s.c o m*/
        positiveWords = Resources.readLines(getClass().getResource(positiveWordlistPath), Charsets.UTF_8)
                .stream().collect(toSet());
        negativeWords = Resources.readLines(getClass().getResource(negativeWordlistPath), Charsets.UTF_8)
                .stream().collect(toSet());
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
    viewNamePrefix = String.class.cast(getParameterValue("view-name-prefix"));
    return true;
}

From source file:com.mapr.synth.samplers.FileSampler.java

@SuppressWarnings({ "UnusedDeclaration" })
public void setResource(String lookup) throws IOException {
    if (lookup.matches(".*\\.json")) {
        readJsonData(Resources.newInputStreamSupplier(Resources.getResource(lookup)));
    } else {/*from ww  w. j  a v a 2s .  c  o  m*/
        List<String> lines = Resources.readLines(Resources.getResource(lookup), Charsets.UTF_8);
        readDelimitedData(lookup, lines);
    }

    setupIndex();
}

From source file:com.qrmedia.commons.multispi.provider.MetaInfServicesProvider.java

public MetaInfServicesProvider() {
    super(new Function<Class<?>, String>() {
        public String apply(Class<?> from) {
            return PROVIDER_FILE_DIR + '/' + from.getName();
        }/*from  w  ww .  j  ava  2  s.c  om*/
    }, new IoFunction<URL, List<String>>() {
        public List<String> apply(URL item) throws IOException {
            return Resources.readLines(item, PROVIDER_FILE_CHARSET);
        }
    });
}

From source file:edu.cmu.lti.oaqa.baseqa.abstract_query.TokenSelectionAbstractQueryGenerator.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);
    // get pos tags
    String posTagsPath = UimaContextHelper.getConfigParameterStringValue(context, "pos-tags-path", null);
    if (posTagsPath != null) {
        try {//from w  w  w  . j a v a  2  s  .co  m
            posTags = Resources.readLines(getClass().getResource(posTagsPath), UTF_8).stream().map(String::trim)
                    .collect(toSet());
        } catch (IOException e) {
            throw new ResourceInitializationException(e);
        }
    }
    // get noun tags
    String nounTagsPath = UimaContextHelper.getConfigParameterStringValue(context, "noun-tags-path", null);
    if (nounTagsPath != null) {
        try {
            nounTags = Resources.readLines(getClass().getResource(nounTagsPath), UTF_8).stream()
                    .map(String::trim).collect(toSet());
        } catch (IOException e) {
            throw new ResourceInitializationException(e);
        }
    }
    // get stop word list
    String stoplistPath = UimaContextHelper.getConfigParameterStringValue(context, "stoplist-path", null);
    if (stoplistPath != null) {
        try {
            stoplist = Resources.readLines(getClass().getResource(stoplistPath), UTF_8).stream()
                    .map(String::trim).collect(toSet());
        } catch (IOException e) {
            throw new ResourceInitializationException(e);
        }
    }
}