0

我正在使用可以在这里找到的雪球词干分析器http://snowball.tartarus.org/

我正在使用这个论坛问题为我自己的项目使用词干算法

是否有 Porter2 词干分析器的 java 实现

我使用给定的类并使用之前回答的帖子中给出的代码

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

真的不知道如何解决这个问题

4

1 回答 1

1

只有实现的类AutoCloseable才能在try-with-resources语句声明中使用。

try-with-resources语句是声明一个或多个资源的 try 语句。资源是程序完成后必须关闭的对象。try-with-resources 语句确保每个资源在语句结束时关闭。任何实现的对象,java.lang.AutoCloseable包括所有实现的对象java.io.Closeable,都可以用作资源。

Class并且SnowballStemmer不是AutoCloseable。将其放在 try 块中:

try {
    Class stemClass = Class.forName("org.tartarus.snowball.ext." +"english"+ "Stemmer");
    SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance();
} catch(Exception e){
    //Do Something
} finally {
    //Do Something
}
于 2016-11-25T04:52:02.760 回答