我有一百万个URL的列表,我想将相似的URL聚类在一起。处理的输出将是一系列正则表达式或模式。理想情况下,我希望使用Ruby来提取数据。我最初的想法是使用机器学习分类器,但我不知道从哪里开始,也不知道使用哪种数据挖掘技术。
可能的示例:
输入:
http://www.example.com/folder-A/file.htmlhttp://www.example.com/folder-A/dude.htmlhttp://www.example.com/folder-B/huh.htmlhttp://www.example.com/folder-C/what-ever.html
输出:
http://www\.example\.com/folder-A/[a-z]\.htmlhttp://www\.example\.com/folder-[A-C]/[-a-z]\.html
回答:
这个程序:
#!/usr/bin/env perluse strict;use warnings;# the following is a CPAN module requiring independent installation:use Regexp::Assemble;my @url_list = qw( http://www.example.com/folder-A/file.html http://www.example.com/folder-A/dude.html http://www.example.com/folder-B/huh.html http://www.example.com/folder-C/what-ever.html);my $asm = Regexp::Assemble->new;for my $url (@url_list) { $asm->add($url);}my $pat = $asm->re;for ($pat) { s/^.*?://; s/\)$//;}print "$pat\n";
运行后,正确输出了:
http://www.example.com/folder-(?:A/(?:dud|fil)e|C/what-ever|B/huh).html
这是您想要的结果吗?