liveBook · Manning

Letter substitution

As explained in Learn AI-Assisted Python Programming, Second Edition

Letter substitution is the operation of generating strings by replacing one letter in an existing string with another letter.

Letter substitution generates a new string by replacing one letter in an existing string with another. In spelling suggestions, it tests whether changing each position to each alphabet letter produces a correctly spelled word. section 7.3

Use letter substitution as one of several small spelling-correction operations:

  • Loop through each letter position in the input word. section 7.5.2
  • Replace the selected letter with each other letter in the alphabet. section 7.3
  • Keep only generated strings that are real words. section 7.4

Correcting acceptible

Changing the i to an a produces acceptable. The change_letter function generates this candidate alongside many other one-letter substitutions, which can then be checked against a collection of correctly spelled words. section 7.3 section 7.5.2

Table 7.2 Examples of the add_letter, delete_letter, and change_letter function
Input String
Strings Returned by add_letter
Strings Returned by delete_letter
Strings Returned by change_letter
cu
acu, bcu, ccu, …, zcu cau, cbu, ccu, …, czu cua, cub, cuc, …, cuz
u, c
au, bu, du, …, zu, ca, cb, cc, …, cz
cad
acad, bcad, ccad, …, zcad caad, cbad, ccad, …, czad caad, cabd, cacd, …, cazd cada, cadb, cadc, …, cadz
ad, cd, ca
aad, bad, dad, …, za cbd, ccd, cdd, …, czd caa, cab, cac, …, caz

Compare the strings produced by changing one letter with those produced by adding or deleting one letter. section 7.5.2

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains position-by-position alphabet substitution and stops short of transposed letters or multiple simultaneous errors.
Examples
Demonstrates correcting acceptible to acceptable and lists substitution candidates for strings such as cu and cad.
What you can do after
Builds a change_letter function that generates every one-letter substitution for later spelling validation.
How it connects
Connects spelling suggestions with top-down design, string processing, and loops.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.