Difference between revisions of "Robust Parse Part 3:Prepositions"

From SCI Wiki
Jump to navigationJump to search
 
 
(One intermediate revision by the same user not shown)
Line 157: Line 157:
  
 
That's it!  Now our game handles prepositions properly.
 
That's it!  Now our game handles prepositions properly.
 +
 +
 
  
 
[[Category:The SCI Parser]]
 
[[Category:The SCI Parser]]
 
[[Category:Examples]]
 
[[Category:Examples]]
 
[[Category:Tutorials]]
 
[[Category:Tutorials]]

Latest revision as of 16:56, 5 August 2013

Robust Parse Part 3: Prepositions

Up to this point, we've sort of glossed over prepostions. I briefly touched on them in the first part of this series, when we took the verb 'climb' and created two different verb definitions so we could get differentiation between 'climb up' and 'climb down'. We also faked a bit of preposition handling by burying them inside our 'verb name' text resource.

So, what happens if we run the last Robust Parse demo game and try the following inputs:

look in
run with
listen to

You'll see that we get the same output as you would get if we simply typed 'look', 'run' or 'listen'. Well, this doesn't make much sense, does it? In order to address this shortcoming, we'll put in some additional parsing of the user input the same way we extract the adjectives and nouns. Again, I'm not going to post the code here, but download the newest Robust Parse demo & you can review the code there. The script which has been modified is the GetObjDescrip.

Next, we'll have to set up some additional parsing of said strings in the ParseVerb script. Here are the relevant sections for LOOK, RUN and LISTEN.

Code:
	/********** LISTEN **********/
	(if(Said('listen/*/*>'))
	   = Verb VERB_LISTEN
	   = unneededSecond TRUE	
	)
 
        (if(Said('listen<*/[/*]>'))  // Here is our new Said() to handle prepositions
           = Verb VERB_LISTEN
           = needNoun TRUE
        )
	
	(if(Said('listen/*[/!*]>')) 
	   = Verb VERB_LISTEN	
	)
	
	(if(Said('listen[/!*]>'))
	   = Verb VERB_LISTEN	
	)
	
	/********** EXAMINE **********/
	(if(Said('(examine,look)/*/*>'))
	   = Verb VERB_LOOK
	   = unneededSecond TRUE
	)   	
 
        (if(Said('(examine,look)<*/[/*]>'))  // Here is our new Said() to handle prepositions
           = Verb VERB_LOOK
           = needNoun TRUE
        )
	
	(if(Said('(examine,look)/*[/*]>'))
	   = Verb VERB_LOOK
	)   
	
	(if(Said('(examine,look)[/*]>'))
	   = Verb VERB_LOOK
	)   
	 
	/********** RUN **********/
	(if(Said('run/*/*>'))
	   = Verb VERB_RUN
	   = unneededNoun TRUE
	   = unneededSecond TRUE
	)
 
        (if(Said('run<*/[/*]>')) // Here is our new Said() to handle prepositions
           = Verb VERB_RUN
           = needNoun TRUE
        )
	
        (if(Said('run/*[/!*]>')) 
           = Verb VERB_RUN	
           = unneededNoun TRUE
	)
	
	(if(Said('run[/!*]>'))
	   = Verb VERB_RUN
	)

Next, we need to modify the RobustPragmaFail script. I've left the old code commented out to see the comparisons:

Code:
/******************************************************************************
 RobustPragmaFail.sc
 ******************************************************************************/
(include "sci.sh")
(include "game.sh")
/******************************************************************************/
(script ROBUSTPRAGMAFAIL_SCRIPT)

(use "main")
(use "controls")
/******************************************************************************/
(procedure public (RobustPragmaFail) 
  (if(== needNoun TRUE and == nounSaidCnt UNSET)
       //FormatPrint("What do you want to %s?" VERB_NAMES Verb)
  	 FormatPrint("What do you want to %s?" @verbDescrip)
  	 return(TRUE)
  )
 
  (if(== needSecond TRUE and == secondSaidCnt UNSET)
       //FormatPrint("What do you want to %s the %s with?" VERB_NAMES Verb @nounDescrip)
  	 FormatPrint("What do you want to %s the %s with?" @verbDescrip @nounDescrip)
  	 return(TRUE)
  )
  
  (if(== unneededNoun TRUE and <> nounSaidCnt UNSET)
       //FormatPrint("I understood only as far as you wanting to %s." VERB_NAMES Verb)
  	 FormatPrint("I understood only as far as you wanting to %s." @verbDescrip)
  	 return(TRUE)
  )
  
  (if(== unneededSecond TRUE and <> secondSaidCnt UNSET)
       //FormatPrint("I understood only as far as you wanting to %s the %s." VERB_NAMES Verb @secondDescrip)
  	 FormatPrint("I understood only as far as you wanting to %s the %s." @verbDescrip @secondDescrip)
  	 return(TRUE)
  )
  
  return(FALSE)
)

As you can see, we've removed all the references to the VERB_NAMES text resource & replaced them with a string similar to our @nounDescrip and @secondDescrip. Finally, we need to change our VERB_NAMES text resources to only hold the verb names themselves. Before this, we occassionally putting verbs and prepositions in this text resource.

Code:
/***************** Text.000 (verb names) *****************/
Num	Text
------	----- 
0	cut
1	run
2	dig
3	listen
4	look
5	run
6	climb

In addition to cleaning up the VERB_NAMES text resource, we also need to create a new text resource for all the prepositions in our game.

Code:
/***************** Text.004 (preposition names) *****************/
Num	Text
------	----- 
0	down
1	with
2	up
3	in
4	to
5	at

Let's put it to the test. Fire up the Robust Parse demo game and try these inputs:

'look in' results in 'What do you want to look in?'
'run up' results in 'What do you want to run up?'
'listen to' results in 'What do you want to listen to?'

That's it! Now our game handles prepositions properly.