Search in sources :

Example 81 with Writer

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();
}
Also used : Writer(java.io.Writer)

Example 82 with Writer

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);
    }
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer)

Example 83 with Writer

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;
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 84 with Writer

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;
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 85 with Writer

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;
    }
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) JavaFileObject(javax.tools.JavaFileObject) Element(javax.lang.model.element.Element) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Aggregations

Writer (java.io.Writer)1410 OutputStreamWriter (java.io.OutputStreamWriter)554 IOException (java.io.IOException)474 StringWriter (java.io.StringWriter)333 File (java.io.File)306 FileOutputStream (java.io.FileOutputStream)213 BufferedWriter (java.io.BufferedWriter)202 FileWriter (java.io.FileWriter)196 PrintWriter (java.io.PrintWriter)175 OutputStream (java.io.OutputStream)139 Test (org.junit.Test)120 InputStreamReader (java.io.InputStreamReader)81 Reader (java.io.Reader)74 BufferedReader (java.io.BufferedReader)72 ByteArrayOutputStream (java.io.ByteArrayOutputStream)70 ArrayList (java.util.ArrayList)66 HashMap (java.util.HashMap)66 Map (java.util.Map)65 InputStream (java.io.InputStream)61 Properties (java.util.Properties)40