[英]How should i interact with a base of words in iPhone app?
I'm making a word game for iPhone. 我正在为iPhone做文字游戏。 User can construct words from separate letters.
用户可以从单独的字母构造单词。 And i need to check is this a real word, which user made, or some kind of pointless stuff like "sfugh".
我需要检查的是这是用户创建的真实单词,还是诸如“ sfugh”之类的毫无意义的东西。 How i should better implement this?
我应该如何更好地实现这一点? Should i have kind dictionary in program(I think it's a bad idea, because with a large amount of words it would work very slow) or maybe there is kind of online dictionaries, where i can check the word from my code, or should i make a trie?
我应该在程序中使用友好的字典吗(我认为这是一个坏主意,因为使用大量单词会导致非常慢的工作),或者可能存在某种在线词典,可以在我的代码中检查单词,还是应该试一试? Or maybe where is other solutions?
也许其他解决方案在哪里?
Use a pre-populated sqlite database. 使用预先填充的sqlite数据库。 That way, you can use string indexes to make the searches in the db faster.
这样,您可以使用字符串索引来加快数据库中的搜索速度。
You can use Apple's native UITextChecker to determine whether or not a word is misspelled/valid. 您可以使用Apple的本机UITextChecker来确定单词是否拼写错误/有效。 Here's a sample project on GitHub .
这是GitHub上的示例项目 。 This way, you don't have to worry about assembling your own database full of words.
这样,您就不必担心组装充满单词的数据库。
For easy digestion, here's the code that does all the work 为了易于消化,下面是完成所有工作的代码
- (IBAction)textFieldTextDidChange:(id)sender {
NSRange rangeOfMisspelling = [self.textChecker rangeOfMisspelledWordInString:self.textField.text
range:NSMakeRange(0, self.textField.text.length)
startingAt:0
wrap:NO
language:@"en_US"];
// no misspellings
if (rangeOfMisspelling.location == NSNotFound) {
self.textLabel.text = @"Valid word!";
}
// misspellings
else {
self.textLabel.text = @"Not a word.";
}
}
One thing to note is UITextChecker seems to count individual letters as valid words. 需要注意的一件事是UITextChecker似乎将单个字母视为有效单词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.