liveBook · Manning

Pattern matching

As explained in Natural Language Processing in Action, Second Edition

A pattern-matching algorithm selects behavior or responses by comparing an input with predefined textual patterns.

A chatbot does not need to understand every sentence to handle a predictable request. Pattern matching compares input with predefined forms, then uses a match to select a response or extract information. A greeting rule, for example, can recognize several ways to say hello and capture a name. This is useful when the information sought has a recognizable shape, but the rule must be written and tested against real variations. section 1.5.2 section 12.3.1

A practical pattern-based workflow has three parts:

  • Define what should count as a match. A regular expression can cover alternative words, punctuation, and capitalization without writing a separate condition for every greeting. section 1.5.2
  • Inspect what matched. Captured groups can supply a name or other field to response logic; a match alone does not establish what the speaker meant. section 1.5.2 section 1.5.3
  • Test near misses as well as successes. A spelling change may prevent a match, while similar-looking strings can have different meanings. section 1.5.3
>>> r = r"[^a-z]*([y]o|[h']?ello|ok|hey|(good[ ])(morn[gin']{0,3}|"
>>> r += r"afternoon|even[gin']{0,3}))[\s,;:]{1,3}([a-z]{1,20})"
>>> re_greeting = re.compile(r, flags=re.IGNORECASE)    #1
>>> re_greeting.match('Hello Rosa')
<re.Match object; span=(0, 10), match='Hello Rosa'>
>>> re_greeting.match('Hello Rosa').groups()
('Hello', None, None, 'Rosa')
>>> re_greeting.match("Good morning Rosa")
<re.Match object; span=(0, 17), match="Good morning Rosa">
>>> re_greeting.match("Good Manning Rosa")                  #2
>>> re_greeting.match('Good evening Rosa Parks').groups()    #3
('Good evening', 'Good ', 'evening', 'Rosa')
>>> re_greeting.match("Good Morn'n Rosa")
<re.Match object; span=(0, 16), match="Good Morn'n Rosa">
>>> re_greeting.match("yo Rosa")
<re.Match object; span=(0, 7), match='yo Rosa'>

The Python `re` example tests one greeting pattern against several inputs and displays captured groups. Notice which variants match, which do not, and how much of a longer name is captured. section 1.5.2

Pattern matching also supports information extraction. Compact, handcrafted conditions can identify fields with constrained forms, such as dates, prices, numbers, and GPS locations. Finding such strings can be a step toward named-entity recognition and a knowledge graph. For these fields, regular expressions can sometimes be as precise as, or more precise than, a neural approach; that is a reason to test both approaches against the particular task, not to assume one always wins. section 11.1.1 section 11.4

Sentence segmentation is another concrete use: a regular expression can split text where punctuation appears to end a sentence. The rule is fast and explicit, but punctuation in quotations, abbreviations, or other unusual text makes boundary decisions harder. Inspect the resulting sentences rather than treating every successful split as a correct one. section 11.2.2

>>> re.split(r'[!.?]+[\s$]+',
...     "Hello World.... Are you there?!?! I'm going to Mars!")
['Hello World', 'Are you there', "I'm going to Mars!"]

A short `re.split` rule separates straightforward sentences, making both the pattern and its output easy to inspect. section 11.2.2

>>> re.split(
...    r'[!.?]+[\s$]+',
...    "The author wrote \"'It isn't conscious.' Turing said.\"")
['The author wrote "\'It isn\'t conscious.\' Turing said."']

A second input exposes the same rule’s boundary limitation: its apparent simplicity does not guarantee useful segmentation of quoted text. section 11.2.2

The central trade-off is control versus coverage. A predefined pattern gives a definite match or nonmatch, but people can phrase the same intent in new ways. Exact character comparisons also mistake surface similarity for similarity of meaning: `good` and `okay` differ in spelling, while `bad` and `bag` differ little. Keep patterns for well-specified jobs, and be especially cautious about mistaking a convincing scripted chatbot reply for understanding. section 1.5.3 section 1.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through Python greeting matches and captured groups, then explains where character matching falls short of meaning.
Examples
Uses greeting regex matches, captured names, and Python sentence-splitting outputs, including a difficult quotation.
What it assumes
Introduces regular expressions through Python examples without requiring prior expertise in their syntax.
What you can do after
Build it from scratch with Python `re` for greeting detection or sentence splitting, then inspect matches and boundary failures.
How it connects
Connects regular expressions and finite-state machines to sentence segmentation and named-entity recognition.

Also explained in

Compare how 12 books explain pattern matching →

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.