A filter that decomposes compound words you find in many Germanic languages into the word parts. This example shows what it does:
The input token is always preserved and the filters do not alter the case of word parts. There are two variants of the filter available:
HyphenationCompoundWordTokenFilter: it uses a hyphenation grammar based approach to find potential word parts of a given word.
DictionaryCompoundWordTokenFilter: it uses a brute-force dictionary-only based approach to find the word parts of a given word.
The HyphenationCompoundWordTokenFilter uses hyphenation grammars to find potential subwords that a worth to check against the dictionary. It can be used without a dictionary as well but then produces a lot of “nonword” tokens. The quality of the output tokens is directly connected to the quality of the grammar file you use. For languages like German they are quite good.
Unfortunately we cannot bundle the hyphenation grammar files with Lucene because they do not use an ASF compatible license (they use the LaTeX Project Public License instead). You can find the XML based grammar files at the Objects For Formatting Objects (OFFO) Sourceforge project (direct link to download the pattern files: http://downloads.sourceforge.net/offo/offo-hyphenation.zip ). The files you need are in the subfolder offo-hyphenation/hyph/ .
Credits for the hyphenation code go to the Apache FOP project .
The DictionaryCompoundWordTokenFilter uses a dictionary-only approach to find subwords in a compound word. It is much slower than the one that uses the hyphenation grammars. You can use it as a first start to see if your dictionary is good or not because it is much simpler in design.
The output quality of both token filters is directly connected to the quality of the dictionary you use. They are language dependent of course. You always should use a dictionary that fits to the text you want to index. If you index medical text for example then you should use a dictionary that contains medical words. A good start for general text are the dictionaries you find at the OpenOffice dictionaries Wiki.
This decision matrix should help you:
const LuceneVersion matchVersion = LuceneVersion.LUCENE_48; [Test] public void TestHyphenationCompoundWordsDE() { string[] dictionary = { "Rind", "Fleisch", "Draht", "Schere", "Gesetz", "Aufgabe", "Überwachung" }; using Stream stream = new FileStream("de_DR.xml", FileMode.Open); HyphenationTree hyphenator = HyphenationCompoundWordTokenFilter.GetHyphenationTree(stream); HyphenationCompoundWordTokenFilter tf = new HyphenationCompoundWordTokenFilter( matchVersion new WhitespaceTokenizer( matchVersion, new StringReader("Rindfleischüberwachungsgesetz Drahtschere abba")), hyphenator, dictionary, CompoundWordTokenFilterBase.DEFAULT_MIN_WORD_SIZE, CompoundWordTokenFilterBase.DEFAULT_MIN_SUBWORD_SIZE, CompoundWordTokenFilterBase.DEFAULT_MAX_SUBWORD_SIZE, onlyLongestMatch: false); ICharTermAttribute t = tf.AddAttribute<ICharTermAttribute>(); while (tf.IncrementToken()) { Console.WriteLine(t); } } [Test] public void TestHyphenationCompoundWordsWithoutDictionaryDE() { using Stream stream = new FileStream("de_DR.xml", FileMode.Open); HyphenationTree hyphenator = HyphenationCompoundWordTokenFilter.GetHyphenationTree(stream); HyphenationCompoundWordTokenFilter tf = new HyphenationCompoundWordTokenFilter( new WhitespaceTokenizer(matchVersion, new StringReader("Rindfleischüberwachungsgesetz Drahtschere abba")), hyphenator); ICharTermAttribute t = tf.AddAttribute<ICharTermAttribute>(); while (tf.IncrementToken()) { Console.WriteLine(t); } } [Test] public void TestDumbCompoundWordsSE() { string[] dictionary = { "Bil", "Dörr", "Motor", "Tak", "Borr", "Slag", "Hammar", "Pelar", "Glas", "Ögon", "Fodral", "Bas", "Fiol", "Makare", "Gesäll", "Sko", "Vind", "Rute", "Torkare", "Blad" }; DictionaryCompoundWordTokenFilter tf = new DictionaryCompoundWordTokenFilter( new WhitespaceTokenizer( matchVersion, new StringReader( "Bildörr Bilmotor Biltak Slagborr Hammarborr Pelarborr Glasögonfodral Basfiolsfodral Basfiolsfodralmakaregesäll Skomakare Vindrutetorkare Vindrutetorkarblad abba")), dictionary); ICharTermAttribute t = tf.AddAttribute<ICharTermAttribute>(); while (tf.IncrementToken()) { Console.WriteLine(t); } }