2009-07-28 10 views

Respuesta

20

Para hacer esto, necesita escribir su propia clase de analizador. Esto es relativamente sencillo. Aquí está el que estoy usando. Combina el filtro de palabra de parada. Porter stemming y (esto puede ser demasiado para sus necesidades) despojar a los acentos de los personajes.

/// <summary> 
/// An analyzer that implements a number of filters. Including porter stemming, 
/// Diacritic stripping, and stop word filtering. 
/// </summary> 
public class CustomAnalyzer : Analyzer 
{ 
    /// <summary> 
    /// A rather short list of stop words that is fine for basic search use. 
    /// </summary> 
    private static readonly string[] stopWords = new[] 
    { 
     "0", "1", "2", "3", "4", "5", "6", "7", "8", 
     "9", "000", "$", "£", 
     "about", "after", "all", "also", "an", "and", 
     "another", "any", "are", "as", "at", "be", 
     "because", "been", "before", "being", "between", 
     "both", "but", "by", "came", "can", "come", 
     "could", "did", "do", "does", "each", "else", 
     "for", "from", "get", "got", "has", "had", 
     "he", "have", "her", "here", "him", "himself", 
     "his", "how","if", "in", "into", "is", "it", 
     "its", "just", "like", "make", "many", "me", 
     "might", "more", "most", "much", "must", "my", 
     "never", "now", "of", "on", "only", "or", 
     "other", "our", "out", "over", "re", "said", 
     "same", "see", "should", "since", "so", "some", 
     "still", "such", "take", "than", "that", "the", 
     "their", "them", "then", "there", "these", 
     "they", "this", "those", "through", "to", "too", 
     "under", "up", "use", "very", "want", "was", 
     "way", "we", "well", "were", "what", "when", 
     "where", "which", "while", "who", "will", 
     "with", "would", "you", "your", 
     "a", "b", "c", "d", "e", "f", "g", "h", "i", 
     "j", "k", "l", "m", "n", "o", "p", "q", "r", 
     "s", "t", "u", "v", "w", "x", "y", "z" 
    }; 

    private Hashtable stopTable; 

    /// <summary> 
    /// Creates an analyzer with the default stop word list. 
    /// </summary> 
    public CustomAnalyzer() : this(stopWords) {} 

    /// <summary> 
    /// Creates an analyzer with the passed in stop words list. 
    /// </summary> 
    public CustomAnalyzer(string[] stopWords) 
    { 
     stopTable = StopFilter.MakeStopSet(stopWords);  
    } 

    public override TokenStream TokenStream(string fieldName, System.IO.TextReader reader) 
    { 
     return new PorterStemFilter(new ISOLatin1AccentFilter(new StopFilter(new LowerCaseTokenizer(reader), stopWords))); 
    } 
} 
+1

Gracias, intentaré esto. – devson

+1

+1 gracias Jack, justo lo que estaba buscando. ¡Si pudiera, marcaría esto como la respuesta! – andy

+0

Utilicé su ejemplo, sin embargo, no obtengo resultados para las consultas de un número '4656' (el analizador estándar funciona) Reemplacé las palabras para detener con el construido en' StopAnalyzer.ENGLISH_STOP_WORDS' que no incluye números, ninguna idea de lo que está pasando ¿alli? – Myster

7

Puede usar Snowball o PorterStemFilter. Consulte el Java Analyzer documentation como una guía para combinar diferentes filtros/Tokenizers/Analizadores. Tenga en cuenta que debe usar el mismo analizador para la indexación y la recuperación, de modo que la manipulación de la derivación comience en el momento de la indexación.

+0

Gracias, intentaré esto. – devson

Cuestiones relacionadas