private void button3_Click(object sender, EventArgs e)
        {
            //bbl = new BoardBoolean(ListDocuments);
            //bbl.CreateBoardVobu();
            //bbl.CreateBoardDictionary();

            var dt = new DataTable();

            dt.Columns.Add("Từ Khóa");
            dt.Columns.Add("Chỉ số tài liệu");
            dt.Columns.Add("Tần số");
            dt.Columns.Add("TF");
            dt.Columns.Add("IDF");
            dt.Columns.Add("TF * IDF");
            foreach (var tuple in bbl.GetDictionList())
            {
                dt.Rows.Add(tuple.Key, tuple.Value.Item1, tuple.Value.Item2, "", tuple.Value.Item3.ToString(CultureInfo.InvariantCulture));
                foreach (var variable in tuple.Value.Item4)
                {
                    dt.Rows.Add("", variable.Item1, variable.Item2, variable.Item3.ToString(CultureInfo.InvariantCulture), "", variable.Item4.ToString(CultureInfo.InvariantCulture));
                }
            }
            var dicAndPosting = new DicAndPosting(dt);

            dicAndPosting.Show();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            var bbl = new BoardBoolean(ListDocuments);

            bbl.CreateBoardVobu();
            var lstBoard = bbl.Vobu;

            foreach (var tuple in lstBoard)
            {
                Console.WriteLine(tuple.Item1 + "\t" + tuple.Item2 + "\t" + tuple.Item3);
            }

            Console.WriteLine("-------------------------------------------------------");

            bbl.CreateBoardDictionary();
            foreach (var tuple in bbl.GetDictionList())
            {
                Console.WriteLine(tuple.Key + "\t" + tuple.Value.Item1 + "\t" + tuple.Value.Item2);
                foreach (var variable in tuple.Value.Item4)
                {
                    Console.WriteLine("\t" + variable.Item1 + "\t" + variable.Item2);
                }
            }
        }
Esempio n. 3
0
        public List <KeyValuePair <string, double> > FileResult()
        {
            var dic = _boolean.GetDictionList();
            var lst = Query.Lstq;
            //tên từ, Tuple<chỉ số tài liêu, tần số, idf.<List<tuple<tài liệu,tần số,tf,tf*idf>>>
            IDictionary <string, Tuple <int, int, double, List <Tuple <string, int, float, double> > > > listItem = new Dictionary <string, Tuple <int, int, double, List <Tuple <string, int, float, double> > > >();
            Tuple <int, int, double, List <Tuple <string, int, float, double> > > Item;
            var lstQuery = new List <Tuple <string, double> >();

            //query: tu, tf
            lst = lst.Distinct().ToList();
            foreach (var item in lst)
            {
                if (!dic.ContainsKey(item.Item1))
                {
                    continue;
                }
                Item = dic.First(a => a.Key == item.Item1).Value;
                //tu, tf*idf
                lstQuery.Add(new Tuple <string, double>(item.Item1, item.Item2 * dic[item.Item1].Item3));// tu ,tf
                listItem.Add(item.Item1, Item);
            }
            //tai lieu, tu, tf*idf
            var lstFile = (from item in listItem from subitem in item.Value.Item4
                           select new Tuple <string, string, double>(subitem.Item1, item.Key, subitem.Item4))
                          .OrderBy(a => a.Item1).ToList();
            // gop lai
            var dicFile = new Dictionary <string, List <Tuple <string, double> > >();

            foreach (var item in lstFile)
            {
                if (dicFile.ContainsKey(item.Item1) == true)
                {
                    dicFile[item.Item1].Add(new Tuple <string, double>(item.Item2, item.Item3));
                }
                else
                {
                    var tup = new Tuple <string, double>(item.Item2, item.Item3);
                    dicFile.Add(item.Item1, new List <Tuple <string, double> >(new Tuple <string, double>[] { tup }));
                }
            }
            var docTfidf = new Dictionary <string, double>();

            foreach (var item in dicFile)
            {
                var doDaiTaiLieu = Math.Sqrt(item.Value.Sum(a => Math.Pow(a.Item2, 2)));
                var file         = item.Value.ToList();
                var d            = 0.0;
                var x            = item.Value.Sum(subitem => lstQuery.FirstOrDefault(a => a.Item1 == subitem.Item1).Item2 *subitem.Item2);
                var doDaiQuery   = Math.Sqrt(lstQuery.Sum(e => Math.Pow(e.Item2, 2)));
                var c            = (float)((float)x / ((float)doDaiTaiLieu * (float)doDaiQuery));
                docTfidf.Add(item.Key, (double)c);
            }
            var listFileSortByFre = docTfidf.ToList();

            return(listFileSortByFre);

            //tai lieu,tu,tf*idf
            //new Dictionary<string,List<Tuple<string,double>>>();
            //var listFileSortByFre = dicFile.ToList().OrderByDescending(a => a.Value.Item2).ThenBy(a => a.Value.Item1).Where(a => a.Value.Item1 >= 3);
            //return listFileSortByFre;
        }