Android中的OpenNLP获取无效的格式输入流

时间:2013-10-09 13:01:23

标签: java android opennlp

我使用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

有没有人有一些想法?

2 个答案:

答案 0 :(得分:1)

确保您的方法返回InputStream的正确类型 - OpenNLP将寻找FileInputStreamByteArrayInputStream。这个错误似乎指出了这个问题。您打开实际文件的方式似乎也有点怀疑 - 您的评论说您正在打开en-pos-maxent.bin,但propertyPrefix+"model"看起来不像我打开文件的方式用那个名字。

答案 1 :(得分:0)

我发现了类似的InvalidFormatException异常。追踪进一步显示失败的功能是

org.xml.sax.helpers.XMLReaderFactory.createXMLReader

post解决了它。