Re:How to Write a Spelling Corrector
by kough on Thursday September 08, 2016 @12:56PM
(#12454764)
You can also use the set-comprehension syntax, which functions equivalently but looks slightly nicer:
could have also directly used the intersection operator on the sets:
or slightly more verbose, but maybe more clear for colleagues who don't regularly use sets in Python:
[ Parent
| on HN ]
Re:How to Write a Spelling Corrector
by abecedarius on Thursday September 08, 2016 @01:34PM
(#12455157)
> return words & WORDS
Not quite. It'd have to be set(WORDS) instead of WORDS -- which'd be expensive. Or WORDS.keys(), which I'm not sure about -- I'd have to benchmark it.
You would only need to do set(WORDS) once at the beginning of the program, so its amortized cost is low.
I wouldn't worry about the expense of set construction vs the expense of an n-squared algorithm.
caught me, I hadn't read through the article, wasn't sure whether or not WORDS was a set or a dict. Like desdiv points out, this isn't a big deal, you can maintain an equivalent WORDS structure that is a set.