2009-07-28 20 views

Trả lời

20

Để thực hiện việc này, bạn cần phải viết lớp phân tích của riêng mình. Điều này là tương đối đơn giản. Đây là cái tôi đang sử dụng. Nó kết hợp lọc từ dừng. Porter xuất phát và (điều này có thể là quá nhiều cho nhu cầu của bạn) tước dấu trọng âm từ các nhân vật.

/// <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

Cảm ơn, tôi sẽ thử điều này. – devson

+1

+1 cảm ơn Jack, chỉ là những gì tôi đang tìm kiếm. Nếu tôi có thể đánh dấu đây là câu trả lời! – andy

+0

Tôi đã sử dụng ví dụ của bạn, tuy nhiên tôi không nhận được kết quả cho các truy vấn cho một số '4656' (công cụ phân tích chuẩn) tôi đã thay thế các từ dừng bằng' StopAnalyzer.ENGLISH_STOP_WORDS' được tích hợp trong đó không bao gồm số, bất kỳ ý tưởng gì trên đó? – Myster

7

Bạn có thể sử dụng Snowball hoặc PorterStemFilter. Xem Java Analyzer documentation làm hướng dẫn kết hợp các Bộ lọc/Bộ mã hóa/Trình phân tích khác nhau. Lưu ý rằng bạn phải sử dụng cùng một bộ phân tích để lập chỉ mục và truy xuất, do đó việc xử lý bắt nguồn sẽ bắt đầu vào thời gian lập chỉ mục.

+0

Cảm ơn, tôi sẽ thử điều này. – devson

Các vấn đề liên quan