use of java.io.Writer in project guava by google.
the class CharSinkTester method testOpenBufferedStream.
public void testOpenBufferedStream() throws IOException {
Writer writer = sink.openBufferedStream();
try {
writer.write(data);
} finally {
writer.close();
}
assertContainsExpectedString();
}
use of java.io.Writer in project CoreNLP by stanfordnlp.
the class DependencyParser method writeModelFile.
public void writeModelFile(String modelFile) {
try {
double[][] W1 = classifier.getW1();
double[] b1 = classifier.getb1();
double[][] W2 = classifier.getW2();
double[][] E = classifier.getE();
Writer output = IOUtils.getPrintWriter(modelFile);
output.write("dict=" + knownWords.size() + "\n");
output.write("pos=" + knownPos.size() + "\n");
output.write("label=" + knownLabels.size() + "\n");
output.write("embeddingSize=" + E[0].length + "\n");
output.write("hiddenSize=" + b1.length + "\n");
output.write("numTokens=" + (W1[0].length / E[0].length) + "\n");
output.write("preComputed=" + preComputed.size() + "\n");
int index = 0;
// First write word / POS / label embeddings
for (String word : knownWords) {
index = writeEmbedding(E[index], output, index, word);
}
for (String pos : knownPos) {
index = writeEmbedding(E[index], output, index, pos);
}
for (String label : knownLabels) {
index = writeEmbedding(E[index], output, index, label);
}
// Now write classifier weights
for (int j = 0; j < W1[0].length; ++j) for (int i = 0; i < W1.length; ++i) {
output.write(String.valueOf(W1[i][j]));
if (i == W1.length - 1)
output.write("\n");
else
output.write(" ");
}
for (int i = 0; i < b1.length; ++i) {
output.write(String.valueOf(b1[i]));
if (i == b1.length - 1)
output.write("\n");
else
output.write(" ");
}
for (int j = 0; j < W2[0].length; ++j) for (int i = 0; i < W2.length; ++i) {
output.write(String.valueOf(W2[i][j]));
if (i == W2.length - 1)
output.write("\n");
else
output.write(" ");
}
// Finish with pre-computation info
for (int i = 0; i < preComputed.size(); ++i) {
output.write(String.valueOf(preComputed.get(i)));
if ((i + 1) % 100 == 0 || i == preComputed.size() - 1)
output.write("\n");
else
output.write(" ");
}
output.close();
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
use of java.io.Writer in project CoreNLP by stanfordnlp.
the class LexicalizedParserClient method getLemmas.
/**
* Get the lemmas for the text according to the parser's lemmatizer
* (only applies to English), return it as whitespace tokenized text.
*/
public String getLemmas(String query) throws IOException {
Socket socket = new Socket(host, port);
Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
out.write("lemma " + query + "\n");
out.flush();
String result = readResult(socket);
socket.close();
return result;
}
use of java.io.Writer in project CoreNLP by stanfordnlp.
the class LexicalizedParserClient method getParse.
/**
* Returns the String output of the parse of the given query.
* <br>
* The "parse" method in the server is mostly useful for clients
* using a language other than Java who don't want to import or wrap
* Tree in any way. However, it is useful to provide getParse to
* test that functionality in the server.
*/
public String getParse(String query, boolean binarized) throws IOException {
Socket socket = new Socket(host, port);
Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8");
out.write("parse" + (binarized ? ":binarized " : " ") + query + "\n");
out.flush();
String result = readResult(socket);
socket.close();
return result;
}
use of java.io.Writer in project javapoet by square.
the class JavaFile method writeTo.
/** Writes this to {@code filer}. */
public void writeTo(Filer filer) throws IOException {
String fileName = packageName.isEmpty() ? typeSpec.name : packageName + "." + typeSpec.name;
List<Element> originatingElements = typeSpec.originatingElements;
JavaFileObject filerSourceFile = filer.createSourceFile(fileName, originatingElements.toArray(new Element[originatingElements.size()]));
try (Writer writer = filerSourceFile.openWriter()) {
writeTo(writer);
} catch (Exception e) {
try {
filerSourceFile.delete();
} catch (Exception ignored) {
}
throw e;
}
}
Aggregations