I want to "implode" URLs, that is shorten them by replacing long and/or frequently seen substrings with single non-visible ASCII chars.
Essentially, the code I have is:
my $tnt = [
"http://www." ,
"https://www." ,
};
my $implode_url = sub {
my ($url) = @_;
my $len = scalar(@$tnt);
for( my $i = 0 ; $i < $len; $i++) {
$url =~ s/$tnt->[$i]/chr($i+1)/eg; # +1 to avoid \0
}
return $url;
};
What I'm looking to do is, given a large list of URLs find the top 31 (chr(1) to chr(31))
most valueable substrings to populate
$tnt.
Obviously, the most valuable is a measure of the substring's length x frequency within the collection.
So, given a collection of URLs (one per line), does anyone have a suggested algorithm outside of brute force (pseudo-code below)?
while( $u = <STDIN> ) {
for( $i=1; $i < length($u)-1; $i++ ) {
for ($j = 0; $j < length($u)-$i ; $j++ ) {
$s = substr($u, $j, $i)
$matches($s) += ( $u =~ m/$s/g );
}
}
# now loop over %matches finding top 30 of:
# $matches($key) * length($key)
#
Thanks in advance!
mG.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.