在Java中,如何找到两个字符串之间的所有重叠短语?

在Java中,如何找到两个字符串之间的所有重叠短语?,java,string,string-matching,Java,String,String Matching,假设我有两个字符串 我喜欢鸡肉沙拉,它是我最喜欢的食物 这本书包含了大量制作各种食物的食谱,包括蛋糕、鸡肉沙拉等 在这里,两个字符串之间的重叠短语是-鸡肉、沙拉、鸡肉沙拉、食物 找到两个字符串之间重叠短语的最佳方法是什么?假设两者的语法和语义都是干净的,而且第一个总是比第二个短。首先,我认为可以使用蛮力算法。您可以在shor字符串中拼写单词,也可以在长字符串中拼写单词,如下所示: String short_words[] = short_string.spilt(" "); String lon

假设我有两个字符串

  • 我喜欢鸡肉沙拉,它是我最喜欢的食物

  • 这本书包含了大量制作各种食物的食谱,包括蛋糕、鸡肉沙拉等

  • 在这里,两个字符串之间的重叠短语是-鸡肉、沙拉、鸡肉沙拉、食物


    找到两个字符串之间重叠短语的最佳方法是什么?假设两者的语法和语义都是干净的,而且第一个总是比第二个短。

    首先,我认为可以使用蛮力算法。您可以在shor字符串中拼写单词,也可以在长字符串中拼写单词,如下所示:

    String short_words[] = short_string.spilt(" ");
    String long_words[] = long_string.spilt(" ");
    
    接下来,您可以对short_words数组中的单词进行迭代,并检查每个单词是否在long_words数组中。但是时间复杂度非常糟糕,为0(m*n)。
    其次,我认为您可以使用哈希函数来实现这一点。

    首先,我认为您可以使用蛮力算法。您可以在shor字符串中拼写单词,也可以在长字符串中拼写单词,如下所示:

    String short_words[] = short_string.spilt(" ");
    String long_words[] = long_string.spilt(" ");
    
    接下来,您可以对short_words数组中的单词进行迭代,并检查每个单词是否在long_words数组中。但是时间复杂度非常糟糕,为0(m*n)。
    其次,我认为您可以使用哈希函数来执行此操作。

    您可以尝试以下操作:

    String short_words[] = short_string.spilt(" ");
    String long_words[] = long_string.spilt(" ");
    
    **

    List al=new ArrayList();
    String one=“我喜欢鸡肉沙拉,这是我最喜欢的食物。”;
    字符串结果=1.replaceAll(“[,]”,“”);
    String[]tokens=result.split(“”);
    String second=“这本书包含了大量制作各种食物的食谱,包括蛋糕、鸡肉沙拉等”;
    系统输出打印项次(结果);
    对于(int i=0;i=0){
    新增(代币[i]);
    }
    }
    系统输出打印项次(al);
    }
    

    **

    您可以尝试以下方法:

    String short_words[] = short_string.spilt(" ");
    String long_words[] = long_string.spilt(" ");
    
    **

    List al=new ArrayList();
    String one=“我喜欢鸡肉沙拉,这是我最喜欢的食物。”;
    字符串结果=1.replaceAll(“[,]”,“”);
    String[]tokens=result.split(“”);
    String second=“这本书包含了大量制作各种食物的食谱,包括蛋糕、鸡肉沙拉等”;
    系统输出打印项次(结果);
    对于(int i=0;i=0){
    新增(代币[i]);
    }
    }
    系统输出打印项次(al);
    }
    

    **满足您需求的方法:

    public static void overlappingPhrases() {
        List<String> list = new ArrayList<>();
        String string1 = "I like chicken salad, it's my favorite food.";
        String string2 = "This book contains tons of recipes on making all sorts of food, including cakes, chicken salad, etc.";
        String[] words = string1.replaceAll("[.,]","").split(" ");
        System.out.println(string1+"\n"+string2);
        for(int i=0;i<words.length;i++){
            if(string2.indexOf(words[i])>=0){
                list.add(words[i]);     
                int j=i;
                String tmp=words[i];
                while(j+1<words.length){
                    if(string2.indexOf(tmp + " " + words[++j])>=0)
                       tmp = tmp + " " + words[j]; 
                    else {
                        if (!tmp.equals(words[i]))
                            list.add(tmp);                         
                        break;
                    }
                }                        
             }                            
        }
        System.out.println("Overlapping phrases: "+list);
    } 
    

    满足您需求的方法:

    public static void overlappingPhrases() {
        List<String> list = new ArrayList<>();
        String string1 = "I like chicken salad, it's my favorite food.";
        String string2 = "This book contains tons of recipes on making all sorts of food, including cakes, chicken salad, etc.";
        String[] words = string1.replaceAll("[.,]","").split(" ");
        System.out.println(string1+"\n"+string2);
        for(int i=0;i<words.length;i++){
            if(string2.indexOf(words[i])>=0){
                list.add(words[i]);     
                int j=i;
                String tmp=words[i];
                while(j+1<words.length){
                    if(string2.indexOf(tmp + " " + words[++j])>=0)
                       tmp = tmp + " " + words[j]; 
                    else {
                        if (!tmp.equals(words[i]))
                            list.add(tmp);                         
                        break;
                    }
                }                        
             }                            
        }
        System.out.println("Overlapping phrases: "+list);
    } 
    

    我试过这种方法。似乎满足了你对沙拉、鸡肉、鸡肉沙拉、食物的需求,就像重叠的短语一样

    public static void main(String a[]) throws IOException{
        String firstSentence = "I like chicken salad, it's my favorite food";
        String secondSentence = "This book contains tons of recipes on making all sorts of food, including cakes, chicken salad, etc";
        String[] firstSentenceWords = firstSentence.replaceAll("[.,]", "").split(" ");
        Set<String> overlappingPhrases = new HashSet<String>();     
        String lastPhrase = "";     
        for(String word : firstSentenceWords){
            if(lastPhrase.isEmpty()){
                lastPhrase = word;
            }else{
                lastPhrase = lastPhrase + " " + word;
            }
            if(secondSentence.contains(word)){
                overlappingPhrases.add(word);
                if(secondSentence.contains(lastPhrase)){
                    overlappingPhrases.add(lastPhrase);
                }
            }else{
                lastPhrase = "";
            }
        }
        System.out.println(overlappingPhrases);
    }
    
    publicstaticvoidmain(字符串a[])引发IOException{
    String first句子=“我喜欢鸡肉沙拉,这是我最喜欢的食物”;
    String second句子=“本书包含大量制作各种食物的食谱,包括蛋糕、鸡肉沙拉等”;
    String[]firstSentenceWords=firstSession.replaceAll(“[,]”,“).split(“”);
    Set overlappingPhrases=new HashSet();
    字符串lastphase=“”;
    for(字符串字:firstSentenceWords){
    if(lastphase.isEmpty()){
    最后一个短语=单词;
    }否则{
    lastphase=lastphase+“”+单词;
    }
    if(第二句,包含(单词)){
    重叠短语。添加(单词);
    if(第二句话.包含(最后一句话)){
    重叠短语。添加(最后一个短语);
    }
    }否则{
    最后一句=”;
    }
    }
    System.out.println(重叠短语);
    }
    

    重叠短语
    集合包含
    [鸡肉沙拉,鸡肉,沙拉,食物]

    我尝试了这种方法。似乎满足了你对沙拉、鸡肉、鸡肉沙拉、食物的需求,就像重叠的短语一样

    public static void main(String a[]) throws IOException{
        String firstSentence = "I like chicken salad, it's my favorite food";
        String secondSentence = "This book contains tons of recipes on making all sorts of food, including cakes, chicken salad, etc";
        String[] firstSentenceWords = firstSentence.replaceAll("[.,]", "").split(" ");
        Set<String> overlappingPhrases = new HashSet<String>();     
        String lastPhrase = "";     
        for(String word : firstSentenceWords){
            if(lastPhrase.isEmpty()){
                lastPhrase = word;
            }else{
                lastPhrase = lastPhrase + " " + word;
            }
            if(secondSentence.contains(word)){
                overlappingPhrases.add(word);
                if(secondSentence.contains(lastPhrase)){
                    overlappingPhrases.add(lastPhrase);
                }
            }else{
                lastPhrase = "";
            }
        }
        System.out.println(overlappingPhrases);
    }
    
    publicstaticvoidmain(字符串a[])引发IOException{
    String first句子=“我喜欢鸡肉沙拉,这是我最喜欢的食物”;
    String second句子=“本书包含大量制作各种食物的食谱,包括蛋糕、鸡肉沙拉等”;
    String[]firstSentenceWords=firstSession.replaceAll(“[,]”,“).split(“”);
    Set overlappingPhrases=new HashSet();
    字符串lastphase=“”;
    for(字符串字:firstSentenceWords){
    if(lastphase.isEmpty()){
    最后一个短语=单词;
    }否则{
    lastphase=lastphase+“”+单词;
    }
    if(第二句,包含(单词)){
    重叠短语。添加(单词);
    if(第二句话.包含(最后一句话)){
    重叠短语。添加(最后一个短语);
    }
    }否则{
    最后一句=”;
    }
    }
    System.out.println(重叠短语);
    }
    

    重叠短语
    集合包含
    [鸡肉沙拉、鸡肉、沙拉、食物]

    您可以在较短的字符串中创建单词的散列,然后将第二个单词与第一个单词进行对比,或者只需将每个单词插入到哈希中,如果它找到一个单词,则使用该单词来表示它重叠。我将标记短字符串,并在长字符串中搜索。另一方面,你应该考虑使用一个附加的停用词列表来忽略常见的词,如,to,at,IT等,如果在更长的字符串中找到短字符串的任何可能的子串,那么就会产生一个HeluuVa的TokStube。对于第一个字符串,我假设部分标记化的结果将包括‘鸡肉’、‘鸡肉沙拉’、‘最喜欢的食物’、‘食物’等。@MattCoubrough,我还希望在进行标记化时,诸如‘I’、‘the’、‘my’等‘噪音’单词也应该被删除。您可以在较短的字符串中创建一个单词散列,然后对照第一个单词检查第二个单词,或者将每个单词插入散列,如果它找到一个单词,则使用该单词来显示它重叠。我将标记短字符串并在长字符串中搜索。另一方面,您应该考虑使用一个附加的停用词列表。