Exemple #1
0
        public void SearchIn(string tagAddingSub)
        {
            conn.Open();
            List <string>            gram     = new List <string>();
            Dictionary <int, string> nameFile = new Dictionary <int, string>();
            Dictionary <int, double> resCode  = new Dictionary <int, double>();
            Analysis search    = new Analysis();
            bool     isDel     = false;
            string   mainCode  = "";
            string   childCode = "";
            double   maxRes    = Double.MinValue;

            using (command = new SqlCommand("select [file].id from [file] join [submit] on [file].id_submit = [submit].id where [submit].tag = @Tag", conn))
            {
                command.Parameters.Add(new SqlParameter("@Tag", tagAddingSub));
                using (IDataReader r = command.ExecuteReader())
                {
                    if (r.Read())
                    {
                        idMainFileForHist = r.GetInt32(0);
                    }
                }
            }

            using (command = new SqlCommand(
                       "select [gram].gram from [gram] join [kgrmams] on [kgrmams].id_gram = [gram].id join [code] on [kgrmams].id_code = [code].id join [file] on [file].id_code = [code].id where [file].id = @id", conn))
            {
                command.Parameters.Add(new SqlParameter("@id", idMainFileForHist));
                using (IDataReader r = command.ExecuteReader())
                {
                    while (r.Read())
                    {
                        gram.Add(r.GetString(0));
                    }
                }
            }


            for (int i = 0; i < gram.Count / 2; i++)
            {
                using (command = new SqlCommand(
                           "select DISTINCT [file].name, [file].id from [file] join [code] on [file].id_code = [code].id join [kgrmams] on [kgrmams].id_code = [code].id " +
                           "join [gram] on [kgrmams].id_gram = [gram].id where [gram].gram = @gram and [file].id != @idFile", conn))
                {
                    command.Parameters.Add(new SqlParameter("@gram", gram[i]));
                    command.Parameters.Add(new SqlParameter("@idFile", idMainFileForHist));
                    using (IDataReader r = command.ExecuteReader())
                    {
                        while (r.Read())
                        {
                            if (!nameFile.ContainsKey(r.GetInt32(1)) && isDel && nameFile.Count > 1)
                            {
                                nameFile.Remove(r.GetInt32(1));
                            }
                            else if (!isDel)
                            {
                                nameFile.Add(r.GetInt32(1), r.GetString(0));
                            }
                        }
                    }
                }
                isDel = true;
            }
            using (command = new SqlCommand(
                       "select [code].normalizecode from [code] join [file] on [file].id_code = [code].id where [file].id = @id", conn))
            {
                command.Parameters.Add(new SqlParameter("@id", idMainFileForHist));
                using (IDataReader r = command.ExecuteReader())
                {
                    if (r.Read())
                    {
                        mainCode += r.GetString(0);
                    }
                }
            }
            search.SetCodeMainFromDB(mainCode);
            foreach (int key in nameFile.Keys)
            {
                using (command = new SqlCommand(
                           "select [code].normalizecode from [code] join [file] on [file].id_code = [code].id where [file].id = @id", conn))
                {
                    command.Parameters.Add(new SqlParameter("@id", key));
                    using (IDataReader r = command.ExecuteReader())
                    {
                        if (r.Read())
                        {
                            childCode += r.GetString(0);
                        }
                    }
                }
                search.SetCodeChildFromDB(childCode);
                resCode.Add(key, ((search.ResultAlgorithm(0) + search.ResultAlgorithm(1) + search.ResultAlgorithm(2)) / 3));
            }

            foreach (int Key in resCode.Keys)
            {
                if (resCode[Key] > maxRes)
                {
                    maxRes         = resCode[Key];
                    idiDenticalFie = Key;
                }
            }
            conn.Close();
        }