예제 #1
0
        public List <CorrectionCandidate> GetCandidates(string spName, int key, string error, string root, string prefix, string suffix, int minSameBigramAmount, int minLengthVariant, int maxLevensthein, out string log)
        {
            // sample: call getCandidates('depat',2,0,1);
            log = "";
            List <CorrectionCandidate> lsCandidates = new List <CorrectionCandidate>();
            MySqlConnection            conn         = new MySqlConnection();

            conn.ConnectionString = MariaDBConn;
            MySqlCommand cmd = new MySqlCommand();

            try
            {
                conn.Open();
                cmd.Connection  = conn;
                cmd.CommandText = spName;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Word", root);
                cmd.Parameters["@Word"].Direction = ParameterDirection.Input;
                cmd.Parameters.AddWithValue("@MinSameBigramAmount", minSameBigramAmount);
                cmd.Parameters["@MinSameBigramAmount"].Direction = ParameterDirection.Input;
                cmd.Parameters.AddWithValue("@MinLengthVariant", minLengthVariant);
                cmd.Parameters["@MinLengthVariant"].Direction = ParameterDirection.Input;
                cmd.Parameters.AddWithValue("@MaxLevensthein", maxLevensthein);
                cmd.Parameters["@MaxLevensthein"].Direction = ParameterDirection.Input;
                cmd.Parameters.AddWithValue("@IsLemma", 1);
                cmd.Parameters["@IsLemma"].Direction = ParameterDirection.Input;
                MySqlDataReader dataReader = cmd.ExecuteReader();

                while (dataReader.Read())
                {
                    string  stem        = dataReader["Unigram"].ToString();
                    Affixer affixer     = new Affixer();
                    string  sCandidate  = affixer.Affixing(stem, prefix, suffix);
                    int     levensthein = EditDistance.LevenshteinDistance(sCandidate, error, 2);
                    if (levensthein != -1 && levensthein <= 2)
                    {
                        CorrectionCandidate candidate = new CorrectionCandidate
                        {
                            Key              = key,
                            Error            = error,
                            Candidate        = sCandidate,
                            SameBigramAmount = Convert.ToInt32(dataReader["SameBigramAmount"]),
                            Frequency        = 0,
                            LengthDifference = Convert.ToInt32(dataReader["LengthDifference"]),
                            Levensthein      = levensthein
                        };
                        lsCandidates.Add(candidate);
                    }
                }
                //close Data Reader
                dataReader.Close();
            }
            catch (MySql.Data.MySqlClient.MySqlException ex)
            {
                throw new Exception(ex.Message);
            }
            conn.Close();

            // Find Frequency then Update to list:
            List <string> sCandidates = new List <string>();;

            foreach (CorrectionCandidate cc in lsCandidates)
            {
                sCandidates.Add(cc.Candidate);
            }
            Dictionary <string, int> dicCandidateAndFreq = GetFrequencies(sCandidates.ToArray());

            foreach (CorrectionCandidate candidate in lsCandidates)
            {
                if (dicCandidateAndFreq.ContainsKey(candidate.Candidate))
                {
                    candidate.Frequency = dicCandidateAndFreq[candidate.Candidate];
                }
                log += candidate.Candidate + "," + candidate.Levensthein + "," + candidate.Frequency.ToString() + ";";
            }

            log = string.Format("[{0},{1},{2}][{3}]", minSameBigramAmount, minLengthVariant, maxLevensthein, log);

            return(lsCandidates);
        }