root / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / geocoding / dataaccess / LuceneDataAccessTXTTest.java @ 32479
History | View | Annotate | Download (8.03 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 Prodevelop S.L vsanjaime programador
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.geocoding.dataaccess; |
29 |
|
30 |
import junit.framework.TestCase; |
31 |
|
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
|
36 |
/**
|
37 |
* Test
|
38 |
*
|
39 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
40 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
41 |
*/
|
42 |
public class LuceneDataAccessTXTTest extends TestCase { |
43 |
private static final Logger log = |
44 |
LoggerFactory.getLogger(LuceneDataAccessTXTTest.class); |
45 |
// String[] stops = {"de","con","para","en"};
|
46 |
// String INDEX_DIRPATH = new File(
|
47 |
// "test")
|
48 |
// .getAbsolutePath();
|
49 |
//
|
50 |
// ArrayList<String> cadenas = null;
|
51 |
//
|
52 |
// public void setUp() {
|
53 |
// }
|
54 |
//
|
55 |
// /* Este test crea los indices */
|
56 |
// public void testGenerateIndex() {
|
57 |
// log.info("--------------GENERACI?N DEL ?NDICE--------------");
|
58 |
//
|
59 |
// File f = new File(
|
60 |
// "src-test/org/gvsig/geocoding/geocoding/dataaccess/testLucene.txt");
|
61 |
//
|
62 |
// cadenas = readTextFile(f);
|
63 |
// log.info("Archivo leido");
|
64 |
//
|
65 |
// IndexWriter indexWriterStandard = null;
|
66 |
// IndexWriter indexWriterSnowBall = null;
|
67 |
//
|
68 |
// try {
|
69 |
// // Escritor de indices que recibe la carpeta donde almacenar los
|
70 |
// // indices,
|
71 |
// // el analizador: (StandardAnalyzer[b?squedas exactas],
|
72 |
// // SnowballAnalyzer[B?squedas semejantes])
|
73 |
// // y booleano que indica si se deben sobrescribir los indices
|
74 |
//
|
75 |
// indexWriterStandard = new IndexWriter(INDEX_DIRPATH + "/standard", new
|
76 |
// StandardAnalyzer(stops), true);
|
77 |
// indexWriterSnowBall = new IndexWriter(INDEX_DIRPATH + "/snowball", new
|
78 |
// SnowballAnalyzer("Spanish"), true);
|
79 |
//
|
80 |
// for (int i = 0; i < cadenas.size(); i++) {
|
81 |
//
|
82 |
// if (i % 10 == 0) {
|
83 |
// log.info("Leyendo cadenas (" + i + "/" + cadenas.size()
|
84 |
// + ")");
|
85 |
// }
|
86 |
//
|
87 |
// // //desordena las cadenas
|
88 |
// // Collections.shuffle(cadenas);
|
89 |
//
|
90 |
// // generar un identificador
|
91 |
// StringBuffer caw = new StringBuffer();
|
92 |
//
|
93 |
// // caw.reset();
|
94 |
// caw.append("quote-");
|
95 |
// caw.append(Long.toString(
|
96 |
// Double.doubleToLongBits(Math.random()), 34));
|
97 |
// String sQuoteId = caw.toString();
|
98 |
//
|
99 |
// // Documento donde se introduce el indice y se puede introducir
|
100 |
// // las cadenas
|
101 |
// Document doc = new Document();
|
102 |
//
|
103 |
// // Cada document recibe el nombre del campo,
|
104 |
// // el segundo es el valor del campo (id, string)
|
105 |
// // el tercero indica que la cadena de texto se almacena junto al
|
106 |
// // indice YES o NO
|
107 |
// // el cuarto indica si se analiza la cadena de texto con el
|
108 |
// // analizador
|
109 |
// // en el caso de TOKENIZED se analiza y en el caso de
|
110 |
// // UN_TOKENIZED no se analiza
|
111 |
// doc.add(new Field("id", sQuoteId, Field.Store.YES,
|
112 |
// Field.Index.UN_TOKENIZED));
|
113 |
// doc.add(new Field("str", cadenas.get(i), Field.Store.YES,
|
114 |
// Field.Index.TOKENIZED));
|
115 |
// // a?adir el documento al escritor de indices
|
116 |
// indexWriterSnowBall.addDocument(doc);
|
117 |
// indexWriterStandard.addDocument(doc);
|
118 |
// }
|
119 |
// indexWriterSnowBall.optimize();
|
120 |
// indexWriterStandard.optimize();
|
121 |
// } catch (Exception e) {
|
122 |
// e.printStackTrace();
|
123 |
// } finally {
|
124 |
// try {
|
125 |
// if (indexWriterSnowBall != null) indexWriterSnowBall.close();
|
126 |
// } catch (Exception e) {}
|
127 |
//
|
128 |
// try {
|
129 |
// if (indexWriterStandard != null) indexWriterStandard.close();
|
130 |
// } catch (Exception e) {}
|
131 |
//
|
132 |
// indexWriterSnowBall = null;
|
133 |
// indexWriterStandard = null;
|
134 |
// log.info("Indices generados");
|
135 |
// }
|
136 |
// log.info("--------------GENERACI?N DEL ?NDICE--------------");
|
137 |
// }
|
138 |
//
|
139 |
// /* Prueba de un analizador del tipo SnowballAnalyzer */
|
140 |
// public void testAnalizer() {
|
141 |
// log.info("--------------TEST DE ANALIZADOR--------------");
|
142 |
// String sTxt = "Villarrasa Vil?larrasa Villarras?";
|
143 |
// StringReader sr = null;
|
144 |
// TokenStream tokenStream = null;
|
145 |
// org.apache.lucene.analysis.Token token = null;
|
146 |
// // StandardAnalyzer analizer = new StandardAnalyzer();
|
147 |
// SnowballAnalyzer analizer = new SnowballAnalyzer("Spanish");
|
148 |
// try {
|
149 |
// sr = new StringReader(sTxt);
|
150 |
// tokenStream = analizer.tokenStream("con", sr);
|
151 |
// while ((token = tokenStream.next()) != null) {
|
152 |
// log.info("[" + token.termText() + "] ");
|
153 |
// }
|
154 |
// System.err.println("");
|
155 |
//
|
156 |
// } catch (Exception e) {
|
157 |
// e.printStackTrace();
|
158 |
// } finally {
|
159 |
//
|
160 |
// }
|
161 |
// log.info("--------------TEST DE ANALIZADOR--------------");
|
162 |
// }
|
163 |
//
|
164 |
// /* Busqueda de cadenas */
|
165 |
// public void testExactSearch() {
|
166 |
// Analyzer an = null;
|
167 |
//
|
168 |
// ArrayList<String> cadenas = new ArrayList<String>();
|
169 |
//
|
170 |
// cadenas.add("Villarrasa");
|
171 |
// cadenas.add("Villarras?");
|
172 |
// cadenas.add("Villarras?");
|
173 |
// cadenas.add("villarrase");
|
174 |
// cadenas.add("villarrasa");
|
175 |
// cadenas.add("vilarrasa");
|
176 |
// cadenas.add("Don Juan de Vil?larrasa");
|
177 |
//
|
178 |
//
|
179 |
// log.info("--------------TEST DE B?SQUEDA EXACTA--------------");
|
180 |
//
|
181 |
// an = new StandardAnalyzer(stops);
|
182 |
// buscar(INDEX_DIRPATH + "/standard", an, cadenas);
|
183 |
// log.info("--------------TEST DE B?SQUEDA EXACTA--------------");
|
184 |
//
|
185 |
// log.info("--------------TEST DE B?SQUEDA SNOWBALL--------------");
|
186 |
// an = new SnowballAnalyzer("Spanish");
|
187 |
// buscar(INDEX_DIRPATH + "/snowball",an, cadenas);
|
188 |
// log.info("--------------TEST DE B?SQUEDA SNOWBALL--------------");
|
189 |
// }
|
190 |
//
|
191 |
//
|
192 |
// private void buscar(String indexPath, Analyzer an, List<String> cadenas){
|
193 |
// IndexSearcher indexSearcher = null;
|
194 |
//
|
195 |
//
|
196 |
//
|
197 |
// try {
|
198 |
// // Instaciar la clase que va a realizar la busqueda donde se indica
|
199 |
// // el directorio donde estan los indices
|
200 |
// indexSearcher = new IndexSearcher(indexPath);
|
201 |
//
|
202 |
// for (String cadena:cadenas){
|
203 |
// log.info("Buscando ``"+ cadena + "`` con el analizador " +
|
204 |
// an.getClass().getName());
|
205 |
// // Se crear la consulta indicando el nombre del campo donde estan
|
206 |
// // las cadenas del indices
|
207 |
// // junto con el analizador
|
208 |
// Query consulta = null;
|
209 |
//
|
210 |
// consulta = new QueryParser("str", an).parse(cadena);
|
211 |
// // consulta = new FuzzyQuery(new Term("str",cadena));
|
212 |
//
|
213 |
//
|
214 |
// // se realiza la busqueda de una cadena y se obtinen los resultados
|
215 |
// Hits hits = indexSearcher.search(consulta);
|
216 |
//
|
217 |
//
|
218 |
// log.info("Resultados: " + hits.length());
|
219 |
// for (int i = 0; i < hits.length(); i++) {
|
220 |
// Document docu = hits.doc(i);
|
221 |
//
|
222 |
// log.info("str: " + docu.get("str") + "\t id: " + docu.get("id"));
|
223 |
// }
|
224 |
// }
|
225 |
// } catch (Exception e) {
|
226 |
// e.printStackTrace();
|
227 |
// } finally {
|
228 |
// try {
|
229 |
// indexSearcher.close();
|
230 |
// } catch (IOException e) {
|
231 |
// log.error("Error cerrando ?ndice",e);
|
232 |
// }
|
233 |
//
|
234 |
// }
|
235 |
// }
|
236 |
//
|
237 |
//
|
238 |
// public void tearDown() {
|
239 |
//
|
240 |
// }
|
241 |
//
|
242 |
// private ArrayList<String> readTextFile(File file) {
|
243 |
//
|
244 |
// ArrayList<String> arr = new ArrayList<String>();
|
245 |
// if (file != null) {
|
246 |
// FileReader fr = null;
|
247 |
// String str = "";
|
248 |
// try {
|
249 |
// fr = new FileReader(file);
|
250 |
// BufferedReader br = new BufferedReader(fr);
|
251 |
// while ((str = br.readLine()) != null) {
|
252 |
// arr.add(str);
|
253 |
// }
|
254 |
// } catch (Exception e) {
|
255 |
// e.printStackTrace();
|
256 |
// log.debug("ERROR reading a file");
|
257 |
// }
|
258 |
// }
|
259 |
// return arr;
|
260 |
// }
|
261 |
|
262 |
} |