亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

opennlp.tools.postag.POSTaggerME.train()

opennlp.tools.postag.POSTaggerME.train()

MMTTMM 2024-01-25 22:01:11
也有同樣的問題!我得到InputSteram = null,我使用 IntelliJ IDEA、OpenNLP 1.9.1。在 Ubuntu 18.04 上    public void makeDataTrainingModel() {    model = null;    System.out.println("POS model started");    //InputStream dataIn = null;    InputStreamFactory dataIn = null;    try {        dataIn = new InputStreamFactory() {            public InputStream createInputStream() throws IOException {                return NLPClassifier.class.getResourceAsStream("/home/int/src              /main/resources/en-pos.txt");            }        };        //I get null pointer here in dataIn        ObjectStream<String> lineStream = new PlainTextByLineStream((InputStreamFactory) , "UTF-8");        ObjectStream<POSSample> sampleStream = new WordTagSampleStream(lineStream);     **//This train part IS NOT WORK ?**        model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), null);    } catch (IOException e) {        // Failed to read or parse training data, training failed        e.printStackTrace();    } finally {        if (dataIn != null) {                       System.out.println("InputStreamFactory was not created!");        }    }    System.out.println("POS model done...");    System.out.println("Success generate model...");    //write Data model    OutputStream modelOut = null;    try {        String currentDir = new File("").getAbsolutePath();        modelOut = new BufferedOutputStream(new FileOutputStream(currentDir + "//src//main//resources//example-bad-model.dat"));        model.serialize(modelOut);    } catch (IOException e) {        // Failed to save model        e.printStackTrace();    } finally {        if (modelOut != null) {            try {                modelOut.close();            } catch (IOException e) {                // Failed to correctly save model.                // Written model might be invalid.                e.printStackTrace();            }        }    }    System.out.println("Model generated and treated successfully...");}
查看完整描述

2 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

如果getResourceAsStream返回null,則表示未找到該資源。

您應該檢查null并執行其他操作,例如拋出異常(IOException或者FileNotFoundException在本例中,因為聲明IOException允許子類throws) - 您不應該讓它傳遞null給代碼的其余部分。

NLPClassifier.class.getResourceAsStream("/home/int/src/main/resources/en-pos.txt")不起作用,因為資源與 Java 包具有相同的結構,只不過點被替換為斜線。它不是文件系統中的路徑。

將其更改為:(getResourceAsStream("/en-pos.txt")因為您的文件位于包層次結構的根目錄)


查看完整回答
反對 回復 2024-01-25
?
慕容森

TA貢獻1853條經驗 獲得超18個贊

我更改了我的代碼,正如 Erwin Bolwidt 所說:


? ? /** I commented this part

? ? return NLPClassifier.class.getResourceAsStream("/home/interceptor/src/main/resources/en-pos.txt");

? ? */

? ? /**

? ? ?Add this location of my resoures:

? ? ?/Project/src/main/resources

? ? */

? ? return getClass().getClassLoader().getResourceAsStream("en-pos.txt");

之后,我發現?Apache OpenNLP: java.io.FileInputStream無法轉換為 opennlp.tools.util.InputStreamFactory,有類似的問題,但使用其他方法。@schrieveslaach 說

您需要一個InputStreamFactory 實例來檢索您的InputStream。此外,TokenNameFinderFactory 不能為 null!像這樣posFactory - 不能為空!

? ?/**

? ? ?*? Factory must not be a null. Add posModel.getFactory()

? ? ?*? model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), null);

? ? ?*/

? ? ?model = POSTaggerME.train("en", sampleStream, TrainingParameters.defaultParams(), posModel.getFactory());

倉庫中項目的完整代碼?https://github.com/AlexTitovWork/NLPclassifier


查看完整回答
反對 回復 2024-01-25
  • 2 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號