Search in sources :

Example 56 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Counters method deserializeStringCounter.

/**
   * Read a Counter from a serialized file
   * @param filename The file to read from
   *
   * @see Counters#serializeStringCounter(Counter, String, double)
   */
public static ClassicCounter<String> deserializeStringCounter(String filename) throws IOException {
    String[] fields = new String[4];
    BufferedReader reader = IOUtils.readerFromString(filename);
    String line;
    ClassicCounter<String> counts = new ClassicCounter<>(1000000);
    while ((line = reader.readLine()) != null) {
        StringUtils.splitOnChar(fields, line, '\t');
        long mantissa = SloppyMath.parseInt(fields[2]);
        int exponent = (int) SloppyMath.parseInt(fields[3]);
        double value = SloppyMath.parseDouble(fields[1].equals("-"), mantissa, exponent);
        counts.setCount(fields[0], value);
    }
    return counts;
}
Also used : BufferedReader(java.io.BufferedReader)

Example 57 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Counters method loadInto2DCounter.

public static <T1, T2> void loadInto2DCounter(String filename, Class<T1> t1, Class<T2> t2, TwoDimensionalCounter<T1, T2> tdc) throws RuntimeException {
    try {
        Constructor<T1> m1 = t1.getConstructor(String.class);
        Constructor<T2> m2 = t2.getConstructor(String.class);
        // new
        BufferedReader in = IOUtils.getBufferedFileReader(filename);
        // FileReader(filename));
        for (String line; (line = in.readLine()) != null; ) {
            String[] tuple = line.trim().split("\t");
            String outer = tuple[0];
            String inner = tuple[1];
            String valStr = tuple[2];
            tdc.setCount(m1.newInstance(outer.trim()), m2.newInstance(inner.trim()), Double.parseDouble(valStr.trim()));
        }
        in.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 58 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class TrueCasingForNISTDocumentReaderAndWriter method main.

/**
   * for test only
   **/
public static void main(String[] args) throws IOException {
    Reader reader = new BufferedReader(new FileReader(args[0]));
    TrueCasingForNISTDocumentReaderAndWriter raw = new TrueCasingForNISTDocumentReaderAndWriter();
    raw.init(null);
    for (Iterator<List<CoreLabel>> it = raw.getIterator(reader); it.hasNext(); ) {
        List<CoreLabel> l = it.next();
        for (CoreLabel cl : l) {
            System.out.println(cl);
        }
        System.out.println("========================================");
    }
}
Also used : CoreLabel(edu.stanford.nlp.ling.CoreLabel) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) FileReader(java.io.FileReader)

Example 59 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Dictionaries method loadCorefDictPMI.

private static void loadCorefDictPMI(String file, Counter<Pair<String, String>> dict) {
    BufferedReader reader = null;
    try {
        reader = IOUtils.readerFromString(file);
        // Skip the first line (header)
        reader.readLine();
        while (reader.ready()) {
            String[] split = reader.readLine().split("\t");
            dict.setCount(new Pair<>(split[0], split[1]), Double.parseDouble(split[3]));
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeIgnoringExceptions(reader);
    }
}
Also used : BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Example 60 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Dictionaries method loadSignatures.

private static void loadSignatures(String file, Map<String, Counter<String>> sigs) {
    BufferedReader reader = null;
    try {
        reader = IOUtils.readerFromString(file);
        while (reader.ready()) {
            String[] split = reader.readLine().split("\t");
            Counter<String> cntr = new ClassicCounter<>();
            sigs.put(split[0], cntr);
            for (int i = 1; i < split.length; i = i + 2) {
                cntr.setCount(split[i], Double.parseDouble(split[i + 1]));
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeIgnoringExceptions(reader);
    }
}
Also used : BufferedReader(java.io.BufferedReader) ClassicCounter(edu.stanford.nlp.stats.ClassicCounter) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Aggregations

BufferedReader (java.io.BufferedReader)5548 InputStreamReader (java.io.InputStreamReader)3430 IOException (java.io.IOException)2601 FileReader (java.io.FileReader)1283 File (java.io.File)942 InputStream (java.io.InputStream)845 ArrayList (java.util.ArrayList)766 FileInputStream (java.io.FileInputStream)694 URL (java.net.URL)526 Test (org.junit.Test)447 FileNotFoundException (java.io.FileNotFoundException)380 StringReader (java.io.StringReader)340 BufferedWriter (java.io.BufferedWriter)242 HashMap (java.util.HashMap)232 HttpURLConnection (java.net.HttpURLConnection)231 Matcher (java.util.regex.Matcher)223 OutputStreamWriter (java.io.OutputStreamWriter)212 PrintWriter (java.io.PrintWriter)208 URLConnection (java.net.URLConnection)208 Reader (java.io.Reader)201