Skip to content

Commit

Permalink
Merge branch 'maw-server' of jamie:/u/nlp/git/javanlp into maw-server
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabor Angeli authored and Stanford NLP committed Sep 3, 2015
1 parent 3d870f8 commit a449d66
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions src/edu/stanford/nlp/pipeline/StanfordCoreNLP.java
Expand Up @@ -769,33 +769,16 @@ public void processFiles(String base, final Collection<File> files, int numThrea
*
* @return A consumer that can be passed into the processFiles method.
*/
protected static BiConsumer<Annotation, String> createOutputter(Properties properties, Supplier<AnnotationOutputter.Options> mkOptions) {
protected static BiConsumer<Annotation, OutputStream> createOutputter(Properties properties, Supplier<AnnotationOutputter.Options> mkOptions) {
final OutputFormat outputFormat =
OutputFormat.valueOf(properties.getProperty("outputFormat", DEFAULT_OUTPUT_FORMAT).toUpperCase());
String defaultExtension;
switch (outputFormat) {
case XML: defaultExtension = ".xml"; break;
case JSON: defaultExtension = ".json"; break;
case CONLL: defaultExtension = ".conll"; break;
case TEXT: defaultExtension = ".out"; break;
case SERIALIZED: defaultExtension = ".ser.gz"; break;
default: throw new IllegalArgumentException("Unknown output format " + outputFormat);
}
final String extension = properties.getProperty("outputExtension", defaultExtension);

final String serializerClass = properties.getProperty("serializer", GenericAnnotationSerializer.class.getName());
final String outputSerializerClass = properties.getProperty("outputSerializer", serializerClass);
final String outputSerializerName = (serializerClass.equals(outputSerializerClass))? "serializer":"outputSerializer";


return (Annotation annotation, String outputFilename) -> {
return (Annotation annotation, OutputStream fos) -> {
try {
// ensure we don't make filenames with doubled extensions like .xml.xml
if (!outputFilename.endsWith(extension)) {
outputFilename += extension;
}
OutputStream fos = new BufferedOutputStream(new FileOutputStream(outputFilename));

switch (outputFormat) {
case XML: {
try {
Expand All @@ -805,32 +788,26 @@ protected static BiConsumer<Annotation, String> createOutputter(Properties prope
} catch (NoSuchMethodException | IllegalAccessException | ClassNotFoundException | InvocationTargetException e) {
throw new RuntimeException(e);
}
fos.close();
break;
}
case JSON: {
new JSONOutputter().print(annotation, fos, mkOptions.get());
fos.close();
break;
}
case CONLL: {
new CoNLLOutputter().print(annotation, fos, mkOptions.get());
fos.close();
break;
}
case TEXT: {
new TextOutputter().print(annotation, fos, mkOptions.get());
fos.close();
break;
}
case SERIALIZED: {
if (outputSerializerClass != null) {
AnnotationSerializer outputSerializer = loadSerializer(outputSerializerClass, outputSerializerName, properties);
outputSerializer.write(annotation, fos).close();
} else {
IOUtils.writeObjectToFile(annotation, outputFilename);
outputSerializer.write(annotation, fos);
break;
}
break;
}
default:
throw new IllegalArgumentException("Unknown output format " + outputFormat);
Expand All @@ -856,7 +833,7 @@ protected static BiConsumer<Annotation, String> createOutputter(Properties prope
*/
protected static void processFiles(String base, final Collection<File> files, int numThreads,
Properties properties, BiConsumer<Annotation, Consumer<Annotation>> annotate,
BiConsumer<Annotation, String> print) throws IOException {
BiConsumer<Annotation, OutputStream> print) throws IOException {
List<Runnable> toRun = new LinkedList<Runnable>();

// Process properties here
Expand Down Expand Up @@ -936,6 +913,7 @@ protected static void processFiles(String base, final Collection<File> files, in
}

final String finalOutputFilename = outputFilename;

//register a task...
//catching exceptions...
try {
Expand Down Expand Up @@ -989,7 +967,14 @@ protected static void processFiles(String base, final Collection<File> files, in
Throwable ex = finishedAnnotation.get(CoreAnnotations.ExceptionAnnotation.class);
if (ex == null) {
//--Output File
print.accept(finishedAnnotation, finalOutputFilename);
try {
OutputStream fos = new BufferedOutputStream(new FileOutputStream(finalOutputFilename));
print.accept(finishedAnnotation, fos);
fos.close();
} catch(IOException e) {
throw new RuntimeIOException(e);
}

synchronized (totalProcessed) {
totalProcessed.incValue(1);
if (totalProcessed.intValue() % 1000 == 0) {
Expand Down

0 comments on commit a449d66

Please sign in to comment.