/** * Resolve a location (which can be many things) to an URL. If the location starts with * {@code classpath:} the location is interpreted as a classpath location. Otherwise it is tried * as a URL, file and at last UIMA resource. If the location is treated as a classpath or file * location, an URL is only returned if the target exists. If it is an URL, it is possible that * the target may not actually exist. * * @param aLocation * a location (classpath, URL, file or UIMA resource location). * @return the resolved URL. * @throws IOException * if the target could not be found. */ public static URL resolveLocation(String aLocation) throws IOException { return resolveLocation(aLocation, null, null); }
@Override protected File produceResource(URL aUrl) throws IOException { return ResourceUtils.getUrlAsFile(aUrl, true); } };
+ "temporary folder.\n"); if (isEnvironmentVariableDefined(XDG_RUNTIME_DIR_ENV_VAR, errorMessage) && checkFolderPermissions(errorMessage, System.getenv(XDG_RUNTIME_DIR_ENV_VAR))) { file = getFileAsExecutable(aUrl, System.getenv(XDG_RUNTIME_DIR_ENV_VAR)); else if (isEnvironmentVariableDefined(DKPRO_HOME_ENV_VAR, errorMessage) && checkFolderPermissions(errorMessage, System.getenv(DKPRO_HOME_ENV_VAR) + File.separator + "temp")) { file = getFileAsExecutable(aUrl, System.getenv(DKPRO_HOME_ENV_VAR) + File.separator + "temp"); if (!isUserHomeDefined(errorMessage) || !checkFolderPermissions(errorMessage, System.getProperty("user.home") + File.separator + ".dkpro" + File.separator + "temp")) { throw new IOException(errorMessage.toString()); file = getFileAsExecutable(aUrl, System.getProperty("user.home") + File.separator + ".dkpro" + File.separator + "temp");
/** * Resolve a location (which can be many things) to an URL. If the location starts with * {@code classpath:} the location is interpreted as a classpath location. Otherwise it is tried * as a URL, file and at last UIMA resource. If the location is treated as a classpath or file * location, an URL is only returned if the target exists. If it is an URL, it is possible that * the target may not actually exist. * * @param aLocation * a location (classpath, URL, file or UIMA resource location). * @param aContext * a UIMA context. * @return the resolved URL. * @throws IOException * if the target could not be found. */ public static URL resolveLocation(String aLocation, UimaContext aContext) throws IOException { return resolveLocation(aLocation, null, aContext); }
@Override protected File produceResource(URL aUrl) throws IOException { return ResourceUtils.getUrlAsFile(aUrl, true); } };
public CharacterNGramMeasure(int n, String idfValuesFile) throws IOException { this.n = n; URL resourceUrl = ResourceUtils.resolveLocation(idfValuesFile, this, null); idf = new HashMap<String, Double>(); for (String line : FileUtils.readLines(new File(resourceUrl.getFile()))) { String[] linesplit = line.split("\t"); idf.put(linesplit[0], Double.parseDouble(linesplit[1])); } }
@Override protected File produceResource(URL aUrl) throws IOException { return ResourceUtils.getUrlAsFile(aUrl, true); } };
public CharacterNGramMeasure(int n, String idfValuesFile) throws IOException { this.n = n; URL resourceUrl = ResourceUtils.resolveLocation(idfValuesFile, this, null); idf = new HashMap<String, Double>(); try (InputStream is = resourceUrl.openStream()) { for (String line : IOUtils.readLines(is, StandardCharsets.UTF_8)) { String[] linesplit = line.split("\t"); idf.put(linesplit[0], Double.parseDouble(linesplit[1])); } } }
@Override protected File produceResource(URL aUrl) throws IOException { return ResourceUtils.getUrlAsFile(aUrl, true); } };
public CharacterNGramMeasure(int n, String idfValuesFile) throws IOException { this.n = n; URL resourceUrl = ResourceUtils.resolveLocation(idfValuesFile, this, null); idf = new HashMap<String, Double>(); try (InputStream is = resourceUrl.openStream()) { for (String line : IOUtils.readLines(is, StandardCharsets.UTF_8)) { String[] linesplit = line.split("\t"); idf.put(linesplit[0], Double.parseDouble(linesplit[1])); } } }
/** * Make the given URL available as a file. A temporary file is created and deleted upon a * regular shutdown of the JVM. If the parameter {@code aCache} is {@code true}, the temporary * file is remembered in a cache and if a file is requested for the same URL at a later time, * the same file is returned again. If the previously created file has been deleted meanwhile, * it is recreated from the URL. This method should not be used for creating executable * binaries. For this purpose, getUrlAsExecutable should be used. * * @param aUrl * the URL. * @param aCache * use the cache or not. * @return a file created from the given URL. * @throws IOException * if the URL cannot be accessed to (re)create the file. */ public static File getUrlAsFile(URL aUrl, boolean aCache) throws IOException { return getUrlAsFile(aUrl, aCache, false); }
public static Set<String> getStopwords(String inputFile, boolean toLowerCase) throws IOException { Set<String> stopwords = new HashSet<String>(); if (inputFile != null) { URL stopUrl = ResourceUtils.resolveLocation(inputFile, null); InputStream is = stopUrl.openStream(); for (String stopword : IOUtils.readLines(is, "UTF-8")) { if (toLowerCase) { stopwords.add(stopword.toLowerCase()); } else { stopwords.add(stopword); } } } return stopwords; }
@Override protected Tagger produceResource(URL aUrl) throws IOException { File modelFile = ResourceUtils.getUrlAsFile(aUrl, true); return new Tagger(modelFile.getPath()); // create a MorphTagger } };
/** * Resolve a location (which can be many things) to an URL. If the location starts with * {@code classpath:} the location is interpreted as a classpath location. Otherwise it is tried * as a URL, file and at last UIMA resource. If the location is treated as a classpath or file * location, an URL is only returned if the target exists. If it is an URL, it is possible that * the target may not actually exist. * * @param aLocation * a location (classpath, URL, file or UIMA resource location). * @param aCaller * the instance calling this method (for classpath loading). * @param aContext * a UIMA context. * @return the resolved URL. * @throws IOException * if the target could not be found. */ public static URL resolveLocation(String aLocation, Object aCaller, UimaContext aContext) throws IOException { ClassLoader cl = null; if (aCaller != null) { cl = aCaller.getClass().getClassLoader(); } return resolveLocation(aLocation, cl, aContext); }
@Override protected Lemmatizer produceResource(URL aUrl) throws IOException { File modelFile = ResourceUtils.getUrlAsFile(aUrl, true); return new Lemmatizer(modelFile.getPath(), uppercase); // create a lemmatizer } };
public static Set<String> getStopwords(String inputFile, boolean toLowerCase) throws IOException { Set<String> stopwords = new HashSet<String>(); if (inputFile != null) { URL stopUrl = ResourceUtils.resolveLocation(inputFile, null); InputStream is = stopUrl.openStream(); for (String stopword : IOUtils.readLines(is, "UTF-8")) { if (toLowerCase) { stopwords.add(stopword.toLowerCase()); } else { stopwords.add(stopword); } } } return stopwords; }
@Override protected SemanticRoleLabeler produceResource(URL aUrl) throws IOException { File modelFile = ResourceUtils.getUrlAsFile(aUrl, false); try { ZipFile zipFile = new ZipFile(modelFile); SemanticRoleLabeler srl = Pipeline.fromZipFile(zipFile); zipFile.close(); return srl; } catch (Exception e) { throw new IOException(e); } } };
private void init(String functionWordListLocation) throws IOException { functionWords = new ArrayList<String>(); InputStream is = null; try { URL url = ResourceUtils.resolveLocation(functionWordListLocation, this, null); is = url.openStream(); String content = IOUtils.toString(is, "UTF-8"); for (String line : Arrays.asList(content.split("\n"))) { if (line.length() > 0) { functionWords.add(line); } } } finally{ IOUtils.closeQuietly(is); } }
public static File loadAndPrepareFeatureDataFile(TaskContext aContext, File tmpLocation, String sourceFolder) throws Exception { // Depending on a users' setup, we might exceed the path-length limit of 255 // character, which is a problem on Windows, we provide all needed files in the // folder where the binary is located to enforce short paths File folder = aContext.getFolder(sourceFolder, AccessMode.READONLY); File srcFile = new File(folder, FILENAME_DATA_IN_CLASSIFIER_FORMAT); if (srcFile.getAbsolutePath().length() < 254 || !isWindows()) { return ResourceUtils.getUrlAsFile(srcFile.toURI().toURL(), true); } File trainDestFile = new File(tmpLocation, FILENAME_DATA_IN_CLASSIFIER_FORMAT); try (InputStream is = new FileInputStream(srcFile)) { FileUtils.copyInputStreamToFile(is, trainDestFile); } return ResourceUtils.getUrlAsFile(trainDestFile.toURI().toURL(), true); }
private void init(String functionWordListLocation) throws IOException { functionWords = new ArrayList<String>(); InputStream is = null; try { URL url = ResourceUtils.resolveLocation(functionWordListLocation, this, null); is = url.openStream(); String content = IOUtils.toString(is, "UTF-8"); for (String line : Arrays.asList(content.split("\n"))) { if (line.length() > 0) { functionWords.add(line); } } } finally{ IOUtils.closeQuietly(is); } }