예제 #1
0
        /// <summary>
        /// 开始计算相似度(英文:一个单词是一个字符)
        /// </summary>
        /// <param name="baiduText"></param>
        /// <param name="orgs"></param>
        /// <param name="contrastText"></param>
        /// <param name="precent"></param>
        /// <param name="index"></param>
        private void RecursionYw(string baiduText, string[] orgs, out string contrastText, out double precent, int index)
        {
            contrastText = "";
            precent      = 0;
            bool is_break = false;

            for (int i = index; i < orgs.Length; i++)
            {
                if (Util.isNotNull(orgs[i]))
                {
                    contrastText += orgs[i];

                    Similar similar = TextContrast.GetStringSimilarityPerYw(baiduText, contrastText);
                    if (similar.success)
                    {
                        precent = similar.precent;
                        if (i < orgs.Length - 1)
                        {
                            for (int j = i + 1; j < orgs.Length; j++)
                            {
                                string next_contrastText = contrastText;
                                next_contrastText += orgs[j];
                                Similar next_similar = TextContrast.GetStringSimilarityPerYw(baiduText, next_contrastText);
                                if (next_similar.success)
                                {
                                    //增加容错率
                                    if (next_similar.precent > precent || (next_similar.precent <= precent && precent - next_similar.precent <= 0.5))
                                    {
                                        precent      = next_similar.precent;
                                        contrastText = next_contrastText;
                                    }
                                    else
                                    {
                                        //如果相似度最高连50%都达不到,那么就当没有匹配项
                                        if (precent <= 50.0)
                                        {
                                            //index = i;//重置
                                            //contrastText = "相似度太低,未找到匹配项";
                                            //如果相似度太低那么继续递归查询
                                            int _index = i + 1;
                                            RecursionYw(baiduText, orgs, out contrastText, out precent, _index);
                                        }
                                        else
                                        {
                                            //移除掉之前匹配的字符串
                                            en_index = j;
                                        }
                                        is_break = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    LogHelper.Error("字符串计算相似度百分比时出错:" + similar.error);
                                    break;
                                }
                            }
                            is_break = true;
                        }
                    }
                    else//出错了就不比较了
                    {
                        LogHelper.Error("字符串计算相似度百分比时出错:" + similar.error);
                        contrastText = "";
                        precent      = 0;
                        en_index     = i + 1;
                        break;
                    }

                    //
                    if (is_break)
                    {
                        break;
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 开始计算相似度(中文:一个汉字是一个字符)
        /// </summary>
        /// <param name="baiduText"></param>
        /// <param name="orgs"></param>
        /// <param name="contrastText"></param>
        /// <param name="precent"></param>
        /// <param name="index"></param>
        private void RecursionHz(string baiduText, string[] orgs, out string contrastText, out double precent, ref int index)
        {
            contrastText = "";
            precent      = 0;
            bool is_break = false;

            for (int i = index; i < orgs.Length; i++)
            {
                if (Util.isNotNull(orgs[i]))
                {
                    contrastText += orgs[i].Replace(" ", "");

                    Similar similar = TextContrast.GetStringSimilarityPerZw(baiduText, contrastText);
                    if (similar.success)
                    {
                        precent = similar.precent;
                        if (i < orgs.Length - 1)
                        {
                            for (int j = i + 1; j < orgs.Length; j++)
                            {
                                string next_contrastText = contrastText;
                                next_contrastText += orgs[j].Replace(" ", "");
                                Similar next_similar = TextContrast.GetStringSimilarityPerZw(baiduText, next_contrastText);
                                if (next_similar.success)
                                {
                                    if (next_similar.precent > precent)
                                    {
                                        precent      = next_similar.precent;
                                        contrastText = next_contrastText;
                                    }
                                    else
                                    {
                                        //移除掉之前匹配的字符串
                                        index    = j;
                                        is_break = true;
                                        break;
                                    }
                                }
                                else
                                {
                                    LogHelper.Error("字符串计算相似度百分比时出错:" + similar.error);
                                    break;
                                }
                            }
                            is_break = true;
                        }
                    }
                    else//出错了就不比较了
                    {
                        LogHelper.Error("字符串计算相似度百分比时出错:" + similar.error);
                        contrastText = "";
                        precent      = 0;
                        index        = i + 1;
                        break;
                    }

                    //
                    if (is_break)
                    {
                        break;
                    }
                }
            }
        }