alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (StempelStemmer.java)

This example Lucene source code file (StempelStemmer.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

bufferedinputstream, charsequence, datainputstream, datainputstream, io, ioexception, ioexception, stempelstemmer, stempelstemmer, string, stringbuilder, stringbuilder, trie, util

The Lucene StempelStemmer.java source code

/**
 * Copyright 2004 The Apache Software Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.apache.lucene.analysis.stempel;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

import org.egothor.stemmer.Diff;
import org.egothor.stemmer.Trie;

/**
 * <p>
 * Stemmer class is a convenient facade for other stemmer-related classes. The
 * core stemming algorithm and its implementation is taken verbatim from the
 * Egothor project ( <a href="http://www.egothor.org">www.egothor.org ).
 * </p>
 * <p>
 * Even though the stemmer tables supplied in the distribution package are built
 * for Polish language, there is nothing language-specific here.
 * </p>
 */
public class StempelStemmer {
  private Trie stemmer = null;
  private StringBuilder buffer = new StringBuilder();

  /**
   * Create a Stemmer using selected stemmer table
   * 
   * @param stemmerTable stemmer table.
   */
  public StempelStemmer(InputStream stemmerTable) throws IOException {
    this(load(stemmerTable));
  }

  /**
   * Create a Stemmer using pre-loaded stemmer table
   * 
   * @param stemmer pre-loaded stemmer table
   */
  public StempelStemmer(Trie stemmer) {
    this.stemmer = stemmer;
  }
  
  /**
   * Load a stemmer table from an inputstream.
   */
  public static Trie load(InputStream stemmerTable) throws IOException {
    DataInputStream in = null;
    try {
      in = new DataInputStream(new BufferedInputStream(stemmerTable));
      String method = in.readUTF().toUpperCase(Locale.ENGLISH);
      if (method.indexOf('M') < 0) {
        return new org.egothor.stemmer.Trie(in);
      } else {
        return new org.egothor.stemmer.MultiTrie2(in);
      }
    } finally {
      in.close();
    }
  }

  /**
   * Stem a word. 
   * 
   * @param word input word to be stemmed.
   * @return stemmed word, or null if the stem could not be generated.
   */
  public StringBuilder stem(CharSequence word) {
    CharSequence cmd = stemmer.getLastOnPath(word);
    
    if (cmd == null)
        return null;
    
    buffer.setLength(0);
    buffer.append(word);

    Diff.apply(buffer, cmd);
    
    if (buffer.length() > 0)
      return buffer;
    else
      return null;
  }
}

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene StempelStemmer.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.