GLBasic forum

Main forum => GLBasic - en => Topic started by: doimus on 2011-Dec-22

Title: How to make a text parser?
Post by: doimus on 2011-Dec-22
I've been playing lots of old-school adventures (again!) - Sierra's "quest"classics: Space, Police, King's Quest and LS Larry. You know, these games with 160x200 resolution and text input.

First thing I noticed is that these games are mostly tedious crap, gameplay-wise, besides Larry, who is still funny as sh!t.
Second thing I noticed is that I want to make a game exactly like this, only mine will be MUCH BETTER(TM).

So I am wondering, what is a proper way to implement text parser in game?

I guess I will use INSTR$ to search for "tokens" or "verbs" in user input. Then the "nouns". Then I'll probably need a list of synonyms to check against. Then it sends a final "command" to the game.

What I would like to know if there are resources online that go into more detail on this topic. Stuff like checking for errors in spelling, or "words inside words" - like "pick" is not the same as "pickaxe", or whatever else...

It would be nice to do this slightly more advanced than those old adventures, which gave annoying errors for almost any combination of words that's not exactly like the one authors envisioned. Something more "interactive-fiction" like. 
Something that will allow people to experiment more, like this....  =D




Title: Re: How to make a text parser?
Post by: erico on 2011-Dec-22
I have no idea about how to do a parser... :(
I never did any good with strings.

In case you wanna take research a little further on the theme, give coco adventures a go. Vortex factor, treckboyer, sea quest, black sanctum, are great examples to research. If you are felling lucky, you can try hunting for IN QUEST OF THE STAR LORD too.

To make things easier (ye no emu config and stuff) go to this coco java emulator page here:
http://members.cox.net/javacoco

strangely, I can´t access it today. In fact, after getting my internet on 10mb, things are quite slow on youtube and some servers around the world are not working, it really pisses me off... good days back in 1998 when you could access whatever you want, it dosen´t semm like that anymore for ages... :(
Title: Re: How to make a text parser?
Post by: erico on 2011-Dec-24
oh, you might wanna check this out for inspiration:
http://www.textadventures.co.uk/quest/

type of a text adventure creator.
Title: Re: How to make a text parser?
Post by: spicypixel on 2011-Dec-24
One thing I'd do is check for a "," or the word "and" in the string to build the sequence of commands and their order of operation, then check for verbs nouns as you mentioned for each seperate sequence. As for the "pick" and "pickaxe" you mentioned I'd check for " pick " and " pick. " unless pick was the first word then I'd check for "pick ". Should stop the "pickaxe" and "toothpick" type issues. Other than that buddy not sure :-)

Of course you would need to be aware of " pick." and other quirks but I think what I put above is on the right track of logic involved. I'd use MID$ too so you can determine sequencing of commands better :)
Title: Re: How to make a text parser?
Post by: erico on 2011-Dec-24
sorry to be off topic, another inspiration for you:
http://samizdat.cc/cyoa/#/_
Title: Re: How to make a text parser?
Post by: hardyx on 2011-Dec-24
About the video, wow! It's an original concept of text adventures. In the early adventures you give a verb + name. For example: open door, get key, use key, etc. You can begin with this. But I think you can allow more complicated sentences, if you have a verbs and names list. And you can analize other word types like prepositions, adverbs, conjunctions, etc.
Title: Re: How to make a text parser?
Post by: Ian Price on 2011-Dec-25
> Go East and kill dwarf with dagger.

I do not know the meaning of "Go"

> east and Kill dwarf with dagger.

I do not know the meaning of "east"

> Kill dwarf

I can not see a dwarf here

> Feck you!!

No need for swearing!!

:P

If you work out a system I have a few suggestions -

1. Ensure that your text routine converts the inputted text to upper or lowercase - that way everything will match your text routine. No need to worry about East/east or EAST etc.

2.  Check the spellings of EVERY word in your vocab. There was one big named adventure on 8bit systems that had a spelling mistake in one of the main objects - if you didn't know this, you couldn't complete the game.

3. Make sure you can save your game. nothing worse than playing an adventure for even 20 minutes and not being able to save/reload your progress.


I used to love adventure games back in the years gone by - made my own in G.A.C. (Graphic Adventure Creator) and The Quill. Also purchased loads of Magnetic Scrolls, Firebird and other adventures. The only one I couldn't get into was a DiscWorld game, GnomeRanger (IIRC). I've never really liked Pratchett, despite trying.

Anyway, good luck :)
Title: Re: How to make a text parser?
Post by: erico on 2011-Dec-25
maybe try out a simple parser?
it makes wonders... :-*
Title: Re: How to make a text parser?
Post by: matchy on 2011-Dec-25
One of the aspects and nature of classic text adventures is that there literally was no help and, I suppose, discovering the word-list was half the challenge but lost flavor to point and click and look-up lists.

Let's assume that a new style of adventure technique works on casual voice input, like Siri and/or basic A.I. techniques, such as Eliza bot, where certain elements can be recognized. For example, words like what, which, when, why, how are for queries which may depend on when the player is first or third person.

There next step would be learning, where patterns could be established like grouping certain words and turns to establish the users intentions. For example, set the state to find the "time" when ready.

Code (glbasic) Select

Welcome to Cabin Log v1.0
It is dark with a slight breeze.
>WHAT TIME IS IT?
You can not see your watch.
>OPEN THE CURTAINS
The room is alight with the morning sunlight. You look at your watch and it is 7:25am.
>COUGH
You cough.


Code (glbasic) Select

Welcome to Cabin Log v1.0
It is dark with a slight breeze.
>OPEN THE CURTAINS
The room is alight with the morning sunlight.
>LOOK AT WATCH
It reads 7:25am.
>COUGH
You cough in the dark.
Title: Re: How to make a text parser?
Post by: doimus on 2011-Dec-25
Quote from: Ian Price on 2011-Dec-25
> Go East and kill dwarf with dagger.


Exactly what I was thinking about. Old Sierra games for example would totally fail on this sentence.

My plan is to have sentence checked for verbs and nouns and act the closest possible action. Regardless of whether that was exactly what player intended. That way at least I avoid the frustration of "You can't. That doesn't work. No way. I do not understand" replies.

In above sentence, it would be relatively easy to sort out "kill" and "go east" verbs and "dwarf" and "dagger" objects and combine that into meaningful commands: "go east",  if player has item "dagger" then "kill dwarf".
That's one sh!tload of string comparing that has to be done, but that's why we have modern CPUs, right?

Also, separating commands into simple sentences is something most of the audience should be fine with. Maybe if parser detects too many verbs and nouns, it then instructs the player to be more verbose. But then, that's another "You can't do that.".


But, the more I think about it, and looking at Facade, I think this stuff is easier than it might seem.

Notice how all the NPC answers in Facade are really, really vague?
I guess it doesn't react directly on specific sentences(commands) but modifies NPC "feelings and emotions" according to players tokens.
Since Facade is purely dialogue oriented, this works great. Player mentions sex, NPC gets uncomfortable. Player mentions sex again, they get more uncomfortable, until player eventually gets thrown out.
Maybe if player poured more and more alcohol before sex talk, stuff woul've gone the other way, who knows?

So basically, it's not like the parser is getting the command "kiss friend's wife".
It actually modifies "emotions":

pseudo
Code (glbasic) Select
"sextalk" -> DEC wife.drive, 1; INC wife.inhibition, 3
"alcohol" -> DEC wife.inhibition, 1
"moar alcohol" -> DEC wife.inhibition, 42
"sextalk" -> INC wife.drive, 12
"kiss" ->
IF wife.drive > 10 AND wife.inhibition < 8
    wife.say("Yeah Baby!")
ELSE
    wife.say("You sick pervert!")
ENDIF


So, it's kind of more an RPG than Adventure game.




Title: Re: How to make a text parser?
Post by: Kitty Hello on 2011-Dec-27
I once started a "Maniac Mansion" Text Adventure.
Here's my stuff, might be helpfull.

Here's how I parse:
Code (glbasic) Select


FUNCTION ParseText%: room$, text$, BYREF verb$, BYREF item1$, BYREF item2$
LOCAL items$[]
verb$=""
item1$=""
item2$=""
LOCAL emergency_item$

// find the verb (first token)
SPLITSTR(text$, items$[], " \t")

IF LEN(items$[])
verb$=LCASE$(items$[0])

IF verb$ = "go"   OR verb$="walk"    OR verb$="enter" THEN verb$="go"
IF verb$ = "look" OR verb$="examine" OR verb$="read"  THEN verb$="look"
IF verb$ = "pick" OR verb$="grab"    OR verb$="get"   THEN verb$="take"
IF verb$ = "list" OR verb$="inventory" OR verb$="items"  THEN verb$="inventory"
IF verb$ = "turn" AND LEN(items$[])>1 THEN INC verb$, " "+items$[1] // on/off?

IF LEN(items$)>1 THEN emergency_item$ = items$[-1]

REDIM items$[0]
ELSE
RETURN FALSE
ENDIF


// find all possible items at this place
GetObjectsAtPlace(room$, items$[])

// and places
WhereCanYouGo(room$, items$[])

// find the leftmost part of an item
text$ = LCASE$(text$)
LOCAL pos1%=-1, pos2%=-1

FOREACH it$ IN items$[]
LOCAL pos = INSTR(text$, LCASE$(it$), 0)
IF pos>=0 AND (pos1<0 OR pos<pos1 OR (pos=pos1 AND LEN(it$)>LEN(item1$)) )
pos1=pos
item1$ = it$
ENDIF
NEXT
IF pos1<0 THEN RETURN TRUE

FOREACH it$ IN items$[]
LOCAL pos = INSTR(text$, LCASE$(it$), pos1+1)
IF pos>0 AND (pos2<0 OR pos<pos2 OR (pos=pos2 AND LEN(it$)>LEN(item2$)) )
pos2=pos
item2$ = it$
ENDIF
NEXT

RETURN TRUE
ENDFUNCTION



Basically I have a list of objects that you can access at this point. And then I go:
VERB blah blah OBJECT1 blah blah OBJECT2 blah blah...

and return "VERB OBJECT1 OBJECT2".

Example:
go down the scary road towards the mansion, but be very carefully.
Verb = 1st word = "GO"
next known object is "MANSION" -> GO MANSION.

Or:
Use the paint remover on the stain on the wall.
-> USE PAINT_REMOVER WALL







[attachment deleted by admin]
Title: Re: How to make a text parser?
Post by: Kaniu on 2011-Dec-27
http://h2g2.com/dna/h2g2/A20600641 might be of interest to you.
Title: Re: How to make a text parser?
Post by: erico on 2011-Dec-28
Quote from: Kaniu on 2011-Dec-27
http://h2g2.com/dna/h2g2/A20600641 might be of interest to you.

great read!
Title: Re: How to make a text parser?
Post by: Crivens on 2011-Dec-28
QuoteThe only one I couldn't get into was a DiscWorld game, GnomeRanger
That had an awesome parser if I remember rightly (even on Speccy48). Wasn't anything to do with Discworld though if I remember rightly...

Cheers
Title: Re: How to make a text parser?
Post by: Ian Price on 2011-Dec-28
Maybe it wasn't GnomeRanger then (I had absolutely loads of adventure games), but it definitely was a DiscWorld game that I hated, as I remember having to type "rimward" or "hubward" or something like that and other disc related directions. It annoyed me in nano-seconds.
Title: Re: How to make a text parser?
Post by: Crivens on 2011-Dec-28
Oh yeah I remember that. Wasn't it Colour of magic?

Did you ever use PAW? I got that one instead of the older adventure writers. Came in a massive plastic box that looked like an old book. And squeaked when you opened it if I remember rightly :) It inspired me to write a mouse driven adventure creator for the Mac at the time (Hypercard was an amazingly English language - even with variables that had spaces in them!) for my CS final year project. Wow the amount of hours I spent on that. Think was like 50% sleeping, 30% beer and women, 19% final year project and 1% lectures :) Aaaaahhhh....

Cheers
Title: Re: How to make a text parser?
Post by: Ian Price on 2011-Dec-28
QuoteOh yeah I remember that. Wasn't it Colour of magic?
It might have been. It was the late 80's and I've slept a bit since then.

Never used PAW. GAC was my tool of choice and superb it was too.
Title: Re: How to make a text parser?
Post by: Crivens on 2011-Dec-29
Think the guy who did gac then went on to improve on it with paw. Either that or it was the quill guy

Cheers