public static int lineCount(String textFileOrUrl) throws IOException { try (BufferedReader r = readerFromString(textFileOrUrl)) { int numLines = 0; while (r.readLine() != null) { numLines++; } return numLines; } }
/** @deprecated Just call readerFromString(filename) */ @Deprecated public static BufferedReader getBufferedFileReader(String filename) throws IOException { return readerFromString(filename, defaultEncoding); }
/** @deprecated Just call readerFromString(filename) */ @Deprecated public static BufferedReader getBufferedReaderFromClasspathOrFileSystem(String filename) throws IOException { return readerFromString(filename, defaultEncoding); }
private static void getWordsFromFile(String filename, Set<String> resultSet, boolean lowercase) throws IOException { if(filename==null) { return ; } try (BufferedReader reader = IOUtils.readerFromString(filename)) { while(reader.ready()) { if(lowercase) resultSet.add(reader.readLine().toLowerCase()); else resultSet.add(reader.readLine()); } } }
private static void getWordsFromFile(String filename, Set<String> resultSet, boolean lowercase) throws IOException { if(filename==null) { return ; } try (BufferedReader reader = IOUtils.readerFromString(filename)) { while (reader.ready()) { if (lowercase) resultSet.add(reader.readLine().toLowerCase()); else resultSet.add(reader.readLine()); } } }
/** * Returns all the text in the given file with the given encoding. * The string may be empty, if the file is empty. */ public static String slurpFile(String filename, String encoding) throws IOException { Reader r = readerFromString(filename, encoding); return IOUtils.slurpReader(r); }
public FileHandler(String fileOrClasspath, String contentType) throws IOException { try (BufferedReader r = IOUtils.readerFromString(fileOrClasspath, "utf-8")) { this.content = IOUtils.slurpReader(r); } this.contentType = contentType + "; charset=utf-8"; // always encode in utf-8 } @Override
private void loadCountriesLists(String file) { try (BufferedReader reader = IOUtils.readerFromString(file)) { for (String line; (line = reader.readLine()) != null; ) { countries.add(line.split("\t")[1].toLowerCase()); } } catch (IOException e) { throw new RuntimeIOException(e); } }
private void loadCountriesLists(String file) { try (BufferedReader reader = IOUtils.readerFromString(file)) { for (String line; (line = reader.readLine()) != null; ) { countries.add(line.split("\t")[1].toLowerCase()); } } catch (IOException e) { throw new RuntimeIOException(e); } }
/** * Parses a tsurgeon script file and compiles all operations in the file into a list * of pairs of tregex and tsurgeon patterns. * * @param filename A file, classpath resource or URL (perhaps gzipped) containing the tsurgeon script * @return A pair of a tregex and tsurgeon pattern read from a file * @throws IOException If there is any I/O problem */ public static List<Pair<TregexPattern, TsurgeonPattern>> getOperationsFromFile(String filename, String encoding, TregexPatternCompiler compiler) throws IOException { BufferedReader reader = IOUtils.readerFromString(filename, encoding); List<Pair<TregexPattern,TsurgeonPattern>> operations = getOperationsFromReader(reader, compiler); reader.close(); return operations; }
/** * Constructs a preprocessor from a file at a path, which can be either * a filesystem location, a classpath entry, or a URL. * * @param docPath The path * @param encoding The character encoding used by Readers */ public DocumentPreprocessor(String docPath, DocType t, String encoding) { if (docPath == null) { throw new IllegalArgumentException("Cannot open null document path!"); } docType = t; try { inputReader = IOUtils.readerFromString(docPath, encoding); } catch (IOException ioe) { throw new RuntimeIOException(String.format("%s: Could not open path %s", this.getClass().getName(), docPath), ioe); } }
private void loadFeatureMap() throws IOException { try (Reader r = IOUtils.readerFromString(FEATURE_MAP_FILE)) { BufferedReader br = new BufferedReader(r); posFeatureMap = new HashMap<>(); wordPosFeatureMap = new HashMap<>(); String line; while ((line = br.readLine()) != null) { String[] parts = line.split("\\s+"); if (parts.length < 3) continue; if (parts[0].equals("*")) { posFeatureMap.put(parts[1], CoNLLUUtils.parseFeatures(parts[2])); } else { wordPosFeatureMap.put(parts[0] + '_' + parts[1], CoNLLUUtils.parseFeatures(parts[2])); } } } }
private static void loadCorefDictPMI(String file, Counter<Pair<String, String>> dict) { BufferedReader reader = null; try { reader = IOUtils.readerFromString(file); // Skip the first line (header) reader.readLine(); while(reader.ready()) { String[] split = reader.readLine().split("\t"); dict.setCount(new Pair<>(split[0], split[1]), Double.parseDouble(split[3])); } } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeIgnoringExceptions(reader); } }
private static void loadCorefDictPMI(String file, Counter<Pair<String, String>> dict) { BufferedReader reader = null; try { reader = IOUtils.readerFromString(file); // Skip the first line (header) reader.readLine(); while(reader.ready()) { String[] split = reader.readLine().split("\t"); dict.setCount(new Pair<>(split[0], split[1]), Double.parseDouble(split[3])); } } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeIgnoringExceptions(reader); } }
private void initGazette() { try { // read in gazettes if (flags.gazettes == null) { flags.gazettes = new ArrayList<>(); } List<String> gazettes = flags.gazettes; for (String gazetteFile : gazettes) { try (BufferedReader r = IOUtils.readerFromString(gazetteFile, flags.inputEncoding)) { readGazette(r); } } } catch (IOException e) { throw new RuntimeIOException(e); } }
private static Map<String,String> loadMixedCaseMap(String mapFile) { Map<String,String> map = Generics.newHashMap(); try (BufferedReader br = IOUtils.readerFromString(mapFile)) { for (String line : ObjectBank.getLineIterator(br)) { line = line.trim(); String[] els = line.split("\\s+"); if (els.length != 2) { throw new RuntimeException("Wrong format: " + mapFile); } map.put(els[0], els[1]); } } catch (IOException e) { throw new RuntimeIOException(e); } return map; }
private static void loadSignatures(String file, Map<String,Counter<String>> sigs) { BufferedReader reader = null; try { reader = IOUtils.readerFromString(file); while(reader.ready()) { String[] split = reader.readLine().split("\t"); Counter<String> cntr = new ClassicCounter<>(); sigs.put(split[0], cntr); for (int i = 1; i < split.length; i=i+2) { cntr.setCount(split[i], Double.parseDouble(split[i+1])); } } } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeIgnoringExceptions(reader); } }
public void loadTextClassifier(String text, Properties props) throws ClassCastException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { // log.info("DEBUG: in loadTextClassifier"); log.info("Loading Text Classifier from " + text); try (BufferedReader br = IOUtils.readerFromString(text)) { loadTextClassifier(br); } catch (Exception ex) { log.info("Exception in loading text classifier from " + text, ex); } }
public HTKLatticeReader(String filename, boolean mergeType, boolean debug, boolean prettyPrint) throws Exception { this.DEBUG = debug; this.PRETTYPRINT = prettyPrint; this.mergeType = mergeType; try (BufferedReader in = IOUtils.readerFromString(filename)) { //log.info(-1); readInput(in); //log.info(0); if (PRETTYPRINT) { printWords(); } processLattice(); } }
private static RVFDataset<String, String> readSVMLightFormat(String filename, Index<String> featureIndex, Index<String> labelIndex, List<String> lines) { BufferedReader in = null; RVFDataset<String, String> dataset; try { dataset = new RVFDataset<>(10, featureIndex, labelIndex); in = IOUtils.readerFromString(filename); while (in.ready()) { String line = in.readLine(); if (lines != null) lines.add(line); dataset.add(svmLightLineToRVFDatum(line)); } } catch (IOException e) { throw new RuntimeIOException(e); } finally { IOUtils.closeIgnoringExceptions(in); } return dataset; }