pete-murphy/elm-nlds/1.0.1

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)
  • 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, 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 ( Float, a )

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" ) ]
runTake : Int -> Nld a -> List String -> List ( Float, a )

Run a parser and take only the first n results.

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

Token Matchers

word : String -> Nld String

Match a specific word/token.

runList (word "delete") [ "please", "delete", "this" ]
--> [ ( 2, "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" ]
    |> List.map Tuple.second
--> [ "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" ]
    |> List.map Tuple.second
--> [ "anything" ]
nat : Nld Int

Match a natural number (non-negative integer).

runList (tuple2 (word "buy") nat) [ "buy", "3", "apples" ]
    |> List.map Tuple.second
--> [ ( "buy", 3 ) ]
tokenMatching : (String -> Bool) -> Nld String

Match tokens satisfying a predicate.

runList (tokenMatching (String.endsWith ".txt")) [ "open", "report.txt" ]
    |> List.map Tuple.second
--> [ "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" ]
    |> List.map Tuple.second
--> [ "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" ]
--> [ ( 2, ( "cat", 1 ) ) ]
indexedWords : List String -> Nld ( String, Int )

Match any of the given words and return the matched word with its position.

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

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

Transform the result of a parser.

runList (map String.toUpper (word "hello")) [ "hello" ]
--> [ ( 1, "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.

tuple2 : Nld a -> Nld b -> Nld ( a, b )

Combine two parsers into a tuple.

runList (tuple2 (word "delete") (word "file")) [ "delete", "file" ]
    |> List.map Tuple.second
--> [ ( "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" ]
    |> List.map Tuple.second
--> [ "delete" ]
repeat : Nld a -> Nld (List a)

Match zero or more occurrences of a parser.

Results include all possible match lengths, with longer matches preferred.

Autocompletion

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

Get autocomplete suggestions as a Peach.

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