/// <summary>
        /// Returns postings for a query
        /// </summary>
        /// <param name="query">the query which the user is making to the search engine</param>
        public List <string> SearchQuery(string query)
        {
            try
            {
                //the list of strings to return
                List <String> results = new List <string>();


                if (mode == false)
                {
                    Console.WriteLine("In Ranked Retrieval");
                    Console.WriteLine("Query:" + query);

                    //parser to parse the query
                    RankedRetrievalParser parser = new RankedRetrievalParser();

                    List <string> finalTerms = parser.ParseQuery(query);

                    //retrieves the top ten documents of the normalized tokens
                    RankedRetrieval rv = new RankedRetrieval(corpus, index, RankedRetrievalMode);

                    IList <MaxPriorityQueue.InvertedIndex> topTenDocs = rv.GetTopTen(finalTerms);

                    //parse the query
                    List <string> terms = parser.ParseQuery(query);

                    if (topTenDocs.Count > 0)
                    {
                        //add the count of the postings to the list of strings to be returned
                        results.Add(topTenDocs.Count.ToString());

                        //for each posting...
                        int numberRank = 1;
                        foreach (MaxPriorityQueue.InvertedIndex p in topTenDocs)
                        {
                            //use the document id to access the document
                            IDocument doc = corpus.GetDocument(p.GetDocumentId());

                            //add the title to the list of strings to be returned
                            results.Add("#" + numberRank + ": (" + Math.Round(p.GetRank(), 5).ToString() + ") " + doc.Title);

                            //add the document id to the list of strings to be returned
                            results.Add(doc.DocumentId.ToString());
                            Console.WriteLine(p.GetDocumentId() + "" + doc.Title);
                            numberRank++;
                        }
                    }

                    return(results);
                }
                // end of ranked retrieval segment (if statement)
                else
                {
                    Console.WriteLine(query);


                    //the list of postings
                    IList <Posting> postings;
                    IQueryComponent component;
                    //create a stemming token processor
                    ITokenProcessor processor = new StemmingTokenProcesor();
                    //create a boolean query parser
                    BooleanQueryParser parser = new BooleanQueryParser();
                    //parse the query
                    component = parser.ParseQuery(query);

                    //get the postings
                    postings = component.GetPostings(index, processor);


                    //if there are any postings...
                    if (postings.Count > 0)
                    {
                        //add the count of the postings to the list of strings to be returned
                        results.Add(postings.Count.ToString());
                        //for each posting...
                        foreach (Posting p in postings)
                        {
                            //use the document id to access the document
                            IDocument doc = corpus.GetDocument(p.DocumentId);
                            //add the title to the list of strings to be returned
                            results.Add(doc.Title);
                            //add the document id to the list of strings to be returned
                            results.Add(doc.DocumentId.ToString());
                        }
                        Console.WriteLine(results.Count);
                    }
                    //if there aren't any postings...
                    else
                    {
                        //add a zero to the list of strings to be returned
                        results.Add("0");
                    }
                    //return the list of strings
                    return(results);
                }
            }
            catch (Exception e)
            {
                return(new List <string>());
            }

            // Console.Write("Corpus size is:");
            // Console.WriteLine(corpus.CorpusSize);
        }
Beispiel #2
0
        public void ParsingSingleQueryTest_ReturnsTermLiteral()
        {
            //Arrange
            string      query    = "smoothie";
            TermLiteral expected = new TermLiteral(query);
            //Act
            IQueryComponent actual = parser.ParseQuery(query);

            //Assert
            actual.Should().BeOfType(typeof(TermLiteral));
            ((TermLiteral)actual).Term.Should().BeSameAs(expected.Term);
        }