Can someone think of a better way?

Status
Not open for further replies.

Mr_Threepwood

Daemon Poster
Messages
961
Im making a game and part of it is a text interpreter that takes a written sentence and trys to interpret what you want the program to do.

Heres a summary of what I do.

The whole sentence gets passed to a class that takes out the trailing and leading spaces, then makes an arraylist of all of the words.

The arraylist of words gets sent to the room object, which then looks for recognizable words that are universal, and recognizable words that are specifically for that room. These words are then stored in a new arrayList;

The arraylist of recognizable words is then passed to a method in the room object which trys to figure out what you want the recognizable words to do.

To do the checks I have an array of local and universal words, this means that I need embedded loops since for each word in the arraylist I have to loop through to see if it matches a recognizable word.


Can anyone think of a more efficient way to do this?


Im figuring out quickly that text interpreters are VERY hard to make. I'm completely ignoring verbs so far and its still very hard.

Because what happens if someone types in something like

"look in exit room"

Does the program look, or exit? So I've got to make ways for it to set priority and ignore certain action words if others are in the array.

UGH.
 
To do the checks I have an array of local and universal words, this means that I need embedded loops since for each word in the arraylist I have to loop through to see if it matches a recognizable word.

If you are storing a large number of universal words, it would probably be more efficient if you stored them in a hashmap. It's faster than a linear search through an array.

If you're having trouble getting your program to understand the semantics of sentences, make the commands simpler. Use only one word action commands, like "look" or "exit". Once you get that working, then you can try to add more complexity.
 
I agree with jaeusm start with simple commands. Then get the program to recognise sentences as commands like walk, run and look then a parameter like a position/room in the game.

E.g. look exit.
Look is the command, exit is the parameter and will look at the exit.
 
Anything more complex than subject - verb - target will require more than loops and simple data structures, that is, if you want to get accurate picture of what user can input, if you put restrictions, then it's not natural language processing anymore, but it certainly will make things alot easier..

Certainly a artificial intelligence book will help you more than i can :)
 
Alright well this method I'm doing it is way to hard for a beggining game project. Heres my new proposed way, tell me if it sounds simpler.

Accept the users sentence

get rid of characters (periods, question marks) at end of sentence, and all trailing and leading spaces

get rid of unneeded words (at, the, in, etc.)

send this string to the room object, which then checks if any part of it matches room commands

run commands if it matches


To me that seems a lot simpler, the only part im unsure of it how to check the new string. What I mean is what if I have a new string that looks like "look wall dasf asdfasdfasdf", and i need to check if the string contains "look wall".

Edit: Nevermin, looks like I do need to find out all the recognizable words. This is a pain in the ***, Im gonna do some other java stuff and come back to this at one point in the future. Programming something you are frustrated at doesnt work.
 
You need to create a standard grammar for the users.

Lets say you figure out that you mainly are going to have a few types of sentence structures....

"Walk to exit"

"Talk to guy near table"

You can say, some simple commands are:
<verb> <preposition> <noun>

And you can have a parser classify the tokens of a sentence. If it meets the particular format of that kind of sentence, pass that string of tokens to a particular decision tree that deals only with that type of sentence.

Lets say a more complex command is

<verb> <preposition> <noun> <preposition> <noun>

Then you also classify each token, and make a separate decision tree for these commands.

That is what I would do.
 
Nevermind I didnt quit on it. I decided to do a way that is fairly complex, but I think it works fairly well.

For recognizing which base command to look at it simply checks a few key words. Once it knows the base command (look,take, etc) it then runs the room command for that function.


The room has an array of every thing that you can look at, take in the room. Each of those objects has its own name which then tries to run the command on that object.

I'm sure I'll run into a few more problems along the way, but now that I've got the major headache out of the way the rest shouldnt be as bad.

Also, props to the guy that made hugos house of horrors. I ran that game and tried a bunch of commands to see how he structured it, and it turns out its fairly similar to mine where the order you enter stuff doesnt really matter.

The neat thing, is once I have this done it can be modified to work for any game without a ton of changes. Maybe next I'll do a graphical adventure...maybe.
 
Status
Not open for further replies.
Back
Top Bottom