Search in sources :

Example 76 with RuntimeIOException

use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.

the class MaxentTagger method saveModel.

protected void saveModel(String filename) {
    try {
        DataOutputStream file = IOUtils.getDataOutputStream(filename);
        saveModel(file);
        file.close();
    } catch (IOException ioe) {
        log.info("Error saving tagger to file " + filename);
        throw new RuntimeIOException(ioe);
    }
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Example 77 with RuntimeIOException

use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.

the class ParsedGigawordReader method iterator.

@Override
public Iterator<Annotation> iterator() {
    return new Iterator<Annotation>() {

        private Iterator<BufferedReader> readers = Iterables.transform(files, file -> IOUtils.readerFromFile(file)).iterator();

        private BufferedReader reader = findReader();

        private Annotation annotation = findAnnotation();

        @Override
        public boolean hasNext() {
            return this.annotation != null;
        }

        @Override
        public Annotation next() {
            if (this.annotation == null) {
                throw new NoSuchElementException();
            }
            Annotation toReturn = this.annotation;
            this.annotation = this.findAnnotation();
            return toReturn;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        private BufferedReader findReader() {
            return this.readers.hasNext() ? this.readers.next() : null;
        }

        private Annotation findAnnotation() {
            if (this.reader == null) {
                return null;
            }
            try {
                String line;
                StringBuilder doc = new StringBuilder();
                while ((line = this.reader.readLine()) != null) {
                    doc.append(line);
                    doc.append('\n');
                    // }
                    if (line.equals("</DOC>")) {
                        break;
                    }
                    if (line.contains("</DOC>")) {
                        throw new RuntimeException(String.format("invalid line '%s'", line));
                    }
                }
                if (line == null) {
                    this.reader.close();
                    this.reader = findReader();
                }
                String xml = doc.toString().replaceAll("&", "&amp;");
                if (xml == null || xml.equals("")) {
                    return findAnnotation();
                }
                xml = xml.replaceAll("num=([0-9]+) (.*)", "num=\"$1\" $2");
                xml = xml.replaceAll("sid=(.*)>", "sid=\"$1\">");
                xml = xml.replaceAll("</SENT>\n</DOC>", "</SENT>\n</TEXT>\n</DOC>");
                xml = new String(xml.getBytes(), "UTF8");
                // log.info("This is what goes in:\n" + xml);
                return toAnnotation(xml);
            } catch (IOException e) {
                throw new RuntimeIOException(e);
            }
        }
    };
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) Annotation(edu.stanford.nlp.pipeline.Annotation)

Example 78 with RuntimeIOException

use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.

the class TTags method read.

protected void read(DataInputStream file) {
    try {
        int size = file.readInt();
        index = new HashIndex<>();
        for (int i = 0; i < size; i++) {
            String tag = file.readUTF();
            boolean inClosed = file.readBoolean();
            index.add(tag);
            if (inClosed)
                closed.add(tag);
        }
    } 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)

Example 79 with RuntimeIOException

use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.

the class ColumnDataClassifier method loadClassifier.

private static Pair<Flags[], Classifier<String, String>> loadClassifier(String path) {
    Timing t = new Timing();
    try (ObjectInputStream ois = IOUtils.readStreamFromString(path)) {
        Pair<Flags[], Classifier<String, String>> pair = loadClassifier(ois);
        t.done(logger, "Loading classifier from " + path);
        return pair;
    } catch (IOException | ClassNotFoundException e) {
        throw new RuntimeIOException("Error loading classifier from " + path, e);
    }
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) WordShapeClassifier(edu.stanford.nlp.process.WordShapeClassifier) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Example 80 with RuntimeIOException

use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.

the class LexicalizedParser method saveParserToSerialized.

/**
 * Saves the parser defined by pd to the given filename.
 * If there is an error, a RuntimeIOException is thrown.
 */
public void saveParserToSerialized(String filename) {
    try {
        log.info("Writing parser in serialized format to file " + filename + ' ');
        ObjectOutputStream out = IOUtils.writeStreamFromString(filename);
        out.writeObject(this);
        out.close();
        log.info("done.");
    } catch (IOException ioe) {
        throw new RuntimeIOException(ioe);
    }
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Aggregations

RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)114 IOException (java.io.IOException)61 BufferedReader (java.io.BufferedReader)22 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)12 CoreLabel (edu.stanford.nlp.ling.CoreLabel)11 File (java.io.File)9 ArrayList (java.util.ArrayList)7 Tree (edu.stanford.nlp.trees.Tree)6 CoreMap (edu.stanford.nlp.util.CoreMap)5 BufferedWriter (java.io.BufferedWriter)5 Properties (java.util.Properties)5 Timing (edu.stanford.nlp.util.Timing)4 FileNotFoundException (java.io.FileNotFoundException)4 FileOutputStream (java.io.FileOutputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4 PrintWriter (java.io.PrintWriter)4 CorefCoreAnnotations (edu.stanford.nlp.coref.CorefCoreAnnotations)3 Annotation (edu.stanford.nlp.pipeline.Annotation)3 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)3 ClassicCounter (edu.stanford.nlp.stats.ClassicCounter)3