Monday, November 9, 2009

Perl 6 Syntax Highlighting

Saw Syntax::Hightlight::Perl6 mentioned on #perl6 over the weekend. I've never been very happy with using gist to provide snippets, so I thought I'd give it a whirl. I was anticipating a struggle getting it installed from CPAN, but it worked on the first go on my 5.10.0 install.

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.)

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...

2 comments:

  1. It looks fine in an rss feed, which is probably how most people will be reading this.

    ReplyDelete