python - 如何删除字符串中 x 字符的 1 个实例并在 Python3 中找到它组成的单词?

标签 python python-3.x nltk

这是我目前所拥有的,但我被卡住了。我正在使用 nltk 作为单词列表,并试图找到所有带有“沙子”中字母的单词。从这个列表中,我想找到我可以用剩余字母组成的所有单词。

import nltk.corpus.words.words()
pwordlist = []

for w in wordlist:
    if 's' in w:
        if 'a' in w:
            if 'n' in w:
                if 'd' in w:
                    pwordlist.append(w)

在这种情况下,我必须使用所有字母来找到可能的单词。 我认为这将有助于找到剩余字母的可能单词,但我无法弄清楚如何仅删除“沙子”中的字母实例。

puzzle_letters = nltk.FreqDist(x)

[w for w in pwordlist if len(w) = len(pwordlist) and nltk.FreqDist(w) = puzzle_letters]

最佳答案

我会将逻辑分为四个部分:

  1. 函数 contains(word, letters),我们将使用它来检测单词是否包含“sand”
  2. 函数 subtract(word, letters),我们将使用它从单词中删除“sand”。
  3. get_anagrams(word) 函数,用于查找单词的所有变位词。
  4. 结合上述所有内容的主要算法,在您删除“sand”后查找与其他词的变位词。

from collections import Counter

words = ??? #todo: somehow get a list of every English word.

def contains(word, letters):
    return not Counter(letters) - Counter(word)

def subtract(word, letters):
    remaining = Counter(word) - Counter(letters)
    return "".join(remaining.elements())

anagrams = {}
for word in words:
    base = "".join(sorted(word))
    anagrams.setdefault(base, []).append(word)
def get_anagrams(word):
    return anagrams.get("".join(sorted(word)), [])

for word in words:
    if contains(word, "sand"):
        reduced_word = subtract(word, "sand")
        matches = get_anagrams(reduced_word)
        if matches:
            print word, matches

在 Words With Friends 词典上运行上面的代码,我得到了很多结果,包括:

...
cowhands ['chow']
credentials ['reticle', 'tiercel']
cyanids ['icy']
daftness ['efts', 'fest', 'fets']
dahoons ['oho', 'ooh']
daikons ['koi']
daintiness ['seniti']
daintinesses ['sienites']
dalapons ['opal']
dalesman ['alme', 'lame', 'male', 'meal']
...

关于python - 如何删除字符串中 x 字符的 1 个实例并在 Python3 中找到它组成的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29989464/

相关文章:

python - scikit-learn 中多类问题的级联分类器

python - Python 中的异步流处理

python - 尝试下载 nltk 数据时出现 405 错误

python - `nltk` CoreNLP解析器: prevent splitting at hyphens in POS tagger

python - 在python中将字符串转换为日期时间对象

在对象列表中匹配时间戳的 Pythonic 方法

python - 将类方法访问到实例方法的最佳方法

python - Scapyd 从 .egg 文件中引发 NotADirectoryError

python - OptionMenu 命令函数需要参数

python - 尝试从 nltk 获取首字母缩略词