public static PrintWriter getPrintWriterIgnoringExceptions(String filename) { try { return getPrintWriter(filename, defaultEncoding); } catch (IOException ioe) { return null; } }
public static PrintWriter getPrintWriter(File textFile) throws IOException { return getPrintWriter(textFile, null); }
public static PrintWriter getPrintWriter(String filename) throws IOException { return getPrintWriter(filename, defaultEncoding); }
public NeuralCorefDataExporter(Properties props, Dictionaries dictionaries, String dataPath, String goldClusterPath) { conll = CorefProperties.conll(props); this.dictionaries = dictionaries; try { dataWriter = IOUtils.getPrintWriter(dataPath); goldClusterWriter = IOUtils.getPrintWriter(goldClusterPath); } catch (Exception e) { throw new RuntimeException("Error creating data exporter", e); } }
private static void newFeaturePrinter(String prefix, String suffix, String encoding) { if (cliqueWriter != null) { closeFeaturePrinter(); } try { cliqueWriter = IOUtils.getPrintWriter(prefix + '.' + suffix, encoding); } catch (IOException ioe) { cliqueWriter = null; } }
public static void processText(AnnotationPipeline pipeline, String text, String out, String date) throws IOException { PrintWriter pw = (out != null)? IOUtils.getPrintWriter(out):new PrintWriter(System.out); String string = textToAnnotatedXml(pipeline, text, date); pw.println(string); pw.flush(); if (out != null) pw.close(); }
public static PrintWriter getPrintWriterOrDie(String filename) { try { return getPrintWriter(filename, defaultEncoding); } catch (IOException ioe) { throw new RuntimeIOException(ioe); } }
public void runCharniak(int n, String infile, String outfile, String errfile) { try { if (n == 1) n++; // Charniak does not output score if n = 1? List<String> args = new ArrayList<>(); args.add(parserExecutable); args.add(infile); ProcessBuilder process = new ProcessBuilder(args); process.directory(new File(this.dir)); PrintWriter out = IOUtils.getPrintWriter(outfile); PrintWriter err = IOUtils.getPrintWriter(errfile); SystemUtils.run(process, out, err); out.close(); err.close(); } catch (IOException ex) { throw new RuntimeException(ex); } }
public static void writeCharacterList(String fileName, List<Person> personList) throws IOException { StringBuilder text = new StringBuilder(); for(Person p : personList) { String gender = ""; switch (p.gender) { case MALE: gender = "M"; break; case FEMALE: gender = "F"; break; case UNK: gender = ""; break; } text.append(p.name + ";" + gender); for (String alias : p.aliases) { text.append(";" + alias); } text.append("\n"); } PrintWriter pw = IOUtils.getPrintWriter(fileName); pw.print(text); pw.close(); }
public static void processTextFile(AnnotationPipeline pipeline, String in, String out, String date) throws IOException { String text = IOUtils.slurpFile(in); PrintWriter pw = (out != null)? IOUtils.getPrintWriter(out):new PrintWriter(System.out); String string = textToAnnotatedXml(pipeline, text, date); pw.println(string); pw.flush(); if (out != null) pw.close(); }
/** * Print scored parse trees in format used by charniak parser * @param trees - trees to output * @param filename - file to output to */ public static void printScoredTrees(Iterable<List<ScoredObject<Tree>>> trees, String filename) { try { PrintWriter pw = IOUtils.getPrintWriter(filename); int i = 0; for (List<ScoredObject<Tree>> treeList:trees) { printScoredTrees(pw, i, treeList); i++; } pw.close(); } catch (IOException ex) { throw new RuntimeException(ex); } }
@Override public void saveToFilename(String s) { PrintWriter bw = null; try { bw = IOUtils.getPrintWriter(s); for (int i = 0, size = indexSize; i < size; i++) { E o = get(i); if (o != null) { bw.printf("%d=%s%n", i, o.toString()); } } bw.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (bw != null) { bw.close(); } } }
/** * Output features from a collection of documents to a file * Format is with one line per token using the following format * word label feat1 feat2 ... * (where each space is actually a tab) * Each document is separated by an empty line * This format is suitable for modified crfsgd. * * @param exportFile file to export the features to * @param documents input collection of documents */ public void printFeatures(String exportFile, Collection<List<IN>> documents) { try { PrintWriter pw = IOUtils.getPrintWriter(exportFile); for (List<IN> doc:documents) { String str = getFeatureString(doc); pw.println(str); } pw.close(); } catch (IOException ex) { throw new RuntimeException(ex); } }
private void writeModel(String name, String modelPath) { try { classifier.writeWeights(modelPath + name + "_model.ser"); classifier.printWeightVector( IOUtils.getPrintWriter(modelPath + name + "_weights")); } catch (Exception e) { throw new RuntimeException(); } }
public void printSentences(Iterable<List<? extends HasWord>> sentences, String filename) { try { PrintWriter pw = IOUtils.getPrintWriter(filename); for (List<? extends HasWord> sentence:sentences) { pw.print("<s> "); // Note: Use <s sentence-id > to identify sentences String sentString = SentenceUtils.listToString(sentence); if (sentence.size() > maxSentenceLength) { logger.warning("Sentence length=" + sentence.size() + " is longer than maximum set length " + maxSentenceLength); logger.warning("Long Sentence: " + sentString); } pw.print(sentString); pw.println(" </s>"); } pw.close(); } catch (IOException ex) { throw new RuntimeException(ex); } }
PrintWriter pw = IOUtils.getPrintWriter(propsFile); props.store(pw, null); pw.close();
/** * Serialize a counter into an efficient string TSV * @param c The counter to serialize * @param filename The file to serialize to * @param minMagnitude Ignore values under this magnitude * @throws IOException * * @see Counters#deserializeStringCounter(String) */ public static void serializeStringCounter(Counter<String> c, String filename, double minMagnitude) throws IOException { PrintWriter writer = IOUtils.getPrintWriter(filename); for (Entry<String, Double> entry : c.entrySet()) { if (Math.abs(entry.getValue()) < minMagnitude) { continue; } Triple<Boolean, Long, Integer> parts = SloppyMath.segmentDouble(entry.getValue()); writer.println( entry.getKey().replace('\t', 'ߝ') + "\t" + (parts.first ? '-' : '+') + "\t" + parts.second + "\t" + parts.third ); } writer.close(); }
private static void generatePrefixDefs(String infile, String outfile) throws IOException { List<UnitPrefix> prefixes = UnitPrefix.loadPrefixes(infile); PrintWriter pw = IOUtils.getPrintWriter(outfile); pw.println("SI_PREFIX_MAP = {"); List<String> items = new ArrayList<>(); for (UnitPrefix prefix : prefixes) { if ("SI".equals(prefix.system)) { items.add("\"" + prefix.name + "\": " + prefix.getName().toUpperCase()); } } pw.println(StringUtils.join(items, ",\n")); pw.println("}"); pw.println("$SiPrefixes = CreateRegex(Keys(SI_PREFIX_MAP))"); pw.println(); pw.println("SI_SYM_PREFIX_MAP = {"); items.clear(); for (UnitPrefix prefix:prefixes) { if ("SI".equals(prefix.system)) { items.add("\"" + prefix.symbol + "\": " + prefix.getName().toUpperCase()); } } pw.println(StringUtils.join(items, ",\n")); pw.println("}"); pw.println("$SiSymPrefixes = CreateRegex(Keys(SI_SYM_PREFIX_MAP))"); pw.close(); }
public static void writeConllFile(String outFile, List<CoreMap> sentences, List<DependencyTree> trees) { try { PrintWriter output = IOUtils.getPrintWriter(outFile); for (int i = 0; i < sentences.size(); i++) { CoreMap sentence = sentences.get(i); DependencyTree tree = trees.get(i); List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class); for (int j = 1, size = tokens.size(); j <= size; ++j) { CoreLabel token = tokens.get(j - 1); output.printf("%d\t%s\t_\t%s\t%s\t_\t%d\t%s\t_\t_%n", j, token.word(), token.tag(), token.tag(), tree.getHead(j), tree.getLabel(j)); } output.println(); } output.close(); } catch (Exception e) { throw new RuntimeIOException(e); } }
private void printClassifier(Classifier classifier) { String classString; if (classifier instanceof LinearClassifier<?,?>) { classString = ((LinearClassifier<?,?>)classifier).toString(globalFlags.printClassifier, globalFlags.printClassifierParam); } else { classString = classifier.toString(); } if (Flags.printTo != null) { PrintWriter fw = null; try { fw = IOUtils.getPrintWriter(Flags.printTo, Flags.encoding); fw.write(classString); fw.println(); } catch (IOException ioe) { logger.warn(ioe); } finally { IOUtils.closeIgnoringExceptions(fw); } logger.info("Built classifier described in file " + Flags.printTo); } else { logger.info("Built this classifier: " + classString); } }