forked from dweiss/compound-splitter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dweiss#1 from wayfair/master
Add/modify classes to adapt compound-splitter for use as a Lucene/Solr p...
- Loading branch information
Showing
6 changed files
with
162 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/org/apache/lucene/analysis/de/compounds/GermanCompoundSplitterTokenFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.apache.lucene.analysis.de.compounds; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.compound.CompoundWordTokenFilterBase; | ||
import org.apache.lucene.util.Version; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class GermanCompoundSplitterTokenFilter extends | ||
CompoundWordTokenFilterBase { | ||
private static Logger log = LoggerFactory | ||
.getLogger(GermanCompoundSplitterTokenFilter.class); | ||
|
||
private GermanCompoundSplitter splitter; | ||
|
||
public GermanCompoundSplitterTokenFilter(Version matchVersion, | ||
TokenStream input, String fstFile) { | ||
super(matchVersion, input, null); | ||
GermanCompoundSplitter.initFSTs(fstFile); | ||
this.splitter = new GermanCompoundSplitter(); | ||
} | ||
|
||
public void decompose() { | ||
String splitWords = new String(); | ||
String incomingPossibleCompound = termAtt.toString(); | ||
try { | ||
CharSequence sw = splitter.split(incomingPossibleCompound); | ||
if (sw != null) { | ||
splitWords = sw.toString(); // supplywood | ||
// -> | ||
// supply.wood,sup.plywood | ||
} else { | ||
splitWords = incomingPossibleCompound; | ||
} | ||
String[] possibleSplits = splitWords.split(","); | ||
String[] words = possibleSplits[0].split("\\."); // Take the first | ||
// suggestion | ||
for (String word : words) { | ||
int startInd = incomingPossibleCompound.indexOf(word); | ||
int length = word.length(); | ||
tokens.add(new CompoundToken(startInd, length)); | ||
} | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
} | ||
|
||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ava/org/apache/lucene/analysis/de/compounds/GermanCompoundSplitterTokenFilterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.apache.lucene.analysis.de.compounds; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.apache.lucene.analysis.TokenStream; | ||
import org.apache.lucene.analysis.util.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class GermanCompoundSplitterTokenFilterFactory extends | ||
TokenFilterFactory { | ||
private static Logger log = LoggerFactory | ||
.getLogger(GermanCompoundSplitterTokenFilterFactory.class); | ||
|
||
private String dataDir; | ||
private String fstFile; | ||
|
||
@Override | ||
public void init(Map<String,String> args) { | ||
super.init(args); | ||
this.dataDir = args.get("dataDir"); | ||
this.fstFile = dataDir + "words.fst"; | ||
String[] inputFiles = { | ||
dataDir + "morphy.txt", | ||
dataDir + "morphy-unknown.txt"}; | ||
try { | ||
Boolean shouldCompileDict = Boolean.parseBoolean(args.get("compileDict")); | ||
if (shouldCompileDict) { | ||
CompileCompoundDictionaries.setDataDir(dataDir); | ||
CompileCompoundDictionaries.compile(inputFiles); | ||
} | ||
} catch (Exception e) { | ||
log.error(e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public TokenStream create(TokenStream input) { | ||
return new GermanCompoundSplitterTokenFilter(luceneMatchVersion, input, | ||
fstFile); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters