예제 #1
0
        public static void findResultQueries(Queries queries, int k)
        {
            // list of hasil tiap query (list of list of result)
            allResults = new List<List<Docvalue>>();

            // list of list query old and new
            lQueryWeightOld = new List<List<WeightedTermQuery>>();
            lQueryWeightOld.Clear();
            lQueryWeightNew = new List<List<WeightedTermQuery>>();
            lDQueryWeightNew = new List<Dictionary<string, double>>();

            //for each query
            for (int i = 0; i < queries.nQuery(); i++)
            {
                List<Docvalue> result = new List<Docvalue>();
                List<WeightedTermQuery> queryWithWeight = new List<WeightedTermQuery>();
                queryWithWeight = weightingQuery(queries.getQuery(i), ListDocuments);

                lQueryWeightOld.Add(queryWithWeight);   // add list of query

                // initialize list new query, same as list old query
                List<WeightedTermQuery> lQueryWeight = new List<WeightedTermQuery>();
                foreach (var item in queryWithWeight)
                {
                    WeightedTermQuery queryWeight = new WeightedTermQuery(item.term, item.weight);
                    lQueryWeight.Add(queryWeight);
                }
                lQueryWeightNew.Add(lQueryWeight);

                // initialize dictionary new query
                Dictionary<string, double> dQueryWeight = new Dictionary<string, double>();
                foreach(var item in queryWithWeight)
                {
                    if(!dQueryWeight.ContainsKey(item.term))
                    {
                        dQueryWeight.Add(item.term, item.weight);
                    }
                }
                lDQueryWeightNew.Add(dQueryWeight);
                //Console.WriteLine("query ke : " + i);
                Similarity sim = new Similarity(queryWithWeight);
                result = sim.calculateDocumentsValue();
                result = result.OrderByDescending(o => o.val).ToList();
                if (k != -1) //-1 kalau hasil diretrieve semua
                {
                    result = result.Take(k).ToList();
                }
                allResults.Add(result);
            }

            //print hasil pencarian to console
            /*
            Console.WriteLine("RESULT : ");
            for (int i = 0; i < allResults.Count(); i++)
            {
                Console.WriteLine("result for query" + i);
                for (int j = 0; j < allResults.ElementAt(i).Count(); j++)
                {
                    Console.Write(allResults[i][j].docNum);
                    Console.Write("-");
                    Console.Write(allResults[i][j].val);
                    Console.Write("\n");
                }
            }*/

            //print hasil ke file

            /*
            string outputResult = "D:/SearchResult.txt";
            string line;
            using (StreamWriter writer = new StreamWriter(outputResult))
            {
                //Console.WriteLine("jumlah allresult count " + allResults.Count());
                for (int i = 0; i < allResults.Count(); i++)
                {
                    //Console.WriteLine("jumlah result count " + i + (" : ") + allResults.ElementAt(i).Count());
                    for (int j = 0; j < allResults.ElementAt(i).Count(); j++)
                    {
                        line = i + 1 + " ";
                        line = line + allResults[i][j].docNum;
                        writer.WriteLine(line);
                    }
                }
            }*/
            Console.WriteLine("Selesai!!");
            Console.ReadLine();
        }
 public static void reCalculateSimilarity(int k)
 {
     //Program.allResults = new List<List<Docvalue>>();
     Program.allResults.Clear();
     for (int i = 0; i < Program.lQueryWeightNew.Count; i++)
     {
         List<Docvalue> result = new List<Docvalue>();
         Similarity sim = new Similarity(Program.lQueryWeightNew[i]);
         result = sim.calculateDocumentsValue();
         result = result.OrderByDescending(o => o.val).ToList();
         /*if (k != -1) //-1 kalau hasil diretrieve semua
         {
             result = result.Take(k).ToList();
         }*/
         Program.allResults.Add(result);
     }
 }