【问题标题】:How to find all overlapping phrases between two strings, in Java?如何在Java中找到两个字符串之间的所有重叠短语?
【发布时间】:2014-12-02 04:58:27
【问题描述】:

假设我有两个字符串

  1. 我喜欢鸡肉沙拉,这是我最喜欢的食物。

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

这里两个字符串之间的重叠短语是 - chicken, salad, chicken salad, food。

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

【问题讨论】:

  • 您可以在较短的字符串中创建单词的哈希,然后将第二个单词的每个单词与第一个单词进行比较,或者将每个单词插入哈希中,如果它找到一个使用它来显示它重叠
  • 我会标记短字符串并在长字符串中搜索。附带说明一下,您应该考虑使用额外的停用词列表来忽略搜索常用词,如 the、to、at、it 等,
  • 如果在较长的字符串中找到任何可能的较短字符串的子字符串,那将产生大量的标记
  • 是否有一个库可以对字符串进行标记化?对于第一个字符串,我假设部分标记化的结果将包括“鸡肉”、“鸡肉沙拉”、“最喜欢的食物”、“食物”等......
  • @MattCoubrough,我还希望在进行标记化时也应删除“I”、“the”、“my”等“噪音”词。

标签: java string string-matching


【解决方案1】:

你可以试试这样的:

**

List<String> al = new ArrayList<String>();
    String one = "I like chicken salad, it's my favorite food.";
    String result = one.replaceAll("[.,]","");
    String[] tokens = result.split(" ");
    String second = "This book contains tons of recipes on making all sorts of food, including cakes, chicken salad, etc.";
    System.out.println(result);
    for(int i=0;i<tokens.length;i++){
        if(second.indexOf(tokens[i])>=0){
            al.add(tokens[i]);
        }
    }
    System.out.println(al);
    }

**

【讨论】:

  • 这与@thinkinjava 解释的算法相同,它无法正常工作,因为它无法返回“鸡肉沙拉”...
【解决方案2】:

我尝试了这种方法。似乎足以满足您对 salad, chicken, chicken salad, food 重叠短语的需求。

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);
}

overlappingPhrases 集合包含[chicken salad, chicken, salad, food]

【讨论】:

    【解决方案3】:

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

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

    接下来你可以迭代short_words数组中的单词。检查每个单词是否在long_words数组中。但是时间复杂度太差了,为0(m * n)。 其次,我认为您可以使用哈希函数来做到这一点。

    【讨论】:

    • 但是蛮力算法不会返回“鸡肉沙拉”,而是返回“鸡肉”、“沙拉”……
    【解决方案4】:

    满足您要求的方法:

    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);
    } 
    

    输出:

    [chicken, chicken salad, salad, food]
    

    【讨论】:

    • 喜欢这个想法,虽然 O 复杂度似乎不低。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2022-01-19
    相关资源
    最近更新 更多