Friday, October 23, 2009

Spelling Corrector

I keep on seeing these marvelous Python scripts by Peter Norvig, which always make me want to translate them to Perl 6. Today it was his spelling corrector in 21 lines of code. It's a lovely piece of code; mine is a fairly direct Perl 6 version. I fudged a little by dropping functions that seemed fairly pointless, and I don't recommend uncommenting the second @candidates = line unless you have a near infinite amount of patience. I haven't tested it exhaustively, but it does work for my simple tests.

Even with two closing braces getting a line of their own, it's shorter than the Python version. My feeling is it is uglier than his version. On the other hand, I'm sure it's not the best possible Perl 6 version. I encourage people to take a stab at improving it!

It feels to me like this code would be much better if it directly implemented the modifications to the string using left-hand side substr rather than Norvig's trick of splitting the word and working with the split versions. Unfortunately as far as I can tell this is NYI in Rakudo. Also substr seems awkward next to the Python equivalent using [x:y].

I felt the interactions between the functional style code and the hash code were uncomfortable. I suspect there is a Perl 6 idiom here that I just don't know yet; the language has to be more elegant than my clumsy attempts here. I'm not wildly happy about the entire correct function, either.

So, does anyone have suggested improvements?

No comments:

Post a Comment

Post a Comment