How to find all overlapping phrases between two strings, in Java? -


assume have 2 strings

  1. i chicken salad, it's favorite food.

  2. this book contains tons of recipes on making sorts of food, including cakes, chicken salad, etc.

here overlapping phrases between 2 strings - chicken, salad, chicken salad, food.

what's best way find overlapping phrases between 2 strings? assume both syntax , semantics clean, , first 1 quite shorter second one.

i tried approach. seems suffice need of salad, chicken, chicken salad, food overlapping phrases.

public static void main(string a[]) throws ioexception{     string firstsentence = "i chicken salad, it's favorite food";     string secondsentence = "this book contains tons of recipes on making 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 set contains [chicken salad, chicken, salad, food]


Comments

Popular posts from this blog

javascript - Wordpress slider, not displayed 100% width -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -