/// <summary> /// save result as txt file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void saveResultButton_Click_2(object sender, EventArgs e) { //set paramaeter of savefiledialog Stream stream; saveFileDialog1.CreatePrompt = true; saveFileDialog1.OverwritePrompt = true; saveFileDialog1.DefaultExt = "txt"; saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); if (infoNeed == "") { MessageBox.Show("Invalid input!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); InfoNeedInput.Clear(); return; } myLuceneApp.CreateSearcher(); Lucene.Net.Search.Query query = myLuceneApp.InfoParser(infoNeed); Lucene.Net.Search.TopDocs result = myLuceneApp.SearchText(query); string query1 = query.ToString(); string path = ""; string oneLine = ""; int rank = 0; string topicID = textBox3.Text; string gropunumber = "0123456798_0987654321_ourteam"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { path = saveFileDialog1.FileName; if (File.Exists(path)) { using (StreamWriter myWritter1 = File.AppendText(path)) { foreach (Lucene.Net.Search.ScoreDoc scoreDoc in result.ScoreDocs) { rank++; Lucene.Net.Documents.Document doc = myLuceneApp.GetSearcher.Doc(scoreDoc.Doc); string docID = doc.Get("id").Replace(" ", "").Replace("\n", "").Replace("\r", "").ToString(); oneLine = string.Format("{0}\tQ0\t{1}\t{2}\t{3}\t{4}", topicID, docID, rank, scoreDoc.Score.ToString(), gropunumber); myWritter1.WriteLine(oneLine); } myWritter1.Flush(); myWritter1.Close(); } } else if ((stream = saveFileDialog1.OpenFile()) != null) { using (StreamWriter myWritter2 = new StreamWriter(stream)) { foreach (Lucene.Net.Search.ScoreDoc scoreDoc in result.ScoreDocs) { rank++; Lucene.Net.Documents.Document doc = myLuceneApp.GetSearcher.Doc(scoreDoc.Doc); string docID = doc.Get("id").Replace(" ", "").Replace("\n", "").Replace("\r", "").ToString(); oneLine = string.Format("{0}\tQ0\t{1}\t{2}\t{3}\t{4}", topicID, docID, rank, scoreDoc.Score.ToString(), gropunumber); myWritter2.WriteLine(oneLine); } myWritter2.Flush(); myWritter2.Close(); //"{0}\tQ0\t{1,10}\t{2,5}\t{3,15}\t{4,38}" } } } }
/// <summary> /// Deal with the information need as suitable query /// search the processed query /// display the result /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void submitButton_Click(object sender, EventArgs e) { CleanResult(); infoNeed = InfoNeedInput.Text; Stopwatch stopwatch = new Stopwatch(); //give an error warning when input is null if (infoNeed == "") { MessageBox.Show("Invalid input!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); InfoNeedInput.Clear(); return; } //take the analyzer selected by users string analyzerSelection = comboBox1.SelectedItem.ToString(); myLuceneApp.AnalyzerSelection(analyzerSelection); //make the input "as is" to search myLuceneApp.CreateSearcher(); if (checkBox1.Checked) { infoNeed = infoNeed.Replace("\"", ""); infoNeed = "\"" + infoNeed + "\""; } string expandedQueryItems = ""; OptionOfQeryExpansionAndWeighterQueries(expandedQueryItems); AnalyzerChoice(); //record searching time stopwatch.Start(); //parse the information need into final query terms Lucene.Net.Search.Query query = myLuceneApp.InfoParser(infoNeed); string queryText = query.ToString(); //display the final query in textbox textBox2.Text = queryText; Lucene.Net.Search.TopDocs result = myLuceneApp.SearchText(query); stopwatch.Stop(); TimeSpan timeSpan = stopwatch.Elapsed; double time = timeSpan.TotalSeconds; time = Math.Round(time, 4); toolStripStatusLabel2.Text = "Searching time: " + time.ToString() + "s"; int rank = 0; pageDivded.DtSource.Columns.Add("list"); //display the number of result groupBox5.Text = "Result: " + result.TotalHits.ToString(); //display the results foreach (Lucene.Net.Search.ScoreDoc scoreDoc in result.ScoreDocs) { rank++; Lucene.Net.Documents.Document doc = myLuceneApp.GetSearcher.Doc(scoreDoc.Doc); string ID = "ID" + doc.Get("id").Replace(" ", "").ToString(); string title = "Title: " + doc.Get("title").ToString(); string author = "Author: " + doc.Get("author").ToString(); string bbibliographic = "Bibliographic: " + doc.Get("bibliographic").ToString(); string textAbstract = doc.Get("firstSentence").ToString(); string row = "Rank:" + rank + "\r\n" + title + "\r\n" + author + "\r\n" + bbibliographic + "\r\n" + textAbstract + "\r\n"; pageDivded.DtSource.Rows.Add(row); } //A prompt message pop up when there is no results if (pageDivded.DtSource.Rows.Count <= 0) { MessageBox.Show("No result", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } pageDivded.DividedPage(); dataGridView1.DataSource = pageDivded.LoadPage(); dataGridView1.Columns["list"].FillWeight = 240; dataGridView1.Show(); label6.Text = pageDivded.PageNumber(); myLuceneApp.CleanUpSearcher(); }