Natural Language Disambiguator - a flexible parser for loosely ordered token sequences.
Nld parses sequences of tokens while:
Unlike traditional parser combinators, Nld is robust to reordering and
alternate phrasings, making it ideal for parsing natural language input.
Run a parser on TokenPositions and return all results.
Run a parser on a list of tokens and return all results as weighted pairs. Results are ordered by weight (best matches first).
runList (word "hello") [ "hello", "world" ]
|> List.map Tuple.second
--> [ "hello" ]
runList (tuple2 (word "delete") (word "file")) [ "file", "delete" ]
|> List.map Tuple.second
--> [ ( "delete", "file" ) ]Run a parser and take only the first n results.
runTake 1 (word "cat") [ "the", "cat", "sat" ]
--> [ ( 2, "cat" ) ]Match a specific word/token.
runList (word "delete") [ "please", "delete", "this" ]
--> [ ( 2, "delete" ) ]Match any of the given words, preferring earlier ones in the list. Useful for synonyms.
runList (words [ "delete", "remove", "erase" ]) [ "please", "erase", "this" ]
|> List.map Tuple.second
--> [ "delete" ]The result is canonicalized to the first word in the list.
Match any token and return it.
runTake 1 token [ "anything" ]
|> List.map Tuple.second
--> [ "anything" ]Match a natural number (non-negative integer).
runList (tuple2 (word "buy") nat) [ "buy", "3", "apples" ]
|> List.map Tuple.second
--> [ ( "buy", 3 ) ]Match tokens satisfying a predicate.
runList (tokenMatching (String.endsWith ".txt")) [ "open", "report.txt" ]
|> List.map Tuple.second
--> [ "report.txt" ]Match any of several weighted tokens. Lower weights are preferred.
runTake 1 (minimalToken [ ( 0.0, "delete" ), ( 1.0, "remove" ) ]) [ "remove", "delete" ]
|> List.map Tuple.second
--> [ "delete" ]These return both the matched value and its position in the input.
Match a specific word and return both the word and its position.
runList (indexedWord "cat") [ "the", "cat", "sat" ]
--> [ ( 2, ( "cat", 1 ) ) ]Match any of the given words and return the matched word with its position.
Match any token and return it with its position.
Match a natural number and return it with its position.
Match tokens satisfying a predicate and return with position.
A parser that always succeeds with the given value without consuming any tokens.
This is the fundamental building block for applicative-style parsing with andMap.
runList (succeed 42) [ "any", "tokens" ]
|> List.map Tuple.second
--> [ 42 ]
-- Use with choice for default values:
runList (choice [ nat, succeed 1 ]) [ "not-a-number" ]
|> List.map Tuple.second
--> [ 1 ]Transform the result of a parser.
runList (map String.toUpper (word "hello")) [ "hello" ]
--> [ ( 1, "HELLO" ) ]Sequence two parsers. The second parser can depend on the result of the first.
Apply a parser producing a function to a parser producing a value.
This enables pipe-style composition for building parsers with arbitrary arity:
runList
(succeed Tuple.pair
|> andMap (word "buy")
|> andMap nat
)
[ "buy", "3" ]
|> List.map Tuple.second
--> [ ( "buy", 3 ) ]Works with any number of fields - no need for map4, map5, etc:
runList
(succeed (\a b c -> { x = a, y = b, z = c })
|> andMap (word "a")
|> andMap (word "b")
|> andMap (word "c")
)
[ "a", "b", "c" ]
|> List.map Tuple.second
--> [ { x = "a", y = "b", z = "c" } ]Combine two parsers into a tuple.
runList (tuple2 (word "delete") (word "file")) [ "delete", "file" ]
|> List.map Tuple.second
--> [ ( "delete", "file" ) ]Try multiple parsers and return all successful parses.
runList (choice [ word "delete", word "add" ]) [ "delete" ]
|> List.map Tuple.second
--> [ "delete" ]Match zero or more occurrences of a parser.
Results include all possible match lengths, with longer matches preferred.
Get autocomplete suggestions as a Peach.
This walks the parse tree, collecting suggestions at every point where the parser fails due to missing tokens. The result is a lazy stream of suggestion sets, ordered by parse weight.
Get autocomplete suggestions for incomplete input. Returns a list of sets of tokens that could complete the parse.
topK 5 (word "buy") []
-- Returns [ Set.fromList [ "buy" ] ]
topK 5 (tuple2 (word "buy") (word "apples")) [ "buy" ]
-- Returns [ Set.fromList [ "apples" ] ]This explores the parse tree lazily, finding all points where the parser needs tokens that aren't present in the input.
An
Nld ais a parser that produces values of typeafrom a sequence of tokens. It explores multiple parse branches lazily, preferring matches where tokens appear in the specified order and closer together.