1。简介
这里有一个系统地解决这个问题的方法:如果你有一个能很好地扮演刽子手的算法,那么你可以将每个单词的难度作为你的程序在猜测该单词时的错误猜测次数。
2。除了刽子手策略
在其他一些答案和 cmets 中隐含了一个想法,即求解器的最佳策略是根据英语字母的频率或某些语料库中单词的频率做出决定。这是一个诱人的想法,但它并不完全正确。如果求解器准确地模拟 setter 选择的单词的分布,则求解器会做得最好,而人类 setter 很可能会根据单词的稀有性或避免常用字母来选择单词。例如,虽然E是英语中最常用的字母,但如果setter总是从JUGFUL、RHYTHM、SYZYGY和ZYTHUM这几个词中进行选择,那么完美的求解器不会从猜测开始E!
对 setter 进行建模的最佳方法取决于上下文,但我想某种贝叶斯归纳推理在求解器与同一个 setter 或一组类似 setter 进行许多游戏的环境中会很好地工作。
3。刽子手算法
在这里,我将概述一个相当不错(但远非完美)的求解器。它将 setter 建模为从固定字典中统一选择单词。这是一个greedy algorithm:在每个阶段,它都会猜测使未命中次数最少的字母,即不包含猜测的单词。例如,如果到目前为止还没有猜到,可能的词是DEED、DEAD和DARE,那么:
- 如果您猜到
D 或E,则没有错过;
- 如果你猜到了
A,那就错了一个(DEED);
- 如果您猜到
R,则有两个未命中(DEED 和DEAD);
- 如果您猜到任何其他字母,则有 3 个未命中。
所以在这种情况下,D 或 E 是一个不错的猜测。
(感谢Colonel Panic in comments 指出,刽子手中的正确猜测是免费的——我在第一次尝试时完全忘记了这一点!)
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。词表的选择
当然,在为您的孩子准备单词表时,您不会从计算机的系统词典开始,而是从您认为他们可能知道的单词列表开始。例如,您可能会查看各种英语语料库中的Wiktionary's lists of the most frequently used words。
例如,10,000 most common words in Project Gutenberg as of 2006 的 1700 个六个字母的单词中,最难的十个是这些:
[(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 是 Forsyte Saga by John Galsworthy 中的一个字符;单词表已转换为小写,因此我无法快速删除专有名称。)