PDA

View Full Version : Need advice - Check NSString object to see if its a valid word?




animefx
Aug 28, 2012, 12:27 PM
I have an NSString object called myWord and I want to check it's contents to see if it's a word in the English Dictionary (not a dictionary in terms of programming). If it's not spelled correctly or if it's someone's name I want it to be considered invalid. I'm not sure exactly how to approach this.

I'm working on a little game in my spare time but I'm keeping it simple since I'm still learning. I would rather have the English Dictionary as a file rather than having my app access an online English Dictionary so that the game could be played without internet access.

Any suggestions would be appreciated.



Reason077
Aug 28, 2012, 01:05 PM
You just need a list of english words to validate against. You'll find one, on unix systems, at /usr/share/dict/words (not sure if the iPhone has this file, but OS X does)

The simplest method would be to simply read the contents of the words file in to an NSDictionary, and call [dictionary objectForKey:stringToCheck] ... if it returns nil then the string is not a dictionary word.

This would use a fair bit of memory, however, and there might be a noticeable pause while your app loads the words file and populates the NSDictionary.

A better performing approach would be to load all the words into an sqlite database, and query against that.

animefx
Aug 31, 2012, 09:44 AM
Thank you, I'll look into SQLite... Would this be much more complicated than say building a plist file?

You just need a list of english words to validate against. You'll find one, on unix systems, at /usr/share/dict/words (not sure if the iPhone has this file, but OS X does)

The simplest method would be to simply read the contents of the words file in to an NSDictionary, and call [dictionary objectForKey:stringToCheck] ... if it returns nil then the string is not a dictionary word.

This would use a fair bit of memory, however, and there might be a noticeable pause while your app loads the words file and populates the NSDictionary.

A better performing approach would be to load all the words into an sqlite database, and query against that.

robbieduncan
Aug 31, 2012, 10:43 AM
If I were you I'd find a word list file (say this one (http://code.google.com/p/dotnetperls-controls/downloads/detail?name=enable1.txt&can=2&q=) and write a program to look over each line inserting that into your database.

KnightWRX
Aug 31, 2012, 11:50 AM
What's wrong with UITextChecker (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextChecker_Class/Reference/Reference.html) ?

phantax
Aug 31, 2012, 11:55 AM
What's wrong with UITextChecker (http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITextChecker_Class/Reference/Reference.html) ?

Seems like that would allow any corrected word thats been added to the database, misspelled or not. From his description it looks like he is interested in making some scrabble type game, I don't think it would work the way he intends in this case.

Reason077
Aug 31, 2012, 01:01 PM
Thank you, I'll look into SQLite... Would this be much more complicated than say building a plist file?

SQLite is a bit more complicated than using a flat file or a plist. But not too bad. And without using a database you could run into performance problems with a large dataset.

If you have a fairly small number of entries (say, under 10,000 or so) then you can probably get away with a flat file. But if you're talking hundreds of thousands of words, then you're probably going to get poor performance and a big pause when loading the file.

KnightWRX
Aug 31, 2012, 01:11 PM
SQLite is a bit more complicated than using a flat file or a plist. But not too bad. And without using a database you could run into performance problems with a large dataset.

Not if you load your datasets intelligently. I'd build 3 levels of NSDictionaries, using the first letter, then the second letter of words as keys with the 3rd level being the words themselves.

Hence, you wouldn't have to run through the entire list each time, just first get the dictionary for the first letter (1 of 26 possible keys), then the dictionary for the second letter (another 1 of 26 possible keys) and finally see if your word is a valid word in a much more limited list.

A full on RDBMS would have even more performance for sure, but this way it isn't complicated, doesn't require learning SQL and will still give acceptable performance.

If you have a fairly small number of entries (say, under 10,000 or so) then you can probably get away with a flat file. But if you're talking hundreds of thousands of words, then you're probably going to get poor performance and a big pause when loading the file.

Load the file asynchronously in another thread while launching the app. So while your main thread is initializing stuff and getting views on screen, heck, your user can even start interacting with the app as long as you don't need to look up words and the file can keep loading in the background at the same time.

GCD should solve that.

Depending on the type of game though, I'd just see if its feasible to use UITextChecker.

Reason077
Aug 31, 2012, 02:41 PM
Not if you load your datasets intelligently. I'd build 3 levels of NSDictionaries, using the first letter, then the second letter of words as keys with the 3rd level being the words themselves.

Sure, that'd work. And it would be fast - once it's loaded and initialized. But once you start talking about using multiple levels of NSDictionaries, and loading asynchronously, the solution has become more complex than just using SQLite - which would also have negligible loading time and use much less memory.

KnightWRX
Aug 31, 2012, 03:02 PM
Sure, that'd work. And it would be fast - once it's loaded and initialized. But once you start talking about using multiple levels of NSDictionaries, and loading asynchronously, the solution has become more complex than just using SQLite - which would also have negligible loading time and use much less memory.

Still have to find a word list, build a relational schema, learn SQL DDL, DML and query syntax.

How about just a pre-populated CoreData model ? That is built graphically with Xcode.

Of course, for my example, you can use a simple app using NSKeyedArchiver and NSMutableData's writeToFile and make use of NSDictionary's implementation of the NSCoder protocol, so it's all just a few lines of code to write the dicts to file and load it back in.

Again I go back to UITextChecker. OP still hasn't mentionned the type of game, if its not a game where user added words really matter, it is by far the simplest solution.