use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.
the class StanfordCoreNLPClient method shell.
/**
* Runs an interactive shell where input text is processed with the given pipeline.
*
* @param pipeline The pipeline to be used
* @throws IOException If IO problem with stdin
*/
private static void shell(StanfordCoreNLPClient pipeline) throws IOException {
log.info("Entering interactive shell. Type q RETURN or EOF to quit.");
final StanfordCoreNLP.OutputFormat outputFormat = StanfordCoreNLP.OutputFormat.valueOf(pipeline.properties.getProperty("outputFormat", "text").toUpperCase(Locale.ROOT));
IOUtils.console("NLP> ", line -> {
if (!line.isEmpty()) {
Annotation anno = pipeline.process(line);
try {
switch(outputFormat) {
case XML:
new XMLOutputter().print(anno, System.out);
break;
case JSON:
new JSONOutputter().print(anno, System.out);
System.out.println();
break;
case CONLL:
new CoNLLOutputter().print(anno, System.out);
System.out.println();
break;
case TEXT:
new TextOutputter().print(anno, System.out);
break;
case SERIALIZED:
warn("You probably cannot read the serialized output, so printing in text instead");
new TextOutputter().print(anno, System.out);
break;
default:
throw new IllegalArgumentException("Cannot output in format " + outputFormat + " from the interactive shell");
}
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
});
}
use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.
the class ChineseCorefBenchmarkSlowITest method runCorefTest.
private static String runCorefTest(boolean deleteOnExit) throws Exception {
final File WORK_DIR_FILE = File.createTempFile("DcorefChineseBenchmarkTest", "");
if (!(WORK_DIR_FILE.delete() && WORK_DIR_FILE.mkdir())) {
throw new RuntimeIOException("Couldn't create temp directory " + WORK_DIR_FILE);
}
if (deleteOnExit) {
WORK_DIR_FILE.deleteOnExit();
}
String baseLogFile = WORK_DIR_FILE + File.separator + "log";
System.err.println("Base log file name: " + WORK_DIR_FILE);
String current = new java.io.File(".").getCanonicalPath();
System.err.println("Current dir:" + current);
String currentDir = System.getProperty("user.dir");
System.err.println("Current dir using System:" + currentDir);
String[] corefArgs = { "-props", "edu/stanford/nlp/coref/hybrid/properties/zh-dcoref-conll.properties", '-' + HybridCorefProperties.LOG_PROP, baseLogFile, '-' + CorefProperties.OUTPUT_PATH_PROP, WORK_DIR_FILE.toString() + File.separator };
Properties props = StringUtils.argsToProperties(corefArgs);
System.err.println("Running coref with arguments:");
System.err.println(props);
HybridCorefSystem.runCoref(corefArgs);
String actualResults = IOUtils.slurpFile(baseLogFile);
return actualResults;
}
use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.
the class DcorefBenchmarkSlowITest method runCorefTest.
private static String runCorefTest(boolean deleteOnExit) throws Exception {
final File WORK_DIR_FILE = File.createTempFile("DcorefBenchmarkTest", "");
if (!(WORK_DIR_FILE.delete() && WORK_DIR_FILE.mkdir())) {
throw new RuntimeIOException("Couldn't create temp directory " + WORK_DIR_FILE);
}
if (deleteOnExit) {
WORK_DIR_FILE.deleteOnExit();
}
String baseLogFile = WORK_DIR_FILE + File.separator + "log";
System.err.println("Base log file name: " + WORK_DIR_FILE);
String current = new java.io.File(".").getCanonicalPath();
System.err.println("Current dir:" + current);
String currentDir = System.getProperty("user.dir");
System.err.println("Current dir using System:" + currentDir);
String[] corefArgs = { "-props", "edu/stanford/nlp/dcoref/coref.properties", '-' + Constants.LOG_PROP, baseLogFile, '-' + Constants.CONLL_OUTPUT_PROP, WORK_DIR_FILE.toString() };
Properties props = StringUtils.argsToProperties(corefArgs);
System.err.println("Running dcoref with properties:");
System.err.println(props);
String logFile = SieveCoreferenceSystem.initializeAndRunCoref(props);
System.err.println(logFile);
String actualResults = IOUtils.slurpFile(logFile);
return actualResults;
}
use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.
the class NERClassifierCombiner method createNERClassifierCombiner.
/**
* This factory method is used to create the NERClassifierCombiner used in NERCombinerAnnotator
* (and, thence, in StanfordCoreNLP).
*
* @param name A "x.y" format property name prefix (the "x" part). This is commonly null,
* and then "ner" is used. If it is the empty string, then no property prefix is used.
* @param passDownProperties Property names for which the property should be passed down
* to the NERClassifierCombiner. The default is not to pass down, but pass down is
* useful for things like charset encoding.
* @param properties Various properties, including a list in "ner.model".
* The used ones start with name + "." or are in passDownProperties
* @return An NERClassifierCombiner with the given properties
*/
public static NERClassifierCombiner createNERClassifierCombiner(String name, Set<String> passDownProperties, Properties properties) {
String prefix = (name == null) ? "ner." : name.isEmpty() ? "" : name + '.';
String modelNames = properties.getProperty(prefix + "model");
if (modelNames == null) {
modelNames = (DefaultPaths.DEFAULT_NER_THREECLASS_MODEL + ',' + DefaultPaths.DEFAULT_NER_MUC_MODEL + ',' + DefaultPaths.DEFAULT_NER_CONLL_MODEL);
}
// but modelNames can still be empty string if set explicitly to be empty!
String[] models;
modelNames = modelNames.trim();
if (!modelNames.isEmpty()) {
models = modelNames.split(",");
} else {
// Allow for no real NER model - can just use numeric classifiers or SUTime
log.info("WARNING: no NER models specified");
models = StringUtils.EMPTY_STRING_ARRAY;
}
NERClassifierCombiner nerCombiner;
try {
boolean applyNumericClassifiers = PropertiesUtils.getBool(properties, prefix + APPLY_NUMERIC_CLASSIFIERS_PROPERTY_BASE, APPLY_NUMERIC_CLASSIFIERS_DEFAULT);
boolean useSUTime = PropertiesUtils.getBool(properties, prefix + NumberSequenceClassifier.USE_SUTIME_PROPERTY_BASE, NumberSequenceClassifier.USE_SUTIME_DEFAULT);
Properties combinerProperties;
if (passDownProperties != null) {
combinerProperties = PropertiesUtils.extractSelectedProperties(properties, passDownProperties);
if (useSUTime) {
// Make sure SUTime parameters are included
Properties sutimeProps = PropertiesUtils.extractPrefixedProperties(properties, NumberSequenceClassifier.SUTIME_PROPERTY + ".", true);
PropertiesUtils.overWriteProperties(combinerProperties, sutimeProps);
}
} else {
// if passDownProperties is null, just pass everything through
combinerProperties = properties;
}
// Properties combinerProperties = PropertiesUtils.extractSelectedProperties(properties, passDownProperties);
Language nerLanguage = Language.fromString(properties.getProperty(prefix + "language"), Language.ENGLISH);
nerCombiner = new NERClassifierCombiner(applyNumericClassifiers, nerLanguage, useSUTime, combinerProperties, models);
} catch (IOException e) {
throw new RuntimeIOException(e);
}
return nerCombiner;
}
use of edu.stanford.nlp.io.RuntimeIOException in project CoreNLP by stanfordnlp.
the class NERClassifierCombiner method serializeClassifier.
// write an NERClassifierCombiner to an ObjectOutputStream
@Override
public void serializeClassifier(ObjectOutputStream oos) {
try {
// first write the ClassifierCombiner part to disk
super.serializeClassifier(oos);
// write whether to use SUTime
oos.writeBoolean(useSUTime);
// write whether to use NumericClassifiers
oos.writeBoolean(applyNumericClassifiers);
} catch (IOException e) {
throw new RuntimeIOException(e);
}
}
Aggregations