/
LuceneApplication.cs
197 lines (168 loc) · 7.21 KB
/
LuceneApplication.cs
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
/* IFN647 Project - EduSearch Application created by
* Huanjiao Qiu n9582541
* Wai Wing Chan n9781463
* Zhiyi Wu n9589147
* on 26 Oct 2017
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Lucene.Net.Analysis; // for Analyser
using Lucene.Net.Documents; // for Document and Field
using Lucene.Net.Index; //for Index Writer
using Lucene.Net.Store; //for Directory
using Lucene.Net.Search; // for IndexSearcher
using Lucene.Net.QueryParsers; // for QueryParser
using System.Text.RegularExpressions;
using System.Data;
namespace a
{
class LuceneApplication
{
Lucene.Net.Store.Directory luceneIndexDirectory;
Lucene.Net.Analysis.Analyzer analyzer;
Lucene.Net.Index.IndexWriter writer;
Lucene.Net.Search.IndexSearcher searcher;
Lucene.Net.QueryParsers.QueryParser parser;
CustomSimilarity customSimilarity;
const string TEXT_FN = "Abstract";
const Lucene.Net.Util.Version VERSION = Lucene.Net.Util.Version.LUCENE_30;
// Initializes objects
public LuceneApplication()
{
luceneIndexDirectory = null;
writer = null;
analyzer = new Lucene.Net.Analysis.Snowball.SnowballAnalyzer(VERSION, "English");
parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, TEXT_FN, analyzer);
customSimilarity = new CustomSimilarity();
}
// Creates index based on selection of analyzer
public void CreateIndex(string indexPath,string name)
{
luceneIndexDirectory = Lucene.Net.Store.FSDirectory.Open(indexPath);
IndexWriter.MaxFieldLength mfl = new IndexWriter.MaxFieldLength(IndexWriter.DEFAULT_MAX_FIELD_LENGTH);
if (name == "WhitespaceAnalyzer")
{
analyzer = new Lucene.Net.Analysis.WhitespaceAnalyzer();
}
if (name == "SimpleAnalyzer")
{
analyzer = new Lucene.Net.Analysis.SimpleAnalyzer();
}
if (name == "StandardAnalyzer")
{
analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(VERSION);
}
if (name == "StopAnalyzer")
{
analyzer = new Lucene.Net.Analysis.StopAnalyzer(VERSION);
}
else
{
writer = new Lucene.Net.Index.IndexWriter(luceneIndexDirectory, analyzer, true, mfl);
}
writer.SetSimilarity(customSimilarity);
}
// Cleans up index
public void CleanUpIndexer()
{
writer.Optimize();
writer.Flush(true, true, true);
writer.Dispose();
}
// Creates multiple fields and sections DocID, Title, Author, Biliographic, Abstract for index
public void IndexText(string text)
{
if (text == "")
{
Lucene.Net.Documents.Field field = new Field(TEXT_FN, text, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
Lucene.Net.Documents.Document doc = new Document();
doc.Add(field);
writer.AddDocument(doc);
}
if ((text != "") && (text != null))
{
string removal = "\n";
text = text.Replace(removal, "");
// Delimeters
string pattern = @"(.T)|(.A)|(.B)|(.W)";
// Splits items by delimeters
string[] substrings = Regex.Split(text, pattern);
Lucene.Net.Documents.Document doc = new Document();
// Creates new doc of multiple fields
doc.Add(new Field("Title", substrings[2], Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.Add(new Field("Author", substrings[4], Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.Add(new Field("Bibliographic", substrings[6], Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
doc.Add(new Field("Abstract", substrings[8], Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(doc);
}
}
// Creates searcher using custom similarity class
public void CreateSearcher()
{
searcher = new IndexSearcher(luceneIndexDirectory);
searcher.Similarity = customSimilarity;
}
// Cleans up searcher
public void CleanUpSearcher()
{
searcher.Dispose();
}
// Processes query and ranks results
public void SearchIndex(string querytext, ref TopDocs results, ref int numberOfDocuments)
{
parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, TEXT_FN, analyzer);
querytext = querytext.ToLower();
Query query = parser.Parse(querytext);
results = searcher.Search(query, 1400);
numberOfDocuments = results.TotalHits;
int rank = 0;
foreach (ScoreDoc scoreDoc in results.ScoreDocs)
{
rank++;
// retrieve the document from the 'ScoreDoc' object
Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
string myFieldValue = doc.Get(TEXT_FN).ToString();
//Console.WriteLine("Rank " + rank + " text " + myFieldValue);
}
}
// Creates result array
public string[,] CreateData(TopDocs topDocs)
{
string[,] dt = new string[topDocs.TotalHits, 4];
int index = 0;
foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
{
string removal = ".";
Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
dt[index, 0] = doc.Get("Title").ToString().Replace(removal, "");
dt[index, 1] = doc.Get("Author").ToString().Replace(removal, "");
dt[index, 2] = doc.Get("Bibliographic").ToString();
if(doc.Get(TEXT_FN).ToString() != "")
{
dt[index, 3] = doc.Get(TEXT_FN).ToString().Replace(doc.Get("Title").ToString(), "");
}
index++;
}
return dt;
}
// Generates top documents by ranking results
public List<string> SaveResults(TopDocs topDocs)
{
List<string> searchResultList = new List<string>();
int rank = 0;
foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
{
Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
rank++;
Lucene.Net.Documents.Document doc1 = searcher.Doc(scoreDoc.Doc);
string myFieldValue = doc.Get(TEXT_FN).ToString();
string result = "Q0" + "\t" + scoreDoc.Doc + "\t" + rank + "\t" + scoreDoc.Score + "\t";
searchResultList.Add(result);
}
return searchResultList;
}
}
}