Find common patterns across strings and group them based on the pattern

945 Views Asked by At

How to automatically extract the common characters or common string from a set of input strings? is there an algorithm that does this?

I am trying to figure out how to parse 1000 input strings and automatically create groups of string based on the largest matching patterns.

Is there a library in ruby which does this?

Sample Input

What is your name?
Who wrote this book?
Your name starts with ABC
Is this book good?
Why is your name so long?
Have you read this book?



Expected Output.

your name
——————
What is your name?
Your name starts with ABC
Why is your name so long?

this book
————
Who wrote this book?
Have you read this book?
Is this book good?

Edited to clarify and fixed an error based on luqui's comment.

  1. The case doesn't matter.
1

There are 1 best solutions below

4
On

You can use core Ruby library:

["your name", "book"].map do |substring|
  [substring, text.lines.map(&:downcase).select { |line| line[substring] }]
end.to_h

# => {
#      "your name" => ["What is your name?", "Your name starts with ABC", ...],
#      "book" => [...]
#    }