Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala get file path of file in resources folder

I am using the Stanford CRFClassifier and in order to run, it requires a file that is the trained classifier model. I have put this file in the resources directory. From the Javadocs for the CRFClassifier http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/ie/crf/CRFClassifier.html#getClassifier(java.lang.String) the path to the file must be an input to CRFClassifier.getClassifier() and it is a java.lang.String object. So my question is how do I tell .getClassifier() that the file is in the resources directory? i.e. how do I get the file path of a file in the resources directory?

I have tried simply

val classifier = CRFClassifier.getClassifier("./src/main/resources/my_model.ser.gz")

But this returns a FileNotFoundException.

I have also tried

Source.fromURL(getClass.getResource("/my_model.ser.gz"))

which returns a BufferedSource object, but I do not know how to get a file path from this.

Any help would be greatly appreciated.

like image 227
user1893354 Avatar asked May 23 '14 14:05

user1893354


People also ask

How do I retrieve files from resources folder?

In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream . // the stream holding the file content InputStream is = getClass().

How do you find the absolute path to a file in the resources folder of your project?

The function getAbsolutePath() will return the absolute (complete) path from the root directories. If the file object is created with an absolute path then getPath() and getAbsolutePath() will give the same results.


1 Answers

I managed to be able to get the file path by doing the following

val url=getClass.getResource("/my_model.ser.gz")

val classifier = CRFClassifier.getClassifier(url.getPath())

like image 82
user1893354 Avatar answered Sep 22 '22 20:09

user1893354