El ejemplo anterior se aplica derivada a una consulta de búsqueda, por lo que si usted es interesante para contener un texto completo usted puede intentar lo siguiente:
import java.io.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.tokenattributes.*;
import org.apache.lucene.analysis.snowball.*;
import org.apache.lucene.util.*;
...
public class Stemmer{
public static String Stem(String text, String language){
StringBuffer result = new StringBuffer();
if (text!=null && text.trim().length()>0){
StringReader tReader = new StringReader(text);
Analyzer analyzer = new SnowballAnalyzer(Version.LUCENE_35,language);
TokenStream tStream = analyzer.tokenStream("contents", tReader);
TermAttribute term = tStream.addAttribute(TermAttribute.class);
try {
while (tStream.incrementToken()){
result.append(term.term());
result.append(" ");
}
} catch (IOException ioe){
System.out.println("Error: "+ioe.getMessage());
}
}
// If, for some reason, the stemming did not happen, return the original text
if (result.length()==0)
result.append(text);
return result.toString().trim();
}
public static void main (String[] args){
Stemmer.Stem("Michele Bachmann amenities pressed her allegations that the former head of her Iowa presidential bid was bribed by the campaign of rival Ron Paul to endorse him, even as one of her own aides denied the charge.", "English");
}
}
La clase TermAttribute ha quedado obsoleto y no dejará de ser apoyado en Lucene 4, pero la documentación no es clara sobre qué usar en su lugar.
También en el primer ejemplo, PorterStemmer no está disponible como clase (oculto) por lo que no puede usarlo directamente.
Espero que esto ayude.
¿Por qué necesita para detener las palabras usted mismo? Lucene tiene un analizador llamado SnowballAnalyzer que usted acaba de crear una instancia con el nombre del tallo, p. 'nuevo SnowballAnalyzer (" Inglés ");'. – Thomas
Implementación del algoritmo Knuth-Pratt http://www.fmi.uni-sofia.bg/fmi/logic/vboutchkova/sources/KMPMatch_java.html –