mirror of https://github.com/apache/lucene.git
SOLR-2484: make SynonymFilterFactory more extensible
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1098800 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
fd0701bf4e
commit
aa102b38b7
|
@ -61,6 +61,16 @@ public class SynonymFilterFactory extends BaseTokenFilterFactory implements Reso
|
|||
tokFactory = loadTokenizerFactory( loader, tf, args );
|
||||
}
|
||||
|
||||
Iterable<String> wlist=loadRules( synonyms, loader );
|
||||
|
||||
synMap = new SynonymMap(ignoreCase);
|
||||
parseRules(wlist, synMap, "=>", ",", expand,tokFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a list of all rules
|
||||
*/
|
||||
protected Iterable<String> loadRules( String synonyms, ResourceLoader loader ) {
|
||||
List<String> wlist=null;
|
||||
try {
|
||||
File synonymFile = new File(synonyms);
|
||||
|
@ -77,13 +87,12 @@ public class SynonymFilterFactory extends BaseTokenFilterFactory implements Reso
|
|||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
synMap = new SynonymMap(ignoreCase);
|
||||
parseRules(wlist, synMap, "=>", ",", expand,tokFactory);
|
||||
return wlist;
|
||||
}
|
||||
|
||||
private SynonymMap synMap;
|
||||
|
||||
static void parseRules(List<String> rules, SynonymMap map, String mappingSep,
|
||||
static void parseRules(Iterable<String> rules, SynonymMap map, String mappingSep,
|
||||
String synSep, boolean expansion, TokenizerFactory tokFactory) {
|
||||
int count=0;
|
||||
for (String rule : rules) {
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.apache.solr.analysis;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
@ -25,6 +27,9 @@ import java.util.Map;
|
|||
import org.apache.lucene.analysis.Token;
|
||||
import org.apache.lucene.analysis.synonym.SynonymMap;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
import org.apache.solr.common.ResourceLoader;
|
||||
|
||||
import visad.UnimplementedException;
|
||||
|
||||
public class TestSynonymMap extends LuceneTestCase {
|
||||
|
||||
|
@ -257,6 +262,43 @@ public class TestSynonymMap extends LuceneTestCase {
|
|||
assertTokIncludes( getSubSynonymMap( getSubSynonymMap( synMap, "ab" ), "bc" ), "cd", "gh" );
|
||||
}
|
||||
|
||||
|
||||
public void testLoadRules() throws Exception {
|
||||
Map<String, String> args = new HashMap<String, String>();
|
||||
args.put( "synonyms", "something.txt" );
|
||||
SynonymFilterFactory ff = new SynonymFilterFactory();
|
||||
ff.init(args);
|
||||
ff.inform( new ResourceLoader() {
|
||||
@Override
|
||||
public List<String> getLines(String resource) throws IOException {
|
||||
if( !"something.txt".equals(resource) ) {
|
||||
throw new RuntimeException( "should not get a differnt resource" );
|
||||
}
|
||||
List<String> rules = new ArrayList<String>();
|
||||
rules.add( "a,b" );
|
||||
return rules;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object newInstance(String cname, String... subpackages) {
|
||||
throw new RuntimeException("stub");
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream openResource(String resource) throws IOException {
|
||||
throw new RuntimeException("stub");
|
||||
}
|
||||
});
|
||||
|
||||
SynonymMap synMap = ff.getSynonymMap();
|
||||
assertEquals( 2, synMap.submap.size() );
|
||||
assertTokIncludes( synMap, "a", "a" );
|
||||
assertTokIncludes( synMap, "a", "b" );
|
||||
assertTokIncludes( synMap, "b", "a" );
|
||||
assertTokIncludes( synMap, "b", "b" );
|
||||
}
|
||||
|
||||
|
||||
private void assertTokIncludes( SynonymMap map, String src, String exp ) throws Exception {
|
||||
Token[] tokens = map.submap.get( src ).synonyms;
|
||||
boolean inc = false;
|
||||
|
|
Loading…
Reference in New Issue