1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/**
* LibraryProvider.java
* Copyright 2009 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
* first published 01.10.2009 on http://yacy.net
*
* This file is part of YaCy Content Integration
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/
package net.yacy.document;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import net.yacy.cora.document.WordCache;
import net.yacy.cora.document.id.MultiProtocolURL;
import net.yacy.cora.geo.GeonamesLocation;
import net.yacy.cora.geo.OpenGeoDBLocation;
import net.yacy.cora.geo.OverarchingLocation;
import net.yacy.cora.language.synonyms.AutotaggingLibrary;
import net.yacy.cora.language.synonyms.SynonymLibrary;
import net.yacy.cora.lod.JenaTripleStore;
import net.yacy.cora.lod.vocabulary.Tagging;
import net.yacy.cora.lod.vocabulary.Tagging.SOTuple;
import net.yacy.cora.storage.Files;
import net.yacy.cora.util.ConcurrentLog;
import net.yacy.crawler.retrieval.URLRewriterLibrary;
import net.yacy.kelondro.util.FileUtils;
import com.hp.hpl.jena.rdf.model.Resource;
public class LibraryProvider {
public static final String path_to_source_dictionaries = "source";
public static final String path_to_did_you_mean_dictionaries = "didyoumean";
public static final String path_to_autotagging_dictionaries = "autotagging";
public static final String path_to_synonym_dictionaries = "synonyms";
public static final String path_to_rewriter_dictionaries = "rewriter";
public static final String disabledExtension = ".disabled";
public static WordCache dymLib = new WordCache(null);
public static AutotaggingLibrary autotagging = null;
public static SynonymLibrary synonyms = null;
public static URLRewriterLibrary urlRewriter = null;
public static OverarchingLocation geoLoc = new OverarchingLocation();
private static File dictSource = null;
private static File dictRoot = null;
public static enum Dictionary {
GEODB0( "geo0", "http://downloads.sourceforge.net/project/opengeodb/Data/0.2.5a/opengeodb-0.2.5a-UTF8-sql.gz" ),
GEODB1( "geo1", "http://fa-technik.adfc.de/code/opengeodb/dump/opengeodb-02624_2011-10-17.sql.gz" ),
GEON0( "geon0", "http://download.geonames.org/export/dump/cities1000.zip" ),
GEON1( "geon1", "http://download.geonames.org/export/dump/cities5000.zip" ),
GEON2( "geon2", "http://download.geonames.org/export/dump/cities15000.zip" ),
DRW0( "drw0", "http://www.ids-mannheim.de/kl/derewo/derewo-v-100000t-2009-04-30-0.1.zip" ),
PND0( "pnd0", "http://downloads.dbpedia.org/3.7-i18n/de/pnd_de.nt.bz2" );
public String nickname, url, filename;
private Dictionary(final String nickname, final String url) {
try {
this.filename = (new MultiProtocolURL(url)).getFileName();
} catch (final MalformedURLException e ) {
assert false;
}
this.nickname = nickname;
this.url = url;
}
public File file() {
return new File(dictSource, this.filename);
}
public File fileDisabled() {
return new File(dictSource, this.filename + disabledExtension);
}
}
/**
* initialize the LibraryProvider as static class. This assigns default paths, and initializes the
* dictionary classes Additionally, if default dictionaries are given in the source path, they are
* translated into the input format inside the DATA/DICTIONARIES directory
*
* @param pathToSource
* @param pathToDICTIONARIES
*/
public static void initialize(final File rootPath) {
dictSource = new File(rootPath, path_to_source_dictionaries);
if ( !dictSource.exists() ) {
dictSource.mkdirs();
}
dictRoot = rootPath;
// initialize libraries
initAutotagging();
activateDeReWo();
initDidYouMean();
initSynonyms();
initRewriter();
integrateOpenGeoDB();
integrateGeonames0(-1);
integrateGeonames1(-1);
integrateGeonames2(100000);
activatePND();
Set<String> allTags = new HashSet<String>() ;
allTags.addAll(autotagging.allTags()); // we must copy this into a clone to prevent circularity
autotagging.addPlaces(geoLoc);
//autotagging.addDictionaries(dymLib.getDictionaries()); // strange results with this: normal word lists are 'too full'
WordCache.learn(allTags);
}
public static void integrateOpenGeoDB() {
final File geo1 = Dictionary.GEODB1.file();
final File geo0 = Dictionary.GEODB0.file();
if ( geo1.exists() ) {
if ( geo0.exists() ) {
geo0.renameTo(Dictionary.GEODB0.fileDisabled());
}
geoLoc.activateLocation(Dictionary.GEODB1.nickname, new OpenGeoDBLocation(geo1, dymLib));
return;
}
if ( geo0.exists() ) {
geoLoc.activateLocation(Dictionary.GEODB0.nickname, new OpenGeoDBLocation(geo0, dymLib));
return;
}
}
public static void integrateGeonames0(long minPopulation) {
final File geon = Dictionary.GEON0.file();
if ( geon.exists() ) {
geoLoc.activateLocation(Dictionary.GEON0.nickname, new GeonamesLocation(geon, dymLib, minPopulation));
return;
}
}
public static void integrateGeonames1(long minPopulation) {
final File geon = Dictionary.GEON1.file();
if ( geon.exists() ) {
geoLoc.activateLocation(Dictionary.GEON1.nickname, new GeonamesLocation(geon, dymLib, minPopulation));
return;
}
}
public static void integrateGeonames2(long minPopulation) {
final File geon = Dictionary.GEON2.file();
if ( geon.exists() ) {
geoLoc.activateLocation(Dictionary.GEON2.nickname, new GeonamesLocation(geon, dymLib, minPopulation));
return;
}
}
public static void initDidYouMean() {
final File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries);
if ( !dymDict.exists() ) {
dymDict.mkdirs();
}
dymLib = new WordCache(dymDict);
}
public static void initAutotagging() {
final File autotaggingPath = new File(dictRoot, path_to_autotagging_dictionaries);
if ( !autotaggingPath.exists() ) {
autotaggingPath.mkdirs();
}
autotagging = new AutotaggingLibrary(autotaggingPath);
}
public static void initSynonyms() {
final File synonymPath = new File(dictRoot, path_to_synonym_dictionaries);
if ( !synonymPath.exists() ) {
synonymPath.mkdirs();
}
synonyms = new SynonymLibrary(synonymPath);
}
public static void initRewriter() {
final File rewriterPath = new File(dictRoot, path_to_rewriter_dictionaries);
if ( !rewriterPath.exists() ) {
rewriterPath.mkdirs();
}
urlRewriter = new URLRewriterLibrary(rewriterPath);
}
public static void activateDeReWo() {
// translate input files (once..)
final File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries);
if ( !dymDict.exists() ) {
dymDict.mkdirs();
}
final File derewoInput = LibraryProvider.Dictionary.DRW0.file();
final File derewoOutput = new File(dymDict, derewoInput.getName() + ".words");
if ( !derewoOutput.exists() && derewoInput.exists() ) {
// create the translation of the derewo file (which is easy in this case)
final ArrayList<String> derewo = loadDeReWo(derewoInput, true);
try {
writeWords(derewoOutput, derewo);
} catch (final IOException e ) {
ConcurrentLog.logException(e);
}
}
}
public static void deactivateDeReWo() {
final File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries);
final File derewoInput = LibraryProvider.Dictionary.DRW0.file();
final File derewoOutput = new File(dymDict, derewoInput.getName() + ".words");
FileUtils.deletedelete(derewoOutput);
}
public static void activatePND() {
// translate input files (once..)
final File dymDict = new File(dictRoot, path_to_did_you_mean_dictionaries);
if ( !dymDict.exists() ) {
dymDict.mkdirs();
}
// read the pnd file and store it into the triplestore
final File dictInput = LibraryProvider.Dictionary.PND0.file();
if ( dictInput.exists() ) {
try {
JenaTripleStore.LoadNTriples(Files.read(dictInput));
} catch (final IOException e ) {
ConcurrentLog.logException(e);
}
}
// read the triplestore and generate a vocabulary
Map<String, SOTuple> map = new HashMap<String, SOTuple>();
ConcurrentLog.info("LibraryProvider", "retrieving PND data from triplestore");
Iterator<Resource> i = JenaTripleStore.getSubjects("http://dbpedia.org/ontology/individualisedPnd");
ConcurrentLog.info("LibraryProvider", "creating vocabulary map from PND triplestore");
String objectspace = "";
while (i.hasNext()) {
Resource resource = i.next();
String subject = resource.toString();
// prepare a proper term from the subject uri
int p = subject.lastIndexOf('/');
if (p < 0) continue;
String term = subject.substring(p + 1);
objectspace = subject.substring(0, p + 1);
p = term.indexOf('(');
if (p >= 0) term = term.substring(0, p);
term = term.replaceAll("_", " ").trim();
if (term.isEmpty()) continue;
if (term.indexOf(' ') < 0) continue; // accept only names that have at least two parts
// store the term into the vocabulary map
map.put(term, new SOTuple(Tagging.normalizeTerm(term), subject));
}
if (!map.isEmpty()) try {
ConcurrentLog.info("LibraryProvider", "adding vocabulary to autotagging");
Tagging pndVoc = new Tagging("Persons", null, objectspace, map);
autotagging.addVocabulary(pndVoc);
ConcurrentLog.info("LibraryProvider", "added pnd vocabulary to autotagging");
} catch (final IOException e) {
}
}
public static void deactivatePND() {
// remove the PND Triples from the triplestore
JenaTripleStore.deleteObjects(null, "http://dbpedia.org/ontology/individualisedPnd");
autotagging.deleteVocabulary("Persons");
}
/*
private static ArrayList<String> loadList(final File file, String comment, boolean toLowerCase) {
final ArrayList<String> list = new ArrayList<String>();
if (!file.exists()) return list;
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if ((line.length() > 0) && (!(line.startsWith(comment)))) {
list.add((toLowerCase) ? line.trim().toLowerCase() : line.trim());
}
}
reader.close();
} catch (final IOException e) {
Log.logException(e);
} finally {
if (reader != null) try { reader.close(); } catch (final Exception e) {}
}
return list;
}
*/
private static Set<String> sortUnique(final List<String> list) {
final Set<String> s = new TreeSet<String>();
for ( final String t : list ) {
s.add(t);
}
return s;
}
private static void writeWords(final File f, final ArrayList<String> list) throws IOException {
final Set<String> s = sortUnique(list);
final PrintWriter w = new PrintWriter(new BufferedWriter(new FileWriter(f)));
for ( final String t : s ) {
w.println(t);
}
w.close();
}
private static ArrayList<String> loadDeReWo(final File file, final boolean toLowerCase) {
final ArrayList<String> list = new ArrayList<String>();
// get the zip file entry from the file
InputStream derewoTxtEntry;
try {
final ZipFile zip = new ZipFile(file);
derewoTxtEntry = zip.getInputStream(zip.getEntry("derewo-v-100000t-2009-04-30-0.1"));
} catch (final ZipException e ) {
ConcurrentLog.logException(e);
return list;
} catch (final IOException e ) {
ConcurrentLog.logException(e);
return list;
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(derewoTxtEntry, "UTF-8"));
String line;
// read until text starts
while ( (line = reader.readLine()) != null ) {
if ( line.startsWith("# -----") ) {
break;
}
}
// read empty line
line = reader.readLine();
// read lines
int p;
//int c;
String w;
while ( (line = reader.readLine()) != null ) {
line = line.trim();
p = line.indexOf(' ', 0);
if ( p > 0 ) {
//c = Integer.parseInt(line.substring(p + 1));
//if (c < 1) continue;
w =
(toLowerCase) ? line.substring(0, p).trim().toLowerCase() : line
.substring(0, p)
.trim();
if ( w.length() < 4 ) {
continue;
}
list.add(w);
}
}
reader.close();
} catch (final IOException e ) {
ConcurrentLog.logException(e);
} finally {
if ( reader != null ) {
try {
reader.close();
} catch (final Exception e ) {
}
}
}
return list;
}
public static void main(final String[] args) {
final File here = new File("dummy").getParentFile();
initialize(new File(here, "DATA/DICTIONARIES"));
System.out.println("dymDict-size = " + dymLib.size());
final Set<StringBuilder> r = dymLib.recommend(new StringBuilder("da"));
for ( final StringBuilder s : r ) {
System.out.println("$ " + s);
}
System.out.println("recommendations: " + r.size());
}
}
|