Re: word game
- Posted by ghaberek (admin) Oct 01, 2009
- 1166 views
Wouldn't that print characters. i think a better way is to look for whitespace and record that position minus 1. What do you think?
I think that was just a demo to show how you'd process the words. But that is certainly a good method of breaking apart a sentence into words with several possible delimiters.
Rather than tracking the exact position of each word, like "1, feed. 2, the. 3, cat." you may have to work on some sort of basic grammar parser, with known verbs and nouns. Something like "feed (verb), the (article), cat (noun)" would probably be better. Then try to track a verb-object relationship. That way "feed cat" and "feed the cat" would be the same (since 'the' is unnecessary here). Order comes into play as well, "feed cat" and "cat feed" are two different things. (While most people say "cat food", "cat feed" is still technically correct, assuming you keep cats as livestock. )
Perhaps I'm over complicating things here. Maybe you just want some sort of command line parser, like "feed [noun] [food type]" where [noun] could be "cat" or "birds breadcrumbs" or "Steve burger", etc. In that case, check out cmd_parse().
-Greg