问题描述
我使用的是Stanford CRFClassifier,为了运行,它需要一个训练有素的分类器模型。我把这个文件放在资源目录中。来自CRFClassifier的Javadocs
文件的路径必须是CRFClassifier.getClassifier()的输入,它是一个java.lang.String对象。所以我的问题是如何告诉.getClassifier()文件在资源目录中?即如何获取资源目录中文件的文件路径?
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?
我试过简单地
val classifier = CRFClassifier.getClassifier("./src/main/resources/my_model.ser.gz")
但是这会返回一个FileNotFoundException。
But this returns a FileNotFoundException.
我也试过了
Source.fromURL(getClass.getResource("/my_model.ser.gz"))
返回一个BufferedSource对象,但我不知道如何从中获取文件路径。
which returns a BufferedSource object, but I do not know how to get a file path from this.
任何帮助都将不胜感激。
Any help would be greatly appreciated.
推荐答案
我设法通过以下方式获取文件路径
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())
这篇关于Scala在资源文件夹中获取文件的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!