Word break problem: Difference between revisions
Content added Content deleted
(→{{header|Javascript}}: Updated tokenTrees to match the Python version) |
(→Functional: improve.) |
||
Line 745: | Line 745: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Functional=== |
===Functional=== |
||
{{improve|Python|Reads like machine translated Haskell rather than idiomatic Python}} |
|||
The '''tokenTrees''' function recursively builds a tree of possible token sequences, using a list monad ('''concatMap''' with a function which returns its result wrapped in a list – an empty list where a parse has failed) to discard all branches which lead to dead ends. This allows us to return more than one possible word-break parse for a given lexicon and input string. (Searches for 'monadic parsing in Python' will yield references to more sophisticated uses of this general approach). |
The '''tokenTrees''' function recursively builds a tree of possible token sequences, using a list monad ('''concatMap''' with a function which returns its result wrapped in a list – an empty list where a parse has failed) to discard all branches which lead to dead ends. This allows us to return more than one possible word-break parse for a given lexicon and input string. (Searches for 'monadic parsing in Python' will yield references to more sophisticated uses of this general approach). |
||