Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I might be slightly confused about what you need, but by borrowing from <a href="http://norvig.com/spell-correct.html" rel="noreferrer">this post</a> I belive I have some code that should be helpful.</p> <pre><code>&gt;&gt;&gt; alphabet = 'abcdefghijklmnopqrstuvwxyz' &gt;&gt;&gt; word = 'java' &gt;&gt;&gt; splits = [(word[:i], word[i:]) for i in range(len(word) + 1)] &gt;&gt;&gt; splits [('', 'java'), ('j', 'ava'), ('ja', 'va'), ('jav', 'a'), ('java', '')] &gt;&gt;&gt; replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b] &gt;&gt;&gt; replaces ['aava', 'bava', 'cava', 'dava', 'eava', 'fava', 'gava', 'hava', 'iava', 'java', 'kava', 'lava', 'mava', 'nava', 'oava', 'pava', 'qava', 'rava', 'sava', 'tava', 'uava', 'vava', 'wav a', 'xava', 'yava', 'zava', 'java', 'jbva', 'jcva', 'jdva', 'jeva', 'jfva', 'jgva', 'jhva', 'jiva', 'jjva', 'jkva', 'jlva', 'jmva', 'jnva', 'jova', 'jpva', 'jqva', 'jrva', 'jsva', ' jtva', 'juva', 'jvva', 'jwva', 'jxva', 'jyva', 'jzva', 'jaaa', 'jaba', 'jaca', 'jada', 'jaea', 'jafa', 'jaga', 'jaha', 'jaia', 'jaja', 'jaka', 'jala', 'jama', 'jana', 'jaoa', 'japa' , 'jaqa', 'jara', 'jasa', 'jata', 'jaua', 'java', 'jawa', 'jaxa', 'jaya', 'jaza', 'java', 'javb', 'javc', 'javd', 'jave', 'javf', 'javg', 'javh', 'javi', 'javj', 'javk', 'javl', 'ja vm', 'javn', 'javo', 'javp', 'javq', 'javr', 'javs', 'javt', 'javu', 'javv', 'javw', 'javx', 'javy', 'javz'] </code></pre> <p>Once you have a list of all possible replaces, you can simply do</p> <pre><code>valid_words = [valid for valid in replaces if lookup(valid)] </code></pre> <p>Which should give you all words that can be formed by replacing 1 character in word. By placing this code in a separate method, you could take a word, obtain possible next words from that current word, and recurse over each of those words. For example:</p> <pre><code>alphabet = 'abcdefghijklmnopqrstuvwxyz' def next_word(word): splits = [(word[:i], word[i:]) for i in range(len(word) + 1)] replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b] return [valid for valid in replaces if lookup(valid)] </code></pre> <p>Is this enough help? I think your code could really benefit by separating tasks into smaller chunks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload