private void button2_Click(object sender, EventArgs e) //zobraz abstrakt pre dbpediu
{
if (textBox2.Text.Equals(""))
{
MessageBox.Show("Zadajte cestu k suboru!");
}
else
{
if (textBox3.Text.Equals(""))
{
MessageBox.Show("Vyplňte políčko: Zadajte hľadaný abstrakt!");
}
else
{
//ak nemam este vyskladany index z mojho suboru
if (!MamIndexDBPedia)
{
directoryDBPedia = FSDirectory.Open(new DirectoryInfo(Environment.CurrentDirectory + "\\DBPediaIndex"));
analyzerDBPedia = new StandardAnalyzer(Version.LUCENE_29);
IndexWriter writer = new IndexWriter(directoryDBPedia, analyzerDBPedia, true, IndexWriter.MaxFieldLength.UNLIMITED);
string riadok;
cestaSuborDBPedia = textBox2.Text;
System.IO.StreamReader subor = new System.IO.StreamReader(cestaSuborDBPedia);
while ((riadok = subor.ReadLine()) != null)
{
string[] poleAbstrakt = riadok.Split(new string[] { ";|?" }, StringSplitOptions.None);
//ideme indexovat kazdy jeden riadok ktory sme si predtym podelili na title a abstrakt
Document doc = new Document();
doc.Add(new Field("Nadpis", poleAbstrakt[0], Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Abstrakt", poleAbstrakt[1], Field.Store.YES, Field.Index.NO));
writer.AddDocument(doc);
}
//zavrieme a aktualizujeme zapisovac a ostatne srandy
MamIndexDBPedia = true;
writer.Optimize();
//writer.Commit();
writer.Close();
MessageBox.Show("Indexovanie súboru prebehlo úspešne!");
}
if (MamIndexDBPedia)
{
//otvorenie directory na citanie indexov a definovanie hladaca
IndexReader indexReader = IndexReader.Open(directoryDBPedia, true);
Searcher indexSearch = new IndexSearcher(indexReader);
//vytvorime samotny vyhladavac, definujeme verziu a pole v ktorom chceme hladat, odovzdame dopyt zadany na vstupe
QueryParser queryParser = new QueryParser(Version.LUCENE_29, "Nadpis", analyzerDBPedia);
Query query = queryParser.Parse(textBox3.Text);
//samotne hladanie a naplnenie struktury pre vysledky
TopDocs resultDocs = indexSearch.Search(query, 20);
//najdene vysledky hladania
var hits = resultDocs.scoreDocs;
richTextBox2.Text = "";
abstraktDBpedia = "";
textBox5.Text = resultDocs.totalHits.ToString();
if (resultDocs.totalHits > 0)
{
foreach (var hit in hits)
{
var documentFromSearcher = indexSearch.Doc(hit.doc);
richTextBox2.Text += documentFromSearcher.Get("Nadpis") + ": " + documentFromSearcher.Get("Abstrakt") + "\n";
abstraktDBpedia += documentFromSearcher.Get("Abstrakt") + " ";
}
}
else
{
richTextBox2.Text = "Nenasla sa ziadna zhoda";
}
}
}
}
}