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")
因為您的文件位于包層次結構的根目錄)

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
添加回答
舉報