001: /**
002: * Milin Radivoj, Jan 11, 2006
003: * rmilin@gmail.com
004: */package org.enhydra.snapper.wrapper.lucene;
005:
006: import java.io.IOException;
007: import java.util.Enumeration;
008: import java.util.Properties;
009: import java.util.Vector;
010:
011: import org.apache.lucene.index.IndexReader;
012: import org.apache.lucene.index.MultiReader;
013: import org.apache.lucene.index.Term;
014: import org.apache.lucene.index.TermEnum;
015:
016: /**
017: * @author Milin Radivoj
018: */
019:
020: public class LuceneSuggest {
021:
022: public Vector suggest(Properties sites, String text) {
023: Vector result = new Vector();
024:
025: IndexReader[] listReader = null;
026: try {
027: long startTime = System.currentTimeMillis();
028:
029: String[] targetIdArray = text.split(" ");
030:
031: String rootText = "";
032:
033: for (int i = 0; i < targetIdArray.length - 1; i++) {
034: rootText = rootText + targetIdArray[i] + " ";
035: }
036:
037: listReader = new IndexReader[sites.size()];
038:
039: int i = 0;
040:
041: for (Enumeration e = sites.propertyNames(); e
042: .hasMoreElements();) {
043: String siteName = (String) e.nextElement();
044: String indexDir = (String) sites.getProperty(siteName);
045:
046: listReader[i] = IndexReader.open(indexDir);
047: i++;
048: }
049:
050: MultiReader mr = new MultiReader(listReader);
051:
052: String suggestString = targetIdArray[targetIdArray.length - 1];
053: //trik da ubrzamo stvari
054: if (suggestString.length() == 1)
055: suggestString = suggestString + "a";
056:
057: suggestString = suggestString.toLowerCase();
058:
059: Term suggestTerm = new Term("contents", suggestString);
060:
061: TermEnum te = mr.terms(suggestTerm);
062:
063: if (te != null) {
064: do {
065: try {
066: if (te.term().text().startsWith(suggestString)) {
067: String sugg = te.term().text();
068: if (sugg.length() > 2 && chack(sugg)) {
069: result.add(rootText + te.term().text());
070: }
071: } else {
072: break;
073: }
074: } catch (Exception e) {
075: e.printStackTrace();
076: }
077: } while (((System.currentTimeMillis() - startTime) < 1000)
078: && (result.size() < 10) && te.next());
079: }
080: return result;
081:
082: } catch (Exception e) {
083: e.printStackTrace();
084: return new Vector();
085: } finally {
086:
087: if (listReader != null) {
088: try {
089: for (int i = 0; i < listReader.length; i++) {
090: if (listReader[i] != null) {
091: listReader[i].close();
092: }
093: }
094: } catch (IOException e) {
095: }
096: }
097:
098: }
099:
100: }
101:
102: public boolean chack(String suggestion) {
103: if (suggestion == null)
104: return false;
105:
106: if (suggestion.indexOf("<") != -1)
107: return false;
108:
109: if (suggestion.indexOf(">") != -1)
110: return false;
111:
112: if (suggestion.indexOf("~") != -1)
113: return false;
114:
115: if (suggestion.indexOf("`") != -1)
116: return false;
117:
118: if (suggestion.indexOf("!") != -1)
119: return false;
120:
121: if (suggestion.indexOf("@") != -1)
122: return false;
123:
124: if (suggestion.indexOf("#") != -1)
125: return false;
126:
127: if (suggestion.indexOf("$") != -1)
128: return false;
129:
130: if (suggestion.indexOf("^") != -1)
131: return false;
132:
133: if (suggestion.indexOf("&") != -1)
134: return false;
135:
136: if (suggestion.indexOf("*") != -1)
137: return false;
138:
139: if (suggestion.indexOf("(") != -1)
140: return false;
141:
142: if (suggestion.indexOf(")") != -1)
143: return false;
144:
145: if (suggestion.indexOf("-") != -1)
146: return false;
147:
148: if (suggestion.indexOf("_") != -1)
149: return false;
150:
151: if (suggestion.indexOf("+") != -1)
152: return false;
153:
154: if (suggestion.indexOf("=") != -1)
155: return false;
156:
157: if (suggestion.indexOf("{") != -1)
158: return false;
159:
160: if (suggestion.indexOf("}") != -1)
161: return false;
162:
163: if (suggestion.indexOf("[") != -1)
164: return false;
165:
166: if (suggestion.indexOf("]") != -1)
167: return false;
168:
169: if (suggestion.indexOf(":") != -1)
170: return false;
171:
172: if (suggestion.indexOf(";") != -1)
173: return false;
174:
175: if (suggestion.indexOf("\"") != -1)
176: return false;
177:
178: if (suggestion.indexOf("'") != -1)
179: return false;
180:
181: if (suggestion.indexOf(",") != -1)
182: return false;
183:
184: if (suggestion.indexOf(".") != -1)
185: return false;
186:
187: if (suggestion.indexOf("?") != -1)
188: return false;
189:
190: if (suggestion.indexOf("/") != -1)
191: return false;
192:
193: if (suggestion.indexOf("|") != -1)
194: return false;
195:
196: if (suggestion.indexOf("\\") != -1)
197: return false;
198:
199: if (suggestion.indexOf("1") != -1)
200: return false;
201:
202: if (suggestion.indexOf("2") != -1)
203: return false;
204:
205: if (suggestion.indexOf("3") != -1)
206: return false;
207:
208: if (suggestion.indexOf("4") != -1)
209: return false;
210:
211: if (suggestion.indexOf("5") != -1)
212: return false;
213:
214: if (suggestion.indexOf("6") != -1)
215: return false;
216:
217: if (suggestion.indexOf("7") != -1)
218: return false;
219:
220: if (suggestion.indexOf("8") != -1)
221: return false;
222:
223: if (suggestion.indexOf("9") != -1)
224: return false;
225:
226: if (suggestion.indexOf("0") != -1)
227: return false;
228:
229: return true;
230: }
231: }
|