验证Rails中的英文字典? [英] Validate words against an English dictionary in Rails?

查看:129
本文介绍了验证Rails中的英文字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一些谷歌搜索,但找不到我想要的东西。

I've done some Google searching but couldn't find what I was looking for.

我正在rails中开发一个拼字游戏,想知道是否有一种简单的方式来验证玩家在游戏中输入的内容实际上是一个字。他们会打字出来。

I'm developing a scrabble-type word game in rails, and was wondering if there was a simple way to validate what the player inputs in the game is actually a word. They'd be typing the word out.

是否针对某种英文语言字典数据库进行验证是否能在应用程序中加载最佳方式来解决这个问题?如果是这样,有没有提供这种功能的图书馆?如果没有,你会建议什么?

Is validation against some sort of English language dictionary database loaded within the app best way to solve this problem? If so, are there any libraries that offer this kind of functionality? If not, what would you suggest?

感谢您的帮助!

推荐答案

你需要两件事:


  1. 一个单词列表

  2. 一些代码

单词列表是棘手的部分。在大多数Unix系统上,在 / usr / share / dict / words / usr / dict / words - 见 http://en.wikipedia.org/wiki/Words_(Unix)了解更多详情。我的Mac上有一个234,936个字。但是它们并不是全部有效的拼字游戏。所以你必须以某种方式获得一个拼字错误字典,确保你拥有正确的使用许可证,并处理它,因此它是一个文本文件。

The word list is the tricky part. On most Unix systems there's a word list at /usr/share/dict/words or /usr/dict/words -- see http://en.wikipedia.org/wiki/Words_(Unix) for more details. The one on my Mac has 234,936 words in it. But they're not all valid Scrabble words. So you'd have to somehow acquire a Scrabble dictionary, make sure you have the right license to use it, and process it so it's a text file.

(更新: LetterPress 的单词列表现在是开源 GitHub上提供。)

(Update: The word list for LetterPress is now open source, and available on GitHub.)

代码在简单的情况下没有问题。这是一个我刚刚鞭打的脚本:

The code is no problem in the simple case. Here's a script I whipped up just now:

words = {}
File.open("/usr/share/dict/words") do |file|
  file.each do |line|
    words[line.strip] = true
  end
end
p words["magic"]
p words["saldkaj"]

这将输出

true
nil

我把它作为一个练习,让读者把它变成一个适当的Words对象。 (技术上它不是一个字典,因为它没有定义。)或使用DAWG而不是哈希,即使一个哈希可能是适合你的需要。

I leave it as an exercise for the reader to make it into a proper Words object. (Technically it's not a Dictionary since it has no definitions.) Or to use a DAWG instead of a hash, even though a hash is probably fine for your needs.

这篇关于验证Rails中的英文字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆