liveBook · Manning

Formal grammar

As explained in Natural Language Processing in Action, Second Edition

A finite set of rules that specifies how symbols can be combined into strings belonging to a formal language.

A formal grammar is a finite set of rules specifying which symbol sequences belong to a formal language. Machines can use those rules either to recognize matching sequences or to generate new ones. Regular grammars provide a practical, predictable subset for text matching, finite-state machines, and scripted dialogue. section 1.4.2 section 11.2.2

Regular grammars are useful because they connect concise patterns with deterministic processing:

  • A regular expression acts as shorthand for conditional rules that identify character patterns. section 11.2.2
  • A finite-state machine processes input step by step and halts after a finite number of transitions when using a true regular grammar. section 1.4.2 section 11.2.2
  • Pattern matches can trigger scripted responses or extract information for a chatbot’s knowledge base. section 1.4.2
  • Python and POSIX regular expressions extend beyond true regular grammars with features such as lookahead and lookback, weakening termination guarantees. section 1.4.2

Splitting ordinary sentences

A pattern such as re.split(r'[!.?]+[\\s$]+', text) separates sentences at runs of punctuation followed by whitespace or the end of the string. It works for ordinary prose, but can consume the terminating punctuation and requires additional rules for cases such as decimal points. section 11.2.2

Figure 1.4 Kinds of automata
figure

Compare the nested kinds of automata associated with formal languages and finite-state processing. section 1.4.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Connects grammar rules to finite-state machines and termination, but defers detailed finite-state-machine theory.
Examples
Uses chatbot response triggers, character-pattern extraction, sentence-boundary regexes, and Python sentence-segmentation comparisons.
What you can do after
Apply it via regular expressions to recognize patterns, extract information, and segment sentences.
How it connects
Connects formal languages with regular expressions, finite-state machines, deterministic finite automata, chatbots, and sentence segmentation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.