c# - c#中字符串与百分比的模糊匹配

标签 c# regex string

<分区>

我的问题是 假设我有一个字符串:

"Quick Brown Fox Jumps over the lazy dog"它共有 8 个字 我还有一些其他字符串,我必须与上面的字符串进行比较 这些字符串是:

  1. 这是与上述字符串不匹配的字符串。

  2. 快速的棕色狐狸跳跃。

  3. 棕色的狐狸跳过懒惰的。

  4. 在狗身上迅速变成棕色的狐狸。

  5. 狐狸跳过懒狗。

  6. 跳过.

  7. 懒狗。

例如用户给出的阈值(匹配字符串的百分比)为 60% 这意味着

=8*60/100(这里8是上面字符串的总字数,60是阈值)

= 4.8

这意味着至少应该匹配 4 个单词,这意味着结果应该是

  1. 快速的棕色狐狸跳跃。

  2. 在狗身上迅速变成棕色的狐狸。

  3. 棕色的狐狸跳过懒惰的。

  4. 狐狸跳过懒狗。

我如何在 C# 中进行这种模糊匹配,请帮助我..

最佳答案

我宁愿建议比较字典,而不是字符串:

  1. 如果句子中有相同的词怎么办,例如“狐狸跳过狗”
  2. 标点符号:句号、逗号等
  3. 大小写,例如,“Fox”、“fox”、“FOX”等等。

所以实现

public static Dictionary<String, int> WordsToCounts(String value) {
  if (String.IsNullOrEmpty(value))
    return new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

  return value
    .Split(' ', '\r', '\n', '\t')
    .Select(item => item.Trim(',', '.', '?', '!', ':', ';', '"'))
    .Where(item => !String.IsNullOrEmpty(item))
    .GroupBy(item => item, StringComparer.OrdinalIgnoreCase)
    .ToDictionary(chunk => chunk.Key, 
                  chunk => chunk.Count(), 
                  StringComparer.OrdinalIgnoreCase);
}

public static Double DictionaryPercentage(
  IDictionary<String, int> left,
  IDictionary<String, int> right) {

  if (null == left)
    if (null == right)
      return 1.0;
    else
      return 0.0;
  else if (null == right)
    return 0.0;

  int all = left.Sum(pair => pair.Value);

  if (all <= 0)
    return 0.0;

  double found = 0.0;

  foreach (var pair in left) {
    int count;

    if (!right.TryGetValue(pair.Key, out count))
      count = 0;

    found += count < pair.Value ? count : pair.Value;
  }

  return found / all;
}

public static Double StringPercentage(String left, String right) {
  return DictionaryPercentage(WordsToCounts(left), WordsToCounts(right));
}

您提供的 sample 将是

  String original = "Quick Brown Fox Jumps over the lazy dog";

  String[] extracts = new String[] {
    "This is un-match string with above string.",
    "Quick Brown fox Jumps.",
    "brown fox jumps over the lazy.",
    "quick brown fox over the dog.",
    "fox jumps over the lazy dog.",
    "jumps over the.",
    "lazy dog.",
  };

  var data = extracts
    .Select(item => new {
      text = item,
      perCent = StringPercentage(original, item) * 100.0
    })
    //.Where(item => item.perCent >= 60.0) // uncomment this to apply threshold
    .Select(item => String.Format(CultureInfo.InvariantCulture, 
      "\"{0}\" \t {1:F2}%", 
      item.text, item.perCent));

  String report = String.Join(Environment.NewLine, data);

  Console.write(report);

报告是

  "This is un-match string with above string."   0.00%
  "Quick Brown fox Jumps."                      50.00%
  "brown fox jumps over the lazy."              75.00%
  "quick brown fox over the dog."               75.00%
  "fox jumps over the lazy dog."                75.00%
  "jumps over the."                             37.50%
  "lazy dog."                                   25.00%

关于c# - c#中字符串与百分比的模糊匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33665871/

相关文章:

regex - 如何将值作为正则表达式而不是字符串常量传递给重新匹配

c# - 如何拆分文本文件每一行包含多个分隔符的字符串?

c# - c# 中更简单的大加法器?

c# - 检查对象是否与给定列表中的任何类型匹配的替代方法

javascript - 否定此正则表达式/(\n.)/g

regex - 使用正则表达式的 Git 日志统计

java - 的解释?和 : operators

arrays - 查看另一个字符串中是否包含大量字符串的更快方法

c# - 如何处理 EntityDataSource 中的自连接?

c# - Selenium Webdriver + PhantomJS 保持在大约 :blank for a specific site