【问题标题】:How to match duplicate words from a paragraph using C# or jquery?如何使用 C# 或 jquery 匹配段落中的重复单词?
【发布时间】:2011-06-21 19:28:38
【问题描述】:

我想匹配两个字符串之间的常用词

// C# code  
string str1,str2;

我要检查字符串str1前十五个单词中的五个单词

str1="Musician Ben Sollee on the Ravages of Coal and the Wonders of the Bicycle"
str2="There is Wonders of Musician Ben Sollee on the Ravages of Coal"

我想跳过“of”、“on”、“the”等字符串中的动词比较。只检查没有动词的单词...

从上面的字符串我想比较 str2str1 如果它包含五个重复的单词然后给它包含一些重复单词的消息..

我如何比较它并检查它是否包含重复项。我对 jQuery 或 C# 答案感到满意。

【问题讨论】:

    标签: c# jquery


    【解决方案1】:

    单线 Linq 查询积分?

    string[] str1Words = ...
    string[] str2Words = ...
    string[] dontCheck = {"of", "a", "the"};
    
    var greaterThanFive = str1Words.Join(str2Words, s1 => s1, s2 => s2, (r1, r2) => r1)
                                   .Distinct()
                                   .Where(s => !dontCheck.Contains(s))
                                   .Count() > 5;
    

    【讨论】:

    • 任何 .NET 3.5 语言都支持这一点。只包括 system.linq
    • 你好@ColinE,如果我设置字符串 str1Words="Facebook 官方 iPad 应用即将登陆 App Store 官方 Facebook iPad 应用即将登陆 App Store #use";和 string[] str2Words="App Coming Soon to Official Facebook iPad the Store App Official" 然后它返回超过 5 个计数..
    • 查看您的测试字符串,您有重复的单词。查看更新的答案,我添加了 Distinct() 子句。使用您的测试数据,这两个字符串之间共有 8 个单词,这是正确的。
    • @Abhishek StackOverflow 是一个问答网站,它不是聊天室。如果你想聊天,试试chat.stackoverflow.com
    【解决方案2】:

    从每个字符串中获取单词:

    string[] str1Words = str1.split(" ");
    
    string[] str2Words = str2.split(" ");
    

    指定你不想检查的词:

    string[] dontCheck = {"of", "a", "the"}; // etc..
    

    看看有多少重复:

    string[] duplicates = Array.FindAll(
        str1Words, srt1word => 
            Array.Exists(str2Words, str1Word => string.Equals(str1word, str2word)) 
            && !Array.Exists(dontCheck, dontCheckWord => string.Equals(dontCheckWord, str1Word))
    );
    
    if(duplicates.length > 5)
    {
        // Give message
    }
    

    【讨论】:

    • 您好@lockstock 谢谢,我想尝试但 Array.Contains 不存在.. 会有什么问题?
    【解决方案3】:

    您可以尝试通过拆分“”将两个字符串拆分为单词列表。 然后简单地遍历指定的单词并检查第二个列表是否包含字符串。

    您还应该维护一个包含被忽略单词的列表或文件。

            List<string> str1List = new List<string>(str1.Split(' '));
            List<string> str2List = new List<string>(str2.Split(' '));
    
            foreach (string word in str1List)
            {
                if (str2List.Contains(word))
                {
                    //do something
                }
            }
    

    【讨论】:

    • 没问题...我猜我有点过时了... :(
    【解决方案4】:
            string str1 = "Musician Ben Sollee on the Ravages of Coal and the Wonders of the Bicycle";          
            string str2="There is Wonders of Musician Ben Sollee on the Ravages of Coal";
            string[] DontCheck = new string[]{"is", "of", "the"};
    
            List<string> List1 = new List<string>(str1.Split(' '));
            List<string> List2 = new List<string>(str2.Split(' '));
    
    
            var Result = ((from s in List1
                           where List2.Contains(s) && !DontCheck.Contains(s)
                          select s).Count() > 5);
    
            if (Result)
            {
                //It Contains some duplicate words
            } 
    

    更新了不同重复检查的代码

            string str1 = "Official Facebook iPad App Coming Soon to the App Store Official Facebook iPad";
            string str2 = "App Coming Soon to Official Facebook iPad the Store App Official App App App App";
    
            string[] DontCheck = new string[]{"is", "of", "the", "to"};           
    
            HashSet<string> Set = new HashSet<string>(new List<string>(str1.Split(' ')));
            HashSet<string> Set2 = new HashSet<string>(new List<string>(str2.Split(' ')));
    
            var Result = ((from s in Set
                           where Set2.Contains(s) && !DontCheck.Contains(s)
                          select s).Count() > 5);
    
            int result =Convert.ToInt32(Result);
    
            if (Result)
            {
               // It Contains more than 5 duplicate words
            }
    

    【讨论】:

    • @Syeda,当我尝试使用字符串 str1 的前 15 个单词并将整个字符串与 str2 进行比较时,它每次都返回超过 5 个
    • 你好@Syeda,如果我设置字符串 str1="Facebook 官方 iPad 应用即将登陆 App Store 官方 Facebook iPad 应用即将登陆 App Store #use";和 str2 = "App Coming Soon to Official Facebook iPad the Store App Official" 然后返回超过 5 个计数。
    • 对不起@Abhishek 我不明白你想说什么。你给出的字符串值包含超过 5 个重复项..
    • 你好@Syeda,我是从@ColinE 那里得到的。比较结果时需要添加 .Distinct
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多