Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

animefx

macrumors regular
Original poster
May 10, 2005
157
0
Illinois
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

macrumors 68040
Aug 14, 2007
3,600
3,572
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

macrumors regular
Original poster
May 10, 2005
157
0
Illinois
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.
 

phantax

macrumors member
Feb 2, 2009
72
0
What's wrong with UITextChecker ?

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

macrumors 68040
Aug 14, 2007
3,600
3,572
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

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
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

macrumors 68040
Aug 14, 2007
3,600
3,572
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

macrumors Pentium
Jan 28, 2009
15,046
4
Quebec, Canada
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.