我使用OpenNLP在Android中开发TTS。 我把每个配置文件放在/ assets /目录中。 并从AssetManager获取InputStream。但问题是当InputStream作为初始化POSModel的输入参数时,抛出InvalidFormatException。
以下代码:
1.get InputStream。
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;
}
new a Pos tagger。
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
有没有人有一些想法?
答案 0 :(得分:1)
确保您的方法返回InputStream
的正确类型 - OpenNLP将寻找FileInputStream
或ByteArrayInputStream
。这个错误似乎指出了这个问题。您打开实际文件的方式似乎也有点怀疑 - 您的评论说您正在打开en-pos-maxent.bin
,但propertyPrefix+"model"
看起来不像我打开文件的方式用那个名字。
答案 1 :(得分:0)
我发现了类似的InvalidFormatException异常。追踪进一步显示失败的功能是
org.xml.sax.helpers.XMLReaderFactory.createXMLReader
这post解决了它。