Nasty.Language.English.PhraseParser (Nasty v0.3.0)

View Source

Phrase structure parser for English.

Builds syntactic phrases (NounPhrase, VerbPhrase, etc.) from POS-tagged tokens using bottom-up pattern matching.

Approach

  • Greedy longest-match: Consume as many tokens as possible for each phrase
  • Bottom-up parsing: Build smaller phrases first, then combine
  • Left-to-right: Process tokens in order

Grammar Rules (Simplified CFG)

NP   Det? Adj* Noun PP*
VP   Aux* MainVerb NP? PP* Adv*
PP   Prep NP
AdjP  Adv? Adj
AdvP  Adv

Examples

iex> tokens = [
...>   %Token{text: "the", pos_tag: :det},
...>   %Token{text: "cat", pos_tag: :noun}
...> ]
iex> PhraseParser.parse_noun_phrase(tokens, 0)
{:ok, noun_phrase, 2}  # Consumed 2 tokens

Summary

Functions

Parses an adjectival phrase starting at the given position.

Parses an adverbial phrase (simple adverb for now).

Parses a noun phrase starting at the given position.

Parses a prepositional phrase starting at the given position.

Parses a relative clause starting at the given position.

Parses a verb phrase starting at the given position.

Functions

parse_adjectival_phrase(tokens, start_pos)

@spec parse_adjectival_phrase([Nasty.AST.Token.t()], non_neg_integer()) ::
  {:ok, Nasty.AST.AdjectivalPhrase.t(), non_neg_integer()} | :error

Parses an adjectival phrase starting at the given position.

Grammar: Adv? Adj

Returns {:ok, adj_phrase, next_pos} or :error

parse_adverbial_phrase(tokens, start_pos)

@spec parse_adverbial_phrase([Nasty.AST.Token.t()], non_neg_integer()) ::
  {:ok, Nasty.AST.AdverbialPhrase.t(), non_neg_integer()} | :error

Parses an adverbial phrase (simple adverb for now).

Grammar: Adv

Returns {:ok, adv_phrase, next_pos} or :error

parse_noun_phrase(tokens, start_pos)

@spec parse_noun_phrase([Nasty.AST.Token.t()], non_neg_integer()) ::
  {:ok, Nasty.AST.NounPhrase.t(), non_neg_integer()} | :error

Parses a noun phrase starting at the given position.

Grammar: Det? Adj (Noun | PropN | Pron) PP

Pronouns can stand alone as NPs (e.g., "I", "he", "they").

Returns {:ok, noun_phrase, next_pos} or :error

parse_prepositional_phrase(tokens, start_pos)

@spec parse_prepositional_phrase([Nasty.AST.Token.t()], non_neg_integer()) ::
  {:ok, Nasty.AST.PrepositionalPhrase.t(), non_neg_integer()} | :error

Parses a prepositional phrase starting at the given position.

Grammar: Prep NP

Returns {:ok, prep_phrase, next_pos} or :error

parse_relative_clause(tokens, start_pos)

@spec parse_relative_clause([Nasty.AST.Token.t()], non_neg_integer()) ::
  {:ok, Nasty.AST.RelativeClause.t(), non_neg_integer()} | :error

Parses a relative clause starting at the given position.

Grammar: RelPron/RelAdv Clause

Relative pronouns: who, whom, whose, which, that Relative adverbs: where, when, why

Returns {:ok, relative_clause, next_pos} or :error

parse_verb_phrase(tokens, start_pos)

@spec parse_verb_phrase([Nasty.AST.Token.t()], non_neg_integer()) ::
  {:ok, Nasty.AST.VerbPhrase.t(), non_neg_integer()} | :error

Parses a verb phrase starting at the given position.

Grammar: Aux MainVerb NP? PP Adv*

Returns {:ok, verb_phrase, next_pos} or :error