首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#中字符串与百分比的模糊匹配

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

Stack Overflow用户
提问于 2015-11-12 06:57:58
回答 2查看 2.3K关注 0票数 2

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

“跳过懒惰的狗”它有8个单词,我还有一些其他字符串,我必须将上面的字符串进行比较,这些字符串是:

  1. 这是与以上字符串不匹配的字符串.
  2. 快跳的棕色狐狸。
  3. 棕色狐狸跳过懒人。
  4. 飞快的棕色狐狸伏在狗身上。
  5. 狐狸跳过那只懒狗。
  6. 跳过了。
  7. 懒狗。

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

=8* 60 /100 (此处8为上述字符串的总字数,60为阈值)

= 4.8

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

  1. 快跳的棕色狐狸。
  2. 飞快的棕色狐狸伏在狗身上。
  3. 棕色狐狸跳过懒人。
  4. 狐狸跳过那只懒狗。

如何在c#中进行模糊匹配,请帮助我。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-12 07:44:51

我建议比较一下dictionarie_s,而不是_strings

  1. 如果句子中有相同的词呢?“狐狸跳过狗”
  2. 标点符号:句号、逗号等。
  3. 例如,“狐狸”等等。

所以实现

代码语言:javascript
复制
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));
}

你们提供的样本将是

代码语言:javascript
复制
  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);

报告是

代码语言:javascript
复制
  "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%
票数 6
EN

Stack Overflow用户

发布于 2015-11-12 07:15:40

正则表达式应该是这样的。

代码语言:javascript
复制
(\bWord1\b|\bWord2\b|\bWord3\b|\betc\b)

然后你只需数一数火柴,并将其与字数进行比较。

代码语言:javascript
复制
string sentence = "Quick Brown Fox Jumps over the lazy dog";
string[] words = sentence.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
Regex regex = new Regex("(" + string.Join("|", words.Select(x => @"\b" + x + @"\b"))) + ")", RegexOptions.IgnoreCase);


string input = "Quick Brown fox Jumps";
int threshold = 60;

var matches = regex.Matches(input);

bool isMatch = words.Length*threshold/100 <= matches.Count;

Console.WriteLine(isMatch);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33665871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档