Search in sources :

Example 66 with BufferedReader

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

the class ISODateInstance method tokenizeDate.

/* -------------------------- Tokenization and Field Extraction -------------------------- */
//These methods are taken directly from or modified slightly from {@link DateInstance}
private void tokenizeDate(String inputDate) {
    tokens = new ArrayList<>();
    Pattern pat = Pattern.compile("[-]");
    if (inputDate == null) {
        System.out.println("Null input date");
    }
    Matcher m = pat.matcher(inputDate);
    String str = m.replaceAll(" - ");
    str = str.replaceAll(",", " ");
    PTBTokenizer<Word> tokenizer = PTBTokenizer.newPTBTokenizer(new BufferedReader(new StringReader(str)));
    while (tokenizer.hasNext()) {
        Word nextToken = tokenizer.next();
        tokens.add(nextToken.toString());
    }
    if (DEBUG) {
        System.out.println("tokens:" + tokens);
    }
}
Also used : Pattern(java.util.regex.Pattern) Word(edu.stanford.nlp.ling.Word) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader)

Example 67 with BufferedReader

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

the class Prior method main.

public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("/tmp/acstats"));
    Prior p = new Prior(br);
    HashSet hs = new HashSet();
    hs.add("workshopname");
    //hs.add("workshopacronym");
    double d = p.get(hs);
    System.out.println("d is " + d);
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) HashSet(java.util.HashSet)

Example 68 with BufferedReader

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

the class UnitPrefix method loadPrefixes.

public static List<UnitPrefix> loadPrefixes(String filename) throws IOException {
    Pattern commaPattern = Pattern.compile("\\s*,\\s*");
    BufferedReader br = IOUtils.getBufferedFileReader(filename);
    String headerString = br.readLine();
    String[] header = commaPattern.split(headerString);
    Map<String, Integer> headerIndex = new HashMap<>();
    for (int i = 0; i < header.length; i++) {
        headerIndex.put(header[i], i);
    }
    int iName = headerIndex.get("name");
    int iPrefix = headerIndex.get("prefix");
    int iBase = headerIndex.get("base");
    int iExp = headerIndex.get("exp");
    int iSystem = headerIndex.get("system");
    String line;
    List<UnitPrefix> list = new ArrayList<>();
    while ((line = br.readLine()) != null) {
        String[] fields = commaPattern.split(line);
        double base = Double.parseDouble(fields[iBase]);
        double exp = Double.parseDouble(fields[iExp]);
        double scale = Math.pow(base, exp);
        UnitPrefix unitPrefix = new UnitPrefix(fields[iName], fields[iPrefix], scale, fields[iSystem]);
        list.add(unitPrefix);
    }
    br.close();
    return list;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList)

Example 69 with BufferedReader

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

the class Units method loadUnits.

public static List<Unit> loadUnits(String filename) throws IOException {
    Pattern commaPattern = Pattern.compile("\\s*,\\s*");
    BufferedReader br = IOUtils.getBufferedFileReader(filename);
    String headerString = br.readLine();
    String[] header = commaPattern.split(headerString);
    Map<String, Integer> headerIndex = new HashMap<>();
    for (int i = 0; i < header.length; i++) {
        headerIndex.put(header[i], i);
    }
    int iName = headerIndex.get("unit");
    int iPrefix = headerIndex.get("prefix");
    int iSymbol = headerIndex.get("symbol");
    int iType = headerIndex.get("type");
    int iSystem = headerIndex.get("system");
    int iDefaultUnit = headerIndex.get("defaultUnit");
    int iDefaultUnitScale = headerIndex.get("defaultUnitScale");
    String line;
    List<Unit> list = new ArrayList<>();
    Map<String, Unit> unitsByName = new HashMap<>();
    Map<String, Pair<String, Double>> unitToDefaultUnits = new HashMap<>();
    while ((line = br.readLine()) != null) {
        String[] fields = commaPattern.split(line);
        Unit unit = new Unit(fields[iName], fields[iSymbol], fields[iType].toUpperCase());
        unit.system = fields[iSystem];
        if (fields.length > iPrefix) {
            unit.prefixSystem = fields[iPrefix];
        }
        if (fields.length > iDefaultUnit) {
            double scale = 1.0;
            if (fields.length > iDefaultUnitScale) {
                scale = Double.parseDouble(fields[iDefaultUnitScale]);
            }
            unitToDefaultUnits.put(unit.getName(), Pair.makePair(fields[iDefaultUnit], scale));
        }
        unitsByName.put(unit.getName(), unit);
        list.add(unit);
    }
    for (Map.Entry<String, Pair<String, Double>> entry : unitToDefaultUnits.entrySet()) {
        Unit unit = unitsByName.get(entry.getKey());
        Unit defaultUnit = unitsByName.get(entry.getValue().first);
        if (defaultUnit != null) {
            unit.defaultUnit = defaultUnit;
            unit.defaultUnitScale = entry.getValue().second;
        } else {
            Redwood.Util.warn("Unknown default unit " + entry.getValue().first + " for " + entry.getKey());
        }
    }
    br.close();
    return list;
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) HashMap(java.util.HashMap) Map(java.util.Map) Pair(edu.stanford.nlp.util.Pair)

Example 70 with BufferedReader

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

the class CMMClassifier method getThresholds.

private static List<Pair<Pattern, Integer>> getThresholds(String filename) {
    BufferedReader in = null;
    try {
        in = IOUtils.readerFromString(filename);
        List<Pair<Pattern, Integer>> thresholds = new ArrayList<>();
        for (String line; (line = in.readLine()) != null; ) {
            int i = line.lastIndexOf(' ');
            Pattern p = Pattern.compile(line.substring(0, i));
            //log.info(":"+line.substring(0,i)+":");
            Integer t = Integer.valueOf(line.substring(i + 1));
            Pair<Pattern, Integer> pair = new Pair<>(p, t);
            thresholds.add(pair);
        }
        in.close();
        return thresholds;
    } catch (IOException e) {
        throw new RuntimeIOException("Error reading threshold file", e);
    } finally {
        IOUtils.closeIgnoringExceptions(in);
    }
}
Also used : Pattern(java.util.regex.Pattern) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) IOException(java.io.IOException)

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