You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

类似Family Feud的猜词游戏如何实现单输入词的模糊匹配判定?

猜词游戏单词匹配实现方案

核心逻辑

  • 先对所有标准答案做标准化预处理:每条答案转小写后拆分为独立单词集合,同步存储原答案内容,新增「是否已命中」标记,避免同一条答案被重复计数
  • 玩家输入内容先做标准化处理:统一转小写、去除首尾空白字符
  • 匹配时仅遍历未命中的标准答案,只要玩家输入的内容属于对应答案的单词集合,直接判定为回答正确

完整修改后代码

from nltk.corpus.reader import WordListCorpusReader
import os

again = True
print("GUESSING GAME")

while again:
    num = int(input("PICK A NUMBER FOR THE TOPIC [1-3]: "))
    if num == 1:
        print("1. Name something people are afraid of. ")
        print("======================================")
        print("You need to guess 10 right answers in order to win. ")
        # 读取标准答案
        reader = WordListCorpusReader('D:/VB/', ['topic1.txt'])
        standard_answers = []
        # 预处理标准答案
        for ans in reader.words():
            standard_answers.append({
                "original": ans,
                "word_set": set(ans.lower().split()),
                "guessed": False
            })
        correct_count = 0
        # 猜词循环
        while correct_count < 10:
            user_input = input(f"请输入你的猜测(已答对{correct_count}/10):").strip().lower()
            matched = False
            # 遍历未被猜过的答案匹配
            for ans in standard_answers:
                if not ans["guessed"] and user_input in ans["word_set"]:
                    print(f"回答正确!正确答案是:{ans['original']}")
                    ans["guessed"] = True
                    correct_count += 1
                    matched = True
                    break
            if not matched:
                print("回答错误,请再试一次")
        print("恭喜你全部答对,游戏胜利!")
        # 如需支持重复游戏可在此处添加是否继续的判断逻辑

匹配效果验证

完全覆盖你提到的场景:

  • 标准答案为The dark时,玩家输入Dark转小写后是dark,存在于该答案的单词集合{'the', 'dark'}中,判定正确
  • 标准答案为Their boss or getting fired时,玩家输入Boss转小写后是boss,存在于该答案的单词集合中,判定正确
  • 缩写类答案The IRS,玩家输入irs也能正常匹配成功

内容的提问来源于stack exchange,提问作者Jay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 15:09:04