pete-murphy/elm-nlds/2.0.0

NOTE: the latest version is 3.0.1

Nld

Natural Language Disambiguator - a flexible parser for loosely ordered token sequences.

Nld parses sequences of tokens while:

  • Ignoring irrelevant tokens
  • Allowing tokens to appear in any order (preferring specified order)
  • Lazily producing results in priority order

Unlike traditional parser combinators, Nld is robust to reordering and alternate phrasings, making it ideal for parsing natural language input.

Running Parsers

type Nld a

An Nld a is a parser that produces values of type a from a sequence of tokens. It explores multiple parse branches lazily, preferring matches where tokens appear in the specified order and closer together.

run : Nld a -> TokenPositions -> List ( Float, a )

Run a parser on TokenPositions and return all results.

runList : Nld a -> List String -> List a

Run a parser on a list of tokens and return all results. Results are ordered by priority (best matches first).

runList (word "hello") [ "hello", "world" ]
--> [ "hello" ]


runList (tuple2 (word "delete") (word "file")) [ "file", "delete" ]
--> [ ( "delete", "file" ) ]
runTake : Int -> Nld a -> List String -> List a

Run a parser and take only the first n results.

runTake 1 (word "cat") [ "the", "cat", "sat" ]
--> [ "cat" ]

Token Matchers

word : String -> Nld String

Match a specific word/token.

runList (word "delete") [ "please", "delete", "this" ]
--> [ "delete" ]
words : List String -> Nld String

Match any of the given words, preferring earlier ones in the list. Useful for synonyms.

runList (words [ "delete", "remove", "erase" ]) [ "please", "erase", "this" ]
--> [ "delete" ]

The result is canonicalized to the first word in the list.

token : Nld String

Match any token and return it.

runTake 1 token [ "anything" ]
--> [ "anything" ]
nat : Nld Int

Match a natural number (non-negative integer).

runList (tuple2 (word "buy") nat) [ "buy", "3", "apples" ]
--> [ ( "buy", 3 ) ]
tokenMatching : (String -> Bool) -> Nld String

Match tokens satisfying a predicate.

runList (tokenMatching (String.endsWith ".txt")) [ "open", "report.txt" ]
--> [ "report.txt" ]
minimalToken : List ( Float, String ) -> Nld String

Match any of several weighted tokens. Lower weights are preferred.

runTake 1 (minimalToken [ ( 0.0, "delete" ), ( 1.0, "remove" ) ]) [ "remove", "delete" ]
--> [ "delete" ]

Indexed Token Matchers

These return both the matched value and its position in the input.

indexedWord : String -> Nld ( String, Int )

Match a specific word and return both the word and its position.

runList (indexedWord "cat") [ "the", "cat", "sat" ]
--> [ ( "cat", 1 ) ]
indexedWords : List String -> Nld ( String, Int )

Match any of the given words and return the canonical (first) word with its position.

Note: Like words, this canonicalizes the result to the first word in the list. For example, indexedWords ["hello", "hi"] with input ["hi"] returns [("hello", 0)].

indexedToken : Nld ( String, Int )

Match any token and return it with its position.

indexedNat : Nld ( Int, Int )

Match a natural number and return it with its position.

indexedTokenMatching : (String -> Bool) -> Nld ( String, Int )

Match tokens satisfying a predicate and return with position.

Transforming and Combining

succeed : a -> Nld a

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" ]
--> [ 42 ]


-- Use with choice for default values:
runList (choice [ nat, succeed 1 ]) [ "not-a-number" ]
--> [ 1 ]
map : (a -> b) -> Nld a -> Nld b

Transform the result of a parser.

runList (map String.toUpper (word "hello")) [ "hello" ]
--> [ "HELLO" ]
map2 : (a -> b -> c) -> Nld a -> Nld b -> Nld c

Combine two parsers.

map3 : (a -> b -> c -> d) -> Nld a -> Nld b -> Nld c -> Nld d

Combine three parsers.

andThen : (a -> Nld b) -> Nld a -> Nld b

Sequence two parsers. The second parser can depend on the result of the first.

andMap : Nld a -> Nld (a -> b) -> Nld b

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" ]
--> [ ( "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" ]
--> [ { x = "a", y = "b", z = "c" } ]
tuple2 : Nld a -> Nld b -> Nld ( a, b )

Combine two parsers into a tuple.

runList (tuple2 (word "delete") (word "file")) [ "delete", "file" ]
--> [ ( "delete", "file" ) ]
tuple3 : Nld a -> Nld b -> Nld c -> Nld ( a, b, c )

Combine three parsers into a tuple.

Alternatives and Repetition

choice : List (Nld a) -> Nld a

Try multiple parsers and return all successful parses.

runList (choice [ word "delete", word "add" ]) [ "delete" ]
--> [ "delete" ]
repeat : Nld a -> Nld (List a)

Match zero or more occurrences of a parser.

Returns all prefixes of the greedy (in-order) match:

  • For repeat nat with ["1", "2", "3"], returns: [[1, 2, 3], [1, 2], [1], []]

The greedy collection always takes elements in position order.

Autocompletion

autocomplete : Nld a -> TokenPositions -> Peach (Set String)

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.

topK : Int -> Nld a -> List String -> List (Set String)

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.