11

将模式与一组字符串逐个进行比较,同时评估模式与每个字符串匹配的数量的最佳方法是什么?在我对正则表达式的有限经验中,使用正则表达式将字符串与模式匹配似乎是一个非常二元的操作......无论模式多么复杂,最终,它要么匹配要么不匹配。我正在寻找更强大的功能,而不仅仅是匹配。是否有与此相关的良好技术或算法?

这是一个例子:

假设我有一个模式foo bar,我想从以下字符串中找到最匹配它的字符串:

foo for
foo bax
foo buo
fxx bar

现在,这些都不匹配模式,但哪个不匹配最接近匹配?在这种情况下,foo bax将是最佳选择,因为它匹配 7 个字符中的 6 个。

抱歉,如果这是一个重复的问题,当我查看这个问题是否已经存在时,我真的不知道要搜索什么。

4

2 回答 2

3

这个有效,我检查了维基百科的例子distance between "kitten" and "sitting" is 3

   public class LevenshteinDistance {

    public static final String TEST_STRING = "foo bar";

    public static void main(String ...args){
        LevenshteinDistance test = new LevenshteinDistance();
        List<String> testList = new ArrayList<String>();
        testList.add("foo for");
        testList.add("foo bax");
        testList.add("foo buo");
        testList.add("fxx bar");
        for (String string : testList) {
          System.out.println("Levenshtein Distance for " + string + " is " + test.getLevenshteinDistance(TEST_STRING, string)); 
        }
    }

    public int getLevenshteinDistance (String s, String t) {
          if (s == null || t == null) {
            throw new IllegalArgumentException("Strings must not be null");
          }

          int n = s.length(); // length of s
          int m = t.length(); // length of t

          if (n == 0) {
            return m;
          } else if (m == 0) {
            return n;
          }

          int p[] = new int[n+1]; //'previous' cost array, horizontally
          int d[] = new int[n+1]; // cost array, horizontally
          int _d[]; //placeholder to assist in swapping p and d

          // indexes into strings s and t
          int i; // iterates through s
          int j; // iterates through t

          char t_j; // jth character of t

          int cost; // cost

          for (i = 0; i<=n; i++) {
             p[i] = i;
          }

          for (j = 1; j<=m; j++) {
             t_j = t.charAt(j-1);
             d[0] = j;

             for (i=1; i<=n; i++) {
                cost = s.charAt(i-1)==t_j ? 0 : 1;
                // minimum of cell to the left+1, to the top+1, diagonally left and up +cost                
                d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1),  p[i-1]+cost);  
             }

             // copy current distance counts to 'previous row' distance counts
             _d = p;
             p = d;
             d = _d;
          } 

          // our last action in the above loop was to switch d and p, so p now 
          // actually has the most recent cost counts
          return p[n];
        }

}
于 2010-11-05T16:54:19.247 回答
0

这是一个有趣的问题!首先想到的是正则表达式的匹配方式是构建一个DFA。如果您可以直接访问为给定正则表达式构建的 DFA (或者自己构建它!),您可以使用最短路径作为度量,运行输入测量从您转换到的最后一个状态到接受状态的距离关于它与被接受的距离有多近,但我不知道有任何图书馆可以让你轻松做到这一点,而且在许多情况下,即使这个衡量标准也可能无法完全映射到你的直觉上。

于 2010-11-05T15:15:15.230 回答