我正在使用 OpenNLP 在 Android 中开发 TTS。 我将每个配置文件放在/assets/目录中。 并从AssetManager获取InputStream。但问题是当InputStream作为初始化POSModel的输入参数时,会抛出InvalidFormatException。
代码如下:
1.获取输入流。
public static InputStream getStream(String propertyName)
throws FileNotFoundException, MaryConfigurationException {
InputStream stream = null;
String propertyValue = getProperty(propertyName);
if (propertyValue == null) {
return null;
} else {
try {
stream = (InputStream)Globals.context.getResources().getAssets().open(propertyValue);
} catch (IOException e) {
e.printStackTrace();
}
}
return stream;
}
新的 Pos 标记器。
InputStream modelStream = (InputStream)MaryProperties.needStream(propertyPrefix+"model"); //here should return the InputStream of en-pos-maxent.bin
try {
tagger = new POSTaggerME(new POSModel(modelStream));
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
此处获取 InvalidFormatException,
opennlp.tools.util.InvalidFormatException: The profile data stream has an invalid format
有人知道吗?
最佳答案
确保您的方法返回正确的InputStream
类型 - OpenNLP 将寻找 FileInputStream
或 ByteArrayInputStream
。该错误似乎表明这是问题所在。您打开实际文件的方式似乎也有点可疑 - 您的评论说您正在打开 en-pos-maxent.bin
,但是 propertyPrefix+"model"
看起来不像我打开具有该名称的文件的方式。
关于java - Android 中的 OpenNLP 获取无效格式输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19272822/