Input/Text Parser solutions

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
2 posts Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Input/Text Parser solutions
CodenStuff
OMG Codenstuff is posting in the help section :o

This isn't really a code problem I'm having, I just want to see how you guys would do this.

I'm making a text adventure type game/editor and for those who don't know a text adventure isn't generally a game with graphics, it's all about text, it's an interactive story or interactive fiction as its known.

The game begins with something like:

"You are standing in a room, you can see a table in the middle of the room and a door to the east."

Then the player would type what to do next like:

"Look at the table"

Then the game does what you ask and returns something like:

"The table is square and made of wood."

So in order to make the game do what the player asks it to do, you have to interpret what the player typed.

How would you parse the players input in order to complete the request, how would you code it?

For an example lets say the player is in a room with a locked door, now if the player typed the following:

"Open door."

That would be pretty easy to parse. We can split that in to a string array ("open","door"), we know the first word is "Open" so the player wants to open something and we know the second word is "Door" so we know the player wants to Open the Door.

But what if they typed: "Open the door with the key" or "Use the key to open the door" or "Unlock the door with the key"

The first word is very important and that is easy to get and identify "Open, use, unlock, take, move.." but how to interpret the rest?

Splitting the input in to an array of words is good but what if the player wants to use or look at something that has multiple words, like "Tin can", "Hot water bottle".

"Open tin can with can opener" or "Use can opener to open the tin can" <--- dunnno;

I'm just trying to find the best way of doing it. It's a tricky one to solve and really gets the mind going.

From all you know about programming, how would you attempt to do this?

Your thoughts and code?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
SumCode
Dedicated Member
Dedicated Member
Posts: 57
Joined: Fri Aug 03, 2012 2:34 am

Re: Input/Text Parser solutions
SumCode
You could use regex to find values that you need.

C# Rollover to view spoiler

Code: Select all
class Foo
{
    public static Tuple<int, int> GetAction(string sentence)
    {
        var strSent = sentence.ToLower();
        var verbs = new List<String>() { "look", "open", "close" };
        var nouns = new List<String>() { "table", "door", "book" };

        string noun = Regex.Match(strSent, string.Format("({0})", string.Join("|", nouns))).Value;
        string verb = Regex.Match(strSent, string.Format("({0})", string.Join("|", verbs))).Value;

        return new Tuple<int, int>(nouns.IndexOf(noun), verbs.IndexOf(verb));
    }
}
[/spoiler]

VB Rollover to view spoiler

Code: Select all
Class Foo
	Public Shared Function GetAction(sentence As String) As Tuple(Of Integer, Integer)
		Dim strSent = sentence.ToLower()
		Dim verbs = New List(Of [String])() From { _
			"look", _
			"open", _
			"close" _
		}
		Dim nouns = New List(Of [String])() From { _
			"table", _
			"door", _
			"book" _
		}

		Dim noun As String = Regex.Match(strSent, String.Format("({0})", String.Join("|", nouns))).Value
		Dim verb As String = Regex.Match(strSent, String.Format("({0})", String.Join("|", verbs))).Value

		Return New Tuple(Of Integer, Integer)(nouns.IndexOf(noun), verbs.IndexOf(verb))
	End Function
End Class
[/spoiler]

Having "with a" or "use a" might be a little more complicated. I'll see what I can come up with.

EDIT: Here's how you can use it:
Code: Select all
var GA = Foo.GetAction("Use a key to open door");
if (GA.Item3 == 0 && GA.Item2 == 1 && GA.Item1 == 1)
    Console.WriteLine("Door opened"); // What to do when a door is unlocked with key
2 posts Page 1 of 1
Return to “Coding Help & Support”