首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache OpenNLP:不能将java.io.FileInputStream转换为opennlp.tools.util.InputStreamFactory

Apache OpenNLP:不能将java.io.FileInputStream转换为opennlp.tools.util.InputStreamFactory
EN

Stack Overflow用户
提问于 2017-01-17 14:38:49
回答 1查看 1.5K关注 0票数 4

我正在尝试使用ApacheApache1.7构建一个自定义OpenNLP。从可用的这里文档中,我开发了以下代码

代码语言:javascript
复制
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import opennlp.tools.namefind.NameFinderME;
import opennlp.tools.namefind.NameSample;
import opennlp.tools.namefind.NameSampleDataStream;
import opennlp.tools.namefind.TokenNameFinderFactory;
import opennlp.tools.namefind.TokenNameFinderModel;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.TrainingParameters;

public class PersonClassifierTrainer {

        static String modelFile = "/opt/NLP/data/en-ner-customperson.bin";

        public static void main(String[] args) throws IOException {

            Charset charset = Charset.forName("UTF-8");
            **ObjectStream<String> lineStream = new PlainTextByLineStream(new FileInputStream("/opt/NLP/data/person.train"), charset);**
            ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);

            TokenNameFinderModel model;
            TokenNameFinderFactory nameFinderFactory = null;

            try {
                model = NameFinderME.train("en", "person", sampleStream, TrainingParameters.defaultParams(),
                        nameFinderFactory);
            } finally {
                sampleStream.close();
            }

            BufferedOutputStream modelOut = null;

            try {
                modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
                model.serialize(modelOut);
            } finally {
                if (modelOut != null)
                    modelOut.close();
            }
        }
    }

上面高亮显示的代码显示- 'Cast参数‘文件’到‘浓缩流工厂’

我被迫这样做,因为否则会显示错误。

现在,当我运行我的代码时,我会得到以下错误

代码语言:javascript
复制
java.io.FileInputStream cannot be cast to opennlp.tools.util.InputStreamFactory

这里有遗漏什么吗?

编辑1: Person.train文件具有以下数据

代码语言:javascript
复制
<START:person> Hardik <END> is a software Professional.<START:person> Hardik works at company<END> and <START:person> is part of development team<END>. <START:person> Hardik<END> lives in New York
<START:person> Hardik<END> loves R statistical software
<START:person> Hardik<END> is a student at ISB
<START:person> Hardik<END> loves nature

Edit2:我现在得到了空指针异常,有帮助吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-17 14:46:14

您需要一个InputStreamFactory实例,它将检索您的InputStream。此外,TokenNameFinderFactory不能是null

代码语言:javascript
复制
public class PersonClassifierTrainer {

    static String modelFile = "/opt/NLP/data/en-ner-customperson.bin";

    public static void main(String[] args) throws IOException {

        InputStreamFactory isf = new InputStreamFactory() {
            public InputStream createInputStream() throws IOException {
                return new FileInputStream("/opt/NLP/data/person.train");
            }
        };

        Charset charset = Charset.forName("UTF-8");
        ObjectStream<String> lineStream = new PlainTextByLineStream(isf, charset);
        ObjectStream<NameSample> sampleStream = new NameSampleDataStream(lineStream);

        TokenNameFinderModel model;
        TokenNameFinderFactory nameFinderFactory = new TokenNameFinderFactory();

        try {
            model = NameFinderME.train("en", "person", sampleStream, TrainingParameters.defaultParams(),
                    nameFinderFactory);
        } finally {
            sampleStream.close();
        }

        BufferedOutputStream modelOut = null;

        try {
            modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
            model.serialize(modelOut);
        } finally {
            if (modelOut != null)
                modelOut.close();
        }
    }
}

编辑1: Person.train文件具有以下数据

代码语言:javascript
复制
<START:person> Hardik <END> is a software Professional.<START:person> Hardik works at company<END> and <START:person> is part of development team<END>. <START:person> Hardik<END> lives in New York
<START:person> Hardik<END> loves R statistical software
<START:person> Hardik<END> is a student at ISB
<START:person> Hardik<END> loves nature
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41699913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档