ruby - 给定大量 URL,将 URL 分组为模式或正则表达式的最佳数据挖掘方法是什么?

标签 ruby pattern-matching machine-learning data-mining information-retrieval

我有一个包含 100 万个 URL 的列表,我想将相似的 URL 聚集在一起。该过程的输出将是一个正则表达式或模式列表。理想情况下,我想使用 Ruby 来派生数据。我最初的想法是使用机器学习分类器,但我不确定从哪里开始或使用哪种数据挖掘技术。

可能的例子:

输入:

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

输出:

http://www\.example\.com/folder-A/[a-z]\.html
http://www\.example\.com/folder-[A-C]/[-a-z]\.html

最佳答案

这个程序:

#!/usr/bin/env perl

use 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

这就是你要找的吗?

关于ruby - 给定大量 URL,将 URL 分组为模式或正则表达式的最佳数据挖掘方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7182224/

相关文章:

ruby-on-rails - Rails : set a model attribute as read-only, 但允许通过方法更新

machine-learning - LIBSVM 过拟合

r - 错误 : *** line 1 of `undefined.cases' : bad value of . .. 属性

regex - PostgreSQL 正则表达式

C# 模式匹配数组

java - 正则表达式: exclusion in character class

python - 如何用python中的另一个值替换pandas数据框中的对象

ruby - 使用 JRuby 在 Windows 上编写 Unix 换行符

ruby-on-rails - Ruby on Rails 取消首字母大写

ruby - 显示错误的模式