【问题标题】:c# finding different words in two texts [closed]c#在两个文本中查找不同的单词[关闭]
【发布时间】:2017-04-18 12:31:55
【问题描述】:

我需要帮助。 我需要找到在text1中但不在text2中的单词,还要计算它们出现的次数。

例子:

Text1(你好,世界苹果,菠萝,卷心菜,苹果)

Text2(你好,世界,菠萝)

结果:

苹果 2;

卷心菜1;

如果没有 List 也很棒

【问题讨论】:

  • 为什么“世界苹果”对“苹果”的计数有贡献?

标签: c# arrays


【解决方案1】:

您可以使用两个数组,然后使用Group By,您可以通过这种方式实现您的目标:

    string[] text1 = new []{"hello", "world", "apple", "pineapple", "cabbage", "apple"};
    string[] text2 = new []{"apple", "pineapple", "cabbage", "apple"};

    string[] combinedText = text1.Concat(text2).ToArray();
    var groups = combinedText.GroupBy(v => v);

    foreach(var group in groups)
        Console.WriteLine("Value {0} has {1} items", group.Key, group.Count());

编辑:

看起来你想要的解决方案略有不同,所以我也在下面展示:

    string[] text1 = new []{"hello", "world", "apple", "pineapple", "cabbage", "apple"};
    string[] text2 = new []{"apple", "pineapple", "cabbage", "apple"};

    var text1Groups = text1.GroupBy(v => v);
    var text2Groups = text2.GroupBy(v => v);

    foreach(var group in text1Groups)
        Console.WriteLine(group.Key.ToString() + group.Count().ToString());

    foreach(var group in text2Groups)
        Console.WriteLine(group.Key.ToString() + group.Count().ToString());

【讨论】:

  • 反对票?为什么投反对票?
【解决方案2】:
string text1 = "hello, world apple,pineapple,cabbage,apple";
string text2 = "hello, world,pineapple";

string pattern = @"\p{L}+";

var list1 = Regex.Matches(text1, pattern).Cast<Match>().Select(x => x.Value);
var list2 = Regex.Matches(text2, pattern).Cast<Match>().Select(x => x.Value);


var result =   list1.Where(x => !list2.Contains(x))
                .GroupBy(x => x)
                .Select(x =>new
                {
                    Word = x.Key,
                    Count= x.Count()
                })
                .ToList();

这将返回

Word = apple,   Count = 2
Word = cabbage, Count = 1

当然还有一些性能改进的空间,但为了清楚起见,我们将把它们排除在外......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2013-04-29
    • 2016-04-07
    • 2016-10-12
    • 2022-01-03
    • 2011-12-17
    • 1970-01-01
    相关资源
    最近更新 更多