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

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

本文整理了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

  1. /**
  2. * Load the file.
  3. *
  4. * @param path path to .fex file
  5. * @throws Exception
  6. */
  7. public FeatureManifest(String path) throws Exception {
  8. this(IOUtils.lsResources(FeatureManifest.class, path).get(0).openStream());
  9. }

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

  1. /**
  2. * Load the file.
  3. *
  4. * @param path path to .fex file
  5. * @throws Exception
  6. */
  7. public FeatureManifest(String path) throws Exception {
  8. this(IOUtils.lsResources(FeatureManifest.class, path).get(0).openStream());
  9. }

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

  1. public static <T> T readObjectAsResource(Class clazz, String fileName) throws Exception {
  2. List<URL> lsResources = IOUtils.lsResources(clazz, fileName);
  3. assert lsResources.size() > 0;
  4. URL resource = lsResources.get(0);
  5. InputStream stream = resource.openStream();
  6. ObjectInputStream in = new ObjectInputStream(stream);
  7. @SuppressWarnings("unchecked")
  8. T obj = (T) in.readObject();
  9. in.close();
  10. return obj;
  11. }

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

  1. public static <T> T readObjectAsResource(Class clazz, String fileName) throws Exception {
  2. List<URL> lsResources = IOUtils.lsResources(clazz, fileName);
  3. assert lsResources.size() > 0;
  4. URL resource = lsResources.get(0);
  5. InputStream stream = resource.openStream();
  6. ObjectInputStream in = new ObjectInputStream(stream);
  7. @SuppressWarnings("unchecked")
  8. T obj = (T) in.readObject();
  9. in.close();
  10. return obj;
  11. }

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

  1. /**
  2. * Lists the contents of a directory that exists either in the local path or in the classpath
  3. *
  4. * @param resourceDir The name of the directory containing the gazetteers
  5. * @return An array of URL objects to be read
  6. * @throws IOException
  7. * @throws
  8. */
  9. public URL[] listGazetteers(String resourceDir) throws IOException {
  10. List<URL> files = new ArrayList<>();
  11. try {
  12. for (URL url : IOUtils.lsResources(SimpleGazetteerAnnotator.class, resourceDir)) {
  13. files.add(url);
  14. }
  15. } catch (URISyntaxException e) {
  16. throw new IOException("URI syntax error.", e);
  17. }
  18. return files.toArray(new URL[files.size()]);
  19. }

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

  1. /**
  2. * Lists the contents of a directory that exists either in the local path or in the classpath
  3. *
  4. * @param resourceDir The name of the directory containing the gazetteers
  5. * @return An array of URL objects to be read
  6. * @throws IOException
  7. * @throws
  8. */
  9. public URL[] listGazetteers(String resourceDir) throws IOException {
  10. List<URL> files = new ArrayList<>();
  11. try {
  12. for (URL url : IOUtils.lsResources(SimpleGazetteerAnnotator.class, resourceDir)) {
  13. files.add(url);
  14. }
  15. } catch (URISyntaxException e) {
  16. throw new IOException("URI syntax error.", e);
  17. }
  18. return files.toArray(new URL[files.size()]);
  19. }

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

  1. public static BufferedReader getReader( String fileName ) throws IOException {
  2. // InputStream inStrm = ClassLoader.getSystemResourceAsStream( fileName );
  3. InputStream inStrm;
  4. try {
  5. inStrm = IOUtils.lsResources(MappingReader.class, fileName).get(0).openStream();
  6. } catch (URISyntaxException e) {
  7. throw new IOException(e.getMessage());
  8. }
  9. // InputStream is = this.getClass().getResourceAsStream( name_ );
  10. if (inStrm == null)
  11. {
  12. // try with a leading slash
  13. inStrm = ClassLoader.getSystemResourceAsStream("/" + fileName );
  14. if (inStrm == null) // try as absolute or correct local path
  15. inStrm = new FileInputStream( fileName);
  16. }
  17. BufferedReader reader =
  18. new BufferedReader( new InputStreamReader( inStrm, "UTF-8" ) );
  19. return reader;
  20. }

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

  1. public static BufferedReader getReader(String fileName) throws IOException {
  2. // InputStream inStrm = ClassLoader.getSystemResourceAsStream( fileName
  3. // );
  4. InputStream inStrm;
  5. try {
  6. inStrm = IOUtils.lsResources(MappingReader.class, fileName).get(0).openStream();
  7. } catch (URISyntaxException e) {
  8. throw new IOException(e.getMessage());
  9. }
  10. // InputStream is = this.getClass().getResourceAsStream( name_ );
  11. if (inStrm == null) {
  12. // try with a leading slash
  13. inStrm = ClassLoader.getSystemResourceAsStream("/" + fileName);
  14. if (inStrm == null) // try as absolute or correct local path
  15. inStrm = new FileInputStream(fileName);
  16. }
  17. BufferedReader reader = new BufferedReader(new InputStreamReader(inStrm, "UTF-8"));
  18. return reader;
  19. }

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

  1. @SuppressWarnings("resource")
  2. private void loadClustersFromClassPath() throws Exception {
  3. logger.debug("Loading brown clusters from {}", file);
  4. List<URL> resources = IOUtils.lsResources(BrownClusterViewGenerator.class, file);
  5. InputStream stream;
  6. if (resources.size() == 0) {
  7. stream = new FileInputStream(file);
  8. } else {
  9. URL url = resources.get(0);
  10. stream = url.openStream();
  11. }
  12. if (gzip)
  13. stream = new GZIPInputStream(stream);
  14. loadFromInputStream(stream);
  15. }

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

  1. @SuppressWarnings("resource")
  2. private void loadClustersFromClassPath() throws Exception {
  3. logger.debug("Loading brown clusters from {}", file);
  4. List<URL> resources = IOUtils.lsResources(BrownClusterViewGenerator.class, file);
  5. InputStream stream;
  6. if (resources.size() == 0) {
  7. stream = new FileInputStream(file);
  8. } else {
  9. URL url = resources.get(0);
  10. stream = url.openStream();
  11. }
  12. if (gzip)
  13. stream = new GZIPInputStream(stream);
  14. loadFromInputStream(stream);
  15. }

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

  1. public static List<String> readFromClasspath(String filename) {
  2. List<String> lines = null;
  3. try {
  4. InputStream resource =
  5. IOUtils.lsResources(IllinoisLemmatizer.class, filename).get(0).openStream();
  6. lines =
  7. LineIO.read(resource, Charset.defaultCharset().name(),
  8. new ITransformer<String, String>() {
  9. public String transform(String line) {
  10. return line;
  11. }
  12. });
  13. } catch (IOException | URISyntaxException e) {
  14. System.err.println("Error while trying to read " + filename + ".");
  15. System.exit(-1);
  16. }
  17. return lines;
  18. }

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

  1. public NomLexReader(String nomLexFile) throws Exception {
  2. InputStream stream;
  3. if (!new File(nomLexFile).exists()) {
  4. logger.info("Loading {} from classpath", nomLexFile);
  5. List<URL> list = IOUtils.lsResources(NomLexReader.class, nomLexFile);
  6. if (list.isEmpty()) {
  7. logger.error("Could not load " + nomLexFile);
  8. System.exit(-1);
  9. }
  10. stream = list.get(0).openStream();
  11. } else {
  12. logger.info("Loading {} from local directory", nomLexFile);
  13. stream = new FileInputStream(nomLexFile);
  14. }
  15. BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  16. nomLex = new HashMap<>();
  17. pluralToSingularMap = new HashMap<>();
  18. readNomLexLisp(reader);
  19. }

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

  1. VerbLemmaDictionary(Logger log) throws IOException, URISyntaxException, EdisonException {
  2. Map<String, String> mm = new HashMap<>();
  3. String file = "verb-lemDict.txt";
  4. InputStream in =
  5. IOUtils.lsResources(VerbLemmaDictionary.class, file).get(0).openStream();
  6. log.info("Found file {} in the classpath", file);
  7. Scanner scanner = new Scanner(in);
  8. int count = 0;
  9. while (scanner.hasNextLine()) {
  10. String line = scanner.nextLine().trim();
  11. if (line.length() == 0)
  12. continue;
  13. String[] parts = line.split("\\s+");
  14. String lemma = parts[0];
  15. for (String p : parts) {
  16. mm.put(p, lemma);
  17. }
  18. count++;
  19. }
  20. log.info("{} verb lemmas found", count);
  21. map = Collections.unmodifiableMap(mm);
  22. }

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

  1. public static List<String> readFromClasspath(String filename) {
  2. List<String> lines = null;
  3. try {
  4. InputStream resource =
  5. IOUtils.lsResources(IllinoisLemmatizer.class, filename).get(0).openStream();
  6. lines =
  7. LineIO.read(resource, Charset.defaultCharset().name(),
  8. new ITransformer<String, String>() {
  9. public String transform(String line) {
  10. return line;
  11. }
  12. });
  13. } catch (IOException | URISyntaxException e) {
  14. System.err.println("Error while trying to read " + filename + ".");
  15. System.exit(-1);
  16. }
  17. return lines;
  18. }

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

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

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

  1. public NomLexReader(String nomLexFile) throws Exception {
  2. InputStream stream;
  3. if (!new File(nomLexFile).exists()) {
  4. logger.info("Loading {} from classpath", nomLexFile);
  5. List<URL> list = IOUtils.lsResources(NomLexReader.class, nomLexFile);
  6. if (list.isEmpty()) {
  7. logger.error("Could not load " + nomLexFile);
  8. System.exit(-1);
  9. }
  10. stream = list.get(0).openStream();
  11. } else {
  12. logger.info("Loading {} from local directory", nomLexFile);
  13. stream = new FileInputStream(nomLexFile);
  14. }
  15. BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
  16. nomLex = new HashMap<>();
  17. pluralToSingularMap = new HashMap<>();
  18. readNomLexLisp(reader);
  19. }

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

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

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

  1. @Deprecated
  2. public static VerbClassDictionary getDictionaryFromClassPath() {
  3. if (verbClassDictionary == null) {
  4. synchronized (LevinVerbClassFeature.class) {
  5. if (verbClassDictionary == null) {
  6. log.info("Reading verb class dictionary. Looking for " + verbClassFile
  7. + " in the classpath");
  8. try {
  9. URL url = IOUtils.lsResources(LevinVerbClassFeature.class, verbClassFile).get(0);
  10. InputStream resource = url.openStream();
  11. verbClassDictionary = new VerbClassDictionary(resource);
  12. } catch (Exception e) {
  13. log.error("Unable to read the verb class dictionary", e);
  14. System.exit(-1);
  15. }
  16. List<String> strings = verbClassDictionary.getClass("give");
  17. log.info("Loaded verb class dictionary. Test: classes for 'give' are {}", strings);
  18. }
  19. }
  20. }
  21. return verbClassDictionary;
  22. }

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

  1. @Override
  2. public Set<Feature> getWordFeatures(TextAnnotation ta, int wordPosition)
  3. throws EdisonException {
  4. Set<Feature> features = new LinkedHashSet<>();
  5. List<String> nomFrames;
  6. try {
  7. URL file =
  8. IOUtils.lsResources(WordFeatureExtractorFactory.class, "nombank.list.gz")
  9. .get(0);
  10. nomFrames = LineIO.readGZip(file.getFile());
  11. String lemma = WordHelpers.getLemma(ta, wordPosition);
  12. if (nomFrames.contains(lemma)) {
  13. features.add(isNom);
  14. }
  15. } catch (Exception e) {
  16. System.err.println("Could not read nombank.list.gz file from classpath");
  17. e.printStackTrace();
  18. }
  19. return features;
  20. }
  21. };

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

  1. @Override
  2. public void initialize(ResourceManager rm) {
  3. try {
  4. // TODO Ugly hack: SL doesn't accept streams and can't create a file from inside a jar
  5. File dest = new File(TEMP_MODEL_FILE_NAME);
  6. String modelName = rm.getString(DepConfigurator.MODEL_NAME.key);
  7. URL fileURL = IOUtils.lsResources(DepAnnotator.class, modelName).get(0);
  8. logger.info("Loading {} into temp file: {}", modelName, TEMP_MODEL_FILE_NAME);
  9. FileUtils.copyURLToFile(fileURL, dest);
  10. model = SLModel.loadModel(TEMP_MODEL_FILE_NAME);
  11. ((LabeledChuLiuEdmondsDecoder) model.infSolver).loadDepRelDict();
  12. if (!dest.delete())
  13. throw new IOException("Could not delete temporary model file "
  14. + TEMP_MODEL_FILE_NAME);
  15. } catch (IOException | ClassNotFoundException | URISyntaxException e) {
  16. e.printStackTrace();
  17. File dest = new File(TEMP_MODEL_FILE_NAME);
  18. if (!dest.delete())
  19. throw new RuntimeException("Could not delete temporary model file "
  20. + TEMP_MODEL_FILE_NAME);
  21. }
  22. }

相关文章