001/** 002 * Copyright (c) 2007-2009, Regents of the University of Colorado 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without 006 * modification, are permitted provided that the following conditions are met: 007 * 008 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 009 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 010 * Neither the name of the University of Colorado at Boulder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 011 * 012 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 013 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 014 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 015 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 016 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 017 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 018 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 019 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 020 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 021 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 022 * POSSIBILITY OF SUCH DAMAGE. 023 */ 024package org.cleartk.snowball; 025 026import org.apache.lucene.analysis.snowball.SnowballFilter; 027import org.apache.uima.UimaContext; 028import org.apache.uima.analysis_engine.AnalysisEngineProcessException; 029import org.apache.uima.cas.FSIterator; 030import org.apache.uima.cas.Type; 031import org.apache.uima.jcas.JCas; 032import org.apache.uima.jcas.tcas.Annotation; 033import org.apache.uima.resource.ResourceInitializationException; 034import org.cleartk.util.ReflectionUtil; 035import org.tartarus.snowball.SnowballProgram; 036import org.apache.uima.fit.component.JCasAnnotator_ImplBase; 037import org.apache.uima.fit.descriptor.ConfigurationParameter; 038import org.apache.uima.fit.factory.initializable.InitializableFactory; 039import org.apache.uima.fit.util.JCasUtil; 040 041/** 042 * This class borrows from {@link SnowballFilter} 043 * 044 * <br> 045 * Copyright (c) 2007-2009, Regents of the University of Colorado <br> 046 * All rights reserved. 047 * 048 * 049 * @author Philip Ogren 050 */ 051public abstract class SnowballStemmer<TOKEN_TYPE extends Annotation> extends JCasAnnotator_ImplBase { 052 053 public static final String PARAM_STEMMER_NAME = "stemmerName"; 054 055 private static final String STEMMER_NAME_DESCRIPTION = "specifies which snowball stemmer to use. Possible values are: " 056 + "Danish, Dutch, English, Finnish, French, German2, German, Italian, Kp, Lovins, Norwegian, Porter, Portuguese, Russian, Spanish, Swedish"; 057 058 @ConfigurationParameter( 059 name = PARAM_STEMMER_NAME, 060 description = STEMMER_NAME_DESCRIPTION, 061 mandatory = true) 062 public String stemmerName; 063 064 private SnowballProgram stemmer; 065 066 private Class<? extends Annotation> tokenClass; 067 068 private Type tokenType = null; 069 070 private boolean typesInitialized = false; 071 072 @Override 073 public void initialize(UimaContext context) throws ResourceInitializationException { 074 super.initialize(context); 075 String className = String.format("org.tartarus.snowball.ext.%sStemmer", stemmerName); 076 this.stemmer = InitializableFactory.create(null, className, SnowballProgram.class); 077 078 this.tokenClass = ReflectionUtil.<Class<? extends Annotation>> uncheckedCast(ReflectionUtil.getTypeArgument( 079 SnowballStemmer.class, 080 "TOKEN_TYPE", 081 this)); 082 } 083 084 private void initializeTypes(JCas jCas) { 085 if (tokenClass != null) { 086 tokenType = JCasUtil.getType(jCas, tokenClass); 087 } 088 typesInitialized = true; 089 } 090 091 @SuppressWarnings("unchecked") 092 @Override 093 public void process(JCas jCas) throws AnalysisEngineProcessException { 094 if (!typesInitialized) 095 initializeTypes(jCas); 096 097 FSIterator<Annotation> tokens = jCas.getAnnotationIndex(tokenType).iterator(); 098 while (tokens.hasNext()) { 099 TOKEN_TYPE token = (TOKEN_TYPE) tokens.next(); 100 stemmer.setCurrent(token.getCoveredText().toLowerCase()); 101 stemmer.stem(); 102 String stem = stemmer.getCurrent(); 103 setStem(token, stem); 104 } 105 } 106 107 public abstract void setStem(TOKEN_TYPE token, String stem); 108 109 public void setStemmerName(String stemmerName) { 110 this.stemmerName = stemmerName; 111 } 112 113}