随笔-43  评论-206  文章-0  trackbacks-11

查找匹配度最高的字符串 -- 代码

定义的一个主体类:

 public delegate decimal CompareCondition(String value);

    
public class StringCompare
    
{
        
private List<IntraString> intraList = new List<IntraString>();

        
public StringCompare(List<String> strlist)
        
{
            
for (int i = 0; i < strlist.Count; i++)
            
{
                intraList.Add(
new IntraString(strlist[i]));
            }

        }


        
public List<IntraString> Compare(CompareCondition condition)
        
{
            
for (int i = 0; i < intraList.Count; i++)
            
{
                intraList[i].Compare(condition);
            }

            intraList.Sort(
delegate(IntraString a, IntraString b)
            
{
                
if (a.CompareResult > b.CompareResult)
                
{
                    
return 1;
                }

                
else if (a.CompareResult == b.CompareResult)
                
{
                    
return 0;
                }

                
else
                
{
                    
return -1;
                }

            }

            );
            
return intraList;
        }

    }


    
public class IntraString
    
{
        
private String value;

        
public IntraString(String value)
        
{
            
this.value = value;
        }


        
public String Value
        
{
            
get return value; }
            
set this.value = value; }
        }


        
private decimal compareResult;

        
public decimal CompareResult
        
{
            
get return compareResult; }
            
set { compareResult = value; }
        }


        
public void Compare(CompareCondition condition)
        
{
            CompareResult 
= condition(Value);
        }

    }

比较规则的实现:
   /// <summary>
        
///定义比较的条件
        
/// </summary>
        
/// <param name="value"></param>
        
/// <returns></returns>

        private static decimal Compare(String value)
        
{
            
string str = Guid.NewGuid().ToString().Replace("-""");
            
int MaxLength = str.Length;
            
if (str.Length < value.Length)
            
{
                MaxLength 
= value.Length;
            }

            
bool find = false;
            
decimal sameCount = 0;
            
for (int i = 0; i < value.Length; i++)
            
{
                
string newStr = value.Substring(i, value.Length - i);
                
for (int j = newStr.Length - 1; j >= 0; j--)
                
{
                    
if (str.IndexOf(newStr.Substring(0,j+1)) >= 0)
                    
{
                        newStr 
= newStr.Substring(0, j + 1);
                        find 
= true;
                        
break;
                    }

                }

                
if (find)
                
{
                    sameCount 
= newStr.Length;
                    
break;
                }

            }

            
return sameCount / MaxLength;
        }
posted on 2006-12-06 20:43 tablefor2 阅读(948) 评论(1) 编辑 收藏

评论:
#1楼 2007-01-30 11:37 | skad[未注册用户]
高手真多!!
反醒一下!!!

 回复 引用   

搜索

 

随笔分类

Links

评论排行榜