Loading an external resource from the Java classpath

28 views
Skip to first unread message

David Kincaid

unread,
Jun 18, 2014, 3:34:31 PM6/18/14
to uimafi...@googlegroups.com
I would like to load an external resource for an analysis engine from the Java classpath. Basically I'm following the uimaFIT doc section 7.1.1, but trying to load the model file from the Java classpath instead of an http url. 

The code in the docs is like this:

// Create descriptor
AnalysisEngineDescription tokenizer = createEngineDescription(
  Tokenizer.class,
  UimaUtil.TOKEN_TYPE_PARAMETER, Token.class.getName(),
  UimaUtil.SENTENCE_TYPE_PARAMETER, Sentence.class.getName());

// Create the external resource dependency for the model and bind it
createDependencyAndBind(tokenizer, UimaUtil.MODEL_PARAMETER,
  TokenizerModelResourceImpl.class,
  "http://opennlp.sourceforge.net/models-1.5/en-token.bin");
and I'd like to be able to specify the model file as something like "classpath:com/company/models/en-sent.bin" instead of that http url. Is this possible and if so how would I do it. Just putting the string in there throws a NPE when it tries to open an input stream.

Thanks,

Dave

Richard Eckart de Castilho

unread,
Jun 18, 2014, 4:24:27 PM6/18/14
to uimafi...@googlegroups.com
The OpenNLP UIMA components use the UIMA DataResource_impl which appears to support URLs and files, but not loading from the classpath. Mind that the "classpath:" pseudo-protocol is not supported by Java out-of-the-box.

You have several options, I suppose.

1) instead of passing "http://"... you could use pass the URL you get as a result of a getClass().getResource("location/in/classpath") (maybe converting it to a string instead of directly passing the URL object) - this is the approach I would recommend if you want to use the UIMA wrappers from the OpenNLP project.

2) register your own url handler for the "classpath:" pseudo-location

3) use UIMA components from a collection which supports the "classpath:" location, e.g. the OpenNLP wrappers from DKPro Core [1]. There are some Groovy-based examples here [2]. Usually DKPro Core automatically loads a model depending on the language of your CAS (hence none of the example specifies a model location explicitly). But a location can be manually specified using PARAM_LOCATION.

Cheers,

-- Richard

[1] http://dkpro-core-asl.googlecode.com
[2] https://code.google.com/p/dkpro-core-asl/wiki/DKProGroovyCookbook

P.S.: I'm working on DKPro Core

David Kincaid

unread,
Jun 18, 2014, 6:15:48 PM6/18/14
to uimafi...@googlegroups.com
Thanks a lot for the answer, Richard. I used your first suggestion successfully. I'll definitely take a look at DKPro too.

Dave
Reply all
Reply to author
Forward
0 new messages