001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.lucene.engine.spellcheck;
018:
019: import java.io.IOException;
020:
021: import org.apache.lucene.index.IndexReader;
022: import org.apache.lucene.search.spell.CompassSpellChecker;
023: import org.compass.core.engine.SearchEngineException;
024: import org.compass.core.engine.spellcheck.SearchEngineSpellCheckSuggestBuilder;
025: import org.compass.core.engine.spellcheck.SearchEngineSpellSuggestions;
026: import org.compass.core.mapping.ResourceMapping;
027:
028: /**
029: * @author kimchy
030: */
031: public class DefaultLuceneSearchEngineSpellCheckSuggestBuilder
032: implements SearchEngineSpellCheckSuggestBuilder {
033:
034: private InternalLuceneSearchEngineSpellCheckManager spellCheckerManager;
035:
036: private String word;
037:
038: private int numberOfSuggestions = 1;
039:
040: private String[] subIndexes;
041:
042: private String[] aliases;
043:
044: private String restrictToProperty;
045:
046: private boolean morePopular;
047:
048: private float accuracy = 0.5f;
049:
050: public DefaultLuceneSearchEngineSpellCheckSuggestBuilder(
051: String word,
052: InternalLuceneSearchEngineSpellCheckManager spellCheckerManager) {
053: this .word = word;
054: this .spellCheckerManager = spellCheckerManager;
055: this .accuracy = spellCheckerManager.getDefaultAccuracy();
056: }
057:
058: public SearchEngineSpellCheckSuggestBuilder subIndexes(
059: String... subIndexes) {
060: this .subIndexes = subIndexes;
061: return this ;
062: }
063:
064: public SearchEngineSpellCheckSuggestBuilder aliases(
065: String... aliases) {
066: this .aliases = aliases;
067: return this ;
068: }
069:
070: public SearchEngineSpellCheckSuggestBuilder types(Class... types) {
071: if (types == null) {
072: this .aliases = null;
073: return this ;
074: }
075: String[] aliases = new String[types.length];
076: for (int i = 0; i < types.length; i++) {
077: ResourceMapping resourceMapping = spellCheckerManager
078: .getMapping().getRootMappingByClass(types[i]);
079: aliases[i] = resourceMapping.getAlias();
080: }
081: aliases(aliases);
082: return this ;
083: }
084:
085: public SearchEngineSpellCheckSuggestBuilder numberOfSuggestions(
086: int numberOfSuggestions) {
087: this .numberOfSuggestions = numberOfSuggestions;
088: return this ;
089: }
090:
091: public SearchEngineSpellCheckSuggestBuilder restrictToProperty(
092: String restrictToProperty) {
093: this .restrictToProperty = restrictToProperty;
094: return this ;
095: }
096:
097: public SearchEngineSpellCheckSuggestBuilder morePopular(
098: boolean morePopular) {
099: this .morePopular = morePopular;
100: return this ;
101: }
102:
103: public SearchEngineSpellCheckSuggestBuilder accuracy(float accuracy) {
104: this .accuracy = accuracy;
105: return this ;
106: }
107:
108: public SearchEngineSpellSuggestions suggest() {
109: return spellCheckerManager
110: .execute(
111: subIndexes,
112: aliases,
113: new SpellCheckerCallback<SearchEngineSpellSuggestions>() {
114: public SearchEngineSpellSuggestions execute(
115: CompassSpellChecker spellChecker,
116: IndexReader reader)
117: throws SearchEngineException {
118: if (morePopular
119: && restrictToProperty == null) {
120: restrictToProperty = spellCheckerManager
121: .getDefaultProperty();
122: }
123: if (spellChecker == null) {
124: return new DefaultLuceneSearchEngineSpellSuggestions(
125: false, new String[0]);
126: }
127: spellChecker.setAccuracy(accuracy);
128: try {
129: if (restrictToProperty == null) {
130: reader = null;
131: }
132: String[] suggestions = spellChecker
133: .suggestSimilar(
134: word,
135: numberOfSuggestions,
136: reader,
137: restrictToProperty,
138: morePopular);
139: return new DefaultLuceneSearchEngineSpellSuggestions(
140: spellChecker.exist(word),
141: suggestions);
142: } catch (IOException e) {
143: throw new SearchEngineException(
144: "Failed to suggest spell check",
145: e);
146: }
147: }
148: });
149: }
150: }
|