Skip to content

Instantly share code, notes, and snippets.

@renaud
Created December 23, 2011 09:25
Show Gist options
  • Save renaud/1513709 to your computer and use it in GitHub Desktop.
Save renaud/1513709 to your computer and use it in GitHub Desktop.
UIMA Annotation Engine for Python, using Jython
@TypeCapability(inputs = { TypeSystem.TOKEN }, outputs = { TypeSystem.BIO_ENTITY_MENTION })
public class JythonAE extends JCasAnnotator_ImplBase {
public static final String SCRIPT_PATH = "script_path";
@ConfigurationParameter(name = SCRIPT_PATH)
private String scriptPath;
private PythonInterpreter interp;
private String scriptFile;
@Override
public void initialize(UimaContext context)
throws ResourceInitializationException {
super.initialize(context);
interp = new PythonInterpreter();
try {
scriptFile = ResourceHelper.getFile(scriptPath).getAbsolutePath();
} catch (Exception e) {
throw new ResourceInitializationException(
ResourceInitializationException.NO_RESOURCE_FOR_PARAMETERS,
new Object[] { scriptPath });
}
}
@Override
public void process(JCas jcas) throws AnalysisEngineProcessException {
try {
interp.set("jcas", jcas); // make jcas available in script
interp.execfile(scriptFile);
} catch (Exception e) {
throw new AnalysisEngineProcessException(
AnalysisEngineProcessException.ANNOTATOR_EXCEPTION,
new Object[] {}, e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment