我正在使用雪球词干分析器,可以在此处找到 http://snowball.tartarus.org/
我正在使用这个论坛问题来为我自己的项目使用词干算法
Is there a java implementation of Porter2 stemmer
我使用给定的类并使用之前回答的帖子中给出的代码
Class stemClass = Class.forName("org.tartarus.snowball.ext." + lang + "Stemmer");
stemmer = (SnowballProgram) stemClass.newInstance();
stemmer.setCurrent("your_word");
stemmer.stem();
String your_stemmed_word = stemmer.getCurrent();
但是当我使用 try catch 语句时,我收到此错误
assmt1/invert3.java:339: error: incompatible types: try-with-resources not applicable to variable type
try( Class stemClass = Class.forName("org.tartarus.snowball.ext." +"english"+ "Stemmer");
^
(Class cannot be converted to AutoCloseable)
assmt1/invert3.java:340: error: incompatible types: try-with-resources not applicable to variable type
SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance())
^
(SnowballStemmer cannot be converted to AutoCloseable)
2 errors
真的不知道如何解决这个问题
最佳答案
只有实现了AutoCloseable
的类才能在 try-with-resources 中使用声明声明。
The
try-with-resources
statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implementsjava.lang.AutoCloseable
, which includes all objects which implementjava.io.Closeable
, can be used as a resource.
Class
和 SnowballStemmer
不是 AutoCloseable
。将其放在 try block 内:
try {
Class stemClass = Class.forName("org.tartarus.snowball.ext." +"english"+ "Stemmer");
SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance();
} catch(Exception e){
//Do Something
} finally {
//Do Something
}
关于java - 错误未报告异常 ClassNotFoundException;必须被捕获或宣布被扔出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40797827/