Here's my source (Perl 5.10) to provide a simple harness for using the module. (Errr... on gist, because it's a Perl 6 syntax highlighter, not a Perl 5 syntax highlighter.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use STD; | |
use Syntax::Highlight::Perl6; | |
my @input = <>; | |
my $input = join "", @input; | |
# Creates the Perl6 syntax highlighter object | |
my $p = Syntax::Highlight::Perl6->new( | |
text => $input | |
); | |
# Prints html that can be embedded in your pages | |
print $p->snippet_html; |
And here's sample output, used on that spelling corrector script:
use v6;
my %dictionary;
slurp("big.txt").comb(/<alpha>+/).map({%dictionary{$_.lc}++});
sub edits($word) {
my @s = (^$word.chars).map({$word.substr(0, $_), $word.substr($_)});
my @deletes = @s.map(-> $a, $b { $a ~ $b.substr(1); });
my @transposes = @s.map(-> $a, $b { $a ~ $b.substr(0, 2).flip ~ $b.substr(2) if $b.chars > 1 });
my @replaces = @s.map(-> $a, $b {$a ~ ':' ~ $b.substr(1)});
my @inserts = (@s,$word,"").map(-> $a, $b {$a ~ ':' ~ $b});
return (@deletes, @transposes, @replaces, @inserts);
}
sub edit_list_to_regex(@el) {
any(@el.uniq>>.subst(':', '<alpha>', :g).map({ rx/ ^ $_ $ / }));
}
sub correct($word) {
return $word if (%dictionary{$word});
my $regex = edit_list_to_regex(edits($word));
my @candidates = %dictionary.keys.grep($regex);
if @candidates.elems == 0 {
$regex = edit_list_to_regex(edits($word).map({edits($_)}));
@candidates = %dictionary.keys.grep($regex);
}
return @candidates.max({%dictionary{$_} // 0});
}
correct("lary").say
Nice, eh?
I'm still working on the Nubs + SVG stuff. It's so close to doing exactly what I want, but I seem to be overworking Rakudo and causing it to croak randomly...
Updated: Errr... nice except for the part where the long lines get clipped. I need to figure out how to reformat this blog...
It looks fine in an rss feed, which is probably how most people will be reading this.
ReplyDeleteI am glad you find it useful :)
ReplyDelete