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);
}
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations