/** * An implementation of {@code read(String, Transformer<String, T>)}. This function reads the * lines in a file and returns them. * * @param fileName The name of the file to be read * @return An arraylist of the lines of the file. */ public static ArrayList<String> read(String fileName, String charsetName) throws FileNotFoundException { return read(fileName, charsetName, identityTransformer); }
/** * Writes a list of strings to a file, one per line. */ public static void write(String fileName, Iterable<String> list) throws IOException { write(fileName, list, identityTransformer); }
/** * Read the contents of a file using the default charset and return as a single string */ public static String slurp(String fileName) throws FileNotFoundException { return slurp(fileName, Charset.defaultCharset().name()); }
/** * This searches for the file on the classpath before reading it. If it doesn't find the file, * it throws a {@code FileNotFoundException}. * * @param fileName The name of the file to be read * @return An arraylist of the lines of the file. */ public static ArrayList<String> readFromClasspath(String fileName) throws FileNotFoundException { return read(getInputStream(fileName), Charset.defaultCharset().name(), identityTransformer); }
try { if (IOUtils.exists(failedFile)) { failed = LineIO.read(failedFile, new ITransformer<String, Integer>() { @Override public Integer transform(String s) { TextAnnotation ta; try { ta = SerializationHelper.deserializeFromJson(LineIO.slurp(file)); } catch (Exception e) { logger.error("Error while reading file {}\n{}", file, e.getMessage());
protected void loadStopwords() throws IOException { ArrayList<String> lines = LineIO.readFromClasspath(m_stopwordFile); m_stopwords = new HashSet<String>(); for (String line : lines) { m_stopwords.add(line.toLowerCase()); } }
/** * Append a list of strings to a file, one per line */ public static void append(String fileName, Iterable<String> list) throws IOException { append(fileName, list, new StringTransformer<String>()); }
/** * This searches for the file on the classpath before reading it. If it doesn't find the file, * it throws a {@code FileNotFoundException}. * * @param fileName The name of the file to be read * @return An arraylist of the lines of the file. */ public static ArrayList<String> readFromClasspath(String fileName) throws FileNotFoundException { return read(getInputStream(fileName), Charset.defaultCharset().name(), identityTransformer); }
private void loadParagram( String vectorFilename, int dimension ) throws FileNotFoundException { vectors = new HashMap<>(); this.dimension = dimension; int count =0; try { for (String line : LineIO.readFromClasspath(vectorFilename)) { line = line.trim(); if(line.length() > 0) { String[] arr = line.split("\\s"); String word = arr[0]; double[] vec = new double[dimension]; for(int i=1; i < arr.length; i++) { vec[i-1] = Double.parseDouble(arr[i]); } vectors.put(word, vec); count++; } } } catch (FileNotFoundException e) { e.printStackTrace(); throw e; } logger.info( "loaded " + count + " vectors with " + dimension + " dimensions." ); }
/** * Append a string to a file */ public static void append(String fileName, String line) throws IOException { append(fileName, line, new StringTransformer<String>()); } }
/** * Creates a new ListFeatureExtractor * * @param listName The name of the list, which is emitted as the feature * @param listFile A file containing the elements of the list * @param caseSensitive Should the checking be case sensitive? * @throws java.io.FileNotFoundException */ public ListFeatureExtractor(String listName, String listFile, boolean caseSensitive) throws FileNotFoundException { this(listName, LineIO.read(listFile), caseSensitive); }
/** * Writes a list of strings to a file, one per line. */ public static void write(String fileName, Iterable<String> list) throws IOException { write(fileName, list, identityTransformer); }
/** * Read the contents of a file using the default charset and return as a single string */ public static String slurp(String fileName) throws FileNotFoundException { return slurp(fileName, Charset.defaultCharset().name()); }
private synchronized static void loadDataFromClassPath() throws EdisonException { if (data.size() > 0) return; List<String> lines; try { lines = LineIO.readFromClasspath(CORLEX_FILE); logger.info("\n"); } catch (FileNotFoundException e) { throw new EdisonException("CORLEX not found in class path at " + CORLEX_FILE); } log.info("Loading CORLEX from {}", CORLEX_FILE); for (String line : lines) { if (line.length() == 0) continue; if (line.startsWith("#")) continue; String[] parts = line.split("\t"); if (parts.length == 2) { String lemma = parts[0].trim(); String type = parts[1].trim().intern(); data.put(lemma, type); } } log.info("Finished loading CORLEX. Found {} nouns", data.size()); }
/** * Append a list of strings to a file, one per line */ public static void append(String fileName, Iterable<String> list) throws IOException { append(fileName, list, new StringTransformer<String>()); }
/** * An implementation of {@code read(String, Transformer<String, T>)}. This function reads the * lines in a file and returns them. * * @param fileName The name of the file to be read * @return An arraylist of the lines of the file. */ public static ArrayList<String> read(String fileName, String charsetName) throws FileNotFoundException { return read(fileName, charsetName, identityTransformer); }
/** * This will save the rules out to file. * @param fname * @param rules * @throws IOException */ public static void saveRules(String fname, HashMap<String, HashMap<String, Integer>> rules) throws IOException { ArrayList<String> rulelines = new ArrayList<>(); ArrayList<String> sortedkeys = new ArrayList<>(rules.keySet()); Collections.sort(sortedkeys); for(String surface : sortedkeys){ HashMap<String, Integer> labelcounts = rules.get(surface); String outs = surface + "\t"; for(String label : labelcounts.keySet()){ outs += label + ":" + labelcounts.get(label) + "\t"; } rulelines.add(outs.trim()); } LineIO.write(fname, rulelines); }