-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuceneExamples2.java
More file actions
312 lines (290 loc) · 11.7 KB
/
LuceneExamples2.java
File metadata and controls
312 lines (290 loc) · 11.7 KB
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
package de.tudarmstadt.ukp.wikipedia;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import de.tudarmstadt.ukp.wikipedia.api.DatabaseConfiguration;
import de.tudarmstadt.ukp.wikipedia.api.Page;
import de.tudarmstadt.ukp.wikipedia.api.Wikipedia;
import de.tudarmstadt.ukp.wikipedia.api.exception.WikiApiException;
import de.tudarmstadt.ukp.wikipedia.api.exception.WikiInitializationException;
import de.tudarmstadt.ukp.wikipedia.api.WikiConstants.Language;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.analysis.core.KeywordTokenizer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.core.StopFilter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.util.CharArraySet;
import org.apache.lucene.document.*;
import org.apache.lucene.document.Field.Store;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class LuceneExamples2 {
public static void main(String[] args) {
//indexDirectory();
//search("350539");
//System.out.println();
printOutlinks("3366");
search("3366");
/*System.out.println(getInlinksNo("3366"));
System.out.println("*******************************");*/
/*printOutlinks("3366");
System.out.println(getOutlinksNo("3366"));
System.out.println("*******************************");
/*Set<String> inLinks = getInlinks("3366");
for(String link:inLinks){
System.out.println(link);
}
System.out.println(inLinks.size());
System.out.println("*******************************");
Set<String> outLinks = getOutlinks("3366");
for(String link:outLinks){
System.out.println(link);
}
System.out.println(outLinks.size());
System.out.println("*******************************");*/
//search("برشلونة");
}
private static void indexDirectory() {
//Apache Lucene Indexing Directory .txt files
try {
//indexing directory
DatabaseConfiguration dbConfig = new DatabaseConfiguration();
dbConfig.setHost("localhost");
dbConfig.setDatabase("arwiki");
dbConfig.setUser("root");
dbConfig.setPassword("root");
dbConfig.setLanguage(Language.arabic);
Wikipedia wiki = new Wikipedia(dbConfig);
Path path = Paths.get("C:/linkIndexestmp1");
Directory directory = FSDirectory.open(path);
IndexWriterConfig config = new IndexWriterConfig(new SimpleAnalyzer());
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.deleteAll();
//Iterator<Page> iter = wiki.getPages().iterator();
// Iterator<Page> iter = wiki.getPages().iterator();
Iterator<Page> iter = wiki.getArticles().iterator();
while(iter.hasNext()) {
try{
Page page = iter.next();
System.out.println("indexed " + page.getPageId());
Document doc = new Document();
if(page.getNumberOfInlinks() != 0 && page.getNumberOfOutlinks() !=0){
doc.add(new TextField("path", ""+page.getPageId(), Store.YES));
doc.add(new TextField("inlinks", page.getInlinkIDs()+" ", Store.YES));
doc.add(new TextField("outlinks", page.getOutlinkIDs()+" ", Store.YES));
doc.add(new TextField("numInlinks",page.getNumberOfInlinks()+" ", Store.YES));
doc.add(new TextField("numOutlinks", page.getNumberOfOutlinks()+" ", Store.YES));
}
indexWriter.addDocument(doc);
}catch(Exception e){
e.printStackTrace();
}
}
indexWriter.close();
directory.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
/* List<String> pages = new ArrayList<String>();
for(int j = 0; j < 10000; j++){
try{
pages.add(wiki.getPage(j).getPageId()+"");
}catch(Exception e){
e.printStackTrace();
j++;
}
}
int i=0;
for (String page : pages) {
System.out.println(page.toString());
Document doc = new Document();
doc.add(new TextField("path", page.toString(), Store.YES));
i++;
doc.add(new TextField("inlinks", wiki.getPage(page.toString()).getInlinkIDs()+" ", Store.YES));
doc.add(new TextField("outlinks", wiki.getPage(page.toString()).getOutlinkIDs()+" ", Store.YES));
doc.add(new TextField("numInlinks", wiki.getPage(page.toString()).getNumberOfInlinks()+" ", Store.YES));
doc.add(new TextField("numOutlinks", wiki.getPage(page.toString()).getNumberOfOutlinks()+" ", Store.YES));
System.out.println("id : "+page.toString());
System.out.println("inlinks: "+wiki.getPage(page.toString()).getInlinkIDs());
indexWriter.addDocument(doc);
}
/*indexWriter.close();
directory.close();*/
/*} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}*/
}
public static void printInlinks(String pageId){
try {
Path path = Paths.get("C:/linkIndexestmp1");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TermQuery query = new TermQuery(new Term("path", pageId));
TopDocs topDocs = indexSearcher.search(query,indexReader.numDocs());
System.out.println("totalHits " + topDocs.totalHits);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println("path " + document.get("path"));
System.out.println(document.get("inlinks"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static Set<String> getInlinks(String pageId){
String inLinks = "";
String[] in;
try {
Path path = Paths.get("C:/linkIndexestmp1");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TermQuery query = new TermQuery(new Term("path", pageId));
TopDocs topDocs = indexSearcher.search(query,indexReader.numDocs());
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
inLinks = document.get("inlinks");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
in = inLinks.split("\\, |\\[|\\]");
/*for(String s:in){
s.trim();
}*/
Set<String> inLink = new HashSet<String>(Arrays.asList(in));
return inLink;
}
public static String getInlinksNo(String pageId){
String inLinksNo = "";
try {
Path path = Paths.get("C:/linkIndexestmp1");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TermQuery query = new TermQuery(new Term("path", pageId));
TopDocs topDocs = indexSearcher.search(query,indexReader.numDocs());
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
inLinksNo = document.get("numInlinks");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return inLinksNo;
}
public static void printOutlinks(String pageId){
try {
Path path = Paths.get("C:/linkIndexestmp1");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TermQuery query = new TermQuery(new Term("path", pageId));
TopDocs topDocs = indexSearcher.search(query,indexReader.numDocs());
System.out.println("totalHits " + topDocs.totalHits);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println("path " + document.get("path"));
System.out.println("outlinks " + document.get("outlinks"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static List<String> getOutlinks(String pageId){
String outLinks = "";
String[] out;
//List<String> outLink = new ArrayList();
try {
Path path = Paths.get("C:/linkIndexestmp1");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TermQuery query = new TermQuery(new Term("path", pageId));
TopDocs topDocs = indexSearcher.search(query,indexReader.numDocs());
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
outLinks = document.get("outlinks");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
out = outLinks.split("\\,|\\[|\\]|\\s+");
/*for(String s:out){
s.trim();
}*/
//Set<String> outLink = new HashSet<String>(Arrays.asList(out));
List<String> outLink = new ArrayList<String>(Arrays.asList(out));
return outLink;
}
public static String getOutlinksNo(String pageId){
String outLinksNo = "";
try {
Path path = Paths.get("C:/linkIndexestmp1");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
TermQuery query = new TermQuery(new Term("path", pageId));
TopDocs topDocs = indexSearcher.search(query,indexReader.numDocs());
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
outLinksNo = document.get("numOutlinks");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return outLinksNo;
}
public static void search(String text) {
//Apache Lucene searching text inside .txt files
try {
Path path = Paths.get("C:/pageTiltes2");
Directory directory = FSDirectory.open(path);
IndexReader indexReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
QueryParser queryParser = new QueryParser("contents", new StandardAnalyzer());
Query query = queryParser.parse(text);
TopDocs topDocs = indexSearcher.search(query,10);
System.out.println("totalHits " + topDocs.totalHits);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println("path " + document.get("path"));
System.out.println("content " + document.get("contents"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}