/** * Builds Document object from Penn treebank corpus. * * @param file * @param encoding * @return * @throws IOException * @throws InvalidFormatException */ public Treebank load(File file, String encoding) throws IOException, InvalidFormatException { InputStream fin = null; Reader in = null; try { fin = new FileInputStream(file); in = new InputStreamReader(fin, encoding); in = new BufferedReader(in); return load(in); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(fin); } }
/** * Writes content to file. * * @param file the file for writing content * @param encoding file encoding * @param content the content to be writed to file. * @throws IOException if I/O error occurs. */ public static void writeFile(File file, String encoding, CharSequence content) throws IOException { OutputStream out = null; Writer writer = null; try { out = new FileOutputStream(file); out = new BufferedOutputStream(out); writer = new OutputStreamWriter(out, encoding); writer.append(content); } finally { closeQuietly(writer); closeQuietly(out); } }
/** * Builds Document object from Penn treebank corpus. * * @param file * @return * @throws IOException * @throws InvalidFormatException */ public Treebank load(File file) throws IOException, InvalidFormatException { Reader in = null; try { in = new BufferedReader(new FileReader(file)); return load(in); } finally { IOUtils.closeQuietly(in); } }
public List<ParseTreeNode> loadParseTrees(File file) throws IOException, InvalidFormatException { Reader in = null; try { in = new BufferedReader(new FileReader(file)); return loadParseTrees(in); } finally { IOUtils.closeQuietly(in); } }
/** * loads head rule from specified file. * * @param file * @throws IOException if I/O error occurs * @throws InvalidFormatException if rule readed from stream is invalid format */ private void load(File file) throws IOException, InvalidFormatException { Reader reader = null; try { reader = new FileReader(file); load(reader); } finally { IOUtils.closeQuietly(reader); } }
/** * Reads String from file. * * @param file file to be read * @param encoding file encoding * @return content which is readed from file * @throws IOException */ public static String readFile(File file, String encoding) throws IOException { InputStream in = null; Reader reader = null; try { in = new FileInputStream(file); in = new BufferedInputStream(in); reader = new InputStreamReader(in, encoding); int ch; StringBuilder buffer = new StringBuilder(); while ((ch = reader.read()) != -1) { buffer.append((char) ch); } return buffer.toString(); } finally { closeQuietly(reader); closeQuietly(in); } }
/** * Creates <code>HeadRule<code> which has default rules. * @return created rule instance */ public static HeadRule createDefaultRule() { HeadRule instance = new HeadRule(); InputStream rawIn = HeadRule.class.getClassLoader().getResourceAsStream(DEFAULT_RULE_PATH); Reader in = null; try { in = new InputStreamReader(rawIn, DEFAULT_RULE_ENCODING); instance.load(in); } catch (UnsupportedOperationException ex) { throw new RuntimeException(ex); } catch (IOException ex) { throw new RuntimeException(ex); } catch (InvalidFormatException ex) { throw new RuntimeException(ex); } finally { IOUtils.closeQuietly(in); IOUtils.closeQuietly(rawIn); } return instance; }