当前位置:  开发笔记 > 编程语言 > 正文

Apache OpenNLP:java.io.FileInputStream无法强制转换为opennlp.tools.util.InputStreamFactory

如何解决《ApacheOpenNLP:java.io.FileInputStream无法强制转换为opennlp.tools.util.InputStreamFactory》经验,为你挑选了1个好方法。

我正在尝试使用Apache OpenNLP 1.7构建自定义NER.从可用的文档在这里,我开发了以下代码

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 lineStream = new PlainTextByLineStream(new FileInputStream("/opt/NLP/data/person.train"), charset);**
            ObjectStream 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 argument'file''to insputstreamfactory'

我被迫投了这个,因为它显示错误.

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

java.io.FileInputStream cannot be cast to opennlp.tools.util.InputStreamFactory

这里有什么遗漏的吗?

编辑1:Person.train文件包含此数据

 Hardik  is a software Professional. Hardik works at company and  is part of development team.  Hardik lives in New York
 Hardik loves R statistical software
 Hardik is a student at ISB
 Hardik loves nature

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



1> schrieveslaa..:

你需要一个实例来InputStreamFactory检索你的InputStream.另外,TokenNameFinderFactory一定不能null.

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 lineStream = new PlainTextByLineStream(isf, charset);
        ObjectStream 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文件包含此数据

 Hardik  is a software Professional. Hardik works at company and  is part of development team.  Hardik lives in New York
 Hardik loves R statistical software
 Hardik is a student at ISB
 Hardik loves nature

推荐阅读
重庆制造漫画社
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有