All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.analysis.compound.HyphenationCompoundWordTokenFilterFactory Maven / Gradle / Ivy

There is a newer version: 9.1.7.Final
Show newest version
package org.apache.lucene.analysis.compound;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.compound.hyphenation.HyphenationTree;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.analysis.util.ResourceLoader;
import org.apache.lucene.analysis.util.ResourceLoaderAware;
import org.apache.lucene.analysis.util.TokenFilterFactory;
import org.apache.lucene.util.IOUtils;

import java.util.Map;
import java.io.IOException;
import java.io.InputStream;
import org.xml.sax.InputSource;

/**
 * Factory for {@link HyphenationCompoundWordTokenFilter}.
 * 

* This factory accepts the following parameters: *

    *
  • hyphenator (mandatory): path to the FOP xml hyphenation pattern. * See http://offo.sourceforge.net/hyphenation/. *
  • encoding (optional): encoding of the xml hyphenation file. defaults to UTF-8. *
  • dictionary (optional): dictionary of words. defaults to no dictionary. *
  • minWordSize (optional): minimal word length that gets decomposed. defaults to 5. *
  • minSubwordSize (optional): minimum length of subwords. defaults to 2. *
  • maxSubwordSize (optional): maximum length of subwords. defaults to 15. *
  • onlyLongestMatch (optional): if true, adds only the longest matching subword * to the stream. defaults to false. *
*

*

 * <fieldType name="text_hyphncomp" class="solr.TextField" positionIncrementGap="100">
 *   <analyzer>
 *     <tokenizer class="solr.WhitespaceTokenizerFactory"/>
 *     <filter class="solr.HyphenationCompoundWordTokenFilterFactory" hyphenator="hyphenator.xml" encoding="UTF-8"
 *         dictionary="dictionary.txt" minWordSize="5" minSubwordSize="2" maxSubwordSize="15" onlyLongestMatch="false"/>
 *   </analyzer>
 * </fieldType>
* * @see HyphenationCompoundWordTokenFilter */ public class HyphenationCompoundWordTokenFilterFactory extends TokenFilterFactory implements ResourceLoaderAware { private CharArraySet dictionary; private HyphenationTree hyphenator; private final String dictFile; private final String hypFile; private final String encoding; private final int minWordSize; private final int minSubwordSize; private final int maxSubwordSize; private final boolean onlyLongestMatch; /** Creates a new HyphenationCompoundWordTokenFilterFactory */ public HyphenationCompoundWordTokenFilterFactory(Map args) { super(args); assureMatchVersion(); dictFile = get(args, "dictionary"); encoding = get(args, "encoding"); hypFile = require(args, "hyphenator"); minWordSize = getInt(args, "minWordSize", CompoundWordTokenFilterBase.DEFAULT_MIN_WORD_SIZE); minSubwordSize = getInt(args, "minSubwordSize", CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE); maxSubwordSize = getInt(args, "maxSubwordSize", CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE); onlyLongestMatch = getBoolean(args, "onlyLongestMatch", false); if (!args.isEmpty()) { throw new IllegalArgumentException("Unknown parameters: " + args); } } @Override public void inform(ResourceLoader loader) throws IOException { InputStream stream = null; try { if (dictFile != null) // the dictionary can be empty. dictionary = getWordSet(loader, dictFile, false); // TODO: Broken, because we cannot resolve real system id // ResourceLoader should also supply method like ClassLoader to get resource URL stream = loader.openResource(hypFile); final InputSource is = new InputSource(stream); is.setEncoding(encoding); // if it's null let xml parser decide is.setSystemId(hypFile); hyphenator = HyphenationCompoundWordTokenFilter.getHyphenationTree(is); } finally { IOUtils.closeWhileHandlingException(stream); } } @Override public HyphenationCompoundWordTokenFilter create(TokenStream input) { return new HyphenationCompoundWordTokenFilter(luceneMatchVersion, input, hyphenator, dictionary, minWordSize, minSubwordSize, maxSubwordSize, onlyLongestMatch); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy