一、简介
这里有一个系统地解决这个问题的方法:如果你有一个能很好地扮演刽子手的算法,那么你可以把每个单词的难度作为你的程序在猜测那个单词时猜错的次数。
2. 除了刽子手策略
在其他一些答案和评论中隐含了一个想法,即求解器的最佳策略是根据英语中字母的频率或某些语料库中单词的频率做出决定。这是一个诱人的想法,但它并不完全正确。如果求解器准确地模拟了 setter 选择的单词的分布,则求解器会做得最好,并且人类 setter 很可能会根据它们的稀有性或避免常用字母来选择单词。例如,虽然E
是英语中最常用的字母,但如果 setter 总是从单词JUGFUL
、RHYTHM
、SYZYGY
和中进行选择ZYTHUM
,那么完美的求解器不会从猜测开始E
!
对 setter 进行建模的最佳方法取决于上下文,但我想某种贝叶斯归纳推理在求解器与同一个 setter 或一组类似 setter 进行许多游戏的环境中会很好地工作。
3. 一个刽子手算法
在这里,我将概述一个非常好的求解器(但远非完美)。它将 setter 建模为从固定字典中统一选择单词。这是一种贪心算法:在每个阶段,它都会猜测使未命中次数最少的字母,即不包含猜测的单词。例如,如果到目前为止还没有做出任何猜测,并且可能的词是DEED
,DEAD
和DARE
, 那么:
- 如果您猜
D
或E
,则没有遗漏;
- 如果你猜
A
的话,有一个错过(DEED
);
- 如果你猜
R
,有两个未命中(DEED
和DEAD
);
- 如果您猜到任何其他字母,则有三个未命中。
因此,在这种情况下,要么 要么D
是E
一个很好的猜测。
(感谢Panic 上校在评论中指出,刽子手中的正确猜测是免费的——我在第一次尝试时完全忘记了这一点!)
4. 实施
这是该算法在 Python 中的实现:
from collections import defaultdict
from string import ascii_lowercase
def partition(guess, words):
"""Apply the single letter 'guess' to the sequence 'words' and return
a dictionary mapping the pattern of occurrences of 'guess' in a
word to the list of words with that pattern.
>>> words = 'deed even eyes mews peep star'.split()
>>> sorted(list(partition('e', words).items()))
[(0, ['star']), (2, ['mews']), (5, ['even', 'eyes']), (6, ['deed', 'peep'])]
"""
result = defaultdict(list)
for word in words:
key = sum(1 << i for i, letter in enumerate(word) if letter == guess)
result[key].append(word)
return result
def guess_cost(guess, words):
"""Return the cost of a guess, namely the number of words that don't
contain the guess.
>>> words = 'deed even eyes mews peep star'.split()
>>> guess_cost('e', words)
1
>>> guess_cost('s', words)
3
"""
return sum(guess not in word for word in words)
def word_guesses(words, wrong = 0, letters = ''):
"""Given the collection 'words' that match all letters guessed so far,
generate tuples (wrong, nguesses, word, guesses) where
'word' is the word that was guessed;
'guesses' is the sequence of letters guessed;
'wrong' is the number of these guesses that were wrong;
'nguesses' is len(guesses).
>>> words = 'deed even eyes heel mere peep star'.split()
>>> from pprint import pprint
>>> pprint(sorted(word_guesses(words)))
[(0, 1, 'mere', 'e'),
(0, 2, 'deed', 'ed'),
(0, 2, 'even', 'en'),
(1, 1, 'star', 'e'),
(1, 2, 'eyes', 'en'),
(1, 3, 'heel', 'edh'),
(2, 3, 'peep', 'edh')]
"""
if len(words) == 1:
yield wrong, len(letters), words[0], letters
return
best_guess = min((g for g in ascii_lowercase if g not in letters),
key = lambda g:guess_cost(g, words))
best_partition = partition(best_guess, words)
letters += best_guess
for pattern, words in best_partition.items():
for guess in word_guesses(words, wrong + (pattern == 0), letters):
yield guess
5. 示例结果
使用这种策略,可以评估猜测集合中每个单词的难度。在这里,我考虑了我的系统词典中的六个字母的单词:
>>> words = [w.strip() for w in open('/usr/share/dict/words') if w.lower() == w]
>>> six_letter_words = set(w for w in words if len(w) == 6)
>>> len(six_letter_words)
15066
>>> results = sorted(word_guesses(six_letter_words))
这本词典中最容易猜测的单词(连同求解器猜测它们所需的猜测序列)如下:
>>> from pprint import pprint
>>> pprint(results[:10])
[(0, 1, 'eelery', 'e'),
(0, 2, 'coneen', 'en'),
(0, 2, 'earlet', 'er'),
(0, 2, 'earner', 'er'),
(0, 2, 'edgrew', 'er'),
(0, 2, 'eerily', 'el'),
(0, 2, 'egence', 'eg'),
(0, 2, 'eleven', 'el'),
(0, 2, 'enaena', 'en'),
(0, 2, 'ennead', 'en')]
最难的词是这些:
>>> pprint(results[-10:])
[(12, 16, 'buzzer', 'eraoiutlnsmdbcfg'),
(12, 16, 'cuffer', 'eraoiutlnsmdbpgc'),
(12, 16, 'jugger', 'eraoiutlnsmdbpgh'),
(12, 16, 'pugger', 'eraoiutlnsmdbpcf'),
(12, 16, 'suddle', 'eaioulbrdcfghmnp'),
(12, 16, 'yucker', 'eraoiutlnsmdbpgc'),
(12, 16, 'zipper', 'eraoinltsdgcbpjk'),
(12, 17, 'tuzzle', 'eaioulbrdcgszmnpt'),
(13, 16, 'wuzzer', 'eraoiutlnsmdbpgc'),
(13, 17, 'wuzzle', 'eaioulbrdcgszmnpt')]
之所以难,是因为你猜到之后-UZZLE
,还有七种可能:
>>> ' '.join(sorted(w for w in six_letter_words if w.endswith('uzzle')))
'buzzle guzzle muzzle nuzzle puzzle tuzzle wuzzle'
6. 词表的选择
当然,在为您的孩子准备单词表时,您不会从计算机的系统词典开始,而是从您认为他们可能知道的单词列表开始。例如,您可能会查看维基词典的各种英语语料库中最常用单词的列表。
例如,在2006 年古腾堡计划中最常见的 10,000 个单词中的 1,700 个六字母单词中,最难的十个是:
[(6, 10, 'losing', 'eaoignvwch'),
(6, 10, 'monkey', 'erdstaoync'),
(6, 10, 'pulled', 'erdaioupfh'),
(6, 10, 'slaves', 'erdsacthkl'),
(6, 10, 'supper', 'eriaoubsfm'),
(6, 11, 'hunter', 'eriaoubshng'),
(6, 11, 'nought', 'eaoiustghbf'),
(6, 11, 'wounds', 'eaoiusdnhpr'),
(6, 11, 'wright', 'eaoithglrbf'),
(7, 10, 'soames', 'erdsacthkl')]
(Soames Forsyte 是John Galsworthy 的 Forsyte Saga中的一个角色;单词表已转换为小写字母,因此我无法快速删除专有名称。)