edu.illinois.cs.cogcomp.core.io.IOUtils.lsResources()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.6k)|赞(0)|评价(0)|浏览(114)

本文整理了Java中edu.illinois.cs.cogcomp.core.io.IOUtils.lsResources()方法的一些代码示例,展示了IOUtils.lsResources()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.lsResources()方法的具体详情如下:
包路径:edu.illinois.cs.cogcomp.core.io.IOUtils
类名称:IOUtils
方法名:lsResources

IOUtils.lsResources介绍

[英]Lists resources that are contained within a path. This works for any resource on the classpath, either in the file system or in a jar file. The function returns a list of URLs, connections to which can be opened for reading.

NB: This method works only for full file names. If you need to list the files of a directory contained in the classpath use #lsResourcesDir(Class,String)
[中]列出路径中包含的资源。这适用于类路径上的任何资源,无论是在文件系统中还是在jar文件中。该函数返回URL列表,可以打开URL的连接进行读取。
注意:此方法仅适用于完整文件名。如果需要列出类路径中包含的目录的文件,请使用#lsResourcesDir(类,字符串)

代码示例

代码示例来源:origin: CogComp/cogcomp-nlp

/**
 * Load the file.
 * 
 * @param path path to .fex file
 * @throws Exception
 */
public FeatureManifest(String path) throws Exception {
  this(IOUtils.lsResources(FeatureManifest.class, path).get(0).openStream());
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-edison

/**
 * Load the file.
 * 
 * @param path path to .fex file
 * @throws Exception
 */
public FeatureManifest(String path) throws Exception {
  this(IOUtils.lsResources(FeatureManifest.class, path).get(0).openStream());
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-core-utilities

public static <T> T readObjectAsResource(Class clazz, String fileName) throws Exception {
  List<URL> lsResources = IOUtils.lsResources(clazz, fileName);
  assert lsResources.size() > 0;
  URL resource = lsResources.get(0);
  InputStream stream = resource.openStream();
  ObjectInputStream in = new ObjectInputStream(stream);
  @SuppressWarnings("unchecked")
  T obj = (T) in.readObject();
  in.close();
  return obj;
}

代码示例来源:origin: CogComp/cogcomp-nlp

public static <T> T readObjectAsResource(Class clazz, String fileName) throws Exception {
  List<URL> lsResources = IOUtils.lsResources(clazz, fileName);
  assert lsResources.size() > 0;
  URL resource = lsResources.get(0);
  InputStream stream = resource.openStream();
  ObjectInputStream in = new ObjectInputStream(stream);
  @SuppressWarnings("unchecked")
  T obj = (T) in.readObject();
  in.close();
  return obj;
}

代码示例来源:origin: CogComp/cogcomp-nlp

/**
 * Lists the contents of a directory that exists either in the local path or in the classpath
 * 
 * @param resourceDir The name of the directory containing the gazetteers
 * @return An array of URL objects to be read
 * @throws IOException
 * @throws
 */
public URL[] listGazetteers(String resourceDir) throws IOException {
  List<URL> files = new ArrayList<>();
  try {
    for (URL url : IOUtils.lsResources(SimpleGazetteerAnnotator.class, resourceDir)) {
      files.add(url);
    }
  } catch (URISyntaxException e) {
    throw new IOException("URI syntax error.", e);
  }
  return files.toArray(new URL[files.size()]);
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-edison

/**
 * Lists the contents of a directory that exists either in the local path or in the classpath
 * 
 * @param resourceDir The name of the directory containing the gazetteers
 * @return An array of URL objects to be read
 * @throws IOException
 * @throws
 */
public URL[] listGazetteers(String resourceDir) throws IOException {
  List<URL> files = new ArrayList<>();
  try {
    for (URL url : IOUtils.lsResources(SimpleGazetteerAnnotator.class, resourceDir)) {
      files.add(url);
    }
  } catch (URISyntaxException e) {
    throw new IOException("URI syntax error.", e);
  }
  return files.toArray(new URL[files.size()]);
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-entity-similarity

public static BufferedReader getReader( String fileName ) throws IOException {
//        InputStream inStrm = ClassLoader.getSystemResourceAsStream( fileName );
    InputStream inStrm;
    try {
      inStrm = IOUtils.lsResources(MappingReader.class, fileName).get(0).openStream();
    } catch (URISyntaxException e) {
      throw new IOException(e.getMessage());
    }

    // InputStream is = this.getClass().getResourceAsStream( name_ );

    if (inStrm == null) 
    {
      // try with a leading slash
      inStrm = ClassLoader.getSystemResourceAsStream("/" + fileName );
      if (inStrm == null) // try as absolute or correct local path
        inStrm = new FileInputStream( fileName);

    }

    BufferedReader reader =
        new BufferedReader( new InputStreamReader( inStrm, "UTF-8" ) );

    return reader;
  }

代码示例来源:origin: CogComp/cogcomp-nlp

public static BufferedReader getReader(String fileName) throws IOException {
  // InputStream inStrm = ClassLoader.getSystemResourceAsStream( fileName
  // );
  InputStream inStrm;
  try {
    inStrm = IOUtils.lsResources(MappingReader.class, fileName).get(0).openStream();
  } catch (URISyntaxException e) {
    throw new IOException(e.getMessage());
  }
  // InputStream is = this.getClass().getResourceAsStream( name_ );
  if (inStrm == null) {
    // try with a leading slash
    inStrm = ClassLoader.getSystemResourceAsStream("/" + fileName);
    if (inStrm == null) // try as absolute or correct local path
      inStrm = new FileInputStream(fileName);
  }
  BufferedReader reader = new BufferedReader(new InputStreamReader(inStrm, "UTF-8"));
  return reader;
}

代码示例来源:origin: CogComp/cogcomp-nlp

@SuppressWarnings("resource")
private void loadClustersFromClassPath() throws Exception {
  logger.debug("Loading brown clusters from {}", file);
  List<URL> resources = IOUtils.lsResources(BrownClusterViewGenerator.class, file);
  InputStream stream;
  if (resources.size() == 0) {
    stream = new FileInputStream(file);
  } else {
    URL url = resources.get(0);
    stream = url.openStream();
  }
  if (gzip)
    stream = new GZIPInputStream(stream);
  loadFromInputStream(stream);
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-edison

@SuppressWarnings("resource")
private void loadClustersFromClassPath() throws Exception {
  logger.debug("Loading brown clusters from {}", file);
  List<URL> resources = IOUtils.lsResources(BrownClusterViewGenerator.class, file);
  InputStream stream;
  if (resources.size() == 0) {
    stream = new FileInputStream(file);
  } else {
    URL url = resources.get(0);
    stream = url.openStream();
  }
  if (gzip)
    stream = new GZIPInputStream(stream);
  loadFromInputStream(stream);
}

代码示例来源:origin: CogComp/cogcomp-nlp

public static List<String> readFromClasspath(String filename) {
  List<String> lines = null;
  try {
    InputStream resource =
        IOUtils.lsResources(IllinoisLemmatizer.class, filename).get(0).openStream();
    lines =
        LineIO.read(resource, Charset.defaultCharset().name(),
            new ITransformer<String, String>() {
              public String transform(String line) {
                return line;
              }
            });
  } catch (IOException | URISyntaxException e) {
    System.err.println("Error while trying to read " + filename + ".");
    System.exit(-1);
  }
  return lines;
}

代码示例来源:origin: CogComp/cogcomp-nlp

public NomLexReader(String nomLexFile) throws Exception {
  InputStream stream;
  if (!new File(nomLexFile).exists()) {
    logger.info("Loading {} from classpath", nomLexFile);
    List<URL> list = IOUtils.lsResources(NomLexReader.class, nomLexFile);
    if (list.isEmpty()) {
      logger.error("Could not load " + nomLexFile);
      System.exit(-1);
    }
    stream = list.get(0).openStream();
  } else {
    logger.info("Loading {} from local directory", nomLexFile);
    stream = new FileInputStream(nomLexFile);
  }
  BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  nomLex = new HashMap<>();
  pluralToSingularMap = new HashMap<>();
  readNomLexLisp(reader);
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-edison

VerbLemmaDictionary(Logger log) throws IOException, URISyntaxException, EdisonException {
  Map<String, String> mm = new HashMap<>();
  String file = "verb-lemDict.txt";
  InputStream in =
      IOUtils.lsResources(VerbLemmaDictionary.class, file).get(0).openStream();
  log.info("Found file {} in the classpath", file);
  Scanner scanner = new Scanner(in);
  int count = 0;
  while (scanner.hasNextLine()) {
    String line = scanner.nextLine().trim();
    if (line.length() == 0)
      continue;
    String[] parts = line.split("\\s+");
    String lemma = parts[0];
    for (String p : parts) {
      mm.put(p, lemma);
    }
    count++;
  }
  log.info("{} verb lemmas found", count);
  map = Collections.unmodifiableMap(mm);
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-lemmatizer

public static List<String> readFromClasspath(String filename) {
  List<String> lines = null;
  try {
    InputStream resource =
        IOUtils.lsResources(IllinoisLemmatizer.class, filename).get(0).openStream();
    lines =
        LineIO.read(resource, Charset.defaultCharset().name(),
            new ITransformer<String, String>() {
              public String transform(String line) {
                return line;
              }
            });
  } catch (IOException | URISyntaxException e) {
    System.err.println("Error while trying to read " + filename + ".");
    System.exit(-1);
  }
  return lines;
}

代码示例来源:origin: CogComp/cogcomp-nlp

private synchronized void loadFromClassPath() throws Exception {
  if (loaded)
    return;
  List<URL> urls = IOUtils.lsResources(RogetThesaurusFeatures.class, fileName);
  if (urls.size() == 0)
    throw new EdisonException("Cannot find " + fileName + " in the classpath");
  loadWithURL(urls.get(0));
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-edison

public NomLexReader(String nomLexFile) throws Exception {
  InputStream stream;
  if (!new File(nomLexFile).exists()) {
    logger.info("Loading {} from classpath", nomLexFile);
    List<URL> list = IOUtils.lsResources(NomLexReader.class, nomLexFile);
    if (list.isEmpty()) {
      logger.error("Could not load " + nomLexFile);
      System.exit(-1);
    }
    stream = list.get(0).openStream();
  } else {
    logger.info("Loading {} from local directory", nomLexFile);
    stream = new FileInputStream(nomLexFile);
  }
  BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  nomLex = new HashMap<>();
  pluralToSingularMap = new HashMap<>();
  readNomLexLisp(reader);
}

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-edison

private synchronized void loadFromClassPath() throws Exception {
  if (loaded)
    return;
  List<URL> urls = IOUtils.lsResources(RogetThesaurusFeatures.class, fileName);
  if (urls.size() == 0)
    throw new EdisonException("Cannot find " + fileName + " in the classpath");
  loadWithURL(urls.get(0));
}

代码示例来源:origin: CogComp/cogcomp-nlp

@Deprecated
public static VerbClassDictionary getDictionaryFromClassPath() {
  if (verbClassDictionary == null) {
    synchronized (LevinVerbClassFeature.class) {
      if (verbClassDictionary == null) {
        log.info("Reading verb class dictionary. Looking for " + verbClassFile
            + " in the classpath");
        try {
          URL url = IOUtils.lsResources(LevinVerbClassFeature.class, verbClassFile).get(0);
          InputStream resource = url.openStream();
          verbClassDictionary = new VerbClassDictionary(resource);
        } catch (Exception e) {
          log.error("Unable to read the verb class dictionary", e);
          System.exit(-1);
        }
        List<String> strings = verbClassDictionary.getClass("give");
        log.info("Loaded verb class dictionary. Test: classes for 'give' are {}", strings);
      }
    }
  }
  return verbClassDictionary;
}

代码示例来源:origin: CogComp/cogcomp-nlp

@Override
  public Set<Feature> getWordFeatures(TextAnnotation ta, int wordPosition)
      throws EdisonException {
    Set<Feature> features = new LinkedHashSet<>();
    List<String> nomFrames;
    try {
      URL file =
          IOUtils.lsResources(WordFeatureExtractorFactory.class, "nombank.list.gz")
              .get(0);
      nomFrames = LineIO.readGZip(file.getFile());
      String lemma = WordHelpers.getLemma(ta, wordPosition);
      if (nomFrames.contains(lemma)) {
        features.add(isNom);
      }
    } catch (Exception e) {
      System.err.println("Could not read nombank.list.gz file from classpath");
      e.printStackTrace();
    }
    return features;
  }
};

代码示例来源:origin: CogComp/cogcomp-nlp

@Override
public void initialize(ResourceManager rm) {
  try {
    // TODO Ugly hack: SL doesn't accept streams and can't create a file from inside a jar
    File dest = new File(TEMP_MODEL_FILE_NAME);
    String modelName = rm.getString(DepConfigurator.MODEL_NAME.key);
    URL fileURL = IOUtils.lsResources(DepAnnotator.class, modelName).get(0);
    logger.info("Loading {} into temp file: {}", modelName, TEMP_MODEL_FILE_NAME);
    FileUtils.copyURLToFile(fileURL, dest);
    model = SLModel.loadModel(TEMP_MODEL_FILE_NAME);
    ((LabeledChuLiuEdmondsDecoder) model.infSolver).loadDepRelDict();
    if (!dest.delete())
      throw new IOException("Could not delete temporary model file "
          + TEMP_MODEL_FILE_NAME);
  } catch (IOException | ClassNotFoundException | URISyntaxException e) {
    e.printStackTrace();
    File dest = new File(TEMP_MODEL_FILE_NAME);
    if (!dest.delete())
      throw new RuntimeException("Could not delete temporary model file "
          + TEMP_MODEL_FILE_NAME);
  }
}

相关文章