我正在使用 IKVM 将一些 java 库移植到我的 c# 项目中。库 api (StanfordNLP) 要求加载一个文件来训练 nlp 函数使用的统计模型。从文件系统加载文件已经运行了好几个星期,但我现在想将文件作为嵌入资源添加到 dll 中,而不是从文件系统中检索它。
问题是 java api 没有找到 .net 嵌入式资源。
以下是从文件系统检索文件时有效的代码片段:
public class SNLPModel
{
public LexicalizedParser LP;
public SNLPModel()
{
// Using a relative file path in the target build directory
LP = LexicalizedParser.loadModel("models-stanford\\englishPCFG.ser.gz");
}
}
但是,当我将“englishPCFG.ser.gz”文件作为 Visual Studio 中的嵌入式资源(使用 VS2012)并更改代码以匹配时:
public class SNLPModel
{
public LexicalizedParser LP;
public SNLPModel()
{
// Using this line of code to verify that the file is being loaded as
// an embedded resource. Running in debug, I have verified that it is, and
// noted its complete name.
string[] s = System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceNames();
java.io.InputStream modelFile = java.lang.ClassLoader
.getSystemResourceAsStream
("FeatureExtraction.StanfordNLP_Models.englishPCFG.ser.gz");
java.io.ObjectInputStream x = new java.io.ObjectInputStream(modelFile);
LP = LexicalizedParser.loadModel(x);
}
}
InputStream 对象 modelFile 始终返回 null。我尝试了各种形式的资源字符串,将前两个点(“.”)替换为正斜杠(“/”)、反斜杠(“\”)和双反斜杠(“\\”)。我开始怀疑 java.io 无法访问 .net 资源。java api 不能识别 .net 资源也就不足为奇了,但我认为 IKVM 可能会提供一个桥梁。我看到了对名为 IKVM.Internals.VirtualFileSystem 的引用,但只有一个引用 (http://old.nabble.com/Manual-by-name-embedded-resource-lookup--td31162421.html) 并且没有t 找到任何实际包含该类的 IKVM dll。
任何帮助将非常感激。我在用:
c#.NET 4.5
视觉工作室 2012
最新的斯坦福 NLP java 库
IKVM 7.0.4335.0