在字符串中找到常见的模式,并根据模式将其分组 [英] Find common patterns across strings and group them based on the pattern

查看:107
本文介绍了在字符串中找到常见的模式,并根据模式将其分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一组输入字符串中自动提取公用字符或公用字符串?

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

我正在尝试弄清楚如何解析1000个输入字符串并根据最大的匹配模式自动创建字符串组。

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

在ruby中有一个图书馆吗?

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?

根据luqui的评论进行澄清和修正错误。

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


  1. 大小写无关紧要。


推荐答案

您可以使用核心Ruby库:

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" => [...]
#    }

这篇关于在字符串中找到常见的模式,并根据模式将其分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆