0

im using the snowball stemmer that can be found here http://snowball.tartarus.org/

and i was using this forum question to use a stemming algorithm for my own project

Is there a java implementation of Porter2 stemmer

i use the given class and use this code given in the previous answered post

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();  

but when i come to use a try catch statement i get this error

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

really not sure how to solve this

1
  • The error message in your title does not appear anywhere in your question. Please fix it.
    – user207421
    Aug 28, 2019 at 4:13

1 Answer 1

1

Only classes that implements AutoCloseable can be used in try-with-resources statement declarations.

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 implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

Class and SnowballStemmer are not AutoCloseable. Place it inside try block instead:

try {
    Class stemClass = Class.forName("org.tartarus.snowball.ext." +"english"+ "Stemmer");
    SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance();
} catch(Exception e){
    //Do Something
} finally {
    //Do Something
}
2
  • i get these two errors error: unreported exception ClassNotFoundException; must be caught or declared to be thrown Class stemClass = Class.forName("org.tartarus.snowball.ext." +"english"+ "Stemmer"); ^ assmt1/invert3.java:343: error: unreported exception InstantiationException; must be caught or declared to be thrown SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance(); ^ 2 errors
    – Hambuzo
    Nov 25, 2016 at 5:00
  • Added catch block Nov 25, 2016 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.