Skip to content

Commit

Permalink
Merge pull request #468 from elifesciences/fix-label-task-very-specia…
Browse files Browse the repository at this point in the history
…l-characters

added workaround for setting JEP value with very special characters
  • Loading branch information
kermitt2 committed Aug 15, 2019
2 parents cad7683 + e40f620 commit 27a1eed
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions grobid-core/src/main/java/org/grobid/core/jni/DeLFTModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.grobid.core.GrobidModels;
import org.grobid.core.exceptions.GrobidException;
import org.grobid.core.utilities.GrobidProperties;
import org.grobid.core.utilities.IOUtilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -68,8 +69,29 @@ public LabelTask(String modelName, String data) {
//System.out.println("label thread: " + Thread.currentThread().getId());
this.modelName = modelName;
this.data = data;
}

}

private void setJepStringValueWithFileFallback(
Jep jep, String name, String value
) throws JepException, IOException {
try {
jep.set(name, value);
} catch(JepException e) {
File tempFile = IOUtilities.newTempFile(name, ".data");
LOGGER.debug(
"Falling back to file {} due to exception: {}",
tempFile, e.toString()
);
IOUtilities.writeInFile(tempFile.getAbsolutePath(), value);
jep.eval("from pathlib import Path");
jep.eval(
name + " = Path('" + tempFile.getAbsolutePath() +
"').read_text(encoding='utf-8')"
);
tempFile.delete();
}
}

@Override
public String call() {
Jep jep = JEPThreadPool.getInstance().getJEPInstance();
Expand All @@ -78,7 +100,7 @@ public String call() {
//System.out.println(this.data);

// load and tag
jep.set("input", this.data);
this.setJepStringValueWithFileFallback(jep, "input", this.data);
jep.eval("x_all, f_all = load_data_crf_string(input)");
Object objectResults = jep.getValue(this.modelName+".tag(x_all, None)");

Expand Down

0 comments on commit 27a1eed

Please sign in to comment.