usra usra - 4 years ago 121
Java Question

frenchstemmer library i java input in file?

i found a library to stemm french words snowball.tartarus.org and i kinda found a code to test it , i just need to apply this code to a file.txt and get the output also on a file .

thanks for answers

import org.junit.Test;
import org.tartarus.snowball.SnowballStemmer;
import org.tartarus.snowball.ext.frenchStemmer;


public class SnowballfrenchStemmerTest {

@Test
public void testSnowbalfrenchStemmer() {

SnowballStemmer stemmer = (SnowballStemmer) new frenchStemmer();

String[] tokens = "attacher attachement attaché ".split(" ");
for (String string : tokens) {
stemmer.setCurrent(string);
stemmer.stem();
String stemmed = stemmer.getCurrent();

System.out.println(stemmed);
}

}

Answer Source

You must read from file. Try to use sth. like new String(Files.readAllBytes(path to your file))

Or

public static void main(String[] args) throws ParseException{
        BufferedReader br = null;

        try {

                String sCurrentLine;
                br = new BufferedReader(new FileReader("C:\\path\testing.txt"));
                SnowballStemmer stemmer = (SnowballStemmer) new frenchStemmer();

                while ((sCurrentLine = br.readLine()) != null) {

                        String[] tokens = sCurrentLine.split(" ");    
                        for (String string : tokens) {
                            stemmer.setCurrent(string);
                            stemmer.stem();
                            String stemmed = stemmer.getCurrent();

                            System.out.println(stemmed);
                        }
                }

        } catch (IOException e) {
                e.printStackTrace();
        } finally {
                try {
                        if (br != null)br.close();
                } catch (IOException ex) {
                        ex.printStackTrace();
                }
        }        
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download