2

I am working on a project that will compare two Strings and pick out similar phrases and words.

For example, read the two questions below.

Question 1: This novel's protagonist frequently eats at a café run by Celeste. It opens with the protagonist attending his mother's funeral and showing no signs of grief. For 10 points, Meursault shoots an Arab on the beach in what novel by Albert Camus?

Question 2: That character in this book jumps on a fire truck with his friend Emmanuel and often eats lunch at Celeste's. It starts at a funeral for the protagonist's mother, where he does not cry. For 10 points, name this novel in which Meursault is condemned to die for senselessly shooting the Arab, by Albert Camus.

I want the program to pick out "Celeste" and the fact that the novel "starts at a funeral". How would I accomplish this? I'm just looking for a few pointers to some resources. (I'm using Java).

Caroline Yi
  • 171
  • 1
  • 1
  • 6
  • 1
    Checkout the levenshtein algorithm, which is describe on this post: http://stackoverflow.com/questions/6087281/similarity-score-levenshtein – Julian Schmuckli Apr 21 '17 at 12:17
  • 1
    What you describe is called "Approximate String Matching" or "Fuzzy Searching". That may point you in the right direction :) https://en.wikipedia.org/wiki/Approximate_string_matching – Jeff Huijsmans Apr 21 '17 at 12:34

1 Answers1

0

Hope that will work for your case. Try it:

public void pringCompare(){

    String s = "This is a sample sentence.";
    String s2 = "This is not the previous, but similar";
    String[] words = s.split("\\s+");
    for (int i = 0; i < words.length; i++) {
        // You may want to check for a non-word character before blindly
        // performing a replacement
        // It may also be necessary to adjust the character class
        words[i] = words[i].replaceAll("[^\\w]", "");
    }

    List result = Arrays.asList(words).stream().filter(x->{
        System.out.println(s2+" "+x);
        return s2.contains(x);
        }).collect(Collectors.toList());
    result.forEach(System.out::println);
}
strash
  • 1,291
  • 2
  • 15
  • 29