01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.core.lucene.engine.analyzer.synonym;
18:
19: import java.io.IOException;
20: import java.util.LinkedList;
21:
22: import org.apache.lucene.analysis.Token;
23: import org.apache.lucene.analysis.TokenFilter;
24: import org.apache.lucene.analysis.TokenStream;
25:
26: /**
27: * A synonym provider which uses {@link SynonymLookupProvider} to provide
28: * synonyms for a given value.
29: *
30: * @author kimchy
31: */
32: public class SynonymFilter extends TokenFilter {
33:
34: public static final String TOKEN_TYPE_SYNONYM = "SYNONYM";
35:
36: private LinkedList synonymStack;
37:
38: private SynonymLookupProvider synonymLookupProvider;
39:
40: public SynonymFilter(TokenStream in,
41: SynonymLookupProvider synonymLookupProvider) {
42: super (in);
43: this .synonymLookupProvider = synonymLookupProvider;
44: this .synonymStack = new LinkedList();
45: }
46:
47: public Token next() throws IOException {
48: if (synonymStack.size() > 0) {
49: return (Token) synonymStack.removeFirst();
50: }
51:
52: Token token = input.next();
53: if (token == null) {
54: return null;
55: }
56:
57: addAliasesToStack(token);
58:
59: return token;
60: }
61:
62: private void addAliasesToStack(Token token) {
63: String[] synonyms = synonymLookupProvider.lookupSynonyms(token
64: .termText());
65:
66: if (synonyms == null) {
67: return;
68: }
69:
70: for (int i = 0; i < synonyms.length; i++) {
71: Token synToken = new Token(synonyms[i],
72: token.startOffset(), token.endOffset(),
73: TOKEN_TYPE_SYNONYM);
74: synToken.setPositionIncrement(0);
75: synonymStack.addFirst(synToken);
76: }
77: }
78: }
|