| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library: parser |
| 7 |
* |
| 8 |
* This modules defines the language-independent parts of the command |
| 9 |
* parser. |
| 10 |
* |
| 11 |
* Portions based on xiny.t, copyright 2002 by Steve Breslin and |
| 12 |
* incorporated by permission. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "adv3.h" |
| 16 |
#include "tok.h" |
| 17 |
#include <dict.h> |
| 18 |
#include <gramprod.h> |
| 19 |
#include <strcomp.h> |
| 20 |
|
| 21 |
|
| 22 |
/* ------------------------------------------------------------------------ */ |
| 23 |
/* |
| 24 |
* property we add to ResolveInfo to store the remaining items from an |
| 25 |
* ambiguous list |
| 26 |
*/ |
| 27 |
property extraObjects; |
| 28 |
|
| 29 |
|
| 30 |
/* ------------------------------------------------------------------------ */ |
| 31 |
/* |
| 32 |
* ResolveResults - an instance of this class is passed to the |
| 33 |
* resolveNouns() routine to receive the results of the resolution. |
| 34 |
* |
| 35 |
* This class's main purpose is to virtualize the handling of error or |
| 36 |
* warning conditions during the resolution process. The ResolveResults |
| 37 |
* object is created by the initiator of the resolution, so it allows |
| 38 |
* the initiator to determine how errors are to be handled without |
| 39 |
* having to pass flags down through the match tree. |
| 40 |
*/ |
| 41 |
class ResolveResults: object |
| 42 |
/* |
| 43 |
* Instances must provide the following methods: |
| 44 |
* |
| 45 |
* noVocabMatch(action, txt) - there are no objects in scope matching |
| 46 |
* the given noun phrase vocabulary. This is "worse" than noMatch(), |
| 47 |
* in the sense that this indicates that the unqualified noun phrase |
| 48 |
* simply doesn't refer to any objects in scope, whereas noMatch() |
| 49 |
* means that some qualification applied to the vocabulary ruled out |
| 50 |
* any matches. 'txt' is the original text of the noun phrase. |
| 51 |
* |
| 52 |
* noMatch(action, txt) - there are no objects in scope matching the |
| 53 |
* noun phrase. This is used in cases where we eliminate all |
| 54 |
* possible matches because of qualifications or constraints, so it's |
| 55 |
* possible that the noun phrase vocabulary, taken by itself, does |
| 56 |
* match some object; it's just that when the larger noun phrase |
| 57 |
* context is considered, there's nothing that matches. |
| 58 |
* |
| 59 |
* noMatchPossessive(action, txt) - same as noMatch, but the |
| 60 |
* unmatched phrase is qualified with a possessive phrase. For |
| 61 |
* ranking matches, the possessive version ranks ahead of treating |
| 62 |
* the possessive words as raw vocabulary when neither matches, since |
| 63 |
* it's more informative to report that we can't even match the |
| 64 |
* underlying qualified noun phrase, let alone the whole phrase with |
| 65 |
* qualification. |
| 66 |
* |
| 67 |
* noMatchForAll() - there's nothing matching "all" |
| 68 |
* |
| 69 |
* noMatchForAllBut() - there's nothing matching "all except..." |
| 70 |
* (there might be something matching all, but there's nothing left |
| 71 |
* when the exception list is applied) |
| 72 |
* |
| 73 |
* noMatchForListBut() - there's nothing matching "<list> except..." |
| 74 |
* |
| 75 |
* noteEmptyBut() - a "but" (or "except") list matches nothing. We |
| 76 |
* don't consider this an error, but we rate an interpretation with a |
| 77 |
* non-empty "but" list and complete exclusion of the "all" or "any" |
| 78 |
* phrase whose objects are excluded by the "but" higher than one |
| 79 |
* with an empty "but" list and a non-empty all/any list - we do this |
| 80 |
* because it probably means that we came up with an incorrect |
| 81 |
* interpretation of the "but" phrase in the empty "but" list case |
| 82 |
* and failed to exclude things we should have excluded. |
| 83 |
* |
| 84 |
* noMatchForPronoun(typ, txt) - there's nothing matching a pronoun. |
| 85 |
* 'typ' is one of the PronounXxx constants, and 'txt' is the text of |
| 86 |
* the word used in the command. |
| 87 |
* |
| 88 |
* allNotAllowed() - the command contained the word ALL (or a |
| 89 |
* synonym), but the verb doesn't allow ALL to be used in its noun |
| 90 |
* phrases. |
| 91 |
* |
| 92 |
* reflexiveNotAllowed(typ, txt) - the reflexive pronoun isn't |
| 93 |
* allowed in this context. This usually means that the pronoun was |
| 94 |
* used with an intransitive or single-object action. 'typ' is one |
| 95 |
* of the PronounXxx constants, and 'txt' is the text of the word |
| 96 |
* used. |
| 97 |
* |
| 98 |
* wrongReflexive(typ, txt) - the reflexive pronoun doesn't agree |
| 99 |
* with its referent in gender, number, or some other way. |
| 100 |
* |
| 101 |
* noMatchForPossessive(owner, txt) - there's nothing matching the |
| 102 |
* phrase 'txt' owned by the resolved possessor object 'owner'. Note |
| 103 |
* that 'owner' is a list, since we can have plural possessive |
| 104 |
* qualifier phrases. |
| 105 |
* |
| 106 |
* noMatchForLocation(loc, txt) - there's nothing matching 'txt' in |
| 107 |
* the location object 'loc'. This is used when a noun phrase is |
| 108 |
* explicitly qualified by location ("the book on the table"). |
| 109 |
* |
| 110 |
* noteBadPrep() - we have a noun phrase or other phrase |
| 111 |
* incorporating an invalid prepositional phrase structure. This is |
| 112 |
* called from "badness" rules that are set up to match phrases with |
| 113 |
* embedded prepositions, as a last resort when no valid |
| 114 |
* interpretation can be found. |
| 115 |
* |
| 116 |
* nothingInLocation(loc) - there's nothing in the given location |
| 117 |
* object. This is used when we try to select the one object or all |
| 118 |
* of the objects in a given container, but the container doesn't |
| 119 |
* actually have any contents. |
| 120 |
* |
| 121 |
* ambiguousNounPhrase(keeper, asker, txt, matchLst, fullMatchList, |
| 122 |
* scopeList, requiredNum, resolver) - an ambiguous noun phrase was |
| 123 |
* entered: the noun phrase matches multiple objects that are all |
| 124 |
* equally qualified, but we only want the given exact number of |
| 125 |
* matches. 'asker' is a ResolveAsker object that we'll use to |
| 126 |
* generate any prompts; if no customization is required, simply pass |
| 127 |
* the base ResolveAsker. 'txt' is the original text of the noun |
| 128 |
* list in the command, which the standard prompt messages can use to |
| 129 |
* generate their questions. 'matchLst' is a list of the qualified |
| 130 |
* objects matching the phrase, with only one object included for |
| 131 |
* each set of equivalents in the original full list; 'fullMatchList' |
| 132 |
* is the full list of matches, including each copy of equivalents; |
| 133 |
* 'scopeList' is the list of everything in scope that matched the |
| 134 |
* original phrase, including illogical items. If it's desirable to |
| 135 |
* interact with the user at this point, prompt the user to resolve |
| 136 |
* the list, and return a new list with the results. If no prompting |
| 137 |
* is desired, the original list can be returned. If it isn't |
| 138 |
* possible to determine the final set of objects, and a final set of |
| 139 |
* objects is required (this is up to the subclass to determine), a |
| 140 |
* parser exception should be thrown to stop further processing of |
| 141 |
* the command. 'keeper' is an AmbigResponseKeeper object, which is |
| 142 |
* usually simply the production object itself; each time we parse an |
| 143 |
* interactive response (if we are interactive at all), we'll call |
| 144 |
* addAmbigResponse() on the calling production object to add it to |
| 145 |
* the saved list of responses, and we'll call getAmbigResponses() to |
| 146 |
* find previous answers to the same question, in case of |
| 147 |
* re-resolving the phrase with 'again' or the like. |
| 148 |
* |
| 149 |
* unknownNounPhrase(match, resolver) - a noun phrase that doesn't |
| 150 |
* match any known noun phrase syntax was entered. 'match' is the |
| 151 |
* production match tree object for the unknown phrase. Returns a |
| 152 |
* list of the resolved objects for the noun phrase, if possible. If |
| 153 |
* it is not possible to resolve the phrase, and a resolution is |
| 154 |
* required (this is up to the subclass to determine), a parser |
| 155 |
* exception should be thrown. |
| 156 |
* |
| 157 |
* getImpliedObject(np, resolver) - a noun phrase was left out |
| 158 |
* entirely. 'np' is the noun phrase production standing in for the |
| 159 |
* missing noun phrase; this is usually an EmptyNounPhraseProd or a |
| 160 |
* subclass. If an object is implicit in the command, or a |
| 161 |
* reasonable default can be assumed, return the implicit or default |
| 162 |
* object or objects. If not, the routine can return nil or can |
| 163 |
* throw an error. The result is a ResolveInfo list. |
| 164 |
* |
| 165 |
* askMissingObject(asker, resolver, responseProd) - a noun phrase |
| 166 |
* was left out entirely, and no suitable default can be found |
| 167 |
* (getImpliedObject has already been called, and that returned nil). |
| 168 |
* If it is possible to ask the player interactively to fill in the |
| 169 |
* missing object, ask the player. If it isn't possible to resolve |
| 170 |
* an object, an error can be thrown, or an empty list can be |
| 171 |
* returned. 'asker' is a ResolveAsker object, which can be used to |
| 172 |
* customize the prompt (if any) that we show; pass the base |
| 173 |
* ResolveAsker if no customization is needed. 'responseProd' is the |
| 174 |
* production to use to parse the response. The return value is the |
| 175 |
* root match tree object of the player's interactive response, with |
| 176 |
* its 'resolvedObjects' property set to the ResolveInfo list from |
| 177 |
* resolving the player's response. (The routine returns the match |
| 178 |
* tree for the player's response so that, if we must run resolution |
| 179 |
* again on another pass, we can re-resolve the same response without |
| 180 |
* asking the player the same question again.) |
| 181 |
* |
| 182 |
* noteLiteral(txt) - note the text of a literal phrase. When |
| 183 |
* selecting among alternative interpretations of a phrase, we'll |
| 184 |
* favor shorter literals, since treating fewer tokens as literals |
| 185 |
* means that we're actually interpreting more tokens. |
| 186 |
* |
| 187 |
* askMissingLiteral(action, which) - a literal phrase was left out |
| 188 |
* entirely. If possible, prompt interactively for a player response |
| 189 |
* and return the result. If it's not possible to ask for a |
| 190 |
* response, an error can be thrown, or nil can be returned. The |
| 191 |
* return value is simply the text string the player enters. |
| 192 |
* |
| 193 |
* emptyNounPhrase(resolver) - a noun phrase involving only |
| 194 |
* qualifiers was entered ('take the'). In most cases, an exception |
| 195 |
* should be thrown. If the empty phrase can be resolved to an |
| 196 |
* object or list of objects, the resolved list should be returned. |
| 197 |
* |
| 198 |
* zeroQuantity(txt) - a noun phrase referred to a zero quantity of |
| 199 |
* something ("take zero books"). |
| 200 |
* |
| 201 |
* insufficientQuantity(txt, matchList, requiredNumber) - a noun |
| 202 |
* phrase is quantified with a number exceeding the number of objects |
| 203 |
* available: "take five books" when only two books are in scope. |
| 204 |
* |
| 205 |
* uniqueObjectRequired(txt, matchList) - a noun phrase yields more |
| 206 |
* than one object, but only one is allowed. For example, we'll call |
| 207 |
* this if the user attempts to use more than one result for a |
| 208 |
* single-noun phrase (such as by answering a disambiguation question |
| 209 |
* with 'all'). |
| 210 |
* |
| 211 |
* singleObjectRequired(txt) - a noun phrase list was used where a |
| 212 |
* single noun phrase is required. |
| 213 |
* |
| 214 |
* noteAdjEnding() - a noun phrase ends in an adjective. This isn't |
| 215 |
* normally an error, but is usually less desirable than interpreting |
| 216 |
* the same noun phrase as ending in a noun (in other words, if a |
| 217 |
* word can be used as both an adjective and a noun, it is usually |
| 218 |
* better to interpret the word as a noun rather than as an adjective |
| 219 |
* when the word appears at the end of a noun phrase, as long as the |
| 220 |
* noun interpretation matches an object in scope). |
| 221 |
* |
| 222 |
* noteIndefinite() - a noun phrase is phrased as an indefinite ("any |
| 223 |
* book", "one book"), meaning that we can arbitrarily choose any |
| 224 |
* matching object in case of ambiguity. Sometimes, an object will |
| 225 |
* have explicit vocabulary that could be taken to be indefinite: a |
| 226 |
* button labeled "1" could be a "1 button", for example, or a subway |
| 227 |
* might have an "A train". By noting the indefinite interpretation, |
| 228 |
* we can give priority to the alternative definite interpretation. |
| 229 |
* |
| 230 |
* noteMatches(matchList) - notifies the results object that the |
| 231 |
* given list of objects is being matched. This allows the results |
| 232 |
* object to inspect the object list for its strength: for example, |
| 233 |
* by noting the presence of truncated words. This should only be |
| 234 |
* called after the nouns have been resolved to the extent possible, |
| 235 |
* so any disambiguation or selection that is to be performed should |
| 236 |
* be performed before this routine is called. |
| 237 |
* |
| 238 |
* noteMiscWordList(txt) - a noun phrase is made up of miscellaneous |
| 239 |
* words. A miscellaneous word list as a noun phrase has non-zero |
| 240 |
* badness, so we will never use a misc word list unless we can't |
| 241 |
* find any more structured interpretation. In most cases, a |
| 242 |
* miscellaneous word list indicates an invalid phrasing, but in some |
| 243 |
* cases objects might use this unstructured type of noun phrase in |
| 244 |
* conjunction with matchName() to perform dynamic or special-case |
| 245 |
* parsing. |
| 246 |
* |
| 247 |
* notePronoun() - a noun phrase is matching as a pronoun. In |
| 248 |
* general, we prefer a match to an object's explicit vocabulary |
| 249 |
* words to a match to a pronoun phrase: if the game goes to the |
| 250 |
* trouble of including a word explicitly among an object's |
| 251 |
* vocabulary, that's a better match than treating the same word as a |
| 252 |
* generic pronoun. |
| 253 |
* |
| 254 |
* notePlural() - a plural phrase is matching. In some cases we |
| 255 |
* require a single object match, in which case a plural phrase is |
| 256 |
* undesirable. (A plural phrase might still match just one object, |
| 257 |
* though, so it can't be ruled out on structural grounds alone.) |
| 258 |
* |
| 259 |
* beginSingleObjSlot() and endSingleObjSlot() are used to bracket |
| 260 |
* resolution of a noun phrase that needs to be resolved as a single |
| 261 |
* object. We use these to explicitly lower the ranking for plural |
| 262 |
* structural phrasings within these slots. |
| 263 |
* |
| 264 |
* beginTopicSlot() and endTopicSlot() are used to bracket resolution |
| 265 |
* of a noun phrase that's being used as a conversation topic. These |
| 266 |
* are of the *form* of single nouns, but singular and plural words |
| 267 |
* are equivalent, so when we're in a topic we don't consider a |
| 268 |
* plural to be a minus the way we would for an ordinary object noun |
| 269 |
* phrase. |
| 270 |
* |
| 271 |
* incCommandCount() - increment the command counter. This is used |
| 272 |
* to keep track of how many subcommands are in a command tree. |
| 273 |
* |
| 274 |
* noteActorSpecified() - note that the command is explicitly |
| 275 |
* addressed to an actor. |
| 276 |
* |
| 277 |
* noteNounSlots(cnt) - note the number of "noun slots" in the verb |
| 278 |
* phrase. This is the number of objects the verb takes: an |
| 279 |
* intransitive verb has no noun slots; a transitive verb with a |
| 280 |
* direct object only has one; a verb with a direct and indirect |
| 281 |
* object has two. Note this has nothing to do with the number of |
| 282 |
* objects specified in a noun list - TAKE BOX, BOOK, AND BELL has |
| 283 |
* only one noun slot (a direct object) even though the slot is |
| 284 |
* occupied by a list with three objects. |
| 285 |
* |
| 286 |
* noteWeakPhrasing(level) - note that the phrasing is "weak," with |
| 287 |
* the given weakness level - higher is weaker. The exact meaning of |
| 288 |
* the weakness levels is up to the language module to define. The |
| 289 |
* English module, for example, considers VERB IOBJ DOBJ phrasing |
| 290 |
* (with no preposition, as in GIVE BOB BOOK) to be weak when the |
| 291 |
* DOBJ part doesn't have a grammatical marker that clarifies that |
| 292 |
* it's really a separate noun phrase (an article serves this purpose |
| 293 |
* in English: GIVE BOB THE BOOK). |
| 294 |
* |
| 295 |
* allowActionRemapping - returns true if we can remap the action |
| 296 |
* during noun phrase resolution. Remapping is usually allowed only |
| 297 |
* during the actual execution phase, not during the ranking phase. |
| 298 |
* |
| 299 |
* allowEquivalentFiltering - returns true if we can filter an |
| 300 |
* ambiguous resolution list by making an arbitrary choice among |
| 301 |
* equivalent objects. This is normally allowed only during a final |
| 302 |
* resolution phase, not during a tentative resolution phase. |
| 303 |
*/ |
| 304 |
; |
| 305 |
|
| 306 |
/* ------------------------------------------------------------------------ */ |
| 307 |
/* |
| 308 |
* Noun phrase resolver "asker." This type of object can be passed to |
| 309 |
* certain ResolveResults methods in order to customize the messages |
| 310 |
* that the parser generates for interactive prompting. |
| 311 |
*/ |
| 312 |
class ResolveAsker: object |
| 313 |
/* |
| 314 |
* Ask for help disambiguating a noun phrase. This asks which of |
| 315 |
* several possible matching objects was intended. This method has |
| 316 |
* the same parameter list as the equivalent message object method. |
| 317 |
*/ |
| 318 |
askDisambig(targetActor, promptTxt, curMatchList, fullMatchList, |
| 319 |
requiredNum, askingAgain, dist) |
| 320 |
{ |
| 321 |
/* let the target actor's parser message object handle it */ |
| 322 |
targetActor.getParserMessageObj().askDisambig( |
| 323 |
targetActor, promptTxt, curMatchList, fullMatchList, |
| 324 |
requiredNum, askingAgain, dist); |
| 325 |
} |
| 326 |
|
| 327 |
/* |
| 328 |
* Ask for a missing object. This prompts for an object that's |
| 329 |
* structurally required for an action, but which was omitted from |
| 330 |
* the player's command. |
| 331 |
*/ |
| 332 |
askMissingObject(targetActor, action, which) |
| 333 |
{ |
| 334 |
/* let the target actor's parser message object handle it */ |
| 335 |
targetActor.getParserMessageObj().askMissingObject( |
| 336 |
targetActor, action, which); |
| 337 |
} |
| 338 |
; |
| 339 |
|
| 340 |
/* ------------------------------------------------------------------------ */ |
| 341 |
/* |
| 342 |
* The resolveNouns() method returns a list of ResolveInfo objects |
| 343 |
* describing the objects matched to the noun phrase. |
| 344 |
*/ |
| 345 |
class ResolveInfo: object |
| 346 |
construct(obj, flags, np = nil) |
| 347 |
{ |
| 348 |
/* remember the members */ |
| 349 |
obj_ = obj; |
| 350 |
flags_ = flags; |
| 351 |
np_ = np; |
| 352 |
} |
| 353 |
|
| 354 |
/* |
| 355 |
* Look for a ResolveInfo item in a list of ResolveInfo items that |
| 356 |
* is equivalent to us. Returns true if we find such an item, nil |
| 357 |
* if not. |
| 358 |
* |
| 359 |
* Another ResolveInfo is equivalent to us if it refers to the same |
| 360 |
* underlying game object that we do, or if it refers to a game |
| 361 |
* object that is indistinguishable from our underlying game object. |
| 362 |
*/ |
| 363 |
isEquivalentInList(lst) |
| 364 |
{ |
| 365 |
/* |
| 366 |
* if we can find our exact item in the list, or we can find an |
| 367 |
* equivalent object, we have an equivalent |
| 368 |
*/ |
| 369 |
return (lst.indexWhich({x: x.obj_ == obj_}) != nil |
| 370 |
|| lst.indexWhich( |
| 371 |
{x: x.obj_.isVocabEquivalent(obj_)}) != nil); |
| 372 |
} |
| 373 |
|
| 374 |
/* |
| 375 |
* Look for a ResolveInfo item in a list of ResolveInfo items that |
| 376 |
* is equivalent to us according to a particular Distinguisher. |
| 377 |
*/ |
| 378 |
isDistEquivInList(lst, dist) |
| 379 |
{ |
| 380 |
/* |
| 381 |
* if we can find our exact item in the list, or we can find an |
| 382 |
* equivalent object, we have an equivalent |
| 383 |
*/ |
| 384 |
return (lst.indexWhich({x: x.obj_ == obj_}) != nil |
| 385 |
|| lst.indexWhich( |
| 386 |
{x: !dist.canDistinguish(x.obj_, obj_)}) != nil); |
| 387 |
} |
| 388 |
|
| 389 |
/* the object matched */ |
| 390 |
obj_ = nil |
| 391 |
|
| 392 |
/* flags describing how we matched the object */ |
| 393 |
flags_ = 0 |
| 394 |
|
| 395 |
/* |
| 396 |
* By default, each ResolveInfo counts as one object, for the |
| 397 |
* purposes of a quantity specifier (as in "five coins" or "both |
| 398 |
* hats"). However, in some cases, a single resolved object might |
| 399 |
* represent a collection of discrete objects and thus count as more |
| 400 |
* than one for the purposes of the quantifier. |
| 401 |
*/ |
| 402 |
quant_ = 1 |
| 403 |
|
| 404 |
/* |
| 405 |
* The possessive ranking, if applicable. If this object is |
| 406 |
* qualified by a possessive phrase ("my books"), we'll set this to |
| 407 |
* a value indicating how strongly the possession applies to our |
| 408 |
* object: 2 indicates that the object is explicitly owned by the |
| 409 |
* object indicated in the possessive phrase, 1 indicates that it's |
| 410 |
* directly held by the named possessor but not explicitly owned, |
| 411 |
* and 0 indicates all else. In cases where there's no posessive |
| 412 |
* qualifier, this will simply be zero. |
| 413 |
*/ |
| 414 |
possRank_ = 0 |
| 415 |
|
| 416 |
/* the pronoun type we matched, if any (as a PronounXxx enum) */ |
| 417 |
pronounType_ = nil |
| 418 |
|
| 419 |
/* the noun phrase we parsed to come up with this object */ |
| 420 |
np_ = nil |
| 421 |
|
| 422 |
/* |
| 423 |
* The pre-calculated multi-object announcement text for this object. |
| 424 |
* When we iterate over the object list in a command with multiple |
| 425 |
* direct or indirect objects (TAKE THE BOOK, BELL, AND CANDLE), we |
| 426 |
* calculate the little announcement messages ("book:") for the |
| 427 |
* objects BEFORE we execute the actual commands. We then use the |
| 428 |
* pre-calculated announcements during our iteration. This ensures |
| 429 |
* consistency in the basis for choosing the names, which is |
| 430 |
* important in cases where the names include state-dependent |
| 431 |
* information for the purposes of distinguishing one object from |
| 432 |
* another. The relevant state can change over the course of |
| 433 |
* executing the command on the objects in the iteration, so if we |
| 434 |
* calculated the names on the fly we could end up with inconsistent |
| 435 |
* naming. The user thinks of the objects in terms of their state at |
| 436 |
* the start of the command, so the pre-calculation approach is not |
| 437 |
* only more internally consistent, but is also more consistent with |
| 438 |
* the user's perspective. |
| 439 |
*/ |
| 440 |
multiAnnounce = nil |
| 441 |
; |
| 442 |
|
| 443 |
/* |
| 444 |
* Intersect two resolved noun lists, returning a list consisting only |
| 445 |
* of the unique objects from the two lists. |
| 446 |
*/ |
| 447 |
intersectNounLists(lst1, lst2) |
| 448 |
{ |
| 449 |
/* we don't have any results yet */ |
| 450 |
local ret = []; |
| 451 |
|
| 452 |
/* |
| 453 |
* run through each element of the first list to see if it has a |
| 454 |
* matching object in the second list |
| 455 |
*/ |
| 456 |
foreach(local cur in lst1) |
| 457 |
{ |
| 458 |
local other; |
| 459 |
/* |
| 460 |
* if this element's object occurs in the second list, we can |
| 461 |
* include it in the result list |
| 462 |
*/ |
| 463 |
if ((other = lst2.valWhich({x: x.obj_ == cur.obj_})) != nil) |
| 464 |
{ |
| 465 |
/* if one or the other has a noun phrase, keep it */ |
| 466 |
local np = cur.np_ ?? other.np_; |
| 467 |
|
| 468 |
/* |
| 469 |
* include this one in the result list, with the combined |
| 470 |
* flags from the two original entries |
| 471 |
*/ |
| 472 |
ret += new ResolveInfo(cur.obj_, cur.flags_ | other.flags_, np); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
/* return the result list */ |
| 477 |
return ret; |
| 478 |
} |
| 479 |
|
| 480 |
/* |
| 481 |
* Extract the objects from a list obtained with resolveNouns(). |
| 482 |
* Returns a list composed only of the objects in the resolution |
| 483 |
* information list. |
| 484 |
*/ |
| 485 |
getResolvedObjects(lst) |
| 486 |
{ |
| 487 |
/* |
| 488 |
* return a list composed only of the objects from the ResolveInfo |
| 489 |
* structures |
| 490 |
*/ |
| 491 |
return lst.mapAll({x: x.obj_}); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
/* ------------------------------------------------------------------------ */ |
| 496 |
/* |
| 497 |
* The basic production node base class. We'll use this as the base |
| 498 |
* class for all of our grammar rule match objects. |
| 499 |
*/ |
| 500 |
class BasicProd: object |
| 501 |
/* get the original text of the command for this match */ |
| 502 |
getOrigText() |
| 503 |
{ |
| 504 |
/* if we have no token list, return an empty string */ |
| 505 |
if (tokenList == nil) |
| 506 |
return ''; |
| 507 |
|
| 508 |
/* build the string based on my original token list */ |
| 509 |
return cmdTokenizer.buildOrigText(getOrigTokenList()); |
| 510 |
} |
| 511 |
|
| 512 |
/* get my original token list, in canonical tokenizer format */ |
| 513 |
getOrigTokenList() |
| 514 |
{ |
| 515 |
/* |
| 516 |
* return the subset of the full token list from my first token |
| 517 |
* to my last token |
| 518 |
*/ |
| 519 |
return nilToList(tokenList).sublist( |
| 520 |
firstTokenIndex, lastTokenIndex - firstTokenIndex + 1); |
| 521 |
} |
| 522 |
|
| 523 |
/* |
| 524 |
* Set my original token list. This replaces the actual token list |
| 525 |
* we matched from the parser with a new token list provided by the |
| 526 |
* caller. |
| 527 |
*/ |
| 528 |
setOrigTokenList(toks) |
| 529 |
{ |
| 530 |
tokenList = toks; |
| 531 |
firstTokenIndex = 1; |
| 532 |
lastTokenIndex = toks.length(); |
| 533 |
} |
| 534 |
|
| 535 |
/* |
| 536 |
* Can this object match tree resolve to the given object? We'll |
| 537 |
* resolve the phrase as though it were a topic phrase, then look for |
| 538 |
* the object among the matches. |
| 539 |
*/ |
| 540 |
canResolveTo(obj, action, issuingActor, targetActor, which) |
| 541 |
{ |
| 542 |
/* set up a topic resolver */ |
| 543 |
local resolver = new TopicResolver( |
| 544 |
action, issuingActor, targetActor, self, which, |
| 545 |
action.createTopicQualifierResolver(issuingActor, targetActor)); |
| 546 |
|
| 547 |
/* |
| 548 |
* set up a results object - use a tentative results object, |
| 549 |
* since we're only looking at the potential matches, not doing a |
| 550 |
* full resolution pass |
| 551 |
*/ |
| 552 |
local results = new TentativeResolveResults(issuingActor, targetActor); |
| 553 |
|
| 554 |
/* resolve the phrase as a topic */ |
| 555 |
local match = resolveNouns(resolver, results); |
| 556 |
|
| 557 |
/* |
| 558 |
* make sure it's packaged in the canonical topic form, as a |
| 559 |
* ResolvedTopic object |
| 560 |
*/ |
| 561 |
match = resolver.packageTopicList(match, self); |
| 562 |
|
| 563 |
/* if the topic can match it, it's a possible resolution */ |
| 564 |
return (match != nil |
| 565 |
&& match.length() > 0 |
| 566 |
&& match[1].obj_.canMatchObject(obj)); |
| 567 |
} |
| 568 |
|
| 569 |
/* |
| 570 |
* Is this match a match to the special syntax for a custom missing |
| 571 |
* object query? This returns true if the match has a wording that |
| 572 |
* strongly distinguishes it from an ordinary new command. In the |
| 573 |
* English parser, for example, this returns true for the |
| 574 |
* PrepSingleTopicProd matches (e.g., inSingleNoun) if the phrase |
| 575 |
* starts with the preposition for the match. |
| 576 |
* |
| 577 |
* This property is used when we ask a missing object question: |
| 578 |
* |
| 579 |
*. >dig |
| 580 |
*. What do you want to dig in? |
| 581 |
*. |
| 582 |
*. >in the dirt |
| 583 |
* |
| 584 |
* In English, the DIG command sets up to receive a response phrased |
| 585 |
* as "in <noun phrase>" - that's done by setting the response |
| 586 |
* production to inSingleNoun. In this case, "in the dirt" would |
| 587 |
* return true, since that's pretty clearly a match to the expected |
| 588 |
* inSingleNoun syntax. In contrast, "the dirt" would return false, |
| 589 |
* since that's just a noun phrase without the special wording for |
| 590 |
* this particular verb. |
| 591 |
*/ |
| 592 |
isSpecialResponseMatch = nil |
| 593 |
|
| 594 |
/* |
| 595 |
* Grammar match objects that come from a GrammarProd.parseTokens() |
| 596 |
* call will always have a set of properties indicating which tokens |
| 597 |
* from the input matched the grammar rule. However, we sometimes |
| 598 |
* synthesize match trees internally rather than getting them from |
| 599 |
* parser input; for synthesized trees, the parser obviously won't |
| 600 |
* supply those properties for us, so we need to define suitable |
| 601 |
* defaults that synthesized match tree nodes can inherit. |
| 602 |
*/ |
| 603 |
firstTokenIndex = 0 |
| 604 |
lastTokenIndex = 0 |
| 605 |
; |
| 606 |
|
| 607 |
|
| 608 |
/* ------------------------------------------------------------------------ */ |
| 609 |
/* |
| 610 |
* Basic disambiguation production class |
| 611 |
*/ |
| 612 |
class DisambigProd: BasicProd |
| 613 |
/* |
| 614 |
* Remove the "ambiguous" flags from a result list. This can be |
| 615 |
* used to mark the response to a disambiguation query as no longer |
| 616 |
* ambiguous. |
| 617 |
*/ |
| 618 |
removeAmbigFlags(lst) |
| 619 |
{ |
| 620 |
/* remove the "unclear disambig" flag from each result item */ |
| 621 |
foreach (local cur in lst) |
| 622 |
cur.flags_ &= ~UnclearDisambig; |
| 623 |
|
| 624 |
/* return the list */ |
| 625 |
return lst; |
| 626 |
} |
| 627 |
; |
| 628 |
|
| 629 |
/* ------------------------------------------------------------------------ */ |
| 630 |
/* |
| 631 |
* Base class for "direction" productions. Each direction (the compass |
| 632 |
* directions, the vertical directions, the shipboard directions, and so |
| 633 |
* on) must have an associated grammar rule, which must produce one of |
| 634 |
* these. This should be subclassed with grammar rules like this: |
| 635 |
* |
| 636 |
* grammar directionName: 'north' | 'n' : DirectionProd |
| 637 |
*. dir = northDirection |
| 638 |
*. ; |
| 639 |
*/ |
| 640 |
class DirectionProd: BasicProd |
| 641 |
/* |
| 642 |
* Each direction-specific grammar rule subclass must set this |
| 643 |
* property to the associated direction object (northDirection, |
| 644 |
* etc). |
| 645 |
*/ |
| 646 |
dir = nil |
| 647 |
; |
| 648 |
|
| 649 |
|
| 650 |
/* ------------------------------------------------------------------------ */ |
| 651 |
/* |
| 652 |
* The base class for commands. A command is the root of the grammar |
| 653 |
* match tree for a single action. A command line can consist of a |
| 654 |
* number of commands joined with command separators; in English, |
| 655 |
* command separators are things like periods, semicolons, commas, and |
| 656 |
* the words "and" and "then". |
| 657 |
*/ |
| 658 |
class CommandProd: BasicProd |
| 659 |
hasTargetActor() |
| 660 |
{ |
| 661 |
/* |
| 662 |
* By default, a command production does not include a |
| 663 |
* specification of a target actor. Command phrases that |
| 664 |
* contain syntax specifically directing the command to a target |
| 665 |
* actor should return true here. |
| 666 |
*/ |
| 667 |
return nil; |
| 668 |
} |
| 669 |
|
| 670 |
/* |
| 671 |
* Get the match tree for the target actor phrase, if any. By |
| 672 |
* default, we have no target actor phrase, so just return nil. |
| 673 |
*/ |
| 674 |
getActorPhrase = nil |
| 675 |
|
| 676 |
/* |
| 677 |
* "Execute" the actor phrase. This lets us know that the parser |
| 678 |
* has decided to use our phrasing to specify the target actor. |
| 679 |
* We're not required to do anything here; it's just a notification |
| 680 |
* for subclass use. Since we don't have a target actor phrase at |
| 681 |
* all, we obviously don't need to do anything here. |
| 682 |
*/ |
| 683 |
execActorPhrase(issuingActor) { } |
| 684 |
; |
| 685 |
|
| 686 |
/* |
| 687 |
* A first-on-line command. The first command on a command line can |
| 688 |
* optionally start with an actor specification, to give orders to the |
| 689 |
* actor. |
| 690 |
*/ |
| 691 |
class FirstCommandProd: CommandProd |
| 692 |
countCommands(results) |
| 693 |
{ |
| 694 |
/* count commands in the underlying command */ |
| 695 |
cmd_.countCommands(results); |
| 696 |
} |
| 697 |
|
| 698 |
getTargetActor() |
| 699 |
{ |
| 700 |
/* |
| 701 |
* we have no actor specified explicitly, so it's the current |
| 702 |
* player character |
| 703 |
*/ |
| 704 |
return libGlobal.playerChar; |
| 705 |
} |
| 706 |
|
| 707 |
/* |
| 708 |
* The tokens of the entire command except for the target actor |
| 709 |
* specification. By default, we take all of the tokens starting |
| 710 |
* with the first command's first token and running to the end of |
| 711 |
* the token list. This assumes that the target actor is specified |
| 712 |
* at the beginning of the command - languages that use some other |
| 713 |
* word ordering must override this accordingly. |
| 714 |
*/ |
| 715 |
getCommandTokens() |
| 716 |
{ |
| 717 |
return tokenList.sublist(cmd_.firstTokenIndex); |
| 718 |
} |
| 719 |
|
| 720 |
/* |
| 721 |
* Resolve my first action. This returns an instance of a subclass |
| 722 |
* of Action that represents the resolved action. We'll ask our |
| 723 |
* first subcommand to resolve its action. |
| 724 |
*/ |
| 725 |
resolveFirstAction(issuingActor, targetActor) |
| 726 |
{ |
| 727 |
return cmd_.resolveFirstAction(issuingActor, targetActor); |
| 728 |
} |
| 729 |
|
| 730 |
/* resolve nouns in the command */ |
| 731 |
resolveNouns(issuingActor, targetActor, results) |
| 732 |
{ |
| 733 |
/* resolve nouns in the underlying command */ |
| 734 |
cmd_.resolveNouns(issuingActor, targetActor, results); |
| 735 |
|
| 736 |
/* count our commands */ |
| 737 |
countCommands(results); |
| 738 |
} |
| 739 |
|
| 740 |
/* |
| 741 |
* Does this command end a sentence? The exact meaning of a |
| 742 |
* sentence may vary by language; in English, a sentence ends with |
| 743 |
* certain punctuation marks (a period, a semicolon, an exclamation |
| 744 |
* point). |
| 745 |
*/ |
| 746 |
isEndOfSentence() |
| 747 |
{ |
| 748 |
/* ask the underlying command phrase */ |
| 749 |
return cmd_.isEndOfSentence(); |
| 750 |
} |
| 751 |
|
| 752 |
/* |
| 753 |
* Get the token index of the first command separator token. This |
| 754 |
* is the first token that is not part of the underlying command. |
| 755 |
*/ |
| 756 |
getCommandSepIndex() |
| 757 |
{ |
| 758 |
/* get the separator index from the underlying command */ |
| 759 |
return cmd_.getCommandSepIndex(); |
| 760 |
} |
| 761 |
|
| 762 |
/* |
| 763 |
* get the token index of the next command - this is the index of |
| 764 |
* the next token after our conjunction if we have one, or after our |
| 765 |
* command if we don't have a conjunction |
| 766 |
*/ |
| 767 |
getNextCommandIndex() |
| 768 |
{ |
| 769 |
/* get the next command index from the underlying command */ |
| 770 |
return cmd_.getNextCommandIndex(); |
| 771 |
} |
| 772 |
; |
| 773 |
|
| 774 |
/* |
| 775 |
* Define the simplest grammar rule for a first-on-line command phrase, |
| 776 |
* which is just an ordinary command phrase. The language-specific |
| 777 |
* grammar must define any other alternatives; specifically, the |
| 778 |
* language might provide an "actor, command" syntax to direct a command |
| 779 |
* to a particular actor. |
| 780 |
*/ |
| 781 |
grammar firstCommandPhrase(commandOnly): commandPhrase->cmd_ |
| 782 |
: FirstCommandProd |
| 783 |
; |
| 784 |
|
| 785 |
/* |
| 786 |
* A command with an actor specification. This should be instantiated |
| 787 |
* with grammar rules in a language-specific module. |
| 788 |
* |
| 789 |
* Instantiating grammar rules should set property actor_ to the actor |
| 790 |
* match tree, and cmd_ to the underlying 'commandPhrase' production |
| 791 |
* match tree. |
| 792 |
*/ |
| 793 |
class CommandProdWithActor: CommandProd |
| 794 |
hasTargetActor() |
| 795 |
{ |
| 796 |
/* this command explicitly specifies an actor */ |
| 797 |
return true; |
| 798 |
} |
| 799 |
getTargetActor() |
| 800 |
{ |
| 801 |
/* return my resolved actor object */ |
| 802 |
return resolvedActor_; |
| 803 |
} |
| 804 |
getActorPhrase() |
| 805 |
{ |
| 806 |
/* return the actor phrase production */ |
| 807 |
return actor_; |
| 808 |
} |
| 809 |
|
| 810 |
/* |
| 811 |
* Execute the target actor phrase. This is a notification, for use |
| 812 |
* by subclasses; we don't have anything we need to do in this base |
| 813 |
* class implementation. |
| 814 |
*/ |
| 815 |
execActorPhrase(issuingActor) { } |
| 816 |
|
| 817 |
/* |
| 818 |
* Resolve nouns. We'll resolve only the nouns in the target actor |
| 819 |
* phrase; we do not resolve nouns in the command phrase, because we |
| 820 |
* must re-parse the command phrase after we've finished resolving |
| 821 |
* the actor phrase, and thus we can't resolve nouns in the command |
| 822 |
* phrase until after the re-parse is completed. |
| 823 |
*/ |
| 824 |
resolveNouns(issuingActor, targetActor, results) |
| 825 |
{ |
| 826 |
local lst; |
| 827 |
|
| 828 |
/* |
| 829 |
* Resolve the actor, then we're done. Note that we do not |
| 830 |
* attempt to resolve anything in the underlying command yet, |
| 831 |
* because we can only resolve the command after we've fully |
| 832 |
* processed the actor clause. |
| 833 |
*/ |
| 834 |
lst = actor_.resolveNouns(getResolver(issuingActor), results); |
| 835 |
|
| 836 |
/* |
| 837 |
* there are no noun phrase slots, since we're not looking at the |
| 838 |
* verb |
| 839 |
*/ |
| 840 |
results.noteNounSlots(0); |
| 841 |
|
| 842 |
/* |
| 843 |
* if we have an object in the list, use it - note that our |
| 844 |
* actor phrase is a single-noun production, and hence should |
| 845 |
* never yield more than a single entry in its resolution list |
| 846 |
*/ |
| 847 |
if (lst.length() > 0) |
| 848 |
{ |
| 849 |
/* remember the resolved actor */ |
| 850 |
resolvedActor_ = lst[1].obj_; |
| 851 |
|
| 852 |
/* note in the results that an actor is specified */ |
| 853 |
results.noteActorSpecified(); |
| 854 |
} |
| 855 |
|
| 856 |
/* count our commands */ |
| 857 |
countCommands(results); |
| 858 |
} |
| 859 |
|
| 860 |
/* get or create my actor resolver */ |
| 861 |
getResolver(issuingActor) |
| 862 |
{ |
| 863 |
/* if I don't already have a resolver, create one and cache it */ |
| 864 |
if (resolver_ == nil) |
| 865 |
resolver_ = new ActorResolver(issuingActor); |
| 866 |
|
| 867 |
/* return our cached resolver */ |
| 868 |
return resolver_; |
| 869 |
} |
| 870 |
|
| 871 |
/* my resolved actor object */ |
| 872 |
resolvedActor_ = nil |
| 873 |
|
| 874 |
/* my actor resolver object */ |
| 875 |
resolver_ = nil |
| 876 |
; |
| 877 |
|
| 878 |
/* |
| 879 |
* First-on-line command with target actor. As with |
| 880 |
* CommandProdWithActor, instantiating grammar rules must set the |
| 881 |
* property actor_ to the actor match tree, and cmd_ to the underlying |
| 882 |
* commandPhrase match. |
| 883 |
*/ |
| 884 |
class FirstCommandProdWithActor: CommandProdWithActor, FirstCommandProd |
| 885 |
; |
| 886 |
|
| 887 |
|
| 888 |
/* ------------------------------------------------------------------------ */ |
| 889 |
/* |
| 890 |
* The 'predicate' production is the grammar rule for all individual |
| 891 |
* command phrases. We don't define anything about the predicate |
| 892 |
* grammar here, since it is completely language-dependent, but we do |
| 893 |
* *use* the predicate production as a sub-production of our |
| 894 |
* commandPhrase rules. |
| 895 |
* |
| 896 |
* The language-dependent implementation of the 'predicate' production |
| 897 |
* must provide the following methods: |
| 898 |
* |
| 899 |
* resolveAction(issuingActor, targetActor): This method returns a |
| 900 |
* newly-created instance of the Action subclass that the command refers |
| 901 |
* to. This method must generally interpret the phrasing of the command |
| 902 |
* to determine what noun phrases are present and what roles they serve, |
| 903 |
* and what verb phrase is present, then create an appropriate Action |
| 904 |
* subclass to represent the verb phrase as applied to the noun phrases |
| 905 |
* in their determined roles. |
| 906 |
* |
| 907 |
* resolveNouns(issuingActor, targetActor, results): This method |
| 908 |
* resolves all of the noun phrases in the predicate, using the |
| 909 |
* 'results' object (an object of class ResolveResults) to store |
| 910 |
* information about the status of the resolution. Generally, this |
| 911 |
* routine should collect information about the roles of the noun phrase |
| 912 |
* production matches, plug these into the Action via appropriate |
| 913 |
* properties (dobjMatch for the direct object of a transitive verb, for |
| 914 |
* example), resolve the Action, and then call the Action to do the |
| 915 |
* resolution. This method has no return value, since the Action is |
| 916 |
* responsible for keeping track of the results of the resolution. |
| 917 |
* |
| 918 |
* For languages like English which encode most information about the |
| 919 |
* phrase roles in word ordering, a good way to design a predicate |
| 920 |
* grammar is to specify the syntax of each possible verb phrase as a |
| 921 |
* 'predicate' rule, and base the match object for that rule on the |
| 922 |
* corresponding Action subclass. For example, the English grammar has |
| 923 |
* this rule to define the syntax for the verb 'take': |
| 924 |
* |
| 925 |
* VerbRule |
| 926 |
*. ('take' | 'pick' 'up' | 'get') dobjList |
| 927 |
*. | 'pick' dobjList 'up' |
| 928 |
*. : TakeAction |
| 929 |
*. ; |
| 930 |
* |
| 931 |
* Since English encodes everything in this command positionally, it's |
| 932 |
* straightforward to write grammar rules for the possible syntax |
| 933 |
* variations of a given action, hence it's easy to associate command |
| 934 |
* syntax directly with its associated action. When each 'predicate' |
| 935 |
* grammar maps to an Action subclass for its match object, |
| 936 |
* resolveAction() is especially easy to implement: it simply returns |
| 937 |
* 'self', since the grammar match and the Action are the same object. |
| 938 |
* It's also easy to plug each noun phrase into its appropriate property |
| 939 |
* slot in the Action subclass: the parser can be told to plug in each |
| 940 |
* noun phrase directly using the "->" notation in the grammar. |
| 941 |
* |
| 942 |
* Many languages encode the roles of noun phrases with case markers or |
| 943 |
* other syntactic devices, and as a result do not impose such strict |
| 944 |
* rules as English does on word order. For such languages, it is |
| 945 |
* usually better to construct the 'predicate' grammar separately from |
| 946 |
* any single action, so that the various acceptable phrase orderings |
| 947 |
* are enumerated, and the verb phrase is just another phrase that plugs |
| 948 |
* into these top-level orderings. In such grammars, the predicate must |
| 949 |
* do some programmatic work in resolveAction(): it must figure out |
| 950 |
* which Action subclass is involved based on the verb phrase sub-match |
| 951 |
* object and the noun phrases present, then must create an instance of |
| 952 |
* that Action subclass. Furthermore, since we can't plug the noun |
| 953 |
* phrases into the Action using the "->" notation in the grammar, the |
| 954 |
* resolveAction() routine must pick out the sub-match objects |
| 955 |
* representing the noun phrases and plug them into the Action itself. |
| 956 |
* |
| 957 |
* Probably the easiest way to accomplish all of this is by designing |
| 958 |
* each verb phrase match object so that it is associated with one or |
| 959 |
* more Action subclasses, according to the specific noun phrases |
| 960 |
* present. The details of this mapping are obviously specific to each |
| 961 |
* language, but it should be possible in most cases to build a base |
| 962 |
* class for all verb phrases that uses parameters to create the Action |
| 963 |
* and noun phrase associations. For example, each verb phrase grammar |
| 964 |
* match object might have a list of possible Action matches, such as |
| 965 |
* this example from a purely hypothetical English grammar based on this |
| 966 |
* technique |
| 967 |
* |
| 968 |
* grammar verbPhrase: 'take' | 'pick' 'up': VerbProd |
| 969 |
*. actionMap = |
| 970 |
*. [ |
| 971 |
*. [TakeAction, BasicProd, &dobjMatch], |
| 972 |
*. [TakeWithAction, BasicProd, &dobjMatch, WithProd, &iobjMatch] |
| 973 |
*. ] |
| 974 |
*. ; |
| 975 |
* |
| 976 |
* The idea is that the base VerbProd class looks through the list given |
| 977 |
* by the actionMap property for a template that matches the number and |
| 978 |
* type of noun phrases present in the predicate; when it finds a match, |
| 979 |
* it creates an instance of the Action subclass given in the template, |
| 980 |
* then plugs the noun phrases into the listed properties of the new |
| 981 |
* Action instance. |
| 982 |
* |
| 983 |
* Note that our verbPhrase example above is not an example of actual |
| 984 |
* working code, because there is no such thing in the |
| 985 |
* language-independent library as a VerbProd base class that reads |
| 986 |
* actionMap properites - this is a purely hypothetical bit of code to |
| 987 |
* illustrate how such a construction might work in a language that |
| 988 |
* requires it, and it is up to the language-specific module to define |
| 989 |
* such a mechanism for its own use. |
| 990 |
*/ |
| 991 |
|
| 992 |
|
| 993 |
/* ------------------------------------------------------------------------ */ |
| 994 |
/* |
| 995 |
* Base classes for grammar matches for full commands. |
| 996 |
* |
| 997 |
* There are two kinds of command separators: ambiguous and unambiguous. |
| 998 |
* Unambiguous separators are those that can separate only commands, such |
| 999 |
* as "then" and periods. Ambiguous separators are those that can |
| 1000 |
* separate nouns in a noun list as well as commands; for example "and" |
| 1001 |
* and commas. |
| 1002 |
* |
| 1003 |
* First, CommandProdWithDefiniteConj, which is for a single full |
| 1004 |
* command, unambiguously terminated with a separator that can only mean |
| 1005 |
* that a new command follows. We parse one command at a time when a |
| 1006 |
* command line includes several commands, since we can only resolve |
| 1007 |
* objects - and thus can only choose structural interpretations - when |
| 1008 |
* we reach the game state in which a given command is to be executed. |
| 1009 |
* |
| 1010 |
* Second, CommandProdWithAmbiguousConj, which is for multiple commands |
| 1011 |
* separated by a conjunction that does not end a sentence, but could |
| 1012 |
* just as well separate two noun phrases. In this case, we parse both |
| 1013 |
* commands, to ensure that we actually have a well-formed command |
| 1014 |
* following the conjunction; this allows us to try interpreting the part |
| 1015 |
* after the conjunction as a command and also as a noun phrase, to see |
| 1016 |
* which interpretation looks better. |
| 1017 |
* |
| 1018 |
* When we find an unambiguous command separator, we can simply use the |
| 1019 |
* '*' grammar match to put off parsing everything after the separator |
| 1020 |
* until later, reducing the complexity of the grammar tree. When we |
| 1021 |
* find an ambiguous separator, we can't put off parsing the rest with |
| 1022 |
* '*', because we need to know if the part after the separator can |
| 1023 |
* indeed be construed as a new command. During resolution, we'll take a |
| 1024 |
* noun list interpretation of 'and' over a command list whenever doing |
| 1025 |
* so would give us resolvable noun phrases. |
| 1026 |
*/ |
| 1027 |
|
| 1028 |
/* |
| 1029 |
* Match class for a command phrase that is separated from anything that |
| 1030 |
* follows with an unambiguous conjunction. |
| 1031 |
* |
| 1032 |
* Grammar rules based on this match class must set property cmd_ to the |
| 1033 |
* underlying 'predicate' production. |
| 1034 |
*/ |
| 1035 |
class CommandProdWithDefiniteConj: CommandProd |
| 1036 |
resolveNouns(issuingActor, targetActor, results) |
| 1037 |
{ |
| 1038 |
/* resolve nouns */ |
| 1039 |
cmd_.resolveNouns(issuingActor, targetActor, results); |
| 1040 |
|
| 1041 |
/* count commands */ |
| 1042 |
countCommands(results); |
| 1043 |
} |
| 1044 |
countCommands(results) |
| 1045 |
{ |
| 1046 |
/* we have only one subcommand */ |
| 1047 |
if (cmdCounted_++ == 0) |
| 1048 |
results.incCommandCount(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
/* counter: have we counted our command in the results object yet? */ |
| 1052 |
cmdCounted_ = 0 |
| 1053 |
|
| 1054 |
/* resolve my first action */ |
| 1055 |
resolveFirstAction(issuingActor, targetActor) |
| 1056 |
{ |
| 1057 |
return cmd_.resolveAction(issuingActor, targetActor); |
| 1058 |
} |
| 1059 |
|
| 1060 |
/* does this command end a sentence */ |
| 1061 |
isEndOfSentence() |
| 1062 |
{ |
| 1063 |
/* |
| 1064 |
* it's the end of the sentence if our predicate encompasses all |
| 1065 |
* of the tokens in the command, or the conjunction is a |
| 1066 |
* sentence-ending conjunction |
| 1067 |
*/ |
| 1068 |
return conj_ == nil || conj_.isEndOfSentence(); |
| 1069 |
} |
| 1070 |
|
| 1071 |
/* |
| 1072 |
* Get the token index of the first command separator token. This |
| 1073 |
* is the first token that is not part of the underlying command. |
| 1074 |
*/ |
| 1075 |
getCommandSepIndex() |
| 1076 |
{ |
| 1077 |
/* |
| 1078 |
* if we have a conjunction, return the first token index of the |
| 1079 |
* conjunction; otherwise, return the index of the next token |
| 1080 |
* after the command itself |
| 1081 |
*/ |
| 1082 |
if (conj_ != nil) |
| 1083 |
return conj_.firstTokenIndex; |
| 1084 |
else |
| 1085 |
return cmd_.lastTokenIndex + 1; |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* |
| 1089 |
* get the token index of the next command - this is the index of |
| 1090 |
* the next token after our conjunction if we have one, or after our |
| 1091 |
* command if we don't have a conjunction |
| 1092 |
*/ |
| 1093 |
getNextCommandIndex() |
| 1094 |
{ |
| 1095 |
return (conj_ == nil ? cmd_ : conj_).lastTokenIndex + 1; |
| 1096 |
} |
| 1097 |
; |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* Match class for two command phrases separated by an ambiguous |
| 1101 |
* conjunction (i.e., a conjunction that could also separate two noun |
| 1102 |
* phrases). Grammar rules based on this class must set the properties |
| 1103 |
* 'cmd1_' to the underlying 'predicate' production match of the first |
| 1104 |
* command, and 'cmd2_' to the underlying 'commandPhrase' production |
| 1105 |
* match of the second command. |
| 1106 |
*/ |
| 1107 |
class CommandProdWithAmbiguousConj: CommandProd |
| 1108 |
resolveNouns(issuingActor, targetActor, results) |
| 1109 |
{ |
| 1110 |
/* |
| 1111 |
* Resolve nouns in the first subcommand only. Do NOT resolve |
| 1112 |
* nouns in any of the additional subcommands (there might be |
| 1113 |
* more than one, since cmd2_ can be a list of subcommands, not |
| 1114 |
* just a single subcommand), because we cannot assume that the |
| 1115 |
* current scope will continue to be valid after executing the |
| 1116 |
* first subcommand - the first command could take us to a |
| 1117 |
* different location, or change the lighting conditions, or add |
| 1118 |
* or remove objects from the location, or any number of other |
| 1119 |
* things that would invalidate the current scope. |
| 1120 |
*/ |
| 1121 |
cmd1_.resolveNouns(issuingActor, targetActor, results); |
| 1122 |
|
| 1123 |
/* count our commands */ |
| 1124 |
countCommands(results); |
| 1125 |
} |
| 1126 |
countCommands(results) |
| 1127 |
{ |
| 1128 |
/* count our first subcommand (cmd1_) */ |
| 1129 |
if (cmdCounted_++ == 0) |
| 1130 |
results.incCommandCount(); |
| 1131 |
|
| 1132 |
/* count results in the rest of the list (cmd2_ and its children) */ |
| 1133 |
cmd2_.countCommands(results); |
| 1134 |
} |
| 1135 |
|
| 1136 |
/* counter: have we counted our command in the results object yet? */ |
| 1137 |
cmdCounted_ = 0 |
| 1138 |
|
| 1139 |
/* resolve my first action */ |
| 1140 |
resolveFirstAction(issuingActor, targetActor) |
| 1141 |
{ |
| 1142 |
return cmd1_.resolveAction(issuingActor, targetActor); |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* does this command end a sentence */ |
| 1146 |
isEndOfSentence() |
| 1147 |
{ |
| 1148 |
/* |
| 1149 |
* it's the end of the sentence if the conjunction is a |
| 1150 |
* sentence-ending conjunction |
| 1151 |
*/ |
| 1152 |
return conj_.isEndOfSentence(); |
| 1153 |
} |
| 1154 |
|
| 1155 |
/* |
| 1156 |
* Get the token index of the first command separator token. This |
| 1157 |
* is the first token that is not part of the underlying command. |
| 1158 |
*/ |
| 1159 |
getCommandSepIndex() |
| 1160 |
{ |
| 1161 |
/* return the conjunction's first token index */ |
| 1162 |
return conj_.firstTokenIndex; |
| 1163 |
} |
| 1164 |
|
| 1165 |
/* |
| 1166 |
* get the token index of the next command - this is simply the |
| 1167 |
* starting index for our second subcommand tree |
| 1168 |
*/ |
| 1169 |
getNextCommandIndex() { return cmd2_.firstTokenIndex; } |
| 1170 |
; |
| 1171 |
|
| 1172 |
/* |
| 1173 |
* The basic grammar rule for an unambiguous end-of-sentence command. |
| 1174 |
* This matches either a predicate with nothing following, or a predicate |
| 1175 |
* with an unambiguous command-only conjunction following. |
| 1176 |
*/ |
| 1177 |
grammar commandPhrase(definiteConj): |
| 1178 |
predicate->cmd_ |
| 1179 |
| predicate->cmd_ commandOnlyConjunction->conj_ * |
| 1180 |
: CommandProdWithDefiniteConj |
| 1181 |
; |
| 1182 |
|
| 1183 |
/* |
| 1184 |
* the basic grammar rule for a pair of commands with an ambiguous |
| 1185 |
* separator (i.e., a separator that could separate commands or |
| 1186 |
* conjunctions) |
| 1187 |
*/ |
| 1188 |
grammar commandPhrase(ambiguousConj): |
| 1189 |
predicate->cmd1_ commandOrNounConjunction->conj_ commandPhrase->cmd2_ |
| 1190 |
: CommandProdWithAmbiguousConj |
| 1191 |
; |
| 1192 |
|
| 1193 |
/* ------------------------------------------------------------------------ */ |
| 1194 |
/* |
| 1195 |
* We leave almost everything about the grammatical rules of noun |
| 1196 |
* phrases to the language-specific implementation, but we provide a set |
| 1197 |
* of base classes for implementing several conceptual structures that |
| 1198 |
* have equivalents in most languages. |
| 1199 |
*/ |
| 1200 |
|
| 1201 |
|
| 1202 |
/* |
| 1203 |
* Basic noun phrase production class. |
| 1204 |
*/ |
| 1205 |
class NounPhraseProd: BasicProd |
| 1206 |
/* |
| 1207 |
* Determine whether this kind of noun phrase prefers to keep a |
| 1208 |
* collective or the collective's individuals when filtering. If |
| 1209 |
* this is true, we'll keep a collective and discard its individuals |
| 1210 |
* when filtering a resolution list; otherwise, we'll drop the |
| 1211 |
* collective and keep the individuals. |
| 1212 |
*/ |
| 1213 |
filterForCollectives = nil |
| 1214 |
|
| 1215 |
/* |
| 1216 |
* Filter a "verify" result list for the results we'd like to keep |
| 1217 |
* in the final resolution of the noun phrase. This is called after |
| 1218 |
* we've run through the verification process on the list of |
| 1219 |
* candidate matches, so 'lst' is a list of ResolveInfo objects, |
| 1220 |
* sorted in descending order of logicalness. |
| 1221 |
* |
| 1222 |
* By default, we keep only the items that are equally as logical as |
| 1223 |
* the best item in the results. Since the items are sorted in |
| 1224 |
* descending order of goodness, the best item is always the first |
| 1225 |
* item. |
| 1226 |
*/ |
| 1227 |
getVerifyKeepers(results) |
| 1228 |
{ |
| 1229 |
local best; |
| 1230 |
|
| 1231 |
/* |
| 1232 |
* Reduce the list to the most logical elements - in other |
| 1233 |
* words, keep only the elements that are exactly as logical as |
| 1234 |
* the first element, which we know to have the best logicalness |
| 1235 |
* ranking in the list by virtue of having sorted the list in |
| 1236 |
* descending order of logicalness. |
| 1237 |
*/ |
| 1238 |
best = results[1]; |
| 1239 |
return results.subset({x: x.compareTo(best) == 0}); |
| 1240 |
} |
| 1241 |
|
| 1242 |
/* |
| 1243 |
* Filter a match list of results for truncated matches. If we have |
| 1244 |
* a mix of truncated matches and exact matches, we'll keep only the |
| 1245 |
* exact matches. If we have only truncated matches, though, we'll |
| 1246 |
* return the list unchanged, as we don't have a better offer going. |
| 1247 |
*/ |
| 1248 |
filterTruncations(lst, resolver) |
| 1249 |
{ |
| 1250 |
local exactLst; |
| 1251 |
|
| 1252 |
/* |
| 1253 |
* If we're in "global scope," it means that we're resolving |
| 1254 |
* something like a topic phrase, and we're not limited to the |
| 1255 |
* current physical scope but can choose objects from the entire |
| 1256 |
* game world. In this case, don't apply any truncation |
| 1257 |
* filtering. The reason is that we could have several unrelated |
| 1258 |
* objects in the game with vocabulary that differs only in |
| 1259 |
* truncation, but the user has forgotten about the ones with the |
| 1260 |
* longer names and is thinking of something she's referred to |
| 1261 |
* with truncation in the past, when the longer-named objects |
| 1262 |
* weren't around. We're aware of all of those longer-named |
| 1263 |
* objects, but it's not helpful to the player, since they might |
| 1264 |
* currently be out of sight. |
| 1265 |
*/ |
| 1266 |
if (resolver.isGlobalScope) |
| 1267 |
return lst; |
| 1268 |
|
| 1269 |
/* get the list of exact (i.e., untruncated) matches */ |
| 1270 |
exactLst = lst.subset( |
| 1271 |
{x: (x.flags_ & (VocabTruncated | PluralTruncated)) == 0}); |
| 1272 |
|
| 1273 |
/* |
| 1274 |
* if we have any exact matches, use only the exact matches; if |
| 1275 |
* we don't, use the full list as is |
| 1276 |
*/ |
| 1277 |
return (exactLst.length() != 0 ? exactLst : lst); |
| 1278 |
} |
| 1279 |
; |
| 1280 |
|
| 1281 |
/* |
| 1282 |
* Basic noun phrase list production class. |
| 1283 |
*/ |
| 1284 |
class NounListProd: BasicProd |
| 1285 |
; |
| 1286 |
|
| 1287 |
/* |
| 1288 |
* Basic "layered" noun phrase production. It's often useful to define |
| 1289 |
* a grammar rule that simply defers to an underlying grammar rule; we |
| 1290 |
* make this simpler by defining this class that automatically delegates |
| 1291 |
* resolveNouns to the underlying noun phrase given by the property np_. |
| 1292 |
*/ |
| 1293 |
class LayeredNounPhraseProd: NounPhraseProd |
| 1294 |
resolveNouns(resolver, results) |
| 1295 |
{ |
| 1296 |
/* let the underlying match tree object do the work */ |
| 1297 |
return np_.resolveNouns(resolver, results); |
| 1298 |
} |
| 1299 |
; |
| 1300 |
|
| 1301 |
|
| 1302 |
/* ------------------------------------------------------------------------ */ |
| 1303 |
/* |
| 1304 |
* A single noun is sometimes required where, structurally, a list is |
| 1305 |
* not allowed. Single nouns should not be used to prohibit lists where |
| 1306 |
* there is no structural reason for the prohibition - these should be |
| 1307 |
* used only where it doesn't make sense to use a list structurally. |
| 1308 |
*/ |
| 1309 |
class SingleNounProd: NounPhraseProd |
| 1310 |
resolveNouns(resolver, results) |
| 1311 |
{ |
| 1312 |
/* tell the results object we're resolving a single-object slot */ |
| 1313 |
results.beginSingleObjSlot(); |
| 1314 |
|
| 1315 |
/* resolve the underlying noun phrase */ |
| 1316 |
local lst = np_.resolveNouns(resolver, results); |
| 1317 |
|
| 1318 |
/* tell the results object we're done with the single-object slot */ |
| 1319 |
results.endSingleObjSlot(); |
| 1320 |
|
| 1321 |
/* make sure the list has only one element */ |
| 1322 |
if (lst.length() > 1) |
| 1323 |
results.uniqueObjectRequired(getOrigText(), lst); |
| 1324 |
|
| 1325 |
/* return the results */ |
| 1326 |
return lst; |
| 1327 |
} |
| 1328 |
; |
| 1329 |
|
| 1330 |
/* |
| 1331 |
* A user could attempt to use a noun list where a single noun is |
| 1332 |
* required. This is not a grammatical error, so we accept it |
| 1333 |
* grammatically; however, for disambiguation purposes we score it lower |
| 1334 |
* than a singleNoun production with only one noun phrase, and if we try |
| 1335 |
* to resolve it, we'll fail with an error. |
| 1336 |
*/ |
| 1337 |
class SingleNounWithListProd: NounPhraseProd |
| 1338 |
resolveNouns(resolver, results) |
| 1339 |
{ |
| 1340 |
/* note the problem */ |
| 1341 |
results.singleObjectRequired(getOrigText()); |
| 1342 |
|
| 1343 |
/* |
| 1344 |
* In case we have any unknown words or other detrimental |
| 1345 |
* aspects, resolve the underlying noun list as well, so we can |
| 1346 |
* score even lower. Note that doing this after noting our |
| 1347 |
* single-object-required problem will avoid any interactive |
| 1348 |
* resolution (such as asking for disambiguation information), |
| 1349 |
* since once we get to the point where we're ready to do |
| 1350 |
* anything interactive, the single-object-required results |
| 1351 |
* notification will fail with an error, so we won't make it to |
| 1352 |
* this point during interactive resolution. |
| 1353 |
*/ |
| 1354 |
np_.resolveNouns(resolver, results); |
| 1355 |
|
| 1356 |
/* we have no resolution */ |
| 1357 |
return []; |
| 1358 |
} |
| 1359 |
; |
| 1360 |
|
| 1361 |
/* |
| 1362 |
* A topic is a noun phrase used in commands like "ask <person> about |
| 1363 |
* <topic>." For our purposes, this works as an ordinary single noun |
| 1364 |
* production. |
| 1365 |
*/ |
| 1366 |
class TopicProd: SingleNounProd |
| 1367 |
/* get the original text and tokens from the underlying phrase */ |
| 1368 |
getOrigTokenList() { return np_.getOrigTokenList(); } |
| 1369 |
getOrigText() { return np_.getOrigText(); } |
| 1370 |
|
| 1371 |
resolveNouns(resolver, results) |
| 1372 |
{ |
| 1373 |
/* note that we're entering a topic slot */ |
| 1374 |
results.beginTopicSlot(); |
| 1375 |
|
| 1376 |
/* do the inherited work */ |
| 1377 |
local ret = inherited(resolver, results); |
| 1378 |
|
| 1379 |
/* we're leaving the topic slot */ |
| 1380 |
results.endTopicSlot(); |
| 1381 |
|
| 1382 |
/* return the resolved list */ |
| 1383 |
return ret; |
| 1384 |
} |
| 1385 |
; |
| 1386 |
|
| 1387 |
/* |
| 1388 |
* A literal is a string enclosed in quotes. |
| 1389 |
*/ |
| 1390 |
class LiteralProd: BasicProd |
| 1391 |
; |
| 1392 |
|
| 1393 |
/* |
| 1394 |
* Basic class for pronoun phrases. The specific pronouns are |
| 1395 |
* language-dependent; each instance should define its pronounType |
| 1396 |
* property to an appropriate PronounXxx constant. |
| 1397 |
*/ |
| 1398 |
class PronounProd: NounPhraseProd |
| 1399 |
resolveNouns(resolver, results) |
| 1400 |
{ |
| 1401 |
local lst; |
| 1402 |
|
| 1403 |
/* |
| 1404 |
* check for a valid anaphoric binding (i.e., a back-reference to |
| 1405 |
* an object mentioned earlier in the current command: ASK BOB |
| 1406 |
* ABOUT HIMSELF) |
| 1407 |
*/ |
| 1408 |
lst = checkAnaphoricBinding(resolver, results); |
| 1409 |
|
| 1410 |
/* |
| 1411 |
* if we didn't get an anaphoric binding, find an antecedent from |
| 1412 |
* a previous command |
| 1413 |
*/ |
| 1414 |
if (lst == nil) |
| 1415 |
{ |
| 1416 |
/* ask the resolver to get the antecedent */ |
| 1417 |
lst = resolver.resolvePronounAntecedent( |
| 1418 |
pronounType, self, results, isPossessive); |
| 1419 |
|
| 1420 |
/* if there are no results, note the error */ |
| 1421 |
if (lst == []) |
| 1422 |
results.noMatchForPronoun(pronounType, |
| 1423 |
getOrigText().toLower()); |
| 1424 |
|
| 1425 |
/* remember the pronoun type in each ResolveInfo */ |
| 1426 |
lst.forEach({x: x.pronounType_ = pronounType}); |
| 1427 |
|
| 1428 |
/* |
| 1429 |
* If the pronoun is singular, but we got multiple potential |
| 1430 |
* antecedents, it means that the previous command had |
| 1431 |
* multiple noun slots (as in UNLOCK DOOR WITH KEY) and |
| 1432 |
* didn't want to decide a priori which one is the antecedent |
| 1433 |
* for future pronouns. So, we have to decide now which of |
| 1434 |
* the potential antecedents to use as the actual antecedent. |
| 1435 |
* Choose the most logical, if there's a clearly more logical |
| 1436 |
* one. |
| 1437 |
*/ |
| 1438 |
if (!isPlural && lst.length() > 1) |
| 1439 |
{ |
| 1440 |
/* filter the phrase using the normal disambiguation */ |
| 1441 |
lst = resolver.filterAmbiguousNounPhrase(lst, 1, self); |
| 1442 |
|
| 1443 |
/* |
| 1444 |
* if that leaves more than one item, pick the first one |
| 1445 |
* and mark it as unclearly disambiguated |
| 1446 |
*/ |
| 1447 |
if (lst.length() > 1) |
| 1448 |
{ |
| 1449 |
/* arbitrarily keep the first item only */ |
| 1450 |
lst = lst.sublist(1, 1); |
| 1451 |
|
| 1452 |
/* mark it as an arbitrary choice */ |
| 1453 |
lst[1].flags_ |= UnclearDisambig; |
| 1454 |
} |
| 1455 |
} |
| 1456 |
} |
| 1457 |
|
| 1458 |
/* note that we have phrase involving a pronoun */ |
| 1459 |
results.notePronoun(); |
| 1460 |
|
| 1461 |
/* return the result list */ |
| 1462 |
return lst; |
| 1463 |
} |
| 1464 |
|
| 1465 |
/* |
| 1466 |
* our pronoun specifier - this must be set in each rule instance to |
| 1467 |
* one of the PronounXxx constants to specify which pronoun to use |
| 1468 |
* when resolving the pronoun phrase |
| 1469 |
*/ |
| 1470 |
pronounType = nil |
| 1471 |
|
| 1472 |
/* is this a possessive usage? */ |
| 1473 |
isPossessive = nil |
| 1474 |
|
| 1475 |
/* |
| 1476 |
* Is this pronoun a singular or a plural? A pronoun like "it" or |
| 1477 |
* "he" is singular, because it refers to a single antecedent; "them" |
| 1478 |
* is plural. Language modules that define their own custom pronoun |
| 1479 |
* subclasses should override this as needed. |
| 1480 |
*/ |
| 1481 |
isPlural = nil |
| 1482 |
|
| 1483 |
/* |
| 1484 |
* Check for an anaphoric binding. Returns a list (which is allowed |
| 1485 |
* to be empty) if this can refer back to an earlier noun phrase in |
| 1486 |
* the same command, nil if not. By default, we consider pronouns |
| 1487 |
* to be non-anaphoric, meaning they refer to something from a |
| 1488 |
* previous sentence, not something in this same sentence. In most |
| 1489 |
* languages, pronouns don't refer to objects in other noun phrases |
| 1490 |
* within the same predicate unless they're reflexive. |
| 1491 |
*/ |
| 1492 |
checkAnaphoricBinding(resolver, results) { return nil; } |
| 1493 |
; |
| 1494 |
|
| 1495 |
/* |
| 1496 |
* For simplicity, define subclasses of PronounProd for the basic set of |
| 1497 |
* pronouns found in most languages. Language-specific grammar |
| 1498 |
* definitions can choose to use these or not, and can add their own |
| 1499 |
* extra subclasses as needed for types of pronouns we don't define |
| 1500 |
* here. |
| 1501 |
*/ |
| 1502 |
class ItProd: PronounProd |
| 1503 |
pronounType = PronounIt |
| 1504 |
; |
| 1505 |
|
| 1506 |
class ThemProd: PronounProd |
| 1507 |
pronounType = PronounThem |
| 1508 |
isPlural = true |
| 1509 |
; |
| 1510 |
|
| 1511 |
class HimProd: PronounProd |
| 1512 |
pronounType = PronounHim |
| 1513 |
; |
| 1514 |
|
| 1515 |
class HerProd: PronounProd |
| 1516 |
pronounType = PronounHer |
| 1517 |
; |
| 1518 |
|
| 1519 |
class MeProd: PronounProd |
| 1520 |
pronounType = PronounMe |
| 1521 |
; |
| 1522 |
|
| 1523 |
class YouProd: PronounProd |
| 1524 |
pronounType = PronounYou |
| 1525 |
; |
| 1526 |
|
| 1527 |
/* |
| 1528 |
* Third-person anaphoric reflexive pronouns. These refer to objects |
| 1529 |
* that appeared earlier in the sentence: "ask bob about himself." |
| 1530 |
*/ |
| 1531 |
class ReflexivePronounProd: PronounProd |
| 1532 |
resolveNouns(resolver, results) |
| 1533 |
{ |
| 1534 |
/* ask the resolver for the reflexive pronoun binding */ |
| 1535 |
local lst = resolver.getReflexiveBinding(pronounType); |
| 1536 |
|
| 1537 |
/* |
| 1538 |
* if the result is empty, the verb will provide the binding |
| 1539 |
* later, on a second pass |
| 1540 |
*/ |
| 1541 |
if (lst == []) |
| 1542 |
return lst; |
| 1543 |
|
| 1544 |
/* |
| 1545 |
* If the result is nil, the verb is saying that the reflexive |
| 1546 |
* pronoun makes no sense internally within the predicate |
| 1547 |
* structure. In this case, or if we did get a list that |
| 1548 |
* doesn't agree with the pronoun type (in number or gender, for |
| 1549 |
* example), consider the reflexive to refer back to the actor, |
| 1550 |
* for a construct such as TELL BOB TO HIT HIMSELF. However, |
| 1551 |
* only do this if the issuer and target actor aren't the same, |
| 1552 |
* since we generally don't refer to the PC in the third person. |
| 1553 |
*/ |
| 1554 |
if ((lst == nil || !checkAgreement(lst)) |
| 1555 |
&& resolver.actor_ != resolver.issuer_) |
| 1556 |
{ |
| 1557 |
/* try treating the actor as the reflexive pronoun */ |
| 1558 |
lst = [new ResolveInfo(resolver.actor_, 0, self)]; |
| 1559 |
} |
| 1560 |
|
| 1561 |
/* if the list is nil, it means reflexives aren't allowed here */ |
| 1562 |
if (lst == nil) |
| 1563 |
{ |
| 1564 |
results.reflexiveNotAllowed(pronounType, getOrigText.toLower()); |
| 1565 |
return []; |
| 1566 |
} |
| 1567 |
|
| 1568 |
/* |
| 1569 |
* Check the list for agreement (in gender, number, and so on). |
| 1570 |
* Don't bother if the list is empty, as this is the action's |
| 1571 |
* way of telling us that it doesn't have a binding for us yet |
| 1572 |
* but will provide one on a subsequent attempt at re-resolving |
| 1573 |
* this phrase. |
| 1574 |
*/ |
| 1575 |
if (!checkAgreement(lst)) |
| 1576 |
results.wrongReflexive(pronounType, getOrigText().toLower()); |
| 1577 |
|
| 1578 |
/* return the result list */ |
| 1579 |
return lst; |
| 1580 |
} |
| 1581 |
|
| 1582 |
/* |
| 1583 |
* Check that the binding we found for our reflexive pronoun agrees |
| 1584 |
* with the pronoun in gender, number, and anything else that it has |
| 1585 |
* to agree with. This must be defined by each concrete subclass. |
| 1586 |
* Note that language-specific subclasses can and *should* override |
| 1587 |
* this to test agreement for the local language's rules. |
| 1588 |
* |
| 1589 |
* This should return true if we agree, nil if not. |
| 1590 |
*/ |
| 1591 |
checkAgreement(lst) { return true; } |
| 1592 |
; |
| 1593 |
|
| 1594 |
class ItselfProd: ReflexivePronounProd |
| 1595 |
pronounType = PronounIt |
| 1596 |
; |
| 1597 |
|
| 1598 |
class ThemselvesProd: ReflexivePronounProd |
| 1599 |
pronounType = PronounThem |
| 1600 |
; |
| 1601 |
|
| 1602 |
class HimselfProd: ReflexivePronounProd |
| 1603 |
pronounType = PronounHim |
| 1604 |
; |
| 1605 |
|
| 1606 |
class HerselfProd: ReflexivePronounProd |
| 1607 |
pronounType = PronounHer |
| 1608 |
; |
| 1609 |
|
| 1610 |
/* |
| 1611 |
* The special 'all' constructions are full noun phrases. |
| 1612 |
*/ |
| 1613 |
class EverythingProd: BasicProd |
| 1614 |
resolveNouns(resolver, results) |
| 1615 |
{ |
| 1616 |
local lst; |
| 1617 |
|
| 1618 |
/* check to make sure 'all' is allowed */ |
| 1619 |
if (!resolver.allowAll()) |
| 1620 |
{ |
| 1621 |
/* it's not - flag an error and give up */ |
| 1622 |
results.allNotAllowed(); |
| 1623 |
return []; |
| 1624 |
} |
| 1625 |
|
| 1626 |
/* get the 'all' list */ |
| 1627 |
lst = resolver.getAll(self); |
| 1628 |
|
| 1629 |
/* |
| 1630 |
* set the "always announce" flag for each item - the player |
| 1631 |
* didn't name these items specifically, so always show what we |
| 1632 |
* chose |
| 1633 |
*/ |
| 1634 |
foreach (local cur in lst) |
| 1635 |
cur.flags_ |= AlwaysAnnounce | MatchedAll; |
| 1636 |
|
| 1637 |
/* make sure there's something in it */ |
| 1638 |
if (lst.length() == 0) |
| 1639 |
results.noMatchForAll(); |
| 1640 |
|
| 1641 |
/* return the list */ |
| 1642 |
return lst; |
| 1643 |
} |
| 1644 |
|
| 1645 |
/* match Collective objects instead of their individuals */ |
| 1646 |
filterForCollectives = true |
| 1647 |
; |
| 1648 |
|
| 1649 |
|
| 1650 |
/* ------------------------------------------------------------------------ */ |
| 1651 |
/* |
| 1652 |
* Basic exclusion list ("except the silver one") production base class. |
| 1653 |
*/ |
| 1654 |
class ExceptListProd: BasicProd |
| 1655 |
; |
| 1656 |
|
| 1657 |
/* |
| 1658 |
* Basic "but" rule, which selects a list of plurals minus a list of |
| 1659 |
* specifically excepted objects. This can be used to construct more |
| 1660 |
* specific production classes for things like "everything but the book" |
| 1661 |
* and "all books except the red ones". |
| 1662 |
* |
| 1663 |
* In each grammar rule based on this class, the 'except_' property must |
| 1664 |
* be set to a suitable noun phrase for the exception list. We'll |
| 1665 |
* resolve this list and remove the objects in it from our main list. |
| 1666 |
*/ |
| 1667 |
class ButProd: NounPhraseProd |
| 1668 |
resolveNouns(resolver, results) |
| 1669 |
{ |
| 1670 |
local mainList; |
| 1671 |
local butList; |
| 1672 |
local butRemapList; |
| 1673 |
local action; |
| 1674 |
local role; |
| 1675 |
local remapProp; |
| 1676 |
|
| 1677 |
/* get our main list of items to include */ |
| 1678 |
mainList = getMainList(resolver, results); |
| 1679 |
|
| 1680 |
/* filter out truncated matches if we have any exact matches */ |
| 1681 |
mainList = filterTruncations(mainList, resolver); |
| 1682 |
|
| 1683 |
/* |
| 1684 |
* resolve the 'except' list - use an 'except' resolver based on |
| 1685 |
* our list so that we resolve these objects in the scope of our |
| 1686 |
* main list |
| 1687 |
*/ |
| 1688 |
butList = except_.resolveNouns( |
| 1689 |
new ExceptResolver(mainList, getOrigText(), resolver), |
| 1690 |
new ExceptResults(results)); |
| 1691 |
|
| 1692 |
/* if the exception list is empty, tell the results about it */ |
| 1693 |
if (butList == []) |
| 1694 |
results.noteEmptyBut(); |
| 1695 |
|
| 1696 |
/* |
| 1697 |
* Get the remapping property for this object role for this |
| 1698 |
* action. This property applies to each of the objects we're |
| 1699 |
* resolving, and tells us if the resolved object is going to |
| 1700 |
* remap its handling of this action when the object is used in |
| 1701 |
* this role. For example, the Take action's remapping property |
| 1702 |
* for the direct object would usually be remapDobjTake, so |
| 1703 |
* book.remapDobjTake would tell us if TAKE BOOK were going to |
| 1704 |
* be remapped. |
| 1705 |
*/ |
| 1706 |
action = resolver.getAction(); |
| 1707 |
role = resolver.whichObject; |
| 1708 |
remapProp = action.getRemapPropForRole(role); |
| 1709 |
|
| 1710 |
/* get the list of simple synonym remappings for the 'except' list */ |
| 1711 |
butRemapList = butList.mapAll( |
| 1712 |
{x: action.getSimpleSynonymRemap(x.obj_, role, remapProp)}); |
| 1713 |
|
| 1714 |
/* |
| 1715 |
* scan the 'all' list, and remove each item that appears in the |
| 1716 |
* 'except' list |
| 1717 |
*/ |
| 1718 |
for (local i = 1, local len = mainList.length() ; i <= len ; ++i) |
| 1719 |
{ |
| 1720 |
local curRemap; |
| 1721 |
|
| 1722 |
/* get the current 'all' list element */ |
| 1723 |
local cur = mainList[i].obj_; |
| 1724 |
|
| 1725 |
/* get the simple synonym remapping for this item, if any */ |
| 1726 |
curRemap = action.getSimpleSynonymRemap(cur, role, remapProp); |
| 1727 |
|
| 1728 |
/* |
| 1729 |
* If this item appears in the 'except' list, remove it. |
| 1730 |
* |
| 1731 |
* Similarly, if this item is remapped to something that |
| 1732 |
* appears in the 'except' list, remove it. |
| 1733 |
* |
| 1734 |
* Similarly, if something in the 'except' list is remapped |
| 1735 |
* to this item, remove this item. |
| 1736 |
*/ |
| 1737 |
if (butList.indexWhich({x: x.obj_ == cur}) != nil |
| 1738 |
|| butRemapList.indexWhich({x: x == cur}) != nil |
| 1739 |
|| butList.indexWhich({x: x.obj_ == curRemap}) != nil) |
| 1740 |
{ |
| 1741 |
/* remove it and adjust our loop counters accordingly */ |
| 1742 |
mainList = mainList.removeElementAt(i); |
| 1743 |
--i; |
| 1744 |
--len; |
| 1745 |
} |
| 1746 |
} |
| 1747 |
|
| 1748 |
/* if that doesn't leave anything, it's an error */ |
| 1749 |
if (mainList == []) |
| 1750 |
flagAllExcepted(resolver, results); |
| 1751 |
|
| 1752 |
/* perform the final filtering on the list for our subclass */ |
| 1753 |
mainList = filterFinalList(mainList); |
| 1754 |
|
| 1755 |
/* add any flags to the result list that our subclass indicates */ |
| 1756 |
foreach (local cur in mainList) |
| 1757 |
cur.flags_ |= addedFlags; |
| 1758 |
|
| 1759 |
/* note the matched objects in the results */ |
| 1760 |
results.noteMatches(mainList); |
| 1761 |
|
| 1762 |
/* return whatever we have left after the exclusions */ |
| 1763 |
return mainList; |
| 1764 |
} |
| 1765 |
|
| 1766 |
/* get my main list, which is the list of items to include */ |
| 1767 |
getMainList(resolver, results) { return []; } |
| 1768 |
|
| 1769 |
/* flag an error - everything has been excluded by the 'but' list */ |
| 1770 |
flagAllExcepted(resolver, results) { } |
| 1771 |
|
| 1772 |
/* filter the final list - by default we just return the same list */ |
| 1773 |
filterFinalList(lst) { return lst; } |
| 1774 |
|
| 1775 |
/* by default, add no extra flags to our resolved object list */ |
| 1776 |
addedFlags = 0 |
| 1777 |
; |
| 1778 |
|
| 1779 |
|
| 1780 |
/* ------------------------------------------------------------------------ */ |
| 1781 |
/* |
| 1782 |
* Base class for "all but" rules, which select everything available |
| 1783 |
* except for objects in a specified list of exceptions; for example, in |
| 1784 |
* English, "take everything but the book". |
| 1785 |
*/ |
| 1786 |
class EverythingButProd: ButProd |
| 1787 |
/* our main list is given by the "all" list */ |
| 1788 |
getMainList(resolver, results) |
| 1789 |
{ |
| 1790 |
/* check to make sure 'all' is allowed */ |
| 1791 |
if (!resolver.allowAll()) |
| 1792 |
{ |
| 1793 |
/* it's not - flag an error and give up */ |
| 1794 |
results.allNotAllowed(); |
| 1795 |
return []; |
| 1796 |
} |
| 1797 |
|
| 1798 |
/* return the 'all' list */ |
| 1799 |
return resolver.getAll(self); |
| 1800 |
} |
| 1801 |
|
| 1802 |
/* flag an error - our main list has been completely excluded */ |
| 1803 |
flagAllExcepted(resolver, results) |
| 1804 |
{ |
| 1805 |
results.noMatchForAllBut(); |
| 1806 |
} |
| 1807 |
|
| 1808 |
/* |
| 1809 |
* set the "always announce" flag for each item - the player didn't |
| 1810 |
* name the selected items specifically, so always show what we |
| 1811 |
* chose |
| 1812 |
*/ |
| 1813 |
addedFlags = AlwaysAnnounce |
| 1814 |
|
| 1815 |
/* match Collective objects instead of their individuals */ |
| 1816 |
filterForCollectives = true |
| 1817 |
; |
| 1818 |
|
| 1819 |
/* |
| 1820 |
* Base class for "list but" rules, which select everything in an |
| 1821 |
* explicitly provided list minus a set of exceptions; for example, in |
| 1822 |
* English, "take all of the books except the red ones". |
| 1823 |
* |
| 1824 |
* Subclasses defining grammar rules must set the 'np_' property to the |
| 1825 |
* main noun list; we'll resolve this list to find the objects to be |
| 1826 |
* included before exclusions are applied. |
| 1827 |
*/ |
| 1828 |
class ListButProd: ButProd |
| 1829 |
/* our main list is given by the 'np_' subproduction */ |
| 1830 |
getMainList(resolver, results) |
| 1831 |
{ |
| 1832 |
return np_.resolveNouns(resolver, results); |
| 1833 |
} |
| 1834 |
|
| 1835 |
/* flag an error - everything has been excluded */ |
| 1836 |
flagAllExcepted(resolver, results) |
| 1837 |
{ |
| 1838 |
results.noMatchForListBut(); |
| 1839 |
} |
| 1840 |
|
| 1841 |
/* |
| 1842 |
* set the "unclear disambig" flag in our results, so we provide an |
| 1843 |
* indication of which object we chose |
| 1844 |
*/ |
| 1845 |
addedFlags = UnclearDisambig |
| 1846 |
; |
| 1847 |
|
| 1848 |
|
| 1849 |
/* ------------------------------------------------------------------------ */ |
| 1850 |
/* |
| 1851 |
* Pre-resolved phrase production. This isn't normally used in any |
| 1852 |
* actual grammar; instead, this is for use when building actions |
| 1853 |
* programmatically. This allows us to fill in an action tree when we |
| 1854 |
* already know the object we want to resolve. |
| 1855 |
*/ |
| 1856 |
class PreResolvedProd: BasicProd |
| 1857 |
construct(obj) |
| 1858 |
{ |
| 1859 |
/* if it's not a collection, we need to make it a list */ |
| 1860 |
if (!obj.ofKind(Collection)) |
| 1861 |
{ |
| 1862 |
/* if it's not already a ResolveInfo, wrap it in a ResolveInfo */ |
| 1863 |
if (!obj.ofKind(ResolveInfo)) |
| 1864 |
obj = new ResolveInfo(obj, 0, self); |
| 1865 |
|
| 1866 |
/* the resolved object list is simply the one ResolveInfo */ |
| 1867 |
obj = [obj]; |
| 1868 |
} |
| 1869 |
|
| 1870 |
/* store the new ResolveInfo list */ |
| 1871 |
obj_ = obj; |
| 1872 |
} |
| 1873 |
|
| 1874 |
/* resolve the nouns: this is easy, since we started out resolved */ |
| 1875 |
resolveNouns(resolver, results) |
| 1876 |
{ |
| 1877 |
/* return our pre-resolved object */ |
| 1878 |
return obj_; |
| 1879 |
} |
| 1880 |
|
| 1881 |
/* |
| 1882 |
* Our pre-resolved object result. This is a list containing a |
| 1883 |
* single ResolveInfo representing our resolved object, since this is |
| 1884 |
* the form required by callers of resolveNouns. |
| 1885 |
*/ |
| 1886 |
obj_ = nil |
| 1887 |
; |
| 1888 |
|
| 1889 |
/* |
| 1890 |
* A pre-resolved *ambiguous* noun phrase. This is used when the game |
| 1891 |
* or library wants to suggest a specific set of objects for a new |
| 1892 |
* action, then ask which one to use. |
| 1893 |
*/ |
| 1894 |
class PreResolvedAmbigProd: DefiniteNounProd |
| 1895 |
construct(objs, asker, phrase) |
| 1896 |
{ |
| 1897 |
/* remember my list of possible objects as a resolved object list */ |
| 1898 |
objs_ = objs.mapAll({x: new ResolveInfo(x, 0, nil)}); |
| 1899 |
|
| 1900 |
/* remember the ResolveAsker to use */ |
| 1901 |
asker_ = asker; |
| 1902 |
|
| 1903 |
/* remember the noun phrase to use in disambiguation questions */ |
| 1904 |
phrase_ = phrase; |
| 1905 |
} |
| 1906 |
|
| 1907 |
resolveNouns(resolver, results) |
| 1908 |
{ |
| 1909 |
/* resolve our list using definite-phrase rules */ |
| 1910 |
return resolveDefinite(asker_, phrase_, objs_, |
| 1911 |
self, resolver, results); |
| 1912 |
} |
| 1913 |
|
| 1914 |
/* my pre-resolved list of ambiguous objects */ |
| 1915 |
objs_ = nil |
| 1916 |
|
| 1917 |
/* the noun phrase to use in disambiguation questions */ |
| 1918 |
phrase_ = nil |
| 1919 |
|
| 1920 |
/* the ResolveAsker to use when prompting for the selection */ |
| 1921 |
asker_ = nil |
| 1922 |
; |
| 1923 |
|
| 1924 |
/* |
| 1925 |
* Pre-resolved literal phrase production |
| 1926 |
*/ |
| 1927 |
class PreResolvedLiteralProd: BasicProd |
| 1928 |
construct(txt) |
| 1929 |
{ |
| 1930 |
/* |
| 1931 |
* If the argument is a ResolveInfo, assume its obj_ property |
| 1932 |
* gives the literal string, and retrieve the string. |
| 1933 |
*/ |
| 1934 |
if (txt.ofKind(ResolveInfo)) |
| 1935 |
txt = txt.obj_; |
| 1936 |
|
| 1937 |
/* save the text */ |
| 1938 |
text_ = txt; |
| 1939 |
} |
| 1940 |
|
| 1941 |
/* get the text */ |
| 1942 |
getLiteralText(results, action, which) { return text_; } |
| 1943 |
getTentativeLiteralText() { return text_; } |
| 1944 |
|
| 1945 |
/* our underlying text */ |
| 1946 |
text_ = nil |
| 1947 |
; |
| 1948 |
|
| 1949 |
/* |
| 1950 |
* A token list production is an internally generated placeholder when we |
| 1951 |
* synthesize a production rather than matching grammar, and we want to |
| 1952 |
* keep track of the token list that triggered the node. |
| 1953 |
*/ |
| 1954 |
class TokenListProd: BasicProd |
| 1955 |
construct(toks) |
| 1956 |
{ |
| 1957 |
tokenList = toks; |
| 1958 |
firstTokenIndex = 1; |
| 1959 |
lastTokenIndex = toks.length(); |
| 1960 |
} |
| 1961 |
|
| 1962 |
/* the token list */ |
| 1963 |
tokenList = nil |
| 1964 |
; |
| 1965 |
|
| 1966 |
|
| 1967 |
/* ------------------------------------------------------------------------ */ |
| 1968 |
/* |
| 1969 |
* Mix-in class for noun phrase productions that use |
| 1970 |
* ResolveResults.ambiguousNounPhrase(). This mix-in provides the |
| 1971 |
* methods that ambiguousNounPhrase() uses to keep track of past |
| 1972 |
* responses to the disambiguation question. |
| 1973 |
*/ |
| 1974 |
class AmbigResponseKeeper: object |
| 1975 |
addAmbigResponse(resp) |
| 1976 |
{ |
| 1977 |
/* add an ambiguous response to our list */ |
| 1978 |
ambigResponses_ += resp; |
| 1979 |
} |
| 1980 |
|
| 1981 |
getAmbigResponses() |
| 1982 |
{ |
| 1983 |
/* return our list of past interactive disambiguation responses */ |
| 1984 |
return ambigResponses_; |
| 1985 |
} |
| 1986 |
|
| 1987 |
/* our list of saved interactive disambiguation responses */ |
| 1988 |
ambigResponses_ = [] |
| 1989 |
; |
| 1990 |
|
| 1991 |
|
| 1992 |
/* ------------------------------------------------------------------------ */ |
| 1993 |
/* |
| 1994 |
* Base class for noun phrase productions with definite articles. |
| 1995 |
*/ |
| 1996 |
class DefiniteNounProd: NounPhraseProd, AmbigResponseKeeper |
| 1997 |
resolveNouns(resolver, results) |
| 1998 |
{ |
| 1999 |
/* resolve our underlying noun phrase using definite rules */ |
| 2000 |
return resolveDefinite(ResolveAsker, np_.getOrigText(), |
| 2001 |
np_.resolveNouns(resolver, results), |
| 2002 |
self, resolver, results); |
| 2003 |
} |
| 2004 |
|
| 2005 |
/* |
| 2006 |
* Resolve an underlying phrase using definite noun phrase rules. |
| 2007 |
*/ |
| 2008 |
resolveDefinite(asker, origText, lst, responseKeeper, resolver, results) |
| 2009 |
{ |
| 2010 |
local scopeList; |
| 2011 |
local fullList; |
| 2012 |
|
| 2013 |
/* filter the list to remove truncations if we have exact matches */ |
| 2014 |
lst = filterTruncations(lst, resolver); |
| 2015 |
|
| 2016 |
/* |
| 2017 |
* Remember the current list, before filtering for logical |
| 2018 |
* matches and before filtering out equivalents, as our full |
| 2019 |
* scope list. If we have to ask for clarification, this is the |
| 2020 |
* scope of the clarification. |
| 2021 |
*/ |
| 2022 |
scopeList = lst; |
| 2023 |
|
| 2024 |
/* filter for possessive qualification strength */ |
| 2025 |
lst = resolver.filterPossRank(lst, 1); |
| 2026 |
|
| 2027 |
/* filter out the obvious mismatches */ |
| 2028 |
lst = resolver.filterAmbiguousNounPhrase(lst, 1, self); |
| 2029 |
|
| 2030 |
/* |
| 2031 |
* remember the current list as the full match list, before |
| 2032 |
* filtering equivalents |
| 2033 |
*/ |
| 2034 |
fullList = lst; |
| 2035 |
|
| 2036 |
/* |
| 2037 |
* reduce the list to include only one of each set of |
| 2038 |
* equivalents; do this only if the results object allows this |
| 2039 |
* kind of filtering |
| 2040 |
*/ |
| 2041 |
if (results.allowEquivalentFiltering) |
| 2042 |
lst = resolver.filterAmbiguousEquivalents(lst, self); |
| 2043 |
|
| 2044 |
/* do any additional subclass-specific filtering on the list */ |
| 2045 |
lst = reduceDefinite(lst, resolver, results); |
| 2046 |
|
| 2047 |
/* |
| 2048 |
* This is (explicitly or implicitly) a definite noun phrase, so |
| 2049 |
* we must resolve to exactly one object. If the list has more |
| 2050 |
* than one object, we must disambiguate it. |
| 2051 |
*/ |
| 2052 |
if (lst.length() == 0) |
| 2053 |
{ |
| 2054 |
/* there are no objects matching the phrase */ |
| 2055 |
results.noMatch(resolver.getAction(), origText); |
| 2056 |
} |
| 2057 |
else if (lst.length() > 1) |
| 2058 |
{ |
| 2059 |
/* |
| 2060 |
* the noun phrase is ambiguous - pass in the full list |
| 2061 |
* (rather than the list with the redundant equivalents |
| 2062 |
* weeded out) so that we can display the appropriate |
| 2063 |
* choices for multiple equivalent items |
| 2064 |
*/ |
| 2065 |
lst = results.ambiguousNounPhrase( |
| 2066 |
responseKeeper, asker, origText, lst, fullList, |
| 2067 |
scopeList, 1, resolver); |
| 2068 |
} |
| 2069 |
|
| 2070 |
/* note the matched objects in the results */ |
| 2071 |
results.noteMatches(lst); |
| 2072 |
|
| 2073 |
/* return the results */ |
| 2074 |
return lst; |
| 2075 |
} |
| 2076 |
|
| 2077 |
/* |
| 2078 |
* Do any additional subclass-specific filtering to further reduce |
| 2079 |
* the list before we decide whether or not we have sufficient |
| 2080 |
* specificity. We call this just before deciding whether or not to |
| 2081 |
* prompt for more information ("which book do you mean...?"). By |
| 2082 |
* default, this simply returns the same list unchanged; subclasses |
| 2083 |
* that have some further way of narrowing down the options can use |
| 2084 |
* this opportunity to apply that extra narrowing. |
| 2085 |
*/ |
| 2086 |
reduceDefinite(lst, resolver, results) { return lst; } |
| 2087 |
; |
| 2088 |
|
| 2089 |
/* |
| 2090 |
* Base class for a plural production |
| 2091 |
*/ |
| 2092 |
class PluralProd: NounPhraseProd |
| 2093 |
/* |
| 2094 |
* Basic plural noun resolution. We'll retrieve the matching objects |
| 2095 |
* and filter them using filterPluralPhrase. |
| 2096 |
*/ |
| 2097 |
basicPluralResolveNouns(resolver, results) |
| 2098 |
{ |
| 2099 |
local lst; |
| 2100 |
|
| 2101 |
/* resolve the underlying noun phrase */ |
| 2102 |
lst = np_.resolveNouns(resolver, results); |
| 2103 |
|
| 2104 |
/* if there's nothing in it, it's an error */ |
| 2105 |
if (lst.length() == 0) |
| 2106 |
results.noMatch(resolver.getAction(), np_.getOrigText()); |
| 2107 |
|
| 2108 |
/* filter out truncated matches if we have any exact matches */ |
| 2109 |
lst = filterTruncations(lst, resolver); |
| 2110 |
|
| 2111 |
/* filter the plurals for the logical subset and return the result */ |
| 2112 |
lst = resolver.filterPluralPhrase(lst, self); |
| 2113 |
|
| 2114 |
/* if we have a list, sort it by pluralOrder */ |
| 2115 |
if (lst != nil) |
| 2116 |
lst = lst.sort(SortAsc, |
| 2117 |
{a, b: a.obj_.pluralOrder - b.obj_.pluralOrder}); |
| 2118 |
|
| 2119 |
/* note the matches */ |
| 2120 |
results.noteMatches(lst); |
| 2121 |
|
| 2122 |
/* note the plural in the results */ |
| 2123 |
results.notePlural(); |
| 2124 |
|
| 2125 |
/* return the result */ |
| 2126 |
return lst; |
| 2127 |
} |
| 2128 |
|
| 2129 |
/* |
| 2130 |
* Get the verify "keepers" for a plural phrase. |
| 2131 |
* |
| 2132 |
* If the "filter plural matches" configuration flag is set to true, |
| 2133 |
* we'll return the subset of items which are logical for this |
| 2134 |
* command. If the filter flag is nil, we'll simply return the full |
| 2135 |
* set of vocabulary matches without any filtering. |
| 2136 |
*/ |
| 2137 |
getVerifyKeepers(results) |
| 2138 |
{ |
| 2139 |
/* check the global "filter plural matches" configuration flag */ |
| 2140 |
if (gameMain.filterPluralMatches) |
| 2141 |
{ |
| 2142 |
local sub; |
| 2143 |
|
| 2144 |
/* get the subset of items that don't exclude plurals */ |
| 2145 |
sub = results.subset({x: !x.excludePluralMatches}); |
| 2146 |
|
| 2147 |
/* |
| 2148 |
* if that's non-empty, use it as the result; otherwise, just |
| 2149 |
* use the original list |
| 2150 |
*/ |
| 2151 |
return (sub.length() != 0 ? sub : results); |
| 2152 |
} |
| 2153 |
else |
| 2154 |
{ |
| 2155 |
/* we don't want any filtering */ |
| 2156 |
return results; |
| 2157 |
} |
| 2158 |
} |
| 2159 |
; |
| 2160 |
|
| 2161 |
/* |
| 2162 |
* A plural phrase that explicitly selects all of matching objects. For |
| 2163 |
* English, this would be a phrase like "all of the marbles". This type |
| 2164 |
* of phrase matches all of the objects that match the name in the |
| 2165 |
* plural, except that if we have a collective object and we also have |
| 2166 |
* objects that are part of the collective (such as a bag of marbles and |
| 2167 |
* some individual marbles), we'll omit the collective, and match only |
| 2168 |
* the individual items. |
| 2169 |
* |
| 2170 |
* Grammar rule instantiations in language modules should set property |
| 2171 |
* np_ to the plural phrase match tree. |
| 2172 |
*/ |
| 2173 |
class AllPluralProd: PluralProd |
| 2174 |
resolveNouns(resolver, results) |
| 2175 |
{ |
| 2176 |
/* return the basic plural resolution */ |
| 2177 |
return basicPluralResolveNouns(resolver, results); |
| 2178 |
} |
| 2179 |
|
| 2180 |
/* |
| 2181 |
* since the player explicitly told us to use ALL of the matching |
| 2182 |
* objects, keep everything in the verify list, logical or not |
| 2183 |
*/ |
| 2184 |
getVerifyKeepers(results) { return results; } |
| 2185 |
|
| 2186 |
/* prefer to keep individuals over collectives */ |
| 2187 |
filterForCollectives = nil |
| 2188 |
; |
| 2189 |
|
| 2190 |
/* |
| 2191 |
* A plural phrase qualified by a definite article ("the books"). This |
| 2192 |
* type of phrasing doesn't specify anything about the specific number of |
| 2193 |
* items involved, except that they number more than one. |
| 2194 |
* |
| 2195 |
* In most cases, we take this to imply that all of the matching objects |
| 2196 |
* are intended to be included, with one exception: when we have an |
| 2197 |
* object that can serve as a collective for some of the other objects, |
| 2198 |
* we match only the collective but not the other objects. For example, |
| 2199 |
* if we type "take marbles," and we have five marbles and a bag of |
| 2200 |
* marbles that serves as a collective object for three of the five |
| 2201 |
* marbles, we'll match the bag and two marbles not in the bag, but NOT |
| 2202 |
* the marbles that are in the bag. This is usually desirable when |
| 2203 |
* there's a collective object, since it applies the command to the |
| 2204 |
* object standing in for the group rather than applying the command one |
| 2205 |
* by one to each of the individuals in the group. |
| 2206 |
*/ |
| 2207 |
class DefinitePluralProd: PluralProd |
| 2208 |
resolveNouns(resolver, results) |
| 2209 |
{ |
| 2210 |
/* return the basic plural resolution */ |
| 2211 |
return basicPluralResolveNouns(resolver, results); |
| 2212 |
} |
| 2213 |
|
| 2214 |
/* prefer to keep collectives instead of their individuals */ |
| 2215 |
filterForCollectives = true |
| 2216 |
; |
| 2217 |
|
| 2218 |
/* |
| 2219 |
* Quantified plural phrase. |
| 2220 |
*/ |
| 2221 |
class QuantifiedPluralProd: PluralProd |
| 2222 |
/* |
| 2223 |
* Resolve the main noun phrase. By default, we simply resolve np_, |
| 2224 |
* but we make this separately overridable to allow this class to be |
| 2225 |
* subclassed for quantifying other types of plural phrases. |
| 2226 |
* |
| 2227 |
* If this is unable to resolve the list, it can flag an appropriate |
| 2228 |
* error via the results object and return nil. If this routine |
| 2229 |
* returns nil, our main resolver will simply return an empty list |
| 2230 |
* without further flagging of any errors. |
| 2231 |
*/ |
| 2232 |
resolveMainPhrase(resolver, results) |
| 2233 |
{ |
| 2234 |
/* resolve the main noun phrase */ |
| 2235 |
return np_.resolveNouns(resolver, results); |
| 2236 |
} |
| 2237 |
|
| 2238 |
/* |
| 2239 |
* get the quantity specified - by default, this comes from the |
| 2240 |
* quantifier phrase in "quant_" |
| 2241 |
*/ |
| 2242 |
getQuantity() { return quant_.getval(); } |
| 2243 |
|
| 2244 |
/* resolve the noun phrase */ |
| 2245 |
resolveNouns(resolver, results) |
| 2246 |
{ |
| 2247 |
local lst; |
| 2248 |
local num; |
| 2249 |
|
| 2250 |
/* resolve the underlying noun phrase */ |
| 2251 |
if ((lst = resolveMainPhrase(resolver, results)) == nil) |
| 2252 |
return []; |
| 2253 |
|
| 2254 |
/* filter out truncated matches if we have any exact matches */ |
| 2255 |
lst = filterTruncations(lst, resolver); |
| 2256 |
|
| 2257 |
/* sort the list in ascending order of pluralOrder */ |
| 2258 |
if (lst != nil) |
| 2259 |
lst = lst.sort(SortAsc, |
| 2260 |
{a, b: a.obj_.pluralOrder - b.obj_.pluralOrder}); |
| 2261 |
|
| 2262 |
/* get the quantity desired */ |
| 2263 |
num = getQuantity(); |
| 2264 |
|
| 2265 |
/* |
| 2266 |
* if we have at least the desired number, arbitrarily choose |
| 2267 |
* the desired number; otherwise, it's an error |
| 2268 |
*/ |
| 2269 |
if (num == 0) |
| 2270 |
{ |
| 2271 |
/* zero objects makes no sense */ |
| 2272 |
results.zeroQuantity(np_.getOrigText()); |
| 2273 |
} |
| 2274 |
else |
| 2275 |
{ |
| 2276 |
local qsum; |
| 2277 |
|
| 2278 |
/* if we have too many, disambiguate */ |
| 2279 |
if (lst.length() >= num) |
| 2280 |
{ |
| 2281 |
local scopeList; |
| 2282 |
|
| 2283 |
/* remember the list of everything in scope that matches */ |
| 2284 |
scopeList = lst; |
| 2285 |
|
| 2286 |
/* filter for possessive qualifier strength */ |
| 2287 |
lst = resolver.filterPossRank(lst, num); |
| 2288 |
|
| 2289 |
/* |
| 2290 |
* Use the normal disambiguation ranking to find the best |
| 2291 |
* set of possibilities. |
| 2292 |
*/ |
| 2293 |
lst = resolver.filterAmbiguousNounPhrase(lst, num, self); |
| 2294 |
|
| 2295 |
/* |
| 2296 |
* If that left us with more than we're looking for, call |
| 2297 |
* our selection routine to select the subset. If it |
| 2298 |
* left us with too few, note it in the results. |
| 2299 |
*/ |
| 2300 |
if (lst.length() > num) |
| 2301 |
{ |
| 2302 |
/* select the desired exact count */ |
| 2303 |
lst = selectExactCount(lst, num, scopeList, |
| 2304 |
resolver, results); |
| 2305 |
} |
| 2306 |
} |
| 2307 |
|
| 2308 |
/* |
| 2309 |
* Check to make sure we have enough items matching. Go by |
| 2310 |
* the 'quant_' property of the ResolveInfo entries, since we |
| 2311 |
* might have a single ResolveInfo object that represents a |
| 2312 |
* quantity of objects from the player's perspective. |
| 2313 |
*/ |
| 2314 |
qsum = 0; |
| 2315 |
lst.forEach({x: qsum += x.quant_}); |
| 2316 |
if (qsum < num) |
| 2317 |
{ |
| 2318 |
/* note in the results that there aren't enough matches */ |
| 2319 |
results.insufficientQuantity(np_.getOrigText(), lst, num); |
| 2320 |
} |
| 2321 |
} |
| 2322 |
|
| 2323 |
/* note the matched objects in the results */ |
| 2324 |
results.noteMatches(lst); |
| 2325 |
|
| 2326 |
/* return the results */ |
| 2327 |
return lst; |
| 2328 |
} |
| 2329 |
|
| 2330 |
/* |
| 2331 |
* Select the desired number of matches from what the normal |
| 2332 |
* disambiguation filtering leaves us with. |
| 2333 |
* |
| 2334 |
* Note that this will never be called with 'num' larger than the |
| 2335 |
* number in the current list. This is only called to select a |
| 2336 |
* smaller subset than we currently have. |
| 2337 |
* |
| 2338 |
* By default, we'll simply select an arbitrary subset, since we |
| 2339 |
* simply want any 'num' of the matches. This can be overridden if |
| 2340 |
* other behaviors are needed. |
| 2341 |
*/ |
| 2342 |
selectExactCount(lst, num, scopeList, resolver, results) |
| 2343 |
{ |
| 2344 |
/* |
| 2345 |
* If we want less than what we actually got, arbitrarily pick |
| 2346 |
* the first 'num' elements; otherwise, return what we have. |
| 2347 |
*/ |
| 2348 |
if (lst.length() > num) |
| 2349 |
return lst.sublist(1, num); |
| 2350 |
else |
| 2351 |
return lst; |
| 2352 |
} |
| 2353 |
|
| 2354 |
/* |
| 2355 |
* Since the player explicitly told us to use a given number of |
| 2356 |
* matching objects, keep the required number, logical or not. |
| 2357 |
*/ |
| 2358 |
getVerifyKeepers(results) |
| 2359 |
{ |
| 2360 |
/* get the quantity desired */ |
| 2361 |
local num = getQuantity(); |
| 2362 |
|
| 2363 |
/* |
| 2364 |
* if we have at least the number required, arbitrarily choose |
| 2365 |
* the initial subset of the desired length; otherwise, use them |
| 2366 |
* all |
| 2367 |
*/ |
| 2368 |
if (results.length() > num) |
| 2369 |
return results.sublist(1, num); |
| 2370 |
else |
| 2371 |
return results; |
| 2372 |
} |
| 2373 |
; |
| 2374 |
|
| 2375 |
/* |
| 2376 |
* Exact quantified plural phrase. This is similar to the normal |
| 2377 |
* quantified plural, but has the additional requirement of matching an |
| 2378 |
* unambiguous set of the exact given number ("the five books" means |
| 2379 |
* that we expect to find exactly five books matching the phrase - no |
| 2380 |
* fewer, and no more). |
| 2381 |
*/ |
| 2382 |
class ExactQuantifiedPluralProd: QuantifiedPluralProd, AmbigResponseKeeper |
| 2383 |
/* |
| 2384 |
* Select the desired number of matches. Since we want an exact set |
| 2385 |
* of matches, we'll run disambiguation on the set. |
| 2386 |
*/ |
| 2387 |
selectExactCount(lst, num, scopeList, resolver, results) |
| 2388 |
{ |
| 2389 |
local fullList; |
| 2390 |
|
| 2391 |
/* remember the list before filtering for redundant equivalents */ |
| 2392 |
fullList = lst; |
| 2393 |
|
| 2394 |
/* reduce the list by removing redundant equivalents, if allowed */ |
| 2395 |
if (results.allowEquivalentFiltering) |
| 2396 |
lst = resolver.filterAmbiguousEquivalents(lst, self); |
| 2397 |
|
| 2398 |
/* |
| 2399 |
* if the reduced list has only one element, everything in the |
| 2400 |
* original list must have been equivalent, so arbitrarily pick |
| 2401 |
* the desired number of items from the original list |
| 2402 |
*/ |
| 2403 |
if (lst.length() == 1) |
| 2404 |
return fullList.sublist(1, num); |
| 2405 |
|
| 2406 |
/* we still have too many items, so disambiguate the results */ |
| 2407 |
return results.ambiguousNounPhrase( |
| 2408 |
self, ResolveAsker, np_.getOrigText(), |
| 2409 |
lst, fullList, scopeList, num, resolver); |
| 2410 |
} |
| 2411 |
|
| 2412 |
/* get the keepers in the verify stage */ |
| 2413 |
getVerifyKeepers(results) |
| 2414 |
{ |
| 2415 |
/* |
| 2416 |
* keep everything: we want an exact quantity, so we want the |
| 2417 |
* keepers to match the required quantity on their own, without |
| 2418 |
* any arbitrary paring down |
| 2419 |
*/ |
| 2420 |
return results; |
| 2421 |
} |
| 2422 |
; |
| 2423 |
|
| 2424 |
/* |
| 2425 |
* Noun phrase with an indefinite article |
| 2426 |
*/ |
| 2427 |
class IndefiniteNounProd: NounPhraseProd |
| 2428 |
/* |
| 2429 |
* resolve the main phrase - this is separately overridable to allow |
| 2430 |
* subclassing |
| 2431 |
*/ |
| 2432 |
resolveMainPhrase(resolver, results) |
| 2433 |
{ |
| 2434 |
/* by default, resolve the main noun phrase */ |
| 2435 |
return np_.resolveNouns(resolver, results); |
| 2436 |
} |
| 2437 |
|
| 2438 |
resolveNouns(resolver, results) |
| 2439 |
{ |
| 2440 |
local lst; |
| 2441 |
local allEquiv = nil; |
| 2442 |
|
| 2443 |
/* resolve the underlying list */ |
| 2444 |
if ((lst = resolveMainPhrase(resolver, results)) == nil) |
| 2445 |
return []; |
| 2446 |
|
| 2447 |
/* filter out truncated matches if we have any exact matches */ |
| 2448 |
lst = filterTruncations(lst, resolver); |
| 2449 |
|
| 2450 |
/* see what we found */ |
| 2451 |
if (lst.length() == 0) |
| 2452 |
{ |
| 2453 |
/* it turned up nothing - note the problem */ |
| 2454 |
results.noMatch(resolver.getAction(), np_.getOrigText()); |
| 2455 |
} |
| 2456 |
else if (lst.length() > 1) |
| 2457 |
{ |
| 2458 |
/* |
| 2459 |
* There are multiple objects, but the phrase is indefinite, |
| 2460 |
* which means that it doesn't refer to a specific matching |
| 2461 |
* object but could refer to any of them. |
| 2462 |
*/ |
| 2463 |
|
| 2464 |
/* start by noting if the choices are all equivalent */ |
| 2465 |
allEquiv = areAllEquiv(lst); |
| 2466 |
|
| 2467 |
/* |
| 2468 |
* Filter using possessive qualifier strength and then normal |
| 2469 |
* disambiguation ranking to find the best set of |
| 2470 |
* possibilities, then pick which we want. |
| 2471 |
*/ |
| 2472 |
lst = resolver.filterPossRank(lst, 1); |
| 2473 |
lst = resolver.filterAmbiguousNounPhrase(lst, 1, self); |
| 2474 |
lst = selectFromList(resolver, results, lst); |
| 2475 |
} |
| 2476 |
|
| 2477 |
/* |
| 2478 |
* Set the "unclear disambiguation" flag on the item we picked - |
| 2479 |
* our selection was arbitrary, so it's polite to let the player |
| 2480 |
* know which we chose. However, don't do this if the possible |
| 2481 |
* matches were all equivalent to start with, as the player's |
| 2482 |
* input must already have been as specific as we can be in |
| 2483 |
* reporting the choice. |
| 2484 |
*/ |
| 2485 |
if (lst.length() == 1 && !allEquiv) |
| 2486 |
lst[1].flags_ |= UnclearDisambig; |
| 2487 |
|
| 2488 |
/* note the matched objects in the results */ |
| 2489 |
results.noteMatches(lst); |
| 2490 |
|
| 2491 |
/* note that this is an indefinite phrasing */ |
| 2492 |
results.noteIndefinite(); |
| 2493 |
|
| 2494 |
/* return the results */ |
| 2495 |
return lst; |
| 2496 |
} |
| 2497 |
|
| 2498 |
/* are all of the items in the resolve list equivalents? */ |
| 2499 |
areAllEquiv(lst) |
| 2500 |
{ |
| 2501 |
local first = lst[1].obj_; |
| 2502 |
|
| 2503 |
/* check each item to see if it's equivalent to the first */ |
| 2504 |
for (local i = 2, local cnt = lst.length() ; i <= cnt ; ++i) |
| 2505 |
{ |
| 2506 |
/* |
| 2507 |
* if this one isn't equivalent to the first, then they're |
| 2508 |
* not all equivalent |
| 2509 |
*/ |
| 2510 |
if (!first.isVocabEquivalent(lst[i].obj_)) |
| 2511 |
return nil; |
| 2512 |
} |
| 2513 |
|
| 2514 |
/* we didn't find any non-equivalents, so they're all equivalents */ |
| 2515 |
return true; |
| 2516 |
} |
| 2517 |
|
| 2518 |
/* |
| 2519 |
* Select an item from the list of potential matches, given the list |
| 2520 |
* sorted from most likely to least likely (according to the |
| 2521 |
* resolver's ambiguous match filter). We'll ask the resolver to |
| 2522 |
* make the selection, because indefinite noun phrases can mean |
| 2523 |
* different things in different contexts. |
| 2524 |
*/ |
| 2525 |
selectFromList(resolver, results, lst) |
| 2526 |
{ |
| 2527 |
/* ask the resolver to select */ |
| 2528 |
return resolver.selectIndefinite(results, lst, 1); |
| 2529 |
} |
| 2530 |
|
| 2531 |
; |
| 2532 |
|
| 2533 |
/* |
| 2534 |
* Noun phrase explicitly asking us to choose an object arbitrarily |
| 2535 |
* (with a word like "any"). This is similar to the indefinite noun |
| 2536 |
* phrase, but differs in that this phrase is *explicitly* arbitrary, |
| 2537 |
* rather than merely indefinite. |
| 2538 |
*/ |
| 2539 |
class ArbitraryNounProd: IndefiniteNounProd |
| 2540 |
/* |
| 2541 |
* Select an object from a list of potential matches. Since the |
| 2542 |
* choice is explicitly arbitrary, we simply choose the first |
| 2543 |
* (they're in order from most likely to least likely, so this will |
| 2544 |
* choose the most likely). |
| 2545 |
*/ |
| 2546 |
selectFromList(resolver, results, lst) |
| 2547 |
{ |
| 2548 |
/* simply select the first item */ |
| 2549 |
return lst.sublist(1, 1); |
| 2550 |
} |
| 2551 |
; |
| 2552 |
|
| 2553 |
/* |
| 2554 |
* Noun phrase with an indefinite article and an exclusion ("any of the |
| 2555 |
* books except the red one") |
| 2556 |
*/ |
| 2557 |
class IndefiniteNounButProd: ButProd |
| 2558 |
/* resolve our main phrase */ |
| 2559 |
resolveMainPhrase(resolver, results) |
| 2560 |
{ |
| 2561 |
/* note that this is an indefinite phrasing */ |
| 2562 |
results.noteIndefinite(); |
| 2563 |
|
| 2564 |
/* by default, simply resolve the underlying noun phrase */ |
| 2565 |
return np_.resolveNouns(resolver, results); |
| 2566 |
} |
| 2567 |
|
| 2568 |
/* get our main list */ |
| 2569 |
getMainList(resolver, results) |
| 2570 |
{ |
| 2571 |
local lst; |
| 2572 |
|
| 2573 |
/* resolve the underlying list */ |
| 2574 |
if ((lst = resolveMainPhrase(resolver, results)) == nil) |
| 2575 |
return []; |
| 2576 |
|
| 2577 |
/* filter for possessive qualifier strength */ |
| 2578 |
lst = resolver.filterPossRank(lst, 1); |
| 2579 |
|
| 2580 |
/* filter it to pick the most likely objects */ |
| 2581 |
lst = resolver.filterAmbiguousNounPhrase(lst, 1, self); |
| 2582 |
|
| 2583 |
/* return the filtered list */ |
| 2584 |
return lst; |
| 2585 |
} |
| 2586 |
|
| 2587 |
/* flag an error - everything has been excluded */ |
| 2588 |
flagAllExcepted(resolver, results) |
| 2589 |
{ |
| 2590 |
results.noMatchForListBut(); |
| 2591 |
} |
| 2592 |
|
| 2593 |
/* filter the final list */ |
| 2594 |
filterFinalList(lst) |
| 2595 |
{ |
| 2596 |
/* we want to keep only one item - arbitrarily take the first one */ |
| 2597 |
return (lst.length() == 0 ? [] : lst.sublist(1, 1)); |
| 2598 |
} |
| 2599 |
|
| 2600 |
/* |
| 2601 |
* set the "unclear disambig" flag in our results, so we provide an |
| 2602 |
* indication of which object we chose |
| 2603 |
*/ |
| 2604 |
addedFlags = UnclearDisambig |
| 2605 |
; |
| 2606 |
|
| 2607 |
/* |
| 2608 |
* A qualified plural phrase explicitly including two objects (such as, |
| 2609 |
* in English, "both books"). |
| 2610 |
*/ |
| 2611 |
class BothPluralProd: ExactQuantifiedPluralProd |
| 2612 |
/* the quantity specified by a "both" phrase is 2 */ |
| 2613 |
getQuantity() { return 2; } |
| 2614 |
; |
| 2615 |
|
| 2616 |
/* ------------------------------------------------------------------------ */ |
| 2617 |
/* |
| 2618 |
* Possessive adjectives |
| 2619 |
*/ |
| 2620 |
|
| 2621 |
class PossessivePronounAdjProd: PronounProd |
| 2622 |
/* |
| 2623 |
* Possessive pronouns can refer to the earlier noun phrases of the |
| 2624 |
* same predicate, which is to say that they're anaphoric. For |
| 2625 |
* example, in GIVE BOB HIS BOOK, 'his' refers to Bob. |
| 2626 |
*/ |
| 2627 |
checkAnaphoricBinding(resolver, results) |
| 2628 |
{ |
| 2629 |
local lst; |
| 2630 |
|
| 2631 |
/* if we simply can't be an anaphor, there's no binding */ |
| 2632 |
if (!canBeAnaphor) |
| 2633 |
return nil; |
| 2634 |
|
| 2635 |
/* ask the resolver for the reflexive binding, if any */ |
| 2636 |
lst = resolver.getReflexiveBinding(pronounType); |
| 2637 |
|
| 2638 |
/* |
| 2639 |
* If there's no binding from the verb, or it doesn't match in |
| 2640 |
* number and gender, try an anaphoric binding from the actor. |
| 2641 |
*/ |
| 2642 |
if (lst == nil || (lst != [] && !checkAnaphorAgreement(lst))) |
| 2643 |
{ |
| 2644 |
/* get the actor's anaphoric possessive binding */ |
| 2645 |
local obj = resolver.actor_.getPossAnaphor(pronounType); |
| 2646 |
|
| 2647 |
/* if we got an object or a list, make a resolve list */ |
| 2648 |
if (obj != nil) |
| 2649 |
{ |
| 2650 |
if (obj.ofKind(Collection)) |
| 2651 |
lst = obj.mapAll({x: new ResolveInfo(x, 0, self)}); |
| 2652 |
else |
| 2653 |
lst = [new ResolveInfo(obj, 0, self)]; |
| 2654 |
} |
| 2655 |
} |
| 2656 |
|
| 2657 |
/* |
| 2658 |
* If we got a binding, make sure we agree in number and gender; |
| 2659 |
* if not, don't use the anaphoric form. This isn't an error; |
| 2660 |
* it just means we're falling back on the regular antecedent |
| 2661 |
* binding. If we have an empty list, it means the action isn't |
| 2662 |
* ready to tell us the binding yet, so we can't verify it yet. |
| 2663 |
*/ |
| 2664 |
if (lst != nil && (lst == [] || checkAnaphorAgreement(lst))) |
| 2665 |
return lst; |
| 2666 |
|
| 2667 |
/* don't use an anaphoric binding */ |
| 2668 |
return nil; |
| 2669 |
} |
| 2670 |
|
| 2671 |
/* this is a possessive usage of the pronoun */ |
| 2672 |
isPossessive = true |
| 2673 |
|
| 2674 |
/* |
| 2675 |
* Can we be an anaphor? By default, we consider third-person |
| 2676 |
* possessive pronouns to be anaphoric, and others to be |
| 2677 |
* non-anaphoric. For example, in GIVE BOB MY BOOK, MY always refers |
| 2678 |
* to the speaker, so it's clearly not anaphoric within the sentence. |
| 2679 |
*/ |
| 2680 |
canBeAnaphor = true |
| 2681 |
|
| 2682 |
/* |
| 2683 |
* Check agreement to a given anaphoric pronoun binding. The |
| 2684 |
* language module should override this for each pronoun type to |
| 2685 |
* ensure that the actual contents of the list agree in number and |
| 2686 |
* gender with this type of pronoun. If so, return true; if not, |
| 2687 |
* return nil. It's not an error or a ranking demerit if we don't |
| 2688 |
* agree; it just means that we'll fall back on the regular pronoun |
| 2689 |
* antecedent rather than trying to use an anaphoric binding. |
| 2690 |
*/ |
| 2691 |
checkAnaphorAgreement(lst) { return true; } |
| 2692 |
|
| 2693 |
/* |
| 2694 |
* By default, the "main text" of a possessive pronoun is the same as |
| 2695 |
* the actual token text. Languages can override this as needed> |
| 2696 |
*/ |
| 2697 |
getOrigMainText() { return getOrigText(); } |
| 2698 |
; |
| 2699 |
|
| 2700 |
class ItsAdjProd: PossessivePronounAdjProd |
| 2701 |
pronounType = PronounIt |
| 2702 |
; |
| 2703 |
|
| 2704 |
class HisAdjProd: PossessivePronounAdjProd |
| 2705 |
pronounType = PronounHim |
| 2706 |
; |
| 2707 |
|
| 2708 |
class HerAdjProd: PossessivePronounAdjProd |
| 2709 |
pronounType = PronounHer |
| 2710 |
; |
| 2711 |
|
| 2712 |
class TheirAdjProd: PossessivePronounAdjProd |
| 2713 |
pronounType = PronounThem |
| 2714 |
; |
| 2715 |
|
| 2716 |
class YourAdjProd: PossessivePronounAdjProd |
| 2717 |
pronounType = PronounYou |
| 2718 |
canBeAnaphor = nil |
| 2719 |
; |
| 2720 |
|
| 2721 |
class MyAdjProd: PossessivePronounAdjProd |
| 2722 |
pronounType = PronounMe |
| 2723 |
canBeAnaphor = nil |
| 2724 |
; |
| 2725 |
|
| 2726 |
/* |
| 2727 |
* Possessive nouns |
| 2728 |
*/ |
| 2729 |
class PossessivePronounNounProd: PronounProd |
| 2730 |
/* this is a possessive usage of the pronoun */ |
| 2731 |
isPossessive = true |
| 2732 |
; |
| 2733 |
|
| 2734 |
class ItsNounProd: PossessivePronounNounProd |
| 2735 |
pronounType = PronounIt |
| 2736 |
; |
| 2737 |
|
| 2738 |
class HisNounProd: PossessivePronounNounProd |
| 2739 |
pronounType = PronounHim |
| 2740 |
; |
| 2741 |
|
| 2742 |
class HersNounProd: PossessivePronounNounProd |
| 2743 |
pronounType = PronounHer |
| 2744 |
; |
| 2745 |
|
| 2746 |
class TheirsNounProd: PossessivePronounNounProd |
| 2747 |
pronounType = PronounThem |
| 2748 |
; |
| 2749 |
|
| 2750 |
class YoursNounProd: PossessivePronounNounProd |
| 2751 |
pronounType = PronounYou |
| 2752 |
; |
| 2753 |
|
| 2754 |
class MineNounProd: PossessivePronounNounProd |
| 2755 |
pronounType = PronounMe |
| 2756 |
; |
| 2757 |
|
| 2758 |
/* |
| 2759 |
* Basic possessive phrase. The grammar rules for these phrases must map |
| 2760 |
* the possessive qualifier phrase to poss_, and the noun phrase being |
| 2761 |
* qualified to np_. We are based on DefiniteNounProd because we resolve |
| 2762 |
* the possessive qualifier as though it had a definite article. |
| 2763 |
* |
| 2764 |
* The possessive production object poss_ must define the method |
| 2765 |
* getOrigMainText() to return the text of its noun phrase in a format |
| 2766 |
* suitable for disambiguation prompts or error messages. In English, |
| 2767 |
* for example, this means that the getOrigMainText() must omit the |
| 2768 |
* apostrophe-S suffix if present. |
| 2769 |
*/ |
| 2770 |
class BasicPossessiveProd: DefiniteNounProd |
| 2771 |
/* |
| 2772 |
* To allow this class to be mixed with other classes that have |
| 2773 |
* mixed-in ambiguous response keepers, create a separate object to |
| 2774 |
* hold our ambiguous response keeper for the possessive phrase. We |
| 2775 |
* will never use our own ambiguous response keeper properties, so |
| 2776 |
* those are available to any other production class we're mixed |
| 2777 |
* into. |
| 2778 |
*/ |
| 2779 |
construct() |
| 2780 |
{ |
| 2781 |
/* create an AmbigResponseKeeper for the possessive phrase */ |
| 2782 |
npKeeper = new AmbigResponseKeeper; |
| 2783 |
} |
| 2784 |
|
| 2785 |
/* |
| 2786 |
* Resolve the possessive, and perform preliminary resolution of the |
| 2787 |
* qualified noun phrase. We find the owner object and reduce the |
| 2788 |
* resolved objects for the qualified phrase to those owned by the |
| 2789 |
* owner. |
| 2790 |
* |
| 2791 |
* If we fail, we return nil. Otherwise, we return a list of the |
| 2792 |
* tentatively resolved objects. The caller can further resolve |
| 2793 |
* this list as needed. |
| 2794 |
*/ |
| 2795 |
resolvePossessive(resolver, results, num) |
| 2796 |
{ |
| 2797 |
/* resolve the underlying noun phrase being qualified */ |
| 2798 |
local lst = np_.resolveNouns(resolver, results); |
| 2799 |
|
| 2800 |
/* if we found no matches for the noun phrase, so note */ |
| 2801 |
if (lst.length() == 0) |
| 2802 |
{ |
| 2803 |
results.noMatchPossessive(resolver.getAction(), np_.getOrigText()); |
| 2804 |
return nil; |
| 2805 |
} |
| 2806 |
|
| 2807 |
/* |
| 2808 |
* resolve the possessive phrase and reduce the list to select |
| 2809 |
* only the items owned by the possessor |
| 2810 |
*/ |
| 2811 |
lst = selectWithPossessive(resolver, results, lst, |
| 2812 |
np_.getOrigText(), num); |
| 2813 |
|
| 2814 |
/* return the tentative resolution list for the qualified phrase */ |
| 2815 |
return lst; |
| 2816 |
} |
| 2817 |
|
| 2818 |
/* |
| 2819 |
* Resolve the possessive, and reduce the given match list by |
| 2820 |
* selecting only those items owned by the resolution of the |
| 2821 |
* possessive phrase. |
| 2822 |
* |
| 2823 |
* 'num' is the number of objects we want to select. If the noun |
| 2824 |
* phrase being qualified is singular, this will be 1; if it's |
| 2825 |
* plural, this will be nil, to indicate that there's no specific |
| 2826 |
* target quantity; if the phrase is something like "bob's five |
| 2827 |
* books," the the number will be the qualifying quantity (5, in this |
| 2828 |
* case). |
| 2829 |
*/ |
| 2830 |
selectWithPossessive(resolver, results, lst, lstOrigText, num) |
| 2831 |
{ |
| 2832 |
/* |
| 2833 |
* Create the possessive resolver. Note that we resolve the |
| 2834 |
* possessive phrase in the context of the resolver's indicated |
| 2835 |
* qualifier resolver, which might not be the same as the |
| 2836 |
* resolver for the overall phrase. |
| 2837 |
*/ |
| 2838 |
local possResolver = resolver.getPossessiveResolver(); |
| 2839 |
|
| 2840 |
/* enter a single-object slot for the possessive phrase */ |
| 2841 |
results.beginSingleObjSlot(); |
| 2842 |
|
| 2843 |
/* resolve the underlying possessive */ |
| 2844 |
local possLst = poss_.resolveNouns(possResolver, results); |
| 2845 |
|
| 2846 |
/* perform the normal resolve list filtering */ |
| 2847 |
possLst = resolver.getAction().finishResolveList( |
| 2848 |
possLst, resolver.whichObject, self, nil); |
| 2849 |
|
| 2850 |
/* done with the single-object slot */ |
| 2851 |
results.endSingleObjSlot(); |
| 2852 |
|
| 2853 |
/* |
| 2854 |
* If that resolved to an empty list, return now with an empty |
| 2855 |
* list. The underlying possessive resolver will have noted an |
| 2856 |
* error if this is indeed an error; if it's not an error, it |
| 2857 |
* means that we're pending resolution of the other noun phrase |
| 2858 |
* to resolve an anaphor in the possessive phrase. |
| 2859 |
*/ |
| 2860 |
if (possLst == []) |
| 2861 |
{ |
| 2862 |
/* |
| 2863 |
* we must have a pending anaphor to resolve - simply return |
| 2864 |
* the current list without any possessive filtering |
| 2865 |
*/ |
| 2866 |
return lst; |
| 2867 |
} |
| 2868 |
|
| 2869 |
/* |
| 2870 |
* If the possessive phrase itself is singular, treat the |
| 2871 |
* possessive phrase as a definite phrase, requiring an |
| 2872 |
* unambiguous referent. If it's plural ("the men's books"), |
| 2873 |
* leave it as it is, taking it to mean that we want to select |
| 2874 |
* things that are owned by any/all of the possessors. |
| 2875 |
* |
| 2876 |
* Note that the possessive phrase has no qualifier - any |
| 2877 |
* qualifier applies to the noun phrase our possessive is also |
| 2878 |
* qualifying, not to the possessive phrase itself. |
| 2879 |
*/ |
| 2880 |
local owner; |
| 2881 |
if (poss_.isPluralPossessive) |
| 2882 |
{ |
| 2883 |
/* |
| 2884 |
* The possessive phrase is plural, so don't reduce its match |
| 2885 |
* to a single object; instead, select all of the objects |
| 2886 |
* owned by any of the possessors. The owner is anyone in |
| 2887 |
* the list. |
| 2888 |
*/ |
| 2889 |
owner = possLst.mapAll({x: x.obj_}); |
| 2890 |
} |
| 2891 |
else |
| 2892 |
{ |
| 2893 |
/* |
| 2894 |
* the possessive phrase is singular, so resolve the |
| 2895 |
* possessive qualifier as a definite noun |
| 2896 |
*/ |
| 2897 |
possLst = resolveDefinite( |
| 2898 |
ResolveAsker, poss_.getOrigMainText(), possLst, |
| 2899 |
npKeeper, possResolver, results); |
| 2900 |
|
| 2901 |
/* |
| 2902 |
* if we didn't manage to find a single resolution to the |
| 2903 |
* possessive phrase, we can't resolve the rest of the phrase |
| 2904 |
* yet |
| 2905 |
*/ |
| 2906 |
if (possLst.length() != 1) |
| 2907 |
{ |
| 2908 |
/* if we got more than one object, it's a separate error */ |
| 2909 |
if (possLst.length() > 1) |
| 2910 |
results.uniqueObjectRequired(poss_.getOrigMainText(), |
| 2911 |
possLst); |
| 2912 |
|
| 2913 |
/* we can't go on */ |
| 2914 |
return []; |
| 2915 |
} |
| 2916 |
|
| 2917 |
/* get the resolved owner object */ |
| 2918 |
owner = [possLst[1].obj_]; |
| 2919 |
} |
| 2920 |
|
| 2921 |
/* select the objects owned by any of the owners */ |
| 2922 |
local newLst = lst.subset({x: owner.indexWhich( |
| 2923 |
{y: x.obj_.isOwnedBy(y)}) != nil}); |
| 2924 |
|
| 2925 |
/* |
| 2926 |
* If that didn't leave any results, try one more thing: if the |
| 2927 |
* owner is itself in the list of possessed objects, keep the |
| 2928 |
* owner. This allows for sequences like this: |
| 2929 |
* |
| 2930 |
* >take book |
| 2931 |
*. Which book? |
| 2932 |
*. >the man's |
| 2933 |
*. Which man, Bob or Dave? |
| 2934 |
*. >bob's |
| 2935 |
* |
| 2936 |
* In this case, the qualified object list will be [bob,dave], |
| 2937 |
* and the owner will be [bob], so we want to keep [bob] as the |
| 2938 |
* result list. |
| 2939 |
*/ |
| 2940 |
if (newLst == []) |
| 2941 |
newLst = lst.subset({x: owner.indexOf(x.obj_) != nil}); |
| 2942 |
|
| 2943 |
/* use the new list we found */ |
| 2944 |
lst = newLst; |
| 2945 |
|
| 2946 |
/* |
| 2947 |
* Give each item an ownership priority ranking, since the items |
| 2948 |
* are being qualified by owner: explicitly owned items have the |
| 2949 |
* highest ranking, items directly held but not explicitly owned |
| 2950 |
* have the second ranking, and items merely held have the |
| 2951 |
* lowest ranking. If we deem the list to be ambiguous later, |
| 2952 |
* we'll apply the ownership priority ranking first in trying to |
| 2953 |
* disambiguate. |
| 2954 |
*/ |
| 2955 |
foreach (local cur in lst) |
| 2956 |
{ |
| 2957 |
/* |
| 2958 |
* give the item a ranking: explicitly owned, directly held, |
| 2959 |
* or other |
| 2960 |
*/ |
| 2961 |
if (owner.indexOf(cur.obj_.owner) != nil) |
| 2962 |
cur.possRank_ = 2; |
| 2963 |
else if (owner.indexWhich({x: cur.obj_.isDirectlyIn(x)}) != nil) |
| 2964 |
cur.possRank_ = 1; |
| 2965 |
} |
| 2966 |
|
| 2967 |
/* if we found nothing, mention it */ |
| 2968 |
if (lst.length() == 0) |
| 2969 |
{ |
| 2970 |
results.noMatchForPossessive(owner, lstOrigText); |
| 2971 |
return []; |
| 2972 |
} |
| 2973 |
|
| 2974 |
/* return the reduced list */ |
| 2975 |
return lst; |
| 2976 |
} |
| 2977 |
|
| 2978 |
/* our ambiguous response keeper */ |
| 2979 |
npKeeper = nil |
| 2980 |
; |
| 2981 |
|
| 2982 |
/* |
| 2983 |
* Possessive phrase + singular noun phrase. The language grammar rule |
| 2984 |
* must map poss_ to the possessive production and np_ to the noun |
| 2985 |
* phrase being qualified. |
| 2986 |
*/ |
| 2987 |
class PossessiveNounProd: BasicPossessiveProd |
| 2988 |
resolveNouns(resolver, results) |
| 2989 |
{ |
| 2990 |
local lst; |
| 2991 |
|
| 2992 |
/* |
| 2993 |
* perform the initial qualification; if that fails, give up now |
| 2994 |
* and return an empty list |
| 2995 |
*/ |
| 2996 |
if ((lst = resolvePossessive(resolver, results, 1)) == nil) |
| 2997 |
return []; |
| 2998 |
|
| 2999 |
/* now resolve the underlying list definitely */ |
| 3000 |
return resolveDefinite(ResolveAsker, np_.getOrigText(), lst, self, |
| 3001 |
resolver, results); |
| 3002 |
} |
| 3003 |
|
| 3004 |
/* our AmbigResponseKeeper for the qualified noun phrase */ |
| 3005 |
npKeeper = nil |
| 3006 |
; |
| 3007 |
|
| 3008 |
/* |
| 3009 |
* Possessive phrase + plural noun phrase. The grammar rule must set |
| 3010 |
* poss_ to the possessive and np_ to the plural. |
| 3011 |
*/ |
| 3012 |
class PossessivePluralProd: BasicPossessiveProd |
| 3013 |
resolveNouns(resolver, results) |
| 3014 |
{ |
| 3015 |
local lst; |
| 3016 |
|
| 3017 |
/* perform the initial qualifier resolution */ |
| 3018 |
if ((lst = resolvePossessive(resolver, results, nil)) == nil) |
| 3019 |
return []; |
| 3020 |
|
| 3021 |
/* filter out truncated matches if we have any exact matches */ |
| 3022 |
lst = filterTruncations(lst, resolver); |
| 3023 |
|
| 3024 |
/* filter the plurals for the logical subset */ |
| 3025 |
lst = resolver.filterPluralPhrase(lst, self); |
| 3026 |
|
| 3027 |
/* if we have a list, sort it by pluralOrder */ |
| 3028 |
if (lst != nil) |
| 3029 |
lst = lst.sort(SortAsc, |
| 3030 |
{a, b: a.obj_.pluralOrder - b.obj_.pluralOrder}); |
| 3031 |
|
| 3032 |
/* note the matched objects in the results */ |
| 3033 |
results.noteMatches(lst); |
| 3034 |
|
| 3035 |
/* we want everything in the list, so return what we found */ |
| 3036 |
return lst; |
| 3037 |
} |
| 3038 |
; |
| 3039 |
|
| 3040 |
/* |
| 3041 |
* Possessive plural with a specific quantity that must be exact |
| 3042 |
*/ |
| 3043 |
class ExactQuantifiedPossessivePluralProd: |
| 3044 |
ExactQuantifiedPluralProd, BasicPossessiveProd |
| 3045 |
|
| 3046 |
/* |
| 3047 |
* resolve the main noun phrase - this is the possessive-qualified |
| 3048 |
* plural phrase |
| 3049 |
*/ |
| 3050 |
resolveMainPhrase(resolver, results) |
| 3051 |
{ |
| 3052 |
return resolvePossessive(resolver, results, getQuantity()); |
| 3053 |
} |
| 3054 |
; |
| 3055 |
|
| 3056 |
/* |
| 3057 |
* Possessive noun used in an exclusion list. This is for things like |
| 3058 |
* the "mine" in a phrase like "take keys except mine". |
| 3059 |
*/ |
| 3060 |
class ButPossessiveProd: BasicPossessiveProd |
| 3061 |
resolveNouns(resolver, results) |
| 3062 |
{ |
| 3063 |
/* |
| 3064 |
* resolve the possessive phrase, and use it to reduce the |
| 3065 |
* resolver's main list (this is the list before the "except" |
| 3066 |
* from which are choosing items to exclude) to those items |
| 3067 |
* owned by the object indicated in the possessive phrase |
| 3068 |
*/ |
| 3069 |
return selectWithPossessive(resolver, results, resolver.mainList, |
| 3070 |
resolver.mainListText, nil); |
| 3071 |
} |
| 3072 |
; |
| 3073 |
|
| 3074 |
/* |
| 3075 |
* Possessive phrase production for disambiguation. This base class can |
| 3076 |
* be used for grammar productions that match possessive phrases in |
| 3077 |
* disambiguation prompt ("which book do you mean...?") responses. |
| 3078 |
*/ |
| 3079 |
class DisambigPossessiveProd: BasicPossessiveProd, DisambigProd |
| 3080 |
resolveNouns(resolver, results) |
| 3081 |
{ |
| 3082 |
local lst; |
| 3083 |
|
| 3084 |
/* |
| 3085 |
* Remember the original qualified list (this is the list of |
| 3086 |
* objects from which we're trying to choose on the basis of the |
| 3087 |
* possessive phrase we're resolving now). We can feed the |
| 3088 |
* qualified-object list back into the selection process for the |
| 3089 |
* qualifier itself, because we're looking for a qualifier that |
| 3090 |
* makes sense when combined with one of the qualified objects. |
| 3091 |
*/ |
| 3092 |
qualifiedList_ = resolver.matchList; |
| 3093 |
|
| 3094 |
/* select from the match list using the possessive phrase */ |
| 3095 |
lst = selectWithPossessive(resolver, results, |
| 3096 |
resolver.matchList, resolver.matchText, 1); |
| 3097 |
|
| 3098 |
/* |
| 3099 |
* if the list has more than one entry, treat the result as still |
| 3100 |
* ambiguous - a simple possessive response to a disambiguation |
| 3101 |
* query is implicitly definite, so we must select a single |
| 3102 |
* object |
| 3103 |
*/ |
| 3104 |
if (lst != nil && lst.length() > 1) |
| 3105 |
lst = results.ambiguousNounPhrase( |
| 3106 |
self, ResolveAsker, resolver.matchText, |
| 3107 |
lst, resolver.fullMatchList, lst, 1, resolver); |
| 3108 |
|
| 3109 |
/* |
| 3110 |
* if we failed to resolve it, return an empty list; otherwise |
| 3111 |
* return the list |
| 3112 |
*/ |
| 3113 |
return (lst == nil ? [] : lst); |
| 3114 |
} |
| 3115 |
|
| 3116 |
/* |
| 3117 |
* Do extra filter during disambiguation. Since we have a list of |
| 3118 |
* objects we're trying to qualify, we can look at that list to see |
| 3119 |
* if some of the possible matches for the qualifier phrase are |
| 3120 |
* owners of things in the qualified list. |
| 3121 |
*/ |
| 3122 |
reduceDefinite(lst, resolver, results) |
| 3123 |
{ |
| 3124 |
local newLst; |
| 3125 |
|
| 3126 |
/* |
| 3127 |
* try reducing the list to owners of objects that appear in the |
| 3128 |
* qualified object list |
| 3129 |
*/ |
| 3130 |
newLst = lst.subset({x: qualifiedList_.indexWhich( |
| 3131 |
{y: y.obj_.isOwnedBy(x.obj_)}) != nil}); |
| 3132 |
|
| 3133 |
/* if there's anything in that list, keep only the subset */ |
| 3134 |
if (newLst.length() > 0) |
| 3135 |
lst = newLst; |
| 3136 |
|
| 3137 |
/* return the result */ |
| 3138 |
return lst; |
| 3139 |
} |
| 3140 |
|
| 3141 |
/* |
| 3142 |
* the list of objects being qualified - this is the list of books, |
| 3143 |
* for example, in "bob's books" |
| 3144 |
*/ |
| 3145 |
qualifiedList_ = [] |
| 3146 |
; |
| 3147 |
|
| 3148 |
/* ------------------------------------------------------------------------ */ |
| 3149 |
/* |
| 3150 |
* A noun phrase with explicit containment. Grammar rules based on this |
| 3151 |
* class must set the property np_ to the main noun phrase, and cont_ to |
| 3152 |
* the noun phrase giving the container. |
| 3153 |
* |
| 3154 |
* We're based on the definite noun phrase production, because we need |
| 3155 |
* to resolve the underlying container phrase to a singe, unambiguous |
| 3156 |
* object. |
| 3157 |
*/ |
| 3158 |
class ContainerNounPhraseProd: DefiniteNounProd |
| 3159 |
resolveNouns(resolver, results) |
| 3160 |
{ |
| 3161 |
local lst; |
| 3162 |
local cRes; |
| 3163 |
local cLst; |
| 3164 |
local cont; |
| 3165 |
|
| 3166 |
/* |
| 3167 |
* We have two separate noun phrases to resolve: the qualified |
| 3168 |
* noun phrase in np_, and the locational qualifier in cont_. |
| 3169 |
* We then want to filter the object matches for np_ to select |
| 3170 |
* the subset that is contained in cont_. |
| 3171 |
* |
| 3172 |
* We must resolve cont_ to a single, unambiguous object. |
| 3173 |
* However, we want to be smart about it by limiting the range |
| 3174 |
* of choices to objects that actually contain something that |
| 3175 |
* could match the possible resolutions of np_. So, tentatively |
| 3176 |
* resolve np_ first, to get the range of possible matches. |
| 3177 |
*/ |
| 3178 |
lst = np_.resolveNouns(resolver, results); |
| 3179 |
|
| 3180 |
/* |
| 3181 |
* We have the tentative resolution of the main noun phrase, so |
| 3182 |
* we can now resolve the locational qualifier phrase. Use our |
| 3183 |
* special container resolver for this step, since this will try |
| 3184 |
* to pick objects that contain something in the tentative |
| 3185 |
* results for the main list. Resolve the container as a |
| 3186 |
* definite noun phrase, since we want a single, unambiguous |
| 3187 |
* match. |
| 3188 |
* |
| 3189 |
* Note that we must base our special container resolver on the |
| 3190 |
* qualifier resolver, not on the main resolver. The location is |
| 3191 |
* a qualifying phrase, so it's resolved in the scope of any |
| 3192 |
* other qualifying phrase. |
| 3193 |
* |
| 3194 |
* The container phrase has to be a single object, so note in the |
| 3195 |
* results that we're working on a single-object slot. |
| 3196 |
*/ |
| 3197 |
results.beginSingleObjSlot(); |
| 3198 |
|
| 3199 |
cRes = new ContainerResolver(lst, np_.getOrigText(), |
| 3200 |
resolver.getQualifierResolver()); |
| 3201 |
cLst = resolveDefinite(ResolveAsker, cont_.getOrigText(), |
| 3202 |
cont_.resolveNouns(cRes, results), |
| 3203 |
self, cRes, results); |
| 3204 |
|
| 3205 |
results.endSingleObjSlot(); |
| 3206 |
|
| 3207 |
/* |
| 3208 |
* We need a single object in the container list. If we have |
| 3209 |
* no objects, or more than one object, it's an error. |
| 3210 |
*/ |
| 3211 |
if (cLst.length() != 1) |
| 3212 |
{ |
| 3213 |
/* it's a separate error if we got more than one object */ |
| 3214 |
if (cLst.length() > 1) |
| 3215 |
results.uniqueObjectRequired(cont_.getOrigText(), cLst); |
| 3216 |
|
| 3217 |
/* we can't go on */ |
| 3218 |
return []; |
| 3219 |
} |
| 3220 |
|
| 3221 |
/* we have a unique item, so it's the container */ |
| 3222 |
cont = cLst[1].obj_; |
| 3223 |
|
| 3224 |
/* reduce the list to those objects inside the container */ |
| 3225 |
lst = lst.subset({x: x.obj_.isNominallyIn(cont)}); |
| 3226 |
|
| 3227 |
/* |
| 3228 |
* If we have some objects directly in the container, and other |
| 3229 |
* objects indirectly in the container, filter the list to |
| 3230 |
* include only the directly contained items. |
| 3231 |
*/ |
| 3232 |
if (lst.indexWhich({x: x.obj_.isDirectlyIn(cont)}) != nil) |
| 3233 |
lst = lst.subset({x: x.obj_.isDirectlyIn(cont)}); |
| 3234 |
|
| 3235 |
/* if that leaves nothing, mention it */ |
| 3236 |
if (lst.length() == 0) |
| 3237 |
{ |
| 3238 |
results.noMatchForLocation(cont, np_.getOrigText()); |
| 3239 |
return []; |
| 3240 |
} |
| 3241 |
|
| 3242 |
/* return the list */ |
| 3243 |
return lst; |
| 3244 |
} |
| 3245 |
; |
| 3246 |
|
| 3247 |
/* |
| 3248 |
* Basic container resolver. This is a common subclass for the standard |
| 3249 |
* container resolver and the "vague" container resolver. |
| 3250 |
*/ |
| 3251 |
class BasicContainerResolver: ProxyResolver |
| 3252 |
/* we're a sub-phrase resolver */ |
| 3253 |
isSubResolver = true |
| 3254 |
|
| 3255 |
/* resolve any qualifiers in the main scope */ |
| 3256 |
getQualifierResolver() { return origResolver; } |
| 3257 |
|
| 3258 |
/* filter an ambiguous noun phrase */ |
| 3259 |
filterAmbiguousNounPhrase(lst, requiredNum, np) |
| 3260 |
{ |
| 3261 |
local outer; |
| 3262 |
local lcl; |
| 3263 |
|
| 3264 |
/* do the normal filtering first */ |
| 3265 |
lst = inherited(lst, requiredNum, np); |
| 3266 |
|
| 3267 |
/* |
| 3268 |
* get the subset that includes only local objects - that is, |
| 3269 |
* objects within the same outermost room as the target actor |
| 3270 |
*/ |
| 3271 |
outer = actor_.getOutermostRoom(); |
| 3272 |
lcl = lst.subset({x: x.obj_.isIn(outer)}); |
| 3273 |
|
| 3274 |
/* if there's a local subset, take the subset */ |
| 3275 |
if (lcl.length() != 0) |
| 3276 |
lst = lcl; |
| 3277 |
|
| 3278 |
/* return the result */ |
| 3279 |
return lst; |
| 3280 |
} |
| 3281 |
; |
| 3282 |
|
| 3283 |
/* |
| 3284 |
* Container Resolver. This is a proxy for the main qualifier resolver |
| 3285 |
* that prefers to match objects that are plausible in the sense that |
| 3286 |
* they contain something in the tentative resolution of the main list. |
| 3287 |
*/ |
| 3288 |
class ContainerResolver: BasicContainerResolver |
| 3289 |
construct(mainList, mainText, origResolver) |
| 3290 |
{ |
| 3291 |
/* inherit base handling */ |
| 3292 |
inherited(origResolver); |
| 3293 |
|
| 3294 |
/* remember my tentative main match list */ |
| 3295 |
self.mainList = mainList; |
| 3296 |
self.mainListText = mainText; |
| 3297 |
} |
| 3298 |
|
| 3299 |
/* filter ambiguous equivalents */ |
| 3300 |
filterAmbiguousEquivalents(lst, np) |
| 3301 |
{ |
| 3302 |
local vec; |
| 3303 |
|
| 3304 |
/* |
| 3305 |
* Check to see if any of the objects in the list are plausible |
| 3306 |
* containers for objects in our main list. If we can find any |
| 3307 |
* plausible entries, keep only the plausible ones. |
| 3308 |
*/ |
| 3309 |
vec = new Vector(lst.length()); |
| 3310 |
foreach (local cur in lst) |
| 3311 |
{ |
| 3312 |
/* if this item is plausible, add it to our result vector */ |
| 3313 |
if (mainList.indexWhich( |
| 3314 |
{x: x.obj_.isNominallyIn(cur.obj_)}) != nil) |
| 3315 |
vec.append(cur); |
| 3316 |
} |
| 3317 |
|
| 3318 |
/* |
| 3319 |
* if we found anything plausible, return only the plausible |
| 3320 |
* subset; otherwise, return the full original list, since |
| 3321 |
* they're all equally implausible |
| 3322 |
*/ |
| 3323 |
if (vec.length() != 0) |
| 3324 |
return vec.toList(); |
| 3325 |
else |
| 3326 |
return lst; |
| 3327 |
} |
| 3328 |
|
| 3329 |
/* the tentative match list for the main phrase we're qualifying */ |
| 3330 |
mainList = nil |
| 3331 |
|
| 3332 |
/* the text of the main phrase we're qualifying */ |
| 3333 |
mainListText = nil |
| 3334 |
; |
| 3335 |
|
| 3336 |
/* ------------------------------------------------------------------------ */ |
| 3337 |
/* |
| 3338 |
* A "vague" container noun phrase. This is a phrase that specifies a |
| 3339 |
* container but nothing else: "the one in the box", "the ones in the |
| 3340 |
* box", "everything in the box". |
| 3341 |
*/ |
| 3342 |
class VagueContainerNounPhraseProd: DefiniteNounProd |
| 3343 |
resolveNouns(resolver, results) |
| 3344 |
{ |
| 3345 |
local cRes; |
| 3346 |
local cLst; |
| 3347 |
local lst; |
| 3348 |
local cont; |
| 3349 |
|
| 3350 |
/* resolve the container as a single-object slot */ |
| 3351 |
results.beginSingleObjSlot(); |
| 3352 |
|
| 3353 |
/* |
| 3354 |
* Resolve the container. Use a special resolver that prefers |
| 3355 |
* objects that have any contents. |
| 3356 |
*/ |
| 3357 |
cRes = new VagueContainerResolver(resolver.getQualifierResolver()); |
| 3358 |
cLst = resolveDefinite(ResolveAsker, cont_.getOrigText(), |
| 3359 |
cont_.resolveNouns(cRes, results), |
| 3360 |
self, cRes, results); |
| 3361 |
|
| 3362 |
/* done with the single-object slot */ |
| 3363 |
results.endSingleObjSlot(); |
| 3364 |
|
| 3365 |
/* make sure we resolved to a unique container */ |
| 3366 |
if (cLst.length() != 1) |
| 3367 |
{ |
| 3368 |
/* it's a separate error if we got more than one object */ |
| 3369 |
if (cLst.length() > 1) |
| 3370 |
results.uniqueObjectRequired(cont_.getOrigText(), cLst); |
| 3371 |
|
| 3372 |
/* we can't go on */ |
| 3373 |
return []; |
| 3374 |
} |
| 3375 |
|
| 3376 |
/* we have a unique item, so it's the container */ |
| 3377 |
cont = cLst[1].obj_; |
| 3378 |
|
| 3379 |
/* |
| 3380 |
* If the container is the nominal drop destination for the |
| 3381 |
* actor's location, then use the actor's location as the actual |
| 3382 |
* container. This way, if we try to get "all on floor" or the |
| 3383 |
* like, we'll correctly get objects that are directly in the |
| 3384 |
* room. |
| 3385 |
*/ |
| 3386 |
if (cont == resolver.actor_.location.getNominalDropDestination()) |
| 3387 |
cont = resolver.actor_.location; |
| 3388 |
|
| 3389 |
/* get the contents */ |
| 3390 |
lst = cont.getAllForTakeFrom(resolver.getScopeList()); |
| 3391 |
|
| 3392 |
/* keep only visible objects */ |
| 3393 |
lst = lst.subset({x: resolver.actor_.canSee(x)}); |
| 3394 |
|
| 3395 |
/* map the contents to a resolved object list */ |
| 3396 |
lst = lst.mapAll({x: new ResolveInfo(x, 0, self)}); |
| 3397 |
|
| 3398 |
/* make sure the list isn't empty */ |
| 3399 |
if (lst.length() == 0) |
| 3400 |
{ |
| 3401 |
/* there's nothing in this container */ |
| 3402 |
results.nothingInLocation(cont); |
| 3403 |
return []; |
| 3404 |
} |
| 3405 |
|
| 3406 |
/* make other subclass-specific checks on the list */ |
| 3407 |
lst = checkContentsList(resolver, results, lst, cont); |
| 3408 |
|
| 3409 |
/* note the matches */ |
| 3410 |
results.noteMatches(lst); |
| 3411 |
|
| 3412 |
/* return the list */ |
| 3413 |
return lst; |
| 3414 |
} |
| 3415 |
|
| 3416 |
/* check a contents list */ |
| 3417 |
checkContentsList(resolver, results, lst, cont) |
| 3418 |
{ |
| 3419 |
/* by default, just return the list */ |
| 3420 |
return lst; |
| 3421 |
} |
| 3422 |
; |
| 3423 |
|
| 3424 |
/* |
| 3425 |
* "All in container" |
| 3426 |
*/ |
| 3427 |
class AllInContainerNounPhraseProd: VagueContainerNounPhraseProd |
| 3428 |
/* check a contents list */ |
| 3429 |
checkContentsList(resolver, results, lst, cont) |
| 3430 |
{ |
| 3431 |
/* keep only items that aren't hidden from "all" */ |
| 3432 |
lst = lst.subset({x: !x.obj_.hideFromAll(resolver.getAction())}); |
| 3433 |
|
| 3434 |
/* set the "matched all" and "always announce as multi" flags */ |
| 3435 |
foreach (local cur in lst) |
| 3436 |
cur.flags_ |= AlwaysAnnounce | MatchedAll; |
| 3437 |
|
| 3438 |
/* if that emptied the list, so note */ |
| 3439 |
if (lst.length() == 0) |
| 3440 |
results.nothingInLocation(cont); |
| 3441 |
|
| 3442 |
/* return the result list */ |
| 3443 |
return lst; |
| 3444 |
} |
| 3445 |
; |
| 3446 |
|
| 3447 |
/* |
| 3448 |
* A definite vague container phrase. This selects a single object in a |
| 3449 |
* given container ("the one in the box"). If more than one object is |
| 3450 |
* present, we'll try to disambiguate it. |
| 3451 |
* |
| 3452 |
* Grammar rules instantiating this class must set the property |
| 3453 |
* 'mainPhraseText' to the text to display for a disambiguation prompt |
| 3454 |
* involving the main phrase. |
| 3455 |
*/ |
| 3456 |
class VagueContainerDefiniteNounPhraseProd: VagueContainerNounPhraseProd |
| 3457 |
construct() |
| 3458 |
{ |
| 3459 |
/* create a disambiguator for the main phrase */ |
| 3460 |
npKeeper = new AmbigResponseKeeper(); |
| 3461 |
} |
| 3462 |
|
| 3463 |
/* check a contents list */ |
| 3464 |
checkContentsList(resolver, results, lst, cont) |
| 3465 |
{ |
| 3466 |
/* |
| 3467 |
* This production type requires a single object in the |
| 3468 |
* container (since it's for phrases like "the one in the box"). |
| 3469 |
* If we have more than one object, try disambiguating. |
| 3470 |
*/ |
| 3471 |
if (lst.length() > 1) |
| 3472 |
{ |
| 3473 |
local scopeList; |
| 3474 |
local fullList; |
| 3475 |
|
| 3476 |
/* |
| 3477 |
* There's more than one object in this container. First, |
| 3478 |
* try filtering it by possessive qualifier strength and |
| 3479 |
* then the normal disambiguation ranking. |
| 3480 |
*/ |
| 3481 |
scopeList = lst; |
| 3482 |
lst = resolver.filterPossRank(lst, 1); |
| 3483 |
lst = resolver.filterAmbiguousNounPhrase(lst, 1, self); |
| 3484 |
|
| 3485 |
/* try removing redundant equivalents */ |
| 3486 |
fullList = lst; |
| 3487 |
if (results.allowEquivalentFiltering) |
| 3488 |
lst = resolver.filterAmbiguousEquivalents(lst, self); |
| 3489 |
|
| 3490 |
/* if we still have too many objects, it's ambiguous */ |
| 3491 |
if (lst.length() > 1) |
| 3492 |
{ |
| 3493 |
/* ask the results object to handle the ambiguous phrase */ |
| 3494 |
lst = results.ambiguousNounPhrase( |
| 3495 |
npKeeper, ResolveAsker, mainPhraseText, |
| 3496 |
lst, fullList, scopeList, 1, resolver); |
| 3497 |
} |
| 3498 |
} |
| 3499 |
|
| 3500 |
/* return the contents of the container */ |
| 3501 |
return lst; |
| 3502 |
} |
| 3503 |
|
| 3504 |
/* our disambiguation result keeper */ |
| 3505 |
npKeeper = nil |
| 3506 |
; |
| 3507 |
|
| 3508 |
/* |
| 3509 |
* An indefinite vague container phrase. This selects a single object, |
| 3510 |
* choosing arbitrarily if multiple objects are in the container. |
| 3511 |
*/ |
| 3512 |
class VagueContainerIndefiniteNounPhraseProd: VagueContainerNounPhraseProd |
| 3513 |
/* check a contents list */ |
| 3514 |
checkContentsList(resolver, results, lst, cont) |
| 3515 |
{ |
| 3516 |
/* choose one object arbitrarily */ |
| 3517 |
if (lst.length() > 1) |
| 3518 |
lst = lst.sublist(1, 1); |
| 3519 |
|
| 3520 |
/* return the (possibly trimmed) list */ |
| 3521 |
return lst; |
| 3522 |
} |
| 3523 |
; |
| 3524 |
|
| 3525 |
|
| 3526 |
/* |
| 3527 |
* Container Resolver for vaguely-specified containment phrases. We'll |
| 3528 |
* select for objects that have contents, but that's about as much as we |
| 3529 |
* can do, since the main phrase is bounded only by the container in |
| 3530 |
* vague containment phrases (and thus provides no information that |
| 3531 |
* would help us narrow down the container itself). |
| 3532 |
*/ |
| 3533 |
class VagueContainerResolver: BasicContainerResolver |
| 3534 |
/* filter ambiguous equivalents */ |
| 3535 |
filterAmbiguousEquivalents(lst, np) |
| 3536 |
{ |
| 3537 |
local vec; |
| 3538 |
|
| 3539 |
/* prefer objects with contents */ |
| 3540 |
vec = new Vector(lst.length()); |
| 3541 |
foreach (local cur in lst) |
| 3542 |
{ |
| 3543 |
/* if this item has any contents, add it to the new list */ |
| 3544 |
if (cur.obj_.contents.length() > 0) |
| 3545 |
vec.append(cur); |
| 3546 |
} |
| 3547 |
|
| 3548 |
/* |
| 3549 |
* if we found anything plausible, return only the plausible |
| 3550 |
* subset; otherwise, return the full original list, since |
| 3551 |
* they're all equally implausible |
| 3552 |
*/ |
| 3553 |
if (vec.length() != 0) |
| 3554 |
return vec.toList(); |
| 3555 |
else |
| 3556 |
return lst; |
| 3557 |
} |
| 3558 |
; |
| 3559 |
|
| 3560 |
|
| 3561 |
/* ------------------------------------------------------------------------ */ |
| 3562 |
/* |
| 3563 |
* Noun phrase with vocabulary resolution. This is a base class for the |
| 3564 |
* various noun phrases that match adjective, noun, and plural tokens. |
| 3565 |
* This class provides dictionary resolution of a vocabulary word into a |
| 3566 |
* list of objects. |
| 3567 |
* |
| 3568 |
* In non-declined languages such as English, the parts of speech of our |
| 3569 |
* words are usually simply 'adjective' and 'noun'. A language |
| 3570 |
* "declines" its noun phrases if the words in a noun phrase have |
| 3571 |
* different forms that depend on the function of the noun phrase in a |
| 3572 |
* sentence; for example, in German, adjectives take suffixes that |
| 3573 |
* depend upon the gender of the noun being modified and the function of |
| 3574 |
* the noun phrase in the sentence (subject, direct object, etc). In a |
| 3575 |
* declined language, it might be desirable to use separate parts of |
| 3576 |
* speech for separate declined adjective and noun forms. |
| 3577 |
*/ |
| 3578 |
class NounPhraseWithVocab: NounPhraseProd |
| 3579 |
/* |
| 3580 |
* Get a list of the matches in the main dictionary for the given |
| 3581 |
* token as the given part of speech (&noun, &adjective, &plural, or |
| 3582 |
* others as appropriate for the local language) that are in scope |
| 3583 |
* according to the resolver. |
| 3584 |
*/ |
| 3585 |
getWordMatches(tok, partOfSpeech, resolver, flags, truncFlags) |
| 3586 |
{ |
| 3587 |
local lst; |
| 3588 |
|
| 3589 |
/* start with the dictionary matches */ |
| 3590 |
lst = cmdDict.findWord(tok, partOfSpeech); |
| 3591 |
|
| 3592 |
/* filter it to eliminate redundant matches */ |
| 3593 |
lst = filterDictMatches(lst); |
| 3594 |
|
| 3595 |
/* add the objects that match the full dictionary word */ |
| 3596 |
lst = inScopeMatches(lst, flags, flags | truncFlags, resolver); |
| 3597 |
|
| 3598 |
/* return the combined results */ |
| 3599 |
return lst; |
| 3600 |
} |
| 3601 |
|
| 3602 |
/* |
| 3603 |
* Combine two word match lists. This simply adds each entry from |
| 3604 |
* the second list that doesn't already have a corresponding entry |
| 3605 |
* in the first list, returning the combined list. |
| 3606 |
*/ |
| 3607 |
combineWordMatches(aLst, bLst) |
| 3608 |
{ |
| 3609 |
/* create a vector copy of the original 'a' list */ |
| 3610 |
aLst = new Vector(aLst.length(), aLst); |
| 3611 |
|
| 3612 |
/* |
| 3613 |
* add each entry from the second list whose object isn't |
| 3614 |
* already represented in the first list |
| 3615 |
*/ |
| 3616 |
foreach (local b in bLst) |
| 3617 |
{ |
| 3618 |
local ia; |
| 3619 |
|
| 3620 |
/* look for an existing entry for this object in the 'a' list */ |
| 3621 |
ia = aLst.indexWhich({aCur: aCur.obj_ == b.obj_}); |
| 3622 |
|
| 3623 |
/* |
| 3624 |
* If this 'b' entry isn't already in the 'a' list, simply |
| 3625 |
* add the 'b' entry. If both lists have this entry, combine |
| 3626 |
* the flags into the existing entry. |
| 3627 |
*/ |
| 3628 |
if (ia == nil) |
| 3629 |
{ |
| 3630 |
/* it's not already in the 'a' list, so simply add it */ |
| 3631 |
aLst += b; |
| 3632 |
} |
| 3633 |
else |
| 3634 |
{ |
| 3635 |
/* |
| 3636 |
* Both lists have the entry, so keep the existing 'a' |
| 3637 |
* entry, but merge the flags. Note that the |
| 3638 |
* original 'a' might still be interesting to the |
| 3639 |
* caller separately, so create a copy of it before |
| 3640 |
* modifying it. |
| 3641 |
*/ |
| 3642 |
local a = aLst[ia]; |
| 3643 |
aLst[ia] = a = a.createClone(); |
| 3644 |
combineWordMatchItems(a, b); |
| 3645 |
} |
| 3646 |
} |
| 3647 |
|
| 3648 |
/* return the combined list */ |
| 3649 |
return aLst; |
| 3650 |
} |
| 3651 |
|
| 3652 |
/* |
| 3653 |
* Combine the given word match entries. We'll merge the flags of |
| 3654 |
* the two entries to produce a single merged entry in 'a'. |
| 3655 |
*/ |
| 3656 |
combineWordMatchItems(a, b) |
| 3657 |
{ |
| 3658 |
/* |
| 3659 |
* If one item was matched with truncation and the other wasn't, |
| 3660 |
* remove the truncation flag entirely. This will make the |
| 3661 |
* combined entry reflect the fact that we were able to match the |
| 3662 |
* word without any truncation. The fact that we also matched it |
| 3663 |
* with truncation isn't relevant, since matching without |
| 3664 |
* truncation is the stronger condition. |
| 3665 |
*/ |
| 3666 |
if ((b.flags_ & VocabTruncated) == 0) |
| 3667 |
a.flags_ &= ~VocabTruncated; |
| 3668 |
|
| 3669 |
/* likewise for plural truncation */ |
| 3670 |
if ((b.flags_ & PluralTruncated) == 0) |
| 3671 |
a.flags_ &= ~PluralTruncated; |
| 3672 |
|
| 3673 |
/* |
| 3674 |
* Other flags generally are set for the entire list of matching |
| 3675 |
* objects for a production, rather than at the level of |
| 3676 |
* individual matching objects, so we don't have to worry about |
| 3677 |
* combining them - they'll naturally be the same at this point. |
| 3678 |
*/ |
| 3679 |
} |
| 3680 |
|
| 3681 |
/* |
| 3682 |
* Filter a dictionary match list. This is called to clean up the |
| 3683 |
* raw match list returned from looking up a vocabulary word in the |
| 3684 |
* dictionary. |
| 3685 |
* |
| 3686 |
* The main purpose of this routine is to eliminate unwanted |
| 3687 |
* redundancy from the dictionary matches; in particular, the |
| 3688 |
* dictionary might have multiple matches for a given word at a given |
| 3689 |
* object, due to truncation, upper/lower folding, accent removal, |
| 3690 |
* and so on. In general, we want to keep only the single strongest |
| 3691 |
* match from the dictionary for a given word matching a given |
| 3692 |
* object. |
| 3693 |
* |
| 3694 |
* The meaning of "stronger" and "exact" matches is |
| 3695 |
* language-dependent, so we abstract these with the separate methods |
| 3696 |
* dictMatchIsExact() and dictMatchIsStronger(). |
| 3697 |
* |
| 3698 |
* Keep in mind that the raw dictionary match list has alternating |
| 3699 |
* entries: object, comparator flags, object, comparator flags, etc. |
| 3700 |
* The return list should be in the same format. |
| 3701 |
*/ |
| 3702 |
filterDictMatches(lst) |
| 3703 |
{ |
| 3704 |
local ret; |
| 3705 |
|
| 3706 |
/* set up a vector to hold the result */ |
| 3707 |
ret = new Vector(lst.length()); |
| 3708 |
|
| 3709 |
/* |
| 3710 |
* check each inexact element of the list for another entry with |
| 3711 |
* a stronger match; keep only the ones without a stronger match |
| 3712 |
*/ |
| 3713 |
truncScan: |
| 3714 |
/* scan for a stronger match */ |
| 3715 |
for (local i = 1, local len = lst.length() ; i < len ; i += 2) |
| 3716 |
{ |
| 3717 |
/* get the current object and its flags */ |
| 3718 |
local curObj = lst[i]; |
| 3719 |
local curFlags = lst[i+1]; |
| 3720 |
|
| 3721 |
/* if it's not an exact match, check for a stronger match */ |
| 3722 |
if (!dictMatchIsExact(curFlags)) |
| 3723 |
{ |
| 3724 |
/* scan for an equal or stronger match later in the list */ |
| 3725 |
for (local j = i + 2 ; j < len ; j += 2) |
| 3726 |
{ |
| 3727 |
/* |
| 3728 |
* if entry j is stronger than or identical to the |
| 3729 |
* current entry, omit the current entry in favor of |
| 3730 |
* entry j |
| 3731 |
*/ |
| 3732 |
local jObj = lst[j], jFlags = lst[j+1]; |
| 3733 |
if (jObj == curObj |
| 3734 |
&& (jFlags == curFlags |
| 3735 |
|| dictMatchIsStronger(jFlags, curFlags))) |
| 3736 |
{ |
| 3737 |
/* there's a better entry; omit the current one */ |
| 3738 |
continue truncScan; |
| 3739 |
} |
| 3740 |
} |
| 3741 |
} |
| 3742 |
|
| 3743 |
/* keep this entry - add it to the result vector */ |
| 3744 |
ret.append(curObj); |
| 3745 |
ret.append(curFlags); |
| 3746 |
} |
| 3747 |
|
| 3748 |
/* return the result vector */ |
| 3749 |
return ret; |
| 3750 |
} |
| 3751 |
|
| 3752 |
/* |
| 3753 |
* Check a dictionary match's string comparator flags to see if the |
| 3754 |
* match is "exact." The exact meaning of "exact" is dependent on |
| 3755 |
* the language's lexical rules; this generic base version considers |
| 3756 |
* a match to be exact if it doesn't have any string comparator flags |
| 3757 |
* other than the base "matched" flag and the case-fold flag. This |
| 3758 |
* should be suitable for most languages, as (1) case folding usually |
| 3759 |
* doesn't improve match strength, and (2) any additional comparator |
| 3760 |
* flags usually indicate some kind of inexact match status. |
| 3761 |
* |
| 3762 |
* A language that depends on upper/lower case as a marker of match |
| 3763 |
* strength will need to override this to consider the case-fold flag |
| 3764 |
* as significant in determining match exactness. In addition, a |
| 3765 |
* language that uses additional string comparator flags to indicate |
| 3766 |
* better (rather than worse) matches will have to override this to |
| 3767 |
* require the presence of those flags. |
| 3768 |
*/ |
| 3769 |
dictMatchIsExact(flags) |
| 3770 |
{ |
| 3771 |
/* |
| 3772 |
* the match is exact if it doesn't have any qualifier flags |
| 3773 |
* other than the basic "yes I matched" flag and the case-fold |
| 3774 |
* flag |
| 3775 |
*/ |
| 3776 |
return ((flags & ~(StrCompMatch | StrCompCaseFold)) == 0); |
| 3777 |
} |
| 3778 |
|
| 3779 |
/* |
| 3780 |
* Compare two dictionary matches for the same object and determine |
| 3781 |
* if the first one is stronger than the second. Both are for the |
| 3782 |
* same object; the only difference is the string comparator flags. |
| 3783 |
* |
| 3784 |
* Language modules might need to override this to supplement the |
| 3785 |
* filtering with their own rules. This generic base version |
| 3786 |
* considers truncation: an untruncated match is stronger than a |
| 3787 |
* truncated match. Non-English languages might want to consider |
| 3788 |
* other lexical factors in the match strength, such as whether we |
| 3789 |
* matched the exact accented characters or approximated with |
| 3790 |
* unaccented equivalents - this information will, of course, need to |
| 3791 |
* be coordinated with the dictionary's string comparator, and |
| 3792 |
* reflected in the comparator match flags. It's the comparator |
| 3793 |
* match flags that we're looking at here. |
| 3794 |
*/ |
| 3795 |
dictMatchIsStronger(flags1, flags2) |
| 3796 |
{ |
| 3797 |
/* |
| 3798 |
* if the first match (flags1) indicates no truncation, and the |
| 3799 |
* second (flags2) was truncated, then the first match is |
| 3800 |
* stronger; otherwise, there's no distinction as far as we're |
| 3801 |
* concerned |
| 3802 |
*/ |
| 3803 |
return ((flags1 & StrCompTrunc) == 0 |
| 3804 |
&& (flags2 & StrCompTrunc) != 0); |
| 3805 |
} |
| 3806 |
|
| 3807 |
/* |
| 3808 |
* Get a list of the matches in the main dictionary for the given |
| 3809 |
* token, intersecting the resulting list with the given list. |
| 3810 |
*/ |
| 3811 |
intersectWordMatches(tok, partOfSpeech, resolver, flags, truncFlags, lst) |
| 3812 |
{ |
| 3813 |
local newList; |
| 3814 |
|
| 3815 |
/* get the matches to the given word */ |
| 3816 |
newList = getWordMatches(tok, partOfSpeech, resolver, |
| 3817 |
flags, truncFlags); |
| 3818 |
|
| 3819 |
/* intersect the result with the other list */ |
| 3820 |
newList = intersectNounLists(newList, lst); |
| 3821 |
|
| 3822 |
/* return the combined results */ |
| 3823 |
return newList; |
| 3824 |
} |
| 3825 |
|
| 3826 |
/* |
| 3827 |
* Given a list of dictionary matches to a given word, construct a |
| 3828 |
* list of ResolveInfo objects for the matches that are in scope. |
| 3829 |
* For regular resolution, "in scope" means the resolver thinks the |
| 3830 |
* object is in scope. |
| 3831 |
*/ |
| 3832 |
inScopeMatches(dictionaryMatches, flags, truncFlags, resolver) |
| 3833 |
{ |
| 3834 |
local ret; |
| 3835 |
|
| 3836 |
/* set up a vector to hold the results */ |
| 3837 |
ret = new Vector(dictionaryMatches.length()); |
| 3838 |
|
| 3839 |
/* |
| 3840 |
* Run through the list and include only the words that are in |
| 3841 |
* scope. Note that the list of dictionary matches has |
| 3842 |
* alternating objects and match flags, so we must scan it two |
| 3843 |
* elements at a time. |
| 3844 |
*/ |
| 3845 |
for (local i = 1, local cnt = dictionaryMatches.length() ; |
| 3846 |
i <= cnt ; i += 2) |
| 3847 |
{ |
| 3848 |
/* get this object */ |
| 3849 |
local cur = dictionaryMatches[i]; |
| 3850 |
|
| 3851 |
/* if it's in scope, add a ResolveInfo for this object */ |
| 3852 |
if (resolver.objInScope(cur)) |
| 3853 |
{ |
| 3854 |
local curResults; |
| 3855 |
local curFlags; |
| 3856 |
|
| 3857 |
/* get the comparator match results for this object */ |
| 3858 |
curResults = dictionaryMatches[i+1]; |
| 3859 |
|
| 3860 |
/* |
| 3861 |
* If the results indication truncation, use the the |
| 3862 |
* truncated flags; otherwise use the ordinary flags. |
| 3863 |
*/ |
| 3864 |
if (dataType(curResults) == TypeInt |
| 3865 |
&& (curResults & StrCompTrunc) != 0) |
| 3866 |
curFlags = truncFlags; |
| 3867 |
else |
| 3868 |
curFlags = flags; |
| 3869 |
|
| 3870 |
/* add the item to the results */ |
| 3871 |
ret.append(new ResolveInfo(cur, curFlags, self)); |
| 3872 |
} |
| 3873 |
} |
| 3874 |
|
| 3875 |
/* return the results as a list */ |
| 3876 |
return ret.toList(); |
| 3877 |
} |
| 3878 |
|
| 3879 |
/* |
| 3880 |
* Resolve the objects. |
| 3881 |
*/ |
| 3882 |
resolveNouns(resolver, results) |
| 3883 |
{ |
| 3884 |
local matchList; |
| 3885 |
local starList; |
| 3886 |
|
| 3887 |
/* |
| 3888 |
* get the preliminary match list - this is simply the set of |
| 3889 |
* objects that match all of the words in the noun phrase |
| 3890 |
*/ |
| 3891 |
matchList = getVocabMatchList(resolver, results, 0); |
| 3892 |
|
| 3893 |
/* |
| 3894 |
* get a list of any in-scope objects that match '*' for nouns - |
| 3895 |
* these are objects that want to do all of their name parsing |
| 3896 |
* themselves |
| 3897 |
*/ |
| 3898 |
starList = inScopeMatches(cmdDict.findWord('*', &noun), |
| 3899 |
0, 0, resolver); |
| 3900 |
|
| 3901 |
/* combine the lists */ |
| 3902 |
matchList = combineWordMatches(matchList, starList); |
| 3903 |
|
| 3904 |
/* run the results through matchName */ |
| 3905 |
return resolveNounsMatchName(results, resolver, matchList); |
| 3906 |
} |
| 3907 |
|
| 3908 |
/* |
| 3909 |
* Run a set of resolved objects through matchName() or a similar |
| 3910 |
* routine. Returns the filtered results. |
| 3911 |
*/ |
| 3912 |
resolveNounsMatchName(results, resolver, matchList) |
| 3913 |
{ |
| 3914 |
local origTokens; |
| 3915 |
local adjustedTokens; |
| 3916 |
local objVec; |
| 3917 |
local ret; |
| 3918 |
|
| 3919 |
/* get the original token list for the command */ |
| 3920 |
origTokens = getOrigTokenList(); |
| 3921 |
|
| 3922 |
/* get the adjusted token list for the command */ |
| 3923 |
adjustedTokens = getAdjustedTokens(); |
| 3924 |
|
| 3925 |
/* set up to receive about the same number of results as inputs */ |
| 3926 |
objVec = new Vector(matchList.length()); |
| 3927 |
|
| 3928 |
/* consider each preliminary match */ |
| 3929 |
foreach (local cur in matchList) |
| 3930 |
{ |
| 3931 |
/* ask this object if it wants to be included */ |
| 3932 |
local newObj = resolver.matchName( |
| 3933 |
cur.obj_, origTokens, adjustedTokens); |
| 3934 |
|
| 3935 |
/* check the result */ |
| 3936 |
if (newObj == nil) |
| 3937 |
{ |
| 3938 |
/* |
| 3939 |
* it's nil - this means it's not a match for the name |
| 3940 |
* after all, so leave it out of the results |
| 3941 |
*/ |
| 3942 |
} |
| 3943 |
else if (newObj.ofKind(Collection)) |
| 3944 |
{ |
| 3945 |
/* |
| 3946 |
* it's a collection of some kind - add each element to |
| 3947 |
* the result list, using the same flags as the original |
| 3948 |
*/ |
| 3949 |
foreach (local curObj in newObj) |
| 3950 |
objVec.append(new ResolveInfo(curObj, cur.flags_, self)); |
| 3951 |
} |
| 3952 |
else |
| 3953 |
{ |
| 3954 |
/* |
| 3955 |
* it's a single object - add it ito the result list, |
| 3956 |
* using the same flags as the original |
| 3957 |
*/ |
| 3958 |
objVec.append(new ResolveInfo(newObj, cur.flags_, self)); |
| 3959 |
} |
| 3960 |
} |
| 3961 |
|
| 3962 |
/* convert the result vector to a list */ |
| 3963 |
ret = objVec.toList(); |
| 3964 |
|
| 3965 |
/* if our list is empty, note it in the results */ |
| 3966 |
if (ret.length() == 0) |
| 3967 |
{ |
| 3968 |
/* |
| 3969 |
* If the adjusted token list contains any tokens of type |
| 3970 |
* "miscWord", send the phrase to the results object for |
| 3971 |
* further consideration. |
| 3972 |
*/ |
| 3973 |
if (adjustedTokens.indexOf(&miscWord) != nil) |
| 3974 |
{ |
| 3975 |
/* |
| 3976 |
* we have miscWord tokens, so this is a miscWordList |
| 3977 |
* match - let the results object process it specially. |
| 3978 |
*/ |
| 3979 |
ret = results.unknownNounPhrase(self, resolver); |
| 3980 |
} |
| 3981 |
|
| 3982 |
/* |
| 3983 |
* if the list is empty, note that we have a noun phrase |
| 3984 |
* whose vocabulary words don't match anything in the game |
| 3985 |
*/ |
| 3986 |
if (ret.length() == 0) |
| 3987 |
results.noVocabMatch(resolver.getAction(), getOrigText()); |
| 3988 |
} |
| 3989 |
|
| 3990 |
/* return the result list */ |
| 3991 |
return ret; |
| 3992 |
} |
| 3993 |
|
| 3994 |
/* |
| 3995 |
* Each subclass must override getAdjustedTokens() to provide the |
| 3996 |
* appropriate set of tokens used to match the object. This is |
| 3997 |
* usually simply the original set of tokens, but in some cases it |
| 3998 |
* may differ; for example, spelled-out numbers normally adjust to |
| 3999 |
* the numeral form of the number. |
| 4000 |
* |
| 4001 |
* For each adjusted token, the list must have two entries: the |
| 4002 |
* first is a string giving the token text, and the second is the |
| 4003 |
* property giving the part of speech for the token. |
| 4004 |
*/ |
| 4005 |
getAdjustedTokens() { return nil; } |
| 4006 |
|
| 4007 |
/* |
| 4008 |
* Get the vocabulary match list. This is simply the set of objects |
| 4009 |
* that match all of the words in the noun phrase. Each rule |
| 4010 |
* subclass must override this to return an appropriate list. Note |
| 4011 |
* that subclasses should use getWordMatches() and |
| 4012 |
* intersectWordMatches() to build the list. |
| 4013 |
*/ |
| 4014 |
getVocabMatchList(resolver, results, extraFlags) { return nil; } |
| 4015 |
; |
| 4016 |
|
| 4017 |
|
| 4018 |
/* ------------------------------------------------------------------------ */ |
| 4019 |
/* |
| 4020 |
* An empty noun phrase production is one that matches, typically with |
| 4021 |
* non-zero badness value, as a placeholder when a command is missing a |
| 4022 |
* noun phrase where one is required. |
| 4023 |
* |
| 4024 |
* Each grammar rule instance of this rule class must define the |
| 4025 |
* property 'responseProd' to be the production that should be used to |
| 4026 |
* parse any response to an interactive prompt for the missing object. |
| 4027 |
*/ |
| 4028 |
class EmptyNounPhraseProd: NounPhraseProd |
| 4029 |
/* customize the way we generate the prompt and parse the response */ |
| 4030 |
setPrompt(response, asker) |
| 4031 |
{ |
| 4032 |
/* remember the new response production and ResolveAsker */ |
| 4033 |
responseProd = response; |
| 4034 |
asker_ = asker; |
| 4035 |
} |
| 4036 |
|
| 4037 |
/* resolve the empty phrase */ |
| 4038 |
resolveNouns(resolver, results) |
| 4039 |
{ |
| 4040 |
/* |
| 4041 |
* if we've filled in our missing phrase already, return the |
| 4042 |
* resolution of that list |
| 4043 |
*/ |
| 4044 |
if (newMatch != nil) |
| 4045 |
return newMatch.resolveNouns(resolver, results); |
| 4046 |
|
| 4047 |
/* |
| 4048 |
* The noun phrase was left out entirely, so try to get an |
| 4049 |
* implied object. |
| 4050 |
*/ |
| 4051 |
local match = getImpliedObject(resolver, results); |
| 4052 |
|
| 4053 |
/* if that succeeded, return the result */ |
| 4054 |
if (match != nil) |
| 4055 |
return match; |
| 4056 |
|
| 4057 |
/* |
| 4058 |
* Parse the next input with our own (subclassed) responseProd if |
| 4059 |
* we have one. If we don't (i.e., it's nil), get one from the |
| 4060 |
* action. |
| 4061 |
*/ |
| 4062 |
local rp = responseProd; |
| 4063 |
if (rp == nil && resolver.getAction() != nil) |
| 4064 |
rp = resolver.getAction().getObjResponseProd(resolver); |
| 4065 |
|
| 4066 |
/* as a last resort, use the fallback response */ |
| 4067 |
if (rp == nil) |
| 4068 |
rp = fallbackResponseProd; |
| 4069 |
|
| 4070 |
/* |
| 4071 |
* There is no implied object, so try to get a result |
| 4072 |
* interactively. Use the production that our rule instance |
| 4073 |
* specifies via the responseProd property to parse the |
| 4074 |
* interactive response. |
| 4075 |
*/ |
| 4076 |
match = results.askMissingObject(asker_, resolver, rp); |
| 4077 |
|
| 4078 |
/* if we didn't get a match, we have nothing to return */ |
| 4079 |
if (match == nil) |
| 4080 |
return []; |
| 4081 |
|
| 4082 |
/* we got a match - remember it as a proxy for our noun phrase */ |
| 4083 |
newMatch = match; |
| 4084 |
|
| 4085 |
/* return the resolved noun phrase from the proxy match */ |
| 4086 |
return newMatch.resolvedObjects; |
| 4087 |
} |
| 4088 |
|
| 4089 |
/* |
| 4090 |
* Get an implied object to automatically fill in for the missing |
| 4091 |
* noun phrase. By default, we simply ask the 'results' object for |
| 4092 |
* the missing object. |
| 4093 |
*/ |
| 4094 |
getImpliedObject(resolver, results) |
| 4095 |
{ |
| 4096 |
/* ask the 'results' object for the information */ |
| 4097 |
return results.getImpliedObject(self, resolver); |
| 4098 |
} |
| 4099 |
|
| 4100 |
/* |
| 4101 |
* Get my tokens. If I have a new match tree, return the tokens |
| 4102 |
* from the new match tree. Otherwise, we don't have any tokens, |
| 4103 |
* since we're empty. |
| 4104 |
*/ |
| 4105 |
getOrigTokenList() |
| 4106 |
{ |
| 4107 |
return (newMatch != nil ? newMatch.getOrigTokenList() : []); |
| 4108 |
} |
| 4109 |
|
| 4110 |
/* |
| 4111 |
* Get my original text. If I have a new match tree, return the |
| 4112 |
* text from the new match tree. Otherwise, we have no original |
| 4113 |
* text, since we're an empty phrase. |
| 4114 |
*/ |
| 4115 |
getOrigText() |
| 4116 |
{ |
| 4117 |
return (newMatch != nil ? newMatch.getOrigText() : ''); |
| 4118 |
} |
| 4119 |
|
| 4120 |
/* |
| 4121 |
* I'm an empty noun phrase, unless I already have a new match |
| 4122 |
* object. |
| 4123 |
*/ |
| 4124 |
isEmptyPhrase { return newMatch == nil; } |
| 4125 |
|
| 4126 |
/* |
| 4127 |
* the new match, when we get an interactive response to a query for |
| 4128 |
* the missing object |
| 4129 |
*/ |
| 4130 |
newMatch = nil |
| 4131 |
|
| 4132 |
/* |
| 4133 |
* Our "response" production - this is the production we use to parse |
| 4134 |
* the player's input in response to our disambiguation prompt. A |
| 4135 |
* subclass can leave this as nil, in which case we'll attempt to get |
| 4136 |
* the appropriate response production from the action. |
| 4137 |
*/ |
| 4138 |
responseProd = nil |
| 4139 |
|
| 4140 |
/* |
| 4141 |
* Our fallback response production - if responseProd is nil, this |
| 4142 |
* must be supplied for cases where we can't get the production from |
| 4143 |
* the action. This is ignored if responseProd is non-nil. |
| 4144 |
*/ |
| 4145 |
fallbackResponseProd = nil |
| 4146 |
|
| 4147 |
/* |
| 4148 |
* The ResolveAsker we use to generate our prompt. Use the base |
| 4149 |
* ResolveAsker by default; this can be overridden when the prompt |
| 4150 |
* is to be customized. |
| 4151 |
*/ |
| 4152 |
asker_ = ResolveAsker |
| 4153 |
; |
| 4154 |
|
| 4155 |
/* |
| 4156 |
* An empty noun phrase production for verb phrasings that imply an |
| 4157 |
* actor, but don't actually include one by name. |
| 4158 |
* |
| 4159 |
* This is similar to EmptyNounPhraseProd, but has an important |
| 4160 |
* difference: if the actor carrying out the command has a current or |
| 4161 |
* implied conversation partner, then we choose the conversation partner |
| 4162 |
* as the implied object. This is important in that we don't count the |
| 4163 |
* noun phrase as technically missing in this case, for the purposes of |
| 4164 |
* command ranking. This is useful for phrasings that inherently imply |
| 4165 |
* an actor strongly enough that there should be no match-strength |
| 4166 |
* penalty for leaving it out. |
| 4167 |
*/ |
| 4168 |
class ImpliedActorNounPhraseProd: EmptyNounPhraseProd |
| 4169 |
/* get my implied object */ |
| 4170 |
getImpliedObject(resolver, results) |
| 4171 |
{ |
| 4172 |
/* |
| 4173 |
* If the actor has a default interlocutor, use that, bypassing |
| 4174 |
* the normal implied object search. |
| 4175 |
*/ |
| 4176 |
local actor = resolver.actor_.getDefaultInterlocutor(); |
| 4177 |
if (actor != nil) |
| 4178 |
return [new ResolveInfo(actor, DefaultObject, nil)]; |
| 4179 |
|
| 4180 |
/* ask the 'results' object for the information */ |
| 4181 |
return results.getImpliedObject(self, resolver); |
| 4182 |
} |
| 4183 |
; |
| 4184 |
|
| 4185 |
/* |
| 4186 |
* Empty literal phrase - this serves a purpose similar to that of |
| 4187 |
* EmptyNounPhraseProd, but can be used where literal phrases are |
| 4188 |
* required. |
| 4189 |
*/ |
| 4190 |
class EmptyLiteralPhraseProd: LiteralProd |
| 4191 |
getLiteralText(results, action, which) |
| 4192 |
{ |
| 4193 |
local toks; |
| 4194 |
local prods; |
| 4195 |
|
| 4196 |
/* if we already have an interactive response, return it */ |
| 4197 |
if (newText != nil) |
| 4198 |
return newText; |
| 4199 |
|
| 4200 |
/* |
| 4201 |
* ask for the missing phrase and remember it for the next time |
| 4202 |
* we're asked for our text |
| 4203 |
*/ |
| 4204 |
newText = results.askMissingLiteral(action, which); |
| 4205 |
|
| 4206 |
/* |
| 4207 |
* Parse the text (if any) as a literal phrase. In most cases, |
| 4208 |
* anything can be parsed as a literal phrase, so this might |
| 4209 |
* seem kind of pointless; however, this is useful when the |
| 4210 |
* language-specific library defines rules for things like |
| 4211 |
* removing quotes from a quoted string. |
| 4212 |
*/ |
| 4213 |
if (newText != nil) |
| 4214 |
{ |
| 4215 |
try |
| 4216 |
{ |
| 4217 |
/* tokenize the input */ |
| 4218 |
toks = cmdTokenizer.tokenize(newText); |
| 4219 |
} |
| 4220 |
catch (TokErrorNoMatch exc) |
| 4221 |
{ |
| 4222 |
/* note the token error */ |
| 4223 |
gLibMessages.invalidCommandToken(exc.curChar_.htmlify()); |
| 4224 |
|
| 4225 |
/* treat the command as empty */ |
| 4226 |
throw new ReplacementCommandStringException(nil, nil, nil); |
| 4227 |
} |
| 4228 |
|
| 4229 |
/* parse the input as a literal phrase */ |
| 4230 |
prods = literalPhrase.parseTokens(toks, cmdDict); |
| 4231 |
|
| 4232 |
/* if we got a match, use it */ |
| 4233 |
if (prods.length() > 0) |
| 4234 |
{ |
| 4235 |
/* |
| 4236 |
* we got a match - this will be a LiteralProd, so ask |
| 4237 |
* the matching LiteralProd for its literal text, and |
| 4238 |
* use that as our new literal text |
| 4239 |
*/ |
| 4240 |
newText = prods[1].getLiteralText(results, action, which); |
| 4241 |
} |
| 4242 |
} |
| 4243 |
|
| 4244 |
/* return the text */ |
| 4245 |
return newText; |
| 4246 |
} |
| 4247 |
|
| 4248 |
/* |
| 4249 |
* Tentatively get my literal text. This is used for pre-resolution |
| 4250 |
* when we have another phrase we want to resolve first, but we want |
| 4251 |
* to provide a tentative form of the text in the meantime. We won't |
| 4252 |
* attempt to ask for more information interactively, but we'll |
| 4253 |
* return any information we do have. |
| 4254 |
*/ |
| 4255 |
getTentativeLiteralText() |
| 4256 |
{ |
| 4257 |
/* |
| 4258 |
* if we have a result from a previous interaactive request, |
| 4259 |
* return it; otherwise we have no tentative text |
| 4260 |
*/ |
| 4261 |
return newText; |
| 4262 |
} |
| 4263 |
|
| 4264 |
/* I'm an empty phrase, unless I already have a text response */ |
| 4265 |
isEmptyPhrase { return newText == nil; } |
| 4266 |
|
| 4267 |
/* the response to a previous interactive query */ |
| 4268 |
newText = nil |
| 4269 |
; |
| 4270 |
|
| 4271 |
/* |
| 4272 |
* Empty topic phrase production. This is the topic equivalent of |
| 4273 |
* EmptyNounPhraseProd. |
| 4274 |
*/ |
| 4275 |
class EmptyTopicPhraseProd: TopicProd |
| 4276 |
resolveNouns(resolver, results) |
| 4277 |
{ |
| 4278 |
local match; |
| 4279 |
|
| 4280 |
/* |
| 4281 |
* if we've filled in our missing phrase already, return the |
| 4282 |
* resolution of that list |
| 4283 |
*/ |
| 4284 |
if (newMatch != nil) |
| 4285 |
return newMatch.resolveNouns(resolver, results); |
| 4286 |
|
| 4287 |
/* ask for a topic interactively, using our responseProd */ |
| 4288 |
match = results.askMissingObject(asker_, resolver, responseProd); |
| 4289 |
|
| 4290 |
/* if we didn't get a match, we have nothing to return */ |
| 4291 |
if (match == nil) |
| 4292 |
return []; |
| 4293 |
|
| 4294 |
/* we got a match - remember it as a proxy for our noun phrase */ |
| 4295 |
newMatch = match; |
| 4296 |
|
| 4297 |
/* return the resolved noun phrase from the proxy match */ |
| 4298 |
return newMatch.resolvedObjects; |
| 4299 |
} |
| 4300 |
|
| 4301 |
/* we're an empty phrase if we don't have a new topic yet */ |
| 4302 |
isEmptyPhrase { return newMatch = nil; } |
| 4303 |
|
| 4304 |
/* get my tokens - use the underlying new match tree if we have one */ |
| 4305 |
getOrigTokenList() |
| 4306 |
{ |
| 4307 |
return (newMatch != nil ? newMatch.getOrigTokenList() : inherited()); |
| 4308 |
} |
| 4309 |
|
| 4310 |
/* get my original text - use the new match tree if we have one */ |
| 4311 |
getOrigText() |
| 4312 |
{ |
| 4313 |
return (newMatch != nil ? newMatch.getOrigText() : inherited()); |
| 4314 |
} |
| 4315 |
|
| 4316 |
/* our new underlying topic phrase */ |
| 4317 |
newMatch = nil |
| 4318 |
|
| 4319 |
/* |
| 4320 |
* by default, parse our interactive response as an ordinary topic |
| 4321 |
* phrase |
| 4322 |
*/ |
| 4323 |
responseProd = topicPhrase |
| 4324 |
|
| 4325 |
/* our ResolveAsker object - this is for customizing the prompt */ |
| 4326 |
asker_ = ResolveAsker |
| 4327 |
; |
| 4328 |
|
| 4329 |
|
| 4330 |
/* ------------------------------------------------------------------------ */ |
| 4331 |
/* |
| 4332 |
* Look for an undefined word in a list of tokens, and give the player a |
| 4333 |
* chance to correct a typo with "OOPS" if appropriate. |
| 4334 |
* |
| 4335 |
* If we find an unknown word and we can prompt for interactive |
| 4336 |
* resolution, we'll do so, and we'll throw an appropriate exception to |
| 4337 |
* handle the response. If we can't resolve the missing word |
| 4338 |
* interactively, we'll throw a parse failure exception. |
| 4339 |
* |
| 4340 |
* If there are no undefined words in the command, we'll simply return. |
| 4341 |
* |
| 4342 |
* tokList is the list of tokens under examination; this is a subset of |
| 4343 |
* the full command token list. cmdTokenList is the full command token |
| 4344 |
* list, in the usual tokenizer format. firstTokenIndex is the index of |
| 4345 |
* the first token in tokList within cmdTokenList. |
| 4346 |
* |
| 4347 |
* cmdType is an rmcXxx code giving the type of input we're reading. |
| 4348 |
*/ |
| 4349 |
tryOops(tokList, issuingActor, targetActor, |
| 4350 |
firstTokenIndex, cmdTokenList, cmdType) |
| 4351 |
{ |
| 4352 |
/* run the main "oops" processor in the player character sense context */ |
| 4353 |
callWithSenseContext(nil, sight, |
| 4354 |
{: tryOopsMain(tokList, issuingActor, targetActor, |
| 4355 |
firstTokenIndex, cmdTokenList, |
| 4356 |
cmdType) }); |
| 4357 |
} |
| 4358 |
|
| 4359 |
/* main "oops" processor */ |
| 4360 |
tryOopsMain(tokList, issuingActor, targetActor, |
| 4361 |
firstTokenIndex, cmdTokenList, cmdType) |
| 4362 |
{ |
| 4363 |
local str; |
| 4364 |
local unknownIdx; |
| 4365 |
local oopsMatch; |
| 4366 |
local toks; |
| 4367 |
local w; |
| 4368 |
|
| 4369 |
/* |
| 4370 |
* Look for a word not in the dictionary. |
| 4371 |
*/ |
| 4372 |
for (unknownIdx = nil, local i = 1, local len = tokList.length() ; |
| 4373 |
i <= len ; ++i) |
| 4374 |
{ |
| 4375 |
local cur; |
| 4376 |
local typ; |
| 4377 |
|
| 4378 |
/* get the token value for this word */ |
| 4379 |
cur = getTokVal(tokList[i]); |
| 4380 |
typ = getTokType(tokList[i]); |
| 4381 |
|
| 4382 |
/* check to see if this word is defined in the dictionary */ |
| 4383 |
if (typ == tokWord && !cmdDict.isWordDefined(cur)) |
| 4384 |
{ |
| 4385 |
/* note that we found an unknown word */ |
| 4386 |
unknownIdx = i; |
| 4387 |
|
| 4388 |
/* no need to look any further */ |
| 4389 |
break; |
| 4390 |
} |
| 4391 |
} |
| 4392 |
|
| 4393 |
/* |
| 4394 |
* if we didn't find an unknown word, there's no need to offer the |
| 4395 |
* user a chance to correct a typo - simply return without any |
| 4396 |
* further processing |
| 4397 |
*/ |
| 4398 |
if (unknownIdx == nil) |
| 4399 |
return; |
| 4400 |
|
| 4401 |
/* |
| 4402 |
* We do have an unknown word, but check one more thing: if we were |
| 4403 |
* asking some kind of follow-up question, such as a missing-object |
| 4404 |
* or disambiguation query, check to see if the new entry would parse |
| 4405 |
* successfully as a new command. It's possible that the new entry |
| 4406 |
* is a brand new command rather than a response to our question, and |
| 4407 |
* that the unknown word is valid in the context of a new command - |
| 4408 |
* it could be part of a literal-phrase, for example. |
| 4409 |
*/ |
| 4410 |
if (cmdType != rmcCommand) |
| 4411 |
{ |
| 4412 |
/* parse the command */ |
| 4413 |
local lst = firstCommandPhrase.parseTokens(cmdTokenList, cmdDict); |
| 4414 |
|
| 4415 |
/* resolve actions */ |
| 4416 |
lst = lst.subset( |
| 4417 |
{x: x.resolveFirstAction(issuingActor, targetActor) != nil}); |
| 4418 |
|
| 4419 |
/* if we managed to match something, treat it as a new command */ |
| 4420 |
if (lst.length() != 0) |
| 4421 |
throw new ReplacementCommandStringException( |
| 4422 |
cmdTokenizer.buildOrigText(cmdTokenList), nil, nil); |
| 4423 |
} |
| 4424 |
|
| 4425 |
|
| 4426 |
/* get the unknown word, in presentable form */ |
| 4427 |
w = getTokOrig(tokList[unknownIdx]).htmlify(); |
| 4428 |
|
| 4429 |
/* |
| 4430 |
* Give them a chance to correct a typo via OOPS if the player |
| 4431 |
* issued the command. If the command came from an actor other than |
| 4432 |
* the player character, simply fail the command. |
| 4433 |
*/ |
| 4434 |
if (!issuingActor.isPlayerChar()) |
| 4435 |
{ |
| 4436 |
/* we can't do this interactively - treat it as a failure */ |
| 4437 |
throw new ParseFailureException(&wordIsUnknown, w); |
| 4438 |
} |
| 4439 |
|
| 4440 |
/* |
| 4441 |
* tell the player about the unknown word, implicitly asking for |
| 4442 |
* an OOPS to fix it |
| 4443 |
*/ |
| 4444 |
targetActor.getParserMessageObj().askUnknownWord(targetActor, w); |
| 4445 |
|
| 4446 |
/* |
| 4447 |
* Prompt for a new command. We'll use the main command prompt, |
| 4448 |
* because we want to pretend that we're asking for a brand new |
| 4449 |
* command, which we'll accept. However, if the player enters |
| 4450 |
* an OOPS command, we'll process it specially. |
| 4451 |
*/ |
| 4452 |
getResponse: |
| 4453 |
str = readMainCommandTokens(rmcOops); |
| 4454 |
|
| 4455 |
/* re-enable the transcript, if we have one */ |
| 4456 |
if (gTranscript) |
| 4457 |
gTranscript.activate(); |
| 4458 |
|
| 4459 |
/* |
| 4460 |
* if the command reader fully processed the input via preparsing, |
| 4461 |
* we have nothing more to do here - simply throw a replace-command |
| 4462 |
* exception with the nil string |
| 4463 |
*/ |
| 4464 |
if (str == nil) |
| 4465 |
throw new ReplacementCommandStringException(nil, nil, nil); |
| 4466 |
|
| 4467 |
/* extract the tokens and string from the result */ |
| 4468 |
toks = str[2]; |
| 4469 |
str = str[1]; |
| 4470 |
|
| 4471 |
/* try parsing it as an "oops" command */ |
| 4472 |
oopsMatch = oopsCommand.parseTokens(toks, cmdDict); |
| 4473 |
|
| 4474 |
/* |
| 4475 |
* if we found a match, process the OOPS command; otherwise, |
| 4476 |
* treat it as a brand new command |
| 4477 |
*/ |
| 4478 |
if (oopsMatch.length() != 0) |
| 4479 |
{ |
| 4480 |
local badIdx; |
| 4481 |
|
| 4482 |
/* |
| 4483 |
* if they typed in just OOPS without any tokens, show an error |
| 4484 |
* and ask again |
| 4485 |
*/ |
| 4486 |
if (oopsMatch[1].getNewTokens() == nil) |
| 4487 |
{ |
| 4488 |
/* tell them they need to supply the missing word */ |
| 4489 |
gLibMessages.oopsMissingWord; |
| 4490 |
|
| 4491 |
/* go back and try reading another response */ |
| 4492 |
goto getResponse; |
| 4493 |
} |
| 4494 |
|
| 4495 |
/* |
| 4496 |
* Build a new token list by removing the errant token, and |
| 4497 |
* splicing the new tokens into the original token list to |
| 4498 |
* replace the errant one. |
| 4499 |
* |
| 4500 |
* Note that we'll arbitrarily take the first "oops" match, |
| 4501 |
* even if there are several. There should be no ambiguity |
| 4502 |
* in the "oops" grammar rule, but even if there is, it |
| 4503 |
* doesn't matter, since ultimately all we care about is the |
| 4504 |
* list of tokens after the "oops". |
| 4505 |
*/ |
| 4506 |
badIdx = firstTokenIndex + unknownIdx - 1; |
| 4507 |
cmdTokenList = spliceList(cmdTokenList, badIdx, |
| 4508 |
oopsMatch[1].getNewTokens()); |
| 4509 |
|
| 4510 |
/* |
| 4511 |
* Turn the token list back into a string. Since we've edited |
| 4512 |
* the original text, we want to start over with the new input, |
| 4513 |
* including running pre-parsing on the text. |
| 4514 |
*/ |
| 4515 |
str = cmdTokenizer.buildOrigText(cmdTokenList); |
| 4516 |
|
| 4517 |
/* |
| 4518 |
* run it through pre-parsing as the same kind of input the |
| 4519 |
* caller was reading |
| 4520 |
*/ |
| 4521 |
str = StringPreParser.runAll(str, cmdType); |
| 4522 |
|
| 4523 |
/* |
| 4524 |
* if it came back nil, it means the preparser fully handled it; |
| 4525 |
* in this case, simply throw a nil replacement command string |
| 4526 |
*/ |
| 4527 |
if (str == nil) |
| 4528 |
throw new ReplacementCommandStringException(nil, nil, nil); |
| 4529 |
|
| 4530 |
/* re-tokenize the result of pre-parsing */ |
| 4531 |
cmdTokenList = cmdTokenizer.tokenize(str); |
| 4532 |
|
| 4533 |
/* retry parsing the edited token list */ |
| 4534 |
throw new RetryCommandTokensException(cmdTokenList); |
| 4535 |
} |
| 4536 |
else |
| 4537 |
{ |
| 4538 |
/* |
| 4539 |
* they didn't enter something that looks like an "OOPS", so |
| 4540 |
* it must be a brand new command - parse it as a new |
| 4541 |
* command by throwing a replacement command exception with |
| 4542 |
* the new string |
| 4543 |
*/ |
| 4544 |
throw new ReplacementCommandStringException(str, nil, nil); |
| 4545 |
} |
| 4546 |
} |
| 4547 |
|
| 4548 |
/* |
| 4549 |
* splice a new sublist into a list, replacing the item at 'idx' |
| 4550 |
*/ |
| 4551 |
spliceList(lst, idx, newItems) |
| 4552 |
{ |
| 4553 |
return (lst.sublist(1, idx - 1) |
| 4554 |
+ newItems |
| 4555 |
+ lst.sublist(idx + 1)); |
| 4556 |
} |
| 4557 |
|
| 4558 |
|
| 4559 |
/* ------------------------------------------------------------------------ */ |
| 4560 |
/* |
| 4561 |
* Try reading a response to a missing object question. If we |
| 4562 |
* successfully read a noun phrase that matches the given production |
| 4563 |
* rule, we'll resolve it, stash the resolved list in the |
| 4564 |
* resolvedObjects_ property of the match tree, and return the match |
| 4565 |
* tree. If they enter something that doesn't look like a response to |
| 4566 |
* the question at all, we'll throw a new-command exception to process |
| 4567 |
* it. |
| 4568 |
*/ |
| 4569 |
tryAskingForObject(issuingActor, targetActor, |
| 4570 |
resolver, results, responseProd) |
| 4571 |
{ |
| 4572 |
/* |
| 4573 |
* Prompt for a new command. We'll use the main command prompt, |
| 4574 |
* because we want to pretend that we're asking for a brand new |
| 4575 |
* command, which we'll accept. However, if the player enters |
| 4576 |
* something that looks like a response to the missing-object query, |
| 4577 |
* we'll handle it as an answer rather than as a new command. |
| 4578 |
*/ |
| 4579 |
local str = readMainCommandTokens(rmcAskObject); |
| 4580 |
|
| 4581 |
/* re-enable the transcript, if we have one */ |
| 4582 |
if (gTranscript) |
| 4583 |
gTranscript.activate(); |
| 4584 |
|
| 4585 |
/* |
| 4586 |
* if it came back nil, it means that the preparser fully processed |
| 4587 |
* the input, which means we have nothing more to do here - simply |
| 4588 |
* treat this is a nil replacement command |
| 4589 |
*/ |
| 4590 |
if (str == nil) |
| 4591 |
throw new ReplacementCommandStringException(nil, nil, nil); |
| 4592 |
|
| 4593 |
/* extract the input line and tokens */ |
| 4594 |
local toks = str[2]; |
| 4595 |
str = str[1]; |
| 4596 |
|
| 4597 |
/* keep going as long as we get replacement token lists */ |
| 4598 |
for (;;) |
| 4599 |
{ |
| 4600 |
/* try parsing it as an object list */ |
| 4601 |
local matchList = responseProd.parseTokens(toks, cmdDict); |
| 4602 |
|
| 4603 |
/* |
| 4604 |
* if we didn't find any match at all, it's probably a brand new |
| 4605 |
* command - go process it as a replacement for the current |
| 4606 |
* command |
| 4607 |
*/ |
| 4608 |
if (matchList == []) |
| 4609 |
{ |
| 4610 |
/* |
| 4611 |
* they didn't enter something that looks like a valid |
| 4612 |
* response, so assume it's a brand new command - parse it |
| 4613 |
* as a new command by throwing a replacement command |
| 4614 |
* exception with the new string |
| 4615 |
*/ |
| 4616 |
throw new ReplacementCommandStringException(str, nil, nil); |
| 4617 |
} |
| 4618 |
|
| 4619 |
/* if we're in debug mode, show the interpretations */ |
| 4620 |
dbgShowGrammarList(matchList); |
| 4621 |
|
| 4622 |
/* create an interactive sub-resolver for resolving the response */ |
| 4623 |
local ires = new InteractiveResolver(resolver); |
| 4624 |
|
| 4625 |
/* |
| 4626 |
* rank them using our response ranker - use the original |
| 4627 |
* resolver to resolve the object list |
| 4628 |
*/ |
| 4629 |
local rankings = MissingObjectRanking.sortByRanking(matchList, ires); |
| 4630 |
|
| 4631 |
/* |
| 4632 |
* If the best item has unknown words, try letting the user |
| 4633 |
* correct typos with OOPS. |
| 4634 |
*/ |
| 4635 |
if (rankings[1].nonMatchCount != 0 |
| 4636 |
&& rankings[1].unknownWordCount != 0) |
| 4637 |
{ |
| 4638 |
try |
| 4639 |
{ |
| 4640 |
/* |
| 4641 |
* complain about the unknown word and look for an OOPS |
| 4642 |
* reply |
| 4643 |
*/ |
| 4644 |
tryOops(toks, issuingActor, targetActor, |
| 4645 |
1, toks, rmcAskObject); |
| 4646 |
} |
| 4647 |
catch (RetryCommandTokensException exc) |
| 4648 |
{ |
| 4649 |
/* get the new token list */ |
| 4650 |
toks = exc.newTokens_; |
| 4651 |
|
| 4652 |
/* replace the string as well */ |
| 4653 |
str = cmdTokenizer.buildOrigText(toks); |
| 4654 |
|
| 4655 |
/* go back for another try at parsing the response */ |
| 4656 |
continue; |
| 4657 |
} |
| 4658 |
} |
| 4659 |
|
| 4660 |
/* |
| 4661 |
* If the best item we could find has no matches, check to see |
| 4662 |
* if it has miscellaneous noun phrases - if so, it's probably |
| 4663 |
* just a new command, since it doesn't have anything we |
| 4664 |
* recognize as a noun phrase. |
| 4665 |
*/ |
| 4666 |
if (rankings[1].nonMatchCount != 0 |
| 4667 |
&& rankings[1].miscWordListCount != 0) |
| 4668 |
{ |
| 4669 |
/* |
| 4670 |
* it's probably not an answer at all - treat it as a new |
| 4671 |
* command |
| 4672 |
*/ |
| 4673 |
throw new ReplacementCommandStringException(str, nil, nil); |
| 4674 |
} |
| 4675 |
|
| 4676 |
/* the highest ranked object is the winner */ |
| 4677 |
local match = rankings[1].match; |
| 4678 |
|
| 4679 |
/* |
| 4680 |
* Check to see if this looks like an ordinary new command as |
| 4681 |
* well as a noun phrase. For example, "e" looks like the noun |
| 4682 |
* phrase "east wall," in that "e" is a synonym for the adjective |
| 4683 |
* "east". But "e" also looks like an ordinary new command. |
| 4684 |
* |
| 4685 |
* We'd have to be able to read the user's mind to know which |
| 4686 |
* they mean in such cases, so we have to make some assumptions |
| 4687 |
* to deal with the ambiguity. In particular, if the phrasing |
| 4688 |
* has special syntax that makes it look like a particularly |
| 4689 |
* close match to the query phrase, assume it's a query response; |
| 4690 |
* otherwise, assume it's a new command. For example: |
| 4691 |
* |
| 4692 |
*. >dig |
| 4693 |
*. What do you want to dig in? [sets up for "in <noun>" reply] |
| 4694 |
* |
| 4695 |
* If the user answers "IN THE DIRT", we have a match to the |
| 4696 |
* special syntax of the reply (the "in <noun>" phrasing), so we |
| 4697 |
* will assume this is a reply to the query, even though it also |
| 4698 |
* matches a valid new command phrasing for ENTER DIRT. If the |
| 4699 |
* user answers "E" or "EAST", we *don't* have a match to the |
| 4700 |
* special syntax, but merely an ordinary noun phrase match, so |
| 4701 |
* we'll assume this is an ordinary GO EAST command. |
| 4702 |
*/ |
| 4703 |
local cmdMatchList = firstCommandPhrase.parseTokens(toks, cmdDict); |
| 4704 |
if (cmdMatchList != []) |
| 4705 |
{ |
| 4706 |
/* |
| 4707 |
* The phrasing looks like it's a valid new command as well |
| 4708 |
* as a noun phrase reply. Check the query reply match for |
| 4709 |
* special syntax that would distinguish it from a new |
| 4710 |
* command; if it doesn't match any special syntax, assume |
| 4711 |
* that it is indeed a new command instead of a query reply. |
| 4712 |
*/ |
| 4713 |
if (!match.isSpecialResponseMatch) |
| 4714 |
throw new ReplacementCommandStringException(str, nil, nil); |
| 4715 |
} |
| 4716 |
|
| 4717 |
/* show our winning interpretation */ |
| 4718 |
dbgShowGrammarWithCaption('Missing Object Winner', match); |
| 4719 |
|
| 4720 |
/* |
| 4721 |
* actually resolve the response to objects, using the original |
| 4722 |
* results and resolver objects |
| 4723 |
*/ |
| 4724 |
local objList = match.resolveNouns(ires, results); |
| 4725 |
|
| 4726 |
/* stash the resolved object list in a property of the match tree */ |
| 4727 |
match.resolvedObjects = objList; |
| 4728 |
|
| 4729 |
/* return the match tree */ |
| 4730 |
return match; |
| 4731 |
} |
| 4732 |
} |
| 4733 |
|
| 4734 |
/* |
| 4735 |
* a property we use to hold the resolved objects of a match tree in |
| 4736 |
* tryAskingForObject |
| 4737 |
*/ |
| 4738 |
property resolvedObjects; |
| 4739 |
|
| 4740 |
/* |
| 4741 |
* Missing-object response ranker. |
| 4742 |
*/ |
| 4743 |
class MissingObjectRanking: CommandRanking |
| 4744 |
/* |
| 4745 |
* missing-object responses have no verb, so they won't count any |
| 4746 |
* noun slots; we just need to give these an arbitrary value so that |
| 4747 |
* we can compare them (and find them equal) |
| 4748 |
*/ |
| 4749 |
nounSlotCount = 0 |
| 4750 |
; |
| 4751 |
|
| 4752 |
/* ------------------------------------------------------------------------ */ |
| 4753 |
/* |
| 4754 |
* An implementation of ResolveResults for normal noun resolution. |
| 4755 |
*/ |
| 4756 |
class BasicResolveResults: ResolveResults |
| 4757 |
/* |
| 4758 |
* The target and issuing actors for the command being resolved. |
| 4759 |
*/ |
| 4760 |
targetActor_ = nil |
| 4761 |
issuingActor_ = nil |
| 4762 |
|
| 4763 |
/* set up the actor parameters */ |
| 4764 |
setActors(target, issuer) |
| 4765 |
{ |
| 4766 |
targetActor_ = target; |
| 4767 |
issuingActor_ = issuer; |
| 4768 |
} |
| 4769 |
|
| 4770 |
/* |
| 4771 |
* Results gatherer methods |
| 4772 |
*/ |
| 4773 |
|
| 4774 |
noVocabMatch(action, txt) |
| 4775 |
{ |
| 4776 |
/* indicate that we couldn't match the phrase */ |
| 4777 |
throw new ParseFailureException(&noMatch, |
| 4778 |
action, txt.toLower().htmlify()); |
| 4779 |
} |
| 4780 |
|
| 4781 |
noMatch(action, txt) |
| 4782 |
{ |
| 4783 |
/* show an error */ |
| 4784 |
throw new ParseFailureException(&noMatch, |
| 4785 |
action, txt.toLower().htmlify()); |
| 4786 |
} |
| 4787 |
|
| 4788 |
noMatchPossessive(action, txt) |
| 4789 |
{ |
| 4790 |
/* use the basic noMatch handling */ |
| 4791 |
noMatch(action, txt); |
| 4792 |
} |
| 4793 |
|
| 4794 |
allNotAllowed() |
| 4795 |
{ |
| 4796 |
/* show an error */ |
| 4797 |
throw new ParseFailureException(&allNotAllowed); |
| 4798 |
} |
| 4799 |
|
| 4800 |
noMatchForAll() |
| 4801 |
{ |
| 4802 |
/* show an error */ |
| 4803 |
throw new ParseFailureException(&noMatchForAll); |
| 4804 |
} |
| 4805 |
|
| 4806 |
noteEmptyBut() |
| 4807 |
{ |
| 4808 |
/* this isn't an error - ignore it */ |
| 4809 |
} |
| 4810 |
|
| 4811 |
noMatchForAllBut() |
| 4812 |
{ |
| 4813 |
/* show an error */ |
| 4814 |
throw new ParseFailureException(&noMatchForAllBut); |
| 4815 |
} |
| 4816 |
|
| 4817 |
noMatchForListBut() |
| 4818 |
{ |
| 4819 |
/* show an error */ |
| 4820 |
throw new ParseFailureException(&noMatchForListBut); |
| 4821 |
} |
| 4822 |
|
| 4823 |
noMatchForPronoun(typ, txt) |
| 4824 |
{ |
| 4825 |
/* show an error */ |
| 4826 |
throw new ParseFailureException(&noMatchForPronoun, |
| 4827 |
typ, txt.toLower().htmlify()); |
| 4828 |
} |
| 4829 |
|
| 4830 |
reflexiveNotAllowed(typ, txt) |
| 4831 |
{ |
| 4832 |
/* show an error */ |
| 4833 |
throw new ParseFailureException(&reflexiveNotAllowed, |
| 4834 |
typ, txt.toLower().htmlify()); |
| 4835 |
} |
| 4836 |
|
| 4837 |
wrongReflexive(typ, txt) |
| 4838 |
{ |
| 4839 |
/* show an error */ |
| 4840 |
throw new ParseFailureException(&wrongReflexive, |
| 4841 |
typ, txt.toLower().htmlify()); |
| 4842 |
} |
| 4843 |
|
| 4844 |
noMatchForPossessive(owner, txt) |
| 4845 |
{ |
| 4846 |
/* if the owner is a list, it's a plural owner */ |
| 4847 |
if (owner.length() > 1) |
| 4848 |
{ |
| 4849 |
/* it's a plural owner */ |
| 4850 |
throw new ParseFailureException(&noMatchForPluralPossessive, txt); |
| 4851 |
} |
| 4852 |
else |
| 4853 |
{ |
| 4854 |
/* let the (singular) owner object generate the error */ |
| 4855 |
owner[1].throwNoMatchForPossessive(txt.toLower().htmlify()); |
| 4856 |
} |
| 4857 |
} |
| 4858 |
|
| 4859 |
noMatchForLocation(loc, txt) |
| 4860 |
{ |
| 4861 |
/* let the location object generate the error */ |
| 4862 |
loc.throwNoMatchForLocation(txt.toLower().htmlify()); |
| 4863 |
} |
| 4864 |
|
| 4865 |
nothingInLocation(loc) |
| 4866 |
{ |
| 4867 |
/* let the location object generate the error */ |
| 4868 |
loc.throwNothingInLocation(); |
| 4869 |
} |
| 4870 |
|
| 4871 |
noteBadPrep() |
| 4872 |
{ |
| 4873 |
/* |
| 4874 |
* bad prepositional phrase structure - assume that the |
| 4875 |
* preposition was intended as part of the verb phrase, so |
| 4876 |
* indicate that the entire command is not understood |
| 4877 |
*/ |
| 4878 |
throw new ParseFailureException(&commandNotUnderstood); |
| 4879 |
} |
| 4880 |
|
| 4881 |
/* |
| 4882 |
* Service routine - determine if we can interactively resolve a |
| 4883 |
* need for more information. If the issuer is the player, and the |
| 4884 |
* target actor can talk to the player, then we can resolve a |
| 4885 |
* question interactively; otherwise, we cannot. |
| 4886 |
* |
| 4887 |
* We can't interactively resolve a question if the issuer isn't the |
| 4888 |
* player, because there's no interactive user to prompt for more |
| 4889 |
* information. |
| 4890 |
* |
| 4891 |
* We can't interactively resolve a question if the target actor |
| 4892 |
* can't talk to the player, because the question to the player |
| 4893 |
* would be coming out of thin air. |
| 4894 |
*/ |
| 4895 |
canResolveInteractively(action) |
| 4896 |
{ |
| 4897 |
/* |
| 4898 |
* If this is an implied action, and the target actor is in |
| 4899 |
* "NPC" mode, we can't resolve interactively. Note that if |
| 4900 |
* there's no action, it can't be implicit (we won't have an |
| 4901 |
* action if we're resolving the actor to whom the action is |
| 4902 |
* targeted). |
| 4903 |
*/ |
| 4904 |
if (action != nil |
| 4905 |
&& action.isImplicit |
| 4906 |
&& targetActor_.impliedCommandMode() == ModeNPC) |
| 4907 |
return nil; |
| 4908 |
|
| 4909 |
/* |
| 4910 |
* if the issuer is the player, and either the target is the |
| 4911 |
* player or the target can talk to the player, we can resolve |
| 4912 |
* interactively |
| 4913 |
*/ |
| 4914 |
return (issuingActor_.isPlayerChar() |
| 4915 |
&& (targetActor_ == issuingActor_ |
| 4916 |
|| targetActor_.canTalkTo(issuingActor_))); |
| 4917 |
} |
| 4918 |
|
| 4919 |
/* |
| 4920 |
* Handle an ambiguous noun phrase. We'll first check to see if we |
| 4921 |
* can find a Distinguisher that can tell at least some of the |
| 4922 |
* matches apart; if we fail to do that, we'll just pick the required |
| 4923 |
* number of objects arbitrarily, since we have no way to distinguish |
| 4924 |
* any of them. Once we've chosen a Distinguisher, we'll ask the |
| 4925 |
* user for help interactively if possible. |
| 4926 |
*/ |
| 4927 |
ambiguousNounPhrase(keeper, asker, txt, |
| 4928 |
matchList, fullMatchList, scopeList, |
| 4929 |
requiredNum, resolver) |
| 4930 |
{ |
| 4931 |
/* put the match list in disambigPromptOrder order */ |
| 4932 |
matchList = matchList.sort( |
| 4933 |
SortAsc, {a, b: (a.obj_.disambigPromptOrder |
| 4934 |
- b.obj_.disambigPromptOrder) }); |
| 4935 |
|
| 4936 |
/* |
| 4937 |
* Get the token list for the original phrase. Since the whole |
| 4938 |
* point is to disambiguate a phrase that matched multiple |
| 4939 |
* objects, all of the matched objects came from the same phrase, |
| 4940 |
* so we can just grab the tokens from the first item. |
| 4941 |
*/ |
| 4942 |
local np = matchList[1].np_; |
| 4943 |
local npToks = np ? np.getOrigTokenList() : []; |
| 4944 |
|
| 4945 |
/* for the prompt, use the lower-cased version of the input text */ |
| 4946 |
local promptTxt = txt.toLower().htmlify(); |
| 4947 |
|
| 4948 |
/* ask the response keeper for its list of past responses, if any */ |
| 4949 |
local pastResponses = keeper.getAmbigResponses(); |
| 4950 |
|
| 4951 |
/* |
| 4952 |
* set up a results object - use the special disambiguation |
| 4953 |
* results object instead of the basic resolver type |
| 4954 |
*/ |
| 4955 |
local disambigResults = new DisambigResults(self); |
| 4956 |
|
| 4957 |
/* |
| 4958 |
* start out with an empty still-to-resolve list - we have only |
| 4959 |
* the one list to resolve so far |
| 4960 |
*/ |
| 4961 |
local stillToResolve = []; |
| 4962 |
|
| 4963 |
/* we have nothing in the result list yet */ |
| 4964 |
local resultList = []; |
| 4965 |
|
| 4966 |
/* determine if we can ask for clarification interactively */ |
| 4967 |
if (!canResolveInteractively(resolver.getAction())) |
| 4968 |
{ |
| 4969 |
/* |
| 4970 |
* don't attempt to resolve this interactively - just treat |
| 4971 |
* it as a resolution failure |
| 4972 |
*/ |
| 4973 |
throw new ParseFailureException( |
| 4974 |
&ambiguousNounPhrase, txt.htmlify(), |
| 4975 |
matchList, fullMatchList); |
| 4976 |
} |
| 4977 |
|
| 4978 |
/* we're asking for the first time */ |
| 4979 |
local everAsked = nil; |
| 4980 |
local askingAgain = nil; |
| 4981 |
|
| 4982 |
/* |
| 4983 |
* Keep going until we run out of things left to resolve. Each |
| 4984 |
* time an answer looks valid but doesn't completely |
| 4985 |
* disambiguate the results, we'll queue it for another question |
| 4986 |
* iteration, so we must continue until we run out of queued |
| 4987 |
* questions. |
| 4988 |
*/ |
| 4989 |
queryLoop: |
| 4990 |
for (local pastIdx = 1 ;; ) |
| 4991 |
{ |
| 4992 |
local str; |
| 4993 |
local toks; |
| 4994 |
local dist; |
| 4995 |
local curMatchList = []; |
| 4996 |
|
| 4997 |
/* |
| 4998 |
* Find the first distinguisher that can tell one or more of |
| 4999 |
* these objects apart. Work through the distinguishers in |
| 5000 |
* priority order, which is the order they appear in an |
| 5001 |
* object's 'distinguishers' property. |
| 5002 |
* |
| 5003 |
* If these objects aren't all equivalents, then we'll |
| 5004 |
* immediately find that the basic distinguisher can tell |
| 5005 |
* them all apart. Every object's first distinguisher is |
| 5006 |
* the basic distinguisher. If these objects are all |
| 5007 |
* equivalents, then they'll all have the same set of |
| 5008 |
* distinguishers. In either case, we don't have to worry |
| 5009 |
* about what set of objects we have, because we're |
| 5010 |
* guaranteed to have a suitable set of distinguishers in |
| 5011 |
* common - so just arbitrarily pick an object and work |
| 5012 |
* through its distinguishers. |
| 5013 |
*/ |
| 5014 |
foreach (dist in matchList[1].obj_.distinguishers) |
| 5015 |
{ |
| 5016 |
/* filter the match list using this distinguisher only */ |
| 5017 |
curMatchList = filterWithDistinguisher(matchList, dist); |
| 5018 |
|
| 5019 |
/* |
| 5020 |
* if this list has more than the required number of |
| 5021 |
* results, then this distinguisher is capable of |
| 5022 |
* telling apart enough of these objects, so use this |
| 5023 |
* distinguisher for now |
| 5024 |
*/ |
| 5025 |
if (curMatchList.length() > requiredNum) |
| 5026 |
break; |
| 5027 |
} |
| 5028 |
|
| 5029 |
/* |
| 5030 |
* If we didn't find any distinguishers that could tell |
| 5031 |
* apart enough of the objects, then we've exhausted our |
| 5032 |
* ability to choose among these objects, so return an |
| 5033 |
* arbitrary set of the desired size. |
| 5034 |
*/ |
| 5035 |
if (curMatchList.length() <= requiredNum) |
| 5036 |
return fullMatchList.sublist(1, requiredNum); |
| 5037 |
|
| 5038 |
/* |
| 5039 |
* If we have past responses, try reusing the past responses |
| 5040 |
* before asking for new responses. |
| 5041 |
*/ |
| 5042 |
if (pastIdx <= pastResponses.length()) |
| 5043 |
{ |
| 5044 |
/* we have another past response - use the next one */ |
| 5045 |
str = pastResponses[pastIdx++]; |
| 5046 |
|
| 5047 |
/* tokenize the new command */ |
| 5048 |
toks = cmdTokenizer.tokenize(str); |
| 5049 |
} |
| 5050 |
else |
| 5051 |
{ |
| 5052 |
/* |
| 5053 |
* Try filtering the options with the basic |
| 5054 |
* distinguisher, to see if all of the remaining options |
| 5055 |
* are basic equivalents - if they are, then we can |
| 5056 |
* refer to them by the object name, since every one has |
| 5057 |
* the same object name. |
| 5058 |
*/ |
| 5059 |
local basicDistList = filterWithDistinguisher( |
| 5060 |
matchList, basicDistinguisher); |
| 5061 |
|
| 5062 |
/* |
| 5063 |
* if we filtered to one object, everything remaining is |
| 5064 |
* a basic equivalent of that one object, so they all |
| 5065 |
* have the same name, so we can use that name |
| 5066 |
*/ |
| 5067 |
if (basicDistList.length() == 1) |
| 5068 |
promptTxt = basicDistList[1].obj_.disambigEquivName; |
| 5069 |
|
| 5070 |
/* |
| 5071 |
* let the distinguisher know we're prompting for more |
| 5072 |
* information based on this distinguisher |
| 5073 |
*/ |
| 5074 |
dist.notePrompt(curMatchList); |
| 5075 |
|
| 5076 |
/* |
| 5077 |
* We don't have any special insight into which object |
| 5078 |
* the user might have intended, and we have no past |
| 5079 |
* responses to re-use, so simply ask for clarification. |
| 5080 |
* Ask using the full match list, so that we correctly |
| 5081 |
* indicate where there are multiple equivalents. |
| 5082 |
*/ |
| 5083 |
asker.askDisambig(targetActor_, promptTxt, |
| 5084 |
curMatchList, fullMatchList, requiredNum, |
| 5085 |
everAsked && askingAgain, dist); |
| 5086 |
|
| 5087 |
/* ask for a new command */ |
| 5088 |
str = readMainCommandTokens(rmcDisambig); |
| 5089 |
|
| 5090 |
/* re-enable the transcript, if we have one */ |
| 5091 |
if (gTranscript) |
| 5092 |
gTranscript.activate(); |
| 5093 |
|
| 5094 |
/* note that we've asked for input interactively */ |
| 5095 |
everAsked = true; |
| 5096 |
|
| 5097 |
/* |
| 5098 |
* if it came back nil, the input was fully processed by |
| 5099 |
* the preparser, so throw a nil replacement string |
| 5100 |
* exception |
| 5101 |
*/ |
| 5102 |
if (str == nil) |
| 5103 |
throw new ReplacementCommandStringException( |
| 5104 |
nil, nil, nil); |
| 5105 |
|
| 5106 |
/* extract the tokens and the string result */ |
| 5107 |
toks = str[2]; |
| 5108 |
str = str[1]; |
| 5109 |
} |
| 5110 |
|
| 5111 |
/* presume we won't have to ask again */ |
| 5112 |
askingAgain = nil; |
| 5113 |
|
| 5114 |
/* |
| 5115 |
* Add the new tokens to the noun phrase token list, at the |
| 5116 |
* end, enclosed in parentheses. The effect is that each |
| 5117 |
* time we answer a disambiguation question, the answer |
| 5118 |
* appears as a parenthetical at the end of the phrase: "take |
| 5119 |
* the box (cardboard) (the big one)". |
| 5120 |
*/ |
| 5121 |
npToks = npToks.append(['(', tokPunct, '(']); |
| 5122 |
npToks += toks; |
| 5123 |
npToks = npToks.append([')', tokPunct, ')']); |
| 5124 |
|
| 5125 |
retryParse: |
| 5126 |
/* |
| 5127 |
* Check for narrowing syntax. If the command doesn't |
| 5128 |
* appear to match a narrowing syntax, then treat it as an |
| 5129 |
* entirely new command. |
| 5130 |
*/ |
| 5131 |
local prodList = mainDisambigPhrase.parseTokens(toks, cmdDict); |
| 5132 |
|
| 5133 |
/* |
| 5134 |
* if we didn't get any structural matches for a |
| 5135 |
* disambiguation response, it must be an entirely new |
| 5136 |
* command |
| 5137 |
*/ |
| 5138 |
if (prodList == []) |
| 5139 |
throw new ReplacementCommandStringException(str, nil, nil); |
| 5140 |
|
| 5141 |
/* if we're in debug mode, show the interpretations */ |
| 5142 |
dbgShowGrammarList(prodList); |
| 5143 |
|
| 5144 |
/* create a disambiguation response resolver */ |
| 5145 |
local disResolver = new DisambigResolver( |
| 5146 |
txt, matchList, fullMatchList, fullMatchList, resolver, dist); |
| 5147 |
|
| 5148 |
/* |
| 5149 |
* create a disambiguation resolver that uses the full scope |
| 5150 |
* list - we'll use this as a fallback if we can't match any |
| 5151 |
* objects with the narrowed possibility list |
| 5152 |
*/ |
| 5153 |
local scopeDisResolver = new DisambigResolver( |
| 5154 |
txt, matchList, fullMatchList, scopeList, resolver, dist); |
| 5155 |
|
| 5156 |
/* |
| 5157 |
* Run the alternatives through the disambiguation response |
| 5158 |
* ranking process. |
| 5159 |
*/ |
| 5160 |
local rankings = |
| 5161 |
DisambigRanking.sortByRanking(prodList, disResolver); |
| 5162 |
|
| 5163 |
/* |
| 5164 |
* If the best item has unknown words, try letting the user |
| 5165 |
* correct typos with OOPS. |
| 5166 |
*/ |
| 5167 |
if (rankings[1].nonMatchCount != 0 |
| 5168 |
&& rankings[1].unknownWordCount != 0) |
| 5169 |
{ |
| 5170 |
try |
| 5171 |
{ |
| 5172 |
/* |
| 5173 |
* complain about the unknown word and look for an |
| 5174 |
* OOPS reply |
| 5175 |
*/ |
| 5176 |
tryOops(toks, issuingActor_, targetActor_, |
| 5177 |
1, toks, rmcDisambig); |
| 5178 |
} |
| 5179 |
catch (RetryCommandTokensException exc) |
| 5180 |
{ |
| 5181 |
/* get the new token list */ |
| 5182 |
toks = exc.newTokens_; |
| 5183 |
|
| 5184 |
/* replace the string as well */ |
| 5185 |
str = cmdTokenizer.buildOrigText(toks); |
| 5186 |
|
| 5187 |
/* go back for another try at parsing the response */ |
| 5188 |
goto retryParse; |
| 5189 |
} |
| 5190 |
} |
| 5191 |
|
| 5192 |
/* |
| 5193 |
* If the best item we could find has no matches, check to |
| 5194 |
* see if it has miscellaneous noun phrases - if so, it's |
| 5195 |
* probably just a new command, since it doesn't have |
| 5196 |
* anything we recognize as a noun phrase. |
| 5197 |
*/ |
| 5198 |
if (rankings[1].nonMatchCount != 0 |
| 5199 |
&& rankings[1].miscWordListCount != 0) |
| 5200 |
{ |
| 5201 |
/* |
| 5202 |
* it's probably not an answer to our disambiguation |
| 5203 |
* question, so treat it as a whole new command - |
| 5204 |
* abandon the current command and start over with the |
| 5205 |
* new string |
| 5206 |
*/ |
| 5207 |
throw new ReplacementCommandStringException(str, nil, nil); |
| 5208 |
} |
| 5209 |
|
| 5210 |
/* if we're in debug mode, show the winning intepretation */ |
| 5211 |
dbgShowGrammarWithCaption('Disambig Winner', rankings[1].match); |
| 5212 |
|
| 5213 |
/* get the response list */ |
| 5214 |
local respList = rankings[1].match.getResponseList(); |
| 5215 |
|
| 5216 |
/* |
| 5217 |
* Select the objects for each response in our winning list. |
| 5218 |
* The user can select more than one of the objects we |
| 5219 |
* offered, so simply take each one they specify here |
| 5220 |
* separately. |
| 5221 |
*/ |
| 5222 |
foreach (local resp in respList) |
| 5223 |
{ |
| 5224 |
try |
| 5225 |
{ |
| 5226 |
try |
| 5227 |
{ |
| 5228 |
/* |
| 5229 |
* select the objects for this match, and add |
| 5230 |
* them into the matches so far |
| 5231 |
*/ |
| 5232 |
local newObjs = resp.resolveNouns( |
| 5233 |
disResolver, disambigResults); |
| 5234 |
|
| 5235 |
/* set the new token list in the new matches */ |
| 5236 |
for (local n in newObjs) |
| 5237 |
{ |
| 5238 |
if (n.np_ != nil) |
| 5239 |
n.np_.setOrigTokenList(npToks); |
| 5240 |
} |
| 5241 |
|
| 5242 |
/* add them to the matches so far */ |
| 5243 |
resultList += newObjs; |
| 5244 |
} |
| 5245 |
catch (UnmatchedDisambigException udExc) |
| 5246 |
{ |
| 5247 |
/* |
| 5248 |
* The response didn't match anything in the |
| 5249 |
* narrowed list. Try again with the full scope |
| 5250 |
* list, in case they actually wanted to apply |
| 5251 |
* the command to something that's in scope but |
| 5252 |
* which didn't make the cut for the narrowed |
| 5253 |
* list we originally offered. |
| 5254 |
*/ |
| 5255 |
resultList += |
| 5256 |
resp.resolveNouns(scopeDisResolver, |
| 5257 |
disambigResults); |
| 5258 |
} |
| 5259 |
} |
| 5260 |
catch (StillAmbiguousException saExc) |
| 5261 |
{ |
| 5262 |
/* |
| 5263 |
* Get the new "reduced" list from the exception. |
| 5264 |
* Use our original full list, and reduce it to |
| 5265 |
* include only the elements in the reduced list - |
| 5266 |
* this will ensure that the items in the new list |
| 5267 |
* are in the same order as they were in the |
| 5268 |
* original list, which keeps the next iteration's |
| 5269 |
* question in the same order. |
| 5270 |
*/ |
| 5271 |
local newList = new Vector(saExc.matchList_.length()); |
| 5272 |
foreach (local cur in fullMatchList) |
| 5273 |
{ |
| 5274 |
/* |
| 5275 |
* If this item from the original list has an |
| 5276 |
* equivalent in the reduced list, include it in |
| 5277 |
* the new match list. |
| 5278 |
*/ |
| 5279 |
if (cur.isDistEquivInList(saExc.matchList_, dist)) |
| 5280 |
newList.append(cur); |
| 5281 |
} |
| 5282 |
|
| 5283 |
/* convert it to a list */ |
| 5284 |
newList = newList.toList(); |
| 5285 |
|
| 5286 |
/* |
| 5287 |
* If that left us with nothing, just keep the |
| 5288 |
* original list unchanged. This can occasionally |
| 5289 |
* happen, such as when a sub-phrase (a locational |
| 5290 |
* qualifier, for example) is itself ambiguous. |
| 5291 |
*/ |
| 5292 |
if (newList == []) |
| 5293 |
newList = matchList; |
| 5294 |
|
| 5295 |
/* |
| 5296 |
* Generate the new "full" list. This is a list of |
| 5297 |
* all of the items from our current "full" list |
| 5298 |
* that either are directly in the new match list, |
| 5299 |
* or are indistinguishable from items in the new |
| 5300 |
* reduced list. |
| 5301 |
* |
| 5302 |
* The exception thrower is not capable of providing |
| 5303 |
* us with a new full list, because it only knows |
| 5304 |
* about the reduced list, as the reduced list is |
| 5305 |
* its scope for narrowing the results. |
| 5306 |
*/ |
| 5307 |
local newFullList = new Vector(fullMatchList.length()); |
| 5308 |
foreach (local cur in fullMatchList) |
| 5309 |
{ |
| 5310 |
/* |
| 5311 |
* if this item is in the new reduced list, or |
| 5312 |
* has an equivalent in the new match list, |
| 5313 |
* include it in the new full list |
| 5314 |
*/ |
| 5315 |
if (cur.isDistEquivInList(newList, dist)) |
| 5316 |
{ |
| 5317 |
/* |
| 5318 |
* we have this item or something |
| 5319 |
* indistinguishable - include it in the |
| 5320 |
* revised full match list |
| 5321 |
*/ |
| 5322 |
newFullList.append(cur); |
| 5323 |
} |
| 5324 |
} |
| 5325 |
|
| 5326 |
/* convert it to a list */ |
| 5327 |
newFullList = newFullList.toList(); |
| 5328 |
|
| 5329 |
/* |
| 5330 |
* They answered, but with insufficient specificity. |
| 5331 |
* Add the given list to the still-to-resolve list, |
| 5332 |
* so that we try again with this new list. |
| 5333 |
*/ |
| 5334 |
stillToResolve += |
| 5335 |
new StillToResolveItem(newList, newFullList, |
| 5336 |
saExc.origText_); |
| 5337 |
} |
| 5338 |
catch (DisambigOrdinalOutOfRangeException oorExc) |
| 5339 |
{ |
| 5340 |
/* |
| 5341 |
* Explain the problem (note that if we didn't want |
| 5342 |
* to offer another chance here, we could simply |
| 5343 |
* throw this message as a ParseFailureException |
| 5344 |
* instead of continuing with the query loop). |
| 5345 |
* |
| 5346 |
* If we've never asked interactively for input |
| 5347 |
* (because we've obtained all of our input from |
| 5348 |
* past responses to a command we're repeating), |
| 5349 |
* don't show this message, because it makes no |
| 5350 |
* sense when they haven't answered any questions in |
| 5351 |
* the first place. |
| 5352 |
*/ |
| 5353 |
if (everAsked) |
| 5354 |
targetActor_.getParserMessageObj(). |
| 5355 |
disambigOrdinalOutOfRange( |
| 5356 |
targetActor_, oorExc.ord_, txt.htmlify()); |
| 5357 |
|
| 5358 |
/* go back to the outer loop to ask for a new response */ |
| 5359 |
askingAgain = true; |
| 5360 |
continue queryLoop; |
| 5361 |
} |
| 5362 |
catch (UnmatchedDisambigException udExc) |
| 5363 |
{ |
| 5364 |
local newList; |
| 5365 |
|
| 5366 |
/* |
| 5367 |
* They entered something that looked like a |
| 5368 |
* disambiguation response, but didn't refer to any |
| 5369 |
* of our objects. Try parsing the input as though |
| 5370 |
* it were a new command, and if it matches any |
| 5371 |
* command syntax, treat it as a new command. |
| 5372 |
*/ |
| 5373 |
newList = firstCommandPhrase.parseTokens(toks, cmdDict); |
| 5374 |
if (newList.length() != 0) |
| 5375 |
{ |
| 5376 |
/* |
| 5377 |
* it appears syntactically to be a new command |
| 5378 |
* - treat it as such by throwing a command |
| 5379 |
* replacement exception |
| 5380 |
*/ |
| 5381 |
throw new ReplacementCommandStringException( |
| 5382 |
str, nil, nil); |
| 5383 |
} |
| 5384 |
|
| 5385 |
/* |
| 5386 |
* Explain the problem (note that if we didn't want |
| 5387 |
* to continue with the query loop, we could simply |
| 5388 |
* throw this message as a ParseFailureException). |
| 5389 |
* |
| 5390 |
* Don't show any error if we've never asked |
| 5391 |
* interactively on this turn (which can only be |
| 5392 |
* because we're using interactive responses from a |
| 5393 |
* previous turn that we're repeating), because it |
| 5394 |
* makes no sense when we haven't apparently asked |
| 5395 |
* for anything yet. |
| 5396 |
*/ |
| 5397 |
if (everAsked) |
| 5398 |
targetActor_.getParserMessageObj(). |
| 5399 |
noMatchDisambig(targetActor_, txt.htmlify(), |
| 5400 |
udExc.resp_); |
| 5401 |
|
| 5402 |
/* go back to the outer loop to ask for a new response */ |
| 5403 |
askingAgain = true; |
| 5404 |
continue queryLoop; |
| 5405 |
} |
| 5406 |
} |
| 5407 |
|
| 5408 |
/* |
| 5409 |
* If we got this far, the last input we parsed was actually |
| 5410 |
* a response to our question, as opposed to a new command |
| 5411 |
* or something unintelligible. |
| 5412 |
* |
| 5413 |
* If this was an interactive response, add the response to |
| 5414 |
* the response keeper's list of past responses. This will |
| 5415 |
* allow the production to re-use the same list if it has to |
| 5416 |
* re-resolve the phrase in the future, without asking the |
| 5417 |
* user to answer the same questions again |
| 5418 |
*/ |
| 5419 |
if (everAsked) |
| 5420 |
keeper.addAmbigResponse(str); |
| 5421 |
|
| 5422 |
/* |
| 5423 |
* if there's nothing left in the still-to-resolve list, we |
| 5424 |
* have nothing left to do, so we can stop working |
| 5425 |
*/ |
| 5426 |
if (stillToResolve.length() == 0) |
| 5427 |
break; |
| 5428 |
|
| 5429 |
/* |
| 5430 |
* set up for the next iteration with the first item in the |
| 5431 |
* still-to-resolve list |
| 5432 |
*/ |
| 5433 |
matchList = stillToResolve[1].matchList; |
| 5434 |
fullMatchList = stillToResolve[1].fullMatchList; |
| 5435 |
txt = stillToResolve[1].origText; |
| 5436 |
|
| 5437 |
/* remove the first item, since we're going to process it now */ |
| 5438 |
stillToResolve = stillToResolve.sublist(2); |
| 5439 |
} |
| 5440 |
|
| 5441 |
/* success - return the final match list */ |
| 5442 |
return resultList; |
| 5443 |
} |
| 5444 |
|
| 5445 |
/* |
| 5446 |
* filter a match list with a specific Distinguisher |
| 5447 |
*/ |
| 5448 |
filterWithDistinguisher(lst, dist) |
| 5449 |
{ |
| 5450 |
local result; |
| 5451 |
|
| 5452 |
/* create a vector for the result list */ |
| 5453 |
result = new Vector(lst.length()); |
| 5454 |
|
| 5455 |
/* scan the list */ |
| 5456 |
foreach (local cur in lst) |
| 5457 |
{ |
| 5458 |
/* |
| 5459 |
* if we have no equivalent of the current item (for the |
| 5460 |
* purposes of our Distinguisher) in the result list, add |
| 5461 |
* the current item to the result list |
| 5462 |
*/ |
| 5463 |
if (!cur.isDistEquivInList(result, dist)) |
| 5464 |
result.append(cur); |
| 5465 |
} |
| 5466 |
|
| 5467 |
/* return the result list */ |
| 5468 |
return result.toList(); |
| 5469 |
} |
| 5470 |
|
| 5471 |
/* |
| 5472 |
* handle a noun phrase that doesn't match any legal grammar rules |
| 5473 |
* for noun phrases |
| 5474 |
*/ |
| 5475 |
unknownNounPhrase(match, resolver) |
| 5476 |
{ |
| 5477 |
local wordList; |
| 5478 |
local ret; |
| 5479 |
|
| 5480 |
/* |
| 5481 |
* ask the resolver to handle it - if it gives us a resolved |
| 5482 |
* object list, simply return it |
| 5483 |
*/ |
| 5484 |
wordList = match.getOrigTokenList(); |
| 5485 |
if ((ret = resolver.resolveUnknownNounPhrase(wordList)) != nil) |
| 5486 |
return ret; |
| 5487 |
|
| 5488 |
/* |
| 5489 |
* The resolver doesn't handle unknown words, so we can't |
| 5490 |
* resolve the phrase. Look for an undefined word and give the |
| 5491 |
* player a chance to correct typos with OOPS. |
| 5492 |
*/ |
| 5493 |
tryOops(wordList, issuingActor_, targetActor_, |
| 5494 |
match.firstTokenIndex, match.tokenList, rmcCommand); |
| 5495 |
|
| 5496 |
/* |
| 5497 |
* If we didn't find any unknown words, it means that they used |
| 5498 |
* a word that's in the dictionary in a way that makes no sense |
| 5499 |
* to us. Simply return an empty list and let the resolver |
| 5500 |
* proceed with its normal handling for unmatched noun phrases. |
| 5501 |
*/ |
| 5502 |
return []; |
| 5503 |
} |
| 5504 |
|
| 5505 |
getImpliedObject(np, resolver) |
| 5506 |
{ |
| 5507 |
/* ask the resolver to supply an implied default object */ |
| 5508 |
return resolver.getDefaultObject(np); |
| 5509 |
} |
| 5510 |
|
| 5511 |
askMissingObject(asker, resolver, responseProd) |
| 5512 |
{ |
| 5513 |
/* if we can't resolve this interactively, fail with an error */ |
| 5514 |
if (!canResolveInteractively(resolver.getAction())) |
| 5515 |
{ |
| 5516 |
/* interactive resolution isn't allowed - fail */ |
| 5517 |
throw new ParseFailureException(&missingObject, |
| 5518 |
resolver.getAction(), |
| 5519 |
resolver.whichMessageObject); |
| 5520 |
} |
| 5521 |
|
| 5522 |
/* have the action show objects already defaulted */ |
| 5523 |
resolver.getAction().announceAllDefaultObjects(nil); |
| 5524 |
|
| 5525 |
/* ask for a default object */ |
| 5526 |
asker.askMissingObject(targetActor_, resolver.getAction(), |
| 5527 |
resolver.whichMessageObject); |
| 5528 |
|
| 5529 |
/* try reading an object response */ |
| 5530 |
return tryAskingForObject(issuingActor_, targetActor_, |
| 5531 |
resolver, self, responseProd); |
| 5532 |
} |
| 5533 |
|
| 5534 |
noteLiteral(txt) |
| 5535 |
{ |
| 5536 |
/* |
| 5537 |
* there's nothing to do with a literal at this point, since |
| 5538 |
* we're not ranking anything |
| 5539 |
*/ |
| 5540 |
} |
| 5541 |
|
| 5542 |
askMissingLiteral(action, which) |
| 5543 |
{ |
| 5544 |
local ret; |
| 5545 |
|
| 5546 |
/* if we can't resolve this interactively, fail with an error */ |
| 5547 |
if (!canResolveInteractively(action)) |
| 5548 |
{ |
| 5549 |
/* interactive resolution isn't allowed - fail */ |
| 5550 |
throw new ParseFailureException(&missingLiteral, action, which); |
| 5551 |
} |
| 5552 |
|
| 5553 |
/* ask for the missing literal */ |
| 5554 |
targetActor_.getParserMessageObj(). |
| 5555 |
askMissingLiteral(targetActor_, action, which); |
| 5556 |
|
| 5557 |
/* read the response */ |
| 5558 |
ret = readMainCommand(rmcAskLiteral); |
| 5559 |
|
| 5560 |
/* re-enable the transcript, if we have one */ |
| 5561 |
if (gTranscript) |
| 5562 |
gTranscript.activate(); |
| 5563 |
|
| 5564 |
/* return the response */ |
| 5565 |
return ret; |
| 5566 |
} |
| 5567 |
|
| 5568 |
emptyNounPhrase(resolver) |
| 5569 |
{ |
| 5570 |
/* abort with an error */ |
| 5571 |
throw new ParseFailureException(&emptyNounPhrase); |
| 5572 |
} |
| 5573 |
|
| 5574 |
zeroQuantity(txt) |
| 5575 |
{ |
| 5576 |
/* abort with an error */ |
| 5577 |
throw new ParseFailureException(&zeroQuantity, |
| 5578 |
txt.toLower().htmlify()); |
| 5579 |
} |
| 5580 |
|
| 5581 |
insufficientQuantity(txt, matchList, requiredNum) |
| 5582 |
{ |
| 5583 |
/* abort with an error */ |
| 5584 |
throw new ParseFailureException( |
| 5585 |
&insufficientQuantity, txt.toLower().htmlify(), |
| 5586 |
matchList, requiredNum); |
| 5587 |
} |
| 5588 |
|
| 5589 |
uniqueObjectRequired(txt, matchList) |
| 5590 |
{ |
| 5591 |
/* abort with an error */ |
| 5592 |
throw new ParseFailureException( |
| 5593 |
&uniqueObjectRequired, txt.toLower().htmlify(), matchList); |
| 5594 |
} |
| 5595 |
|
| 5596 |
singleObjectRequired(txt) |
| 5597 |
{ |
| 5598 |
/* abort with an error */ |
| 5599 |
throw new ParseFailureException( |
| 5600 |
&singleObjectRequired, txt.toLower().htmlify()); |
| 5601 |
} |
| 5602 |
|
| 5603 |
noteAdjEnding() |
| 5604 |
{ |
| 5605 |
/* we don't care about adjective-ending noun phrases at this point */ |
| 5606 |
} |
| 5607 |
|
| 5608 |
noteIndefinite() |
| 5609 |
{ |
| 5610 |
/* we don't care about indefinites at this point */ |
| 5611 |
} |
| 5612 |
|
| 5613 |
noteMiscWordList(txt) |
| 5614 |
{ |
| 5615 |
/* we don't care about unstructured noun phrases at this point */ |
| 5616 |
} |
| 5617 |
|
| 5618 |
notePronoun() |
| 5619 |
{ |
| 5620 |
/* we don't care about pronouns right now */ |
| 5621 |
} |
| 5622 |
|
| 5623 |
noteMatches(matchList) |
| 5624 |
{ |
| 5625 |
/* we don't care about the matches just now */ |
| 5626 |
} |
| 5627 |
|
| 5628 |
notePlural() |
| 5629 |
{ |
| 5630 |
/* we don't care about these right now */ |
| 5631 |
} |
| 5632 |
|
| 5633 |
beginSingleObjSlot() { } |
| 5634 |
endSingleObjSlot() { } |
| 5635 |
|
| 5636 |
beginTopicSlot() { } |
| 5637 |
endTopicSlot() { } |
| 5638 |
|
| 5639 |
incCommandCount() |
| 5640 |
{ |
| 5641 |
/* we don't care about how many subcommands there are */ |
| 5642 |
} |
| 5643 |
|
| 5644 |
noteActorSpecified() |
| 5645 |
{ |
| 5646 |
/* |
| 5647 |
* we don't care about this during execution - it only matters |
| 5648 |
* for determining the strength of the command during the |
| 5649 |
* ranking process |
| 5650 |
*/ |
| 5651 |
} |
| 5652 |
|
| 5653 |
noteNounSlots(cnt) |
| 5654 |
{ |
| 5655 |
/* |
| 5656 |
* we don't care about this during execution; it only matters |
| 5657 |
* for the ranking process |
| 5658 |
*/ |
| 5659 |
} |
| 5660 |
|
| 5661 |
noteWeakPhrasing(level) |
| 5662 |
{ |
| 5663 |
/* ignore this during execution; it only matters during ranking */ |
| 5664 |
} |
| 5665 |
|
| 5666 |
/* allow remapping the action */ |
| 5667 |
allowActionRemapping = true |
| 5668 |
|
| 5669 |
/* allow making an arbitrary choice among equivalents */ |
| 5670 |
allowEquivalentFiltering = true |
| 5671 |
; |
| 5672 |
|
| 5673 |
|
| 5674 |
/* |
| 5675 |
* List entry for the still-to-resolve list |
| 5676 |
*/ |
| 5677 |
class StillToResolveItem: object |
| 5678 |
construct(lst, fullList, txt) |
| 5679 |
{ |
| 5680 |
/* remember the equivalent-reduced and full match lists */ |
| 5681 |
matchList = lst; |
| 5682 |
fullMatchList = fullList; |
| 5683 |
|
| 5684 |
/* note the text */ |
| 5685 |
origText = txt; |
| 5686 |
} |
| 5687 |
|
| 5688 |
/* the reduced (equivalent-eliminated) match list */ |
| 5689 |
matchList = [] |
| 5690 |
|
| 5691 |
/* full (equivalent-inclusive) match list */ |
| 5692 |
fullMatchList = [] |
| 5693 |
|
| 5694 |
/* the original command text being disambiguated */ |
| 5695 |
origText = '' |
| 5696 |
; |
| 5697 |
|
| 5698 |
/* ------------------------------------------------------------------------ */ |
| 5699 |
/* |
| 5700 |
* Specialized noun-phrase resolution results gatherer for resolving a |
| 5701 |
* command actor (i.e., the target actor of a command). |
| 5702 |
*/ |
| 5703 |
class ActorResolveResults: BasicResolveResults |
| 5704 |
construct() |
| 5705 |
{ |
| 5706 |
/* do the inherited work */ |
| 5707 |
inherited(); |
| 5708 |
|
| 5709 |
/* |
| 5710 |
* set the initial actor context to the PC - this type of |
| 5711 |
* resolver is set up to determine the actor context, so we don't |
| 5712 |
* usually know the actual actor context yet when setting up this |
| 5713 |
* resolver |
| 5714 |
*/ |
| 5715 |
targetActor_ = issuingActor_ = gPlayerChar; |
| 5716 |
} |
| 5717 |
|
| 5718 |
getImpliedObject(np, resolver) |
| 5719 |
{ |
| 5720 |
/* |
| 5721 |
* there's no default for the actor - it's usually simply a |
| 5722 |
* syntax error when the actor is omitted |
| 5723 |
*/ |
| 5724 |
throw new ParseFailureException(&missingActor); |
| 5725 |
} |
| 5726 |
|
| 5727 |
uniqueObjectRequired(txt, matchList) |
| 5728 |
{ |
| 5729 |
/* an actor phrase must address a single actor */ |
| 5730 |
throw new ParseFailureException(&singleActorRequired); |
| 5731 |
} |
| 5732 |
|
| 5733 |
singleObjectRequired(txt) |
| 5734 |
{ |
| 5735 |
/* an actor phrase must address a single actor */ |
| 5736 |
throw new ParseFailureException(&singleActorRequired); |
| 5737 |
} |
| 5738 |
|
| 5739 |
/* don't allow action remapping while resolving the actor */ |
| 5740 |
allowActionRemapping = nil |
| 5741 |
; |
| 5742 |
|
| 5743 |
/* |
| 5744 |
* A results object for resolving an actor in a command with an unknown |
| 5745 |
* word or invalid phrasing in the predicate. For this type of |
| 5746 |
* resolution, we're trying to interpret the actor portion of the command |
| 5747 |
* as a noun phrase referring to an actor, but it could also just be |
| 5748 |
* another command. E.g., we could have "bob, asdf" or "east, asdf". |
| 5749 |
* Since we're only tentatively interpreting the phrase as a noun phrase, |
| 5750 |
* to see if that interpretation goes anywhere, we don't want to throw |
| 5751 |
* any errors on failures; instead we simply allow empty match lists. |
| 5752 |
*/ |
| 5753 |
class TryAsActorResolveResults: ResolveResults |
| 5754 |
noVocabMatch(action, txt) { } |
| 5755 |
noMatch(action, txt) { } |
| 5756 |
noMatchPossessive(action, txt) { } |
| 5757 |
noMatchForAll() { } |
| 5758 |
noMatchForAllBut() { } |
| 5759 |
noMatchForListBut() { } |
| 5760 |
noteEmptyBut() { } |
| 5761 |
noMatchForPronoun() { } |
| 5762 |
allNotAllowed() { } |
| 5763 |
reflexiveNotAllowed() { } |
| 5764 |
wrongReflexive(typ, txt) { } |
| 5765 |
noMatchForPossessive(owner, txt) { } |
| 5766 |
noMatchForLocation(loc, txt) { } |
| 5767 |
noteBadPrep() { } |
| 5768 |
nothingInLocation(loc) { } |
| 5769 |
ambiguousNounPhrase(keeper, askwer, txt, matchLst, fullMatchLst, |
| 5770 |
scopeList, requiredNum, resolver) { } |
| 5771 |
unknownNounPhrase(match, resolver) { } |
| 5772 |
getImpliedObject(np, resolver) { return []; } |
| 5773 |
askMissingObject(asker, resolver, responseProd) { return []; } |
| 5774 |
noteLiteral(txt) { } |
| 5775 |
askMissingLiteral(action, which) { return nil; } |
| 5776 |
emptyNounPhrase(resolver) { } |
| 5777 |
zeroQuantity(txt) { } |
| 5778 |
insufficientQuantity(txt, matchList, requiredNum) { } |
| 5779 |
uniqueObjectRequired(txt, matchList) { } |
| 5780 |
singleObjectRequired(txt) { } |
| 5781 |
noteAdjEnding() { } |
| 5782 |
noteIndefinite() { } |
| 5783 |
noteMatches(matchList) { } |
| 5784 |
noteMiscWord(txt) { } |
| 5785 |
notePronoun() { } |
| 5786 |
notePlural() { } |
| 5787 |
beginSingleObjSlot() { } |
| 5788 |
endSingleObjSlot() { } |
| 5789 |
beginTopicSlot() { } |
| 5790 |
endTopicSlot() { } |
| 5791 |
incCommandCount() { } |
| 5792 |
noteActorSpecified() { } |
| 5793 |
noteNounSlots(cnt) { } |
| 5794 |
noteWeakPhrasing() { } |
| 5795 |
allowActionRemapping = true |
| 5796 |
allowEquivalentFiltering = true |
| 5797 |
; |
| 5798 |
|
| 5799 |
|
| 5800 |
/* ------------------------------------------------------------------------ */ |
| 5801 |
/* |
| 5802 |
* Command ranking criterion. This is used by the CommandRanking class |
| 5803 |
* to represent one criterion for comparing two parse trees. |
| 5804 |
* |
| 5805 |
* Rankings are performed in two passes. The first pass is the rough, |
| 5806 |
* qualitative pass, meant to determine if one parse tree has big, |
| 5807 |
* obvious differences from another. In most cases, this means that one |
| 5808 |
* tree has a particular type of problem or special advantage that the |
| 5809 |
* other doesn't have at all. |
| 5810 |
* |
| 5811 |
* The second pass is the fine-grained pass. We only reach the second |
| 5812 |
* pass if we can't find any coarse differences on the first rough pass. |
| 5813 |
* In most cases, the second pass compares the magnitude of problems or |
| 5814 |
* advantages to determine if one tree is slightly better than the other. |
| 5815 |
*/ |
| 5816 |
class CommandRankingCriterion: object |
| 5817 |
/* |
| 5818 |
* Compare two CommandRanking objects on the basis of this criterion, |
| 5819 |
* for the first, coarse-grained pass. Returns a positive number if |
| 5820 |
* a is better than b, 0 if they're indistinguishable, or -1 if a is |
| 5821 |
* worse than b. |
| 5822 |
*/ |
| 5823 |
comparePass1(a, b) { return 0; } |
| 5824 |
|
| 5825 |
/* compare two rankings for the second, fine-grained pass */ |
| 5826 |
comparePass2(a, b) { return 0; } |
| 5827 |
; |
| 5828 |
|
| 5829 |
/* |
| 5830 |
* A command ranking criterion that measures a "problem" by a count of |
| 5831 |
* occurrences stored in a property of the CommandRanking object. For |
| 5832 |
* example, we could count the number of noun phrases that don't resolve |
| 5833 |
* to any objects. |
| 5834 |
* |
| 5835 |
* On the first, coarse-grained pass, we measure only the presence or |
| 5836 |
* absence of our problem. That is, if one parse tree has zero |
| 5837 |
* occurrences of the problem and the other has a non-zero number of |
| 5838 |
* occurrences of the problem (as measured by our counting property), |
| 5839 |
* then we'll prefer the one with zero occurrences. If both have no |
| 5840 |
* occurrences, or both have a non-zero number of occurrences, we'll |
| 5841 |
* consider the two equivalent for the first pass, since we only care |
| 5842 |
* about the presence or absence of the problem. |
| 5843 |
* |
| 5844 |
* On the second, fine-grained pass, we measure the actual number of |
| 5845 |
* occurrences of the problem, and choose the parse tree with the lower |
| 5846 |
* number. |
| 5847 |
*/ |
| 5848 |
class CommandRankingByProblem: CommandRankingCriterion |
| 5849 |
/* |
| 5850 |
* our ranking property - this is a property of the CommandRanking |
| 5851 |
* object that gives us a count of the number of times our "problem" |
| 5852 |
* has occurred in the ranking object's parse tree |
| 5853 |
*/ |
| 5854 |
prop_ = nil |
| 5855 |
|
| 5856 |
/* first pass - compare by presence or absence of the problem */ |
| 5857 |
comparePass1(a, b) |
| 5858 |
{ |
| 5859 |
local acnt = a.(self.prop_); |
| 5860 |
local bcnt = b.(self.prop_); |
| 5861 |
|
| 5862 |
/* if b has the problem but a doesn't, a is better */ |
| 5863 |
if (acnt == 0 && bcnt != 0) |
| 5864 |
return 1; |
| 5865 |
|
| 5866 |
/* if a has the problem but b doesn't, b is better */ |
| 5867 |
if (acnt != 0 && bcnt == 0) |
| 5868 |
return -1; |
| 5869 |
|
| 5870 |
/* we can't tell the difference at this stage */ |
| 5871 |
return 0; |
| 5872 |
} |
| 5873 |
|
| 5874 |
/* second pass - compare by number of occurrences of the problem */ |
| 5875 |
comparePass2(a, b) |
| 5876 |
{ |
| 5877 |
/* |
| 5878 |
* Return the difference in the problem counts. We want to |
| 5879 |
* return >0 if a has fewer problems, <0 if b has fewer problems: |
| 5880 |
* so compute (a-b) and negate it, which is the same as computing |
| 5881 |
* (a-b). |
| 5882 |
*/ |
| 5883 |
return b.(self.prop_) - a.(self.prop_); |
| 5884 |
} |
| 5885 |
; |
| 5886 |
|
| 5887 |
/* |
| 5888 |
* A "weakness" criterion. This is similar to the rank-by-problem |
| 5889 |
* criterion, but rather than ranking on an actual structural problem, it |
| 5890 |
* ranks on a structural weakness. This is suitable for things like |
| 5891 |
* adjective endings and truncations, where the weakness isn't on the |
| 5892 |
* same order as a "problem" but where we'd still rather avoid the |
| 5893 |
* weakness if we can. |
| 5894 |
* |
| 5895 |
* The point of the separate "weakness" criterion is that we only allow |
| 5896 |
* weaknesses to come into play on pass 2, after we've already |
| 5897 |
* discriminated based on problems. If we can discriminate based on |
| 5898 |
* problems, we'll do so in pass 1 and won't even get to pass 2; we'll |
| 5899 |
* only discriminate based on weakness if we can't tell the difference |
| 5900 |
* based on real problems. |
| 5901 |
*/ |
| 5902 |
class CommandRankingByWeakness: CommandRankingCriterion |
| 5903 |
/* on pass 1, ignore weaknesses */ |
| 5904 |
comparePass1(a, b) { return 0; } |
| 5905 |
|
| 5906 |
/* on pass 2, compare based on weaknesses */ |
| 5907 |
comparePass2(a, b) { return b.(self.prop_) - a.(self.prop_); } |
| 5908 |
|
| 5909 |
/* our command-ranking property */ |
| 5910 |
prop_ = nil |
| 5911 |
; |
| 5912 |
|
| 5913 |
/* |
| 5914 |
* command-ranking-by-problem and by-weakness objects for the pre-defined |
| 5915 |
* ranking criteria |
| 5916 |
*/ |
| 5917 |
rankByVocabNonMatch: CommandRankingByProblem prop_ = &vocabNonMatchCount; |
| 5918 |
rankByNonMatch: CommandRankingByProblem prop_ = &nonMatchCount; |
| 5919 |
rankByInsufficient: CommandRankingByProblem prop_ = &insufficientCount; |
| 5920 |
rankByListForSingle: CommandRankingByProblem prop_ = &listForSingle; |
| 5921 |
rankByEmptyBut: CommandRankingByProblem prop_ = &emptyButCount; |
| 5922 |
rankByAllExcluded: CommandRankingByProblem prop_ = &allExcludedCount; |
| 5923 |
rankByActorSpecified: CommandRankingByProblem prop_ = &actorSpecifiedCount; |
| 5924 |
rankByMiscWordList: CommandRankingByProblem prop_ = &miscWordListCount; |
| 5925 |
rankByPluralTrunc: CommandRankingByWeakness prop_ = &pluralTruncCount; |
| 5926 |
rankByEndAdj: CommandRankingByWeakness prop_ = &endAdjCount; |
| 5927 |
rankByIndefinite: CommandRankingByProblem prop_ = &indefiniteCount; |
| 5928 |
rankByTrunc: CommandRankingByWeakness prop_ = &truncCount; |
| 5929 |
rankByMissing: CommandRankingByProblem prop_ = &missingCount; |
| 5930 |
rankByPronoun: CommandRankingByWeakness prop_ = &pronounCount; |
| 5931 |
rankByWeakness: CommandRankingByWeakness prop_ = &weaknessLevel; |
| 5932 |
rankByUnwantedPlural: CommandRankingByProblem prop_ = &unwantedPluralCount; |
| 5933 |
|
| 5934 |
/* |
| 5935 |
* Rank by unmatched possessive-qualified phrases. If we have two |
| 5936 |
* unknown phrases, one with a possessive qualifier and one without, and |
| 5937 |
* other things being equal, prefer the one with the possessive |
| 5938 |
* qualifier. |
| 5939 |
* |
| 5940 |
* We prefer the qualified version because it lets us report a smaller |
| 5941 |
* phrase that we can't match. For example, in X BOB'S WALLET, if we |
| 5942 |
* can't match WALLET all by itself, it's more useful to report that "you |
| 5943 |
* see no wallet" than to report that you see no "bob's wallet", because |
| 5944 |
* the latter incorrectly implies that there might still be a wallet in |
| 5945 |
* scope as long as it's not Bob's we're looking for. |
| 5946 |
*/ |
| 5947 |
rankByNonMatchPoss: CommandRankingCriterion |
| 5948 |
/* |
| 5949 |
* ignore on pass 1 - this only counts if other factors are equal, so |
| 5950 |
* we want to consider all of the other factors on pass 1 before |
| 5951 |
* taking this criterion into account |
| 5952 |
*/ |
| 5953 |
comparePass1(a, b) { return 0; } |
| 5954 |
|
| 5955 |
/* pass 2 - more possessives are better */ |
| 5956 |
comparePass2(a, b) |
| 5957 |
{ |
| 5958 |
/* |
| 5959 |
* if we don't have the same underlying non-match count, |
| 5960 |
* possessive qualification is irrelevant |
| 5961 |
*/ |
| 5962 |
if (a.nonMatchCount != b.nonMatchCount) |
| 5963 |
return 0; |
| 5964 |
|
| 5965 |
/* more possessives are better */ |
| 5966 |
return a.nonMatchPossCount - b.nonMatchPossCount; |
| 5967 |
} |
| 5968 |
; |
| 5969 |
|
| 5970 |
/* |
| 5971 |
* Command ranking by literal phrase length. We prefer interpretations |
| 5972 |
* that treat less text as uninterpreted literal text. By "less text," |
| 5973 |
* we simply mean that one has a shorter string treated as literal text |
| 5974 |
* than the other. (We prefer shorter literals because when the parser |
| 5975 |
* matches a string of literal text, it's essentially throwing up its |
| 5976 |
* hands and admitting it can't parse the text; so the less text is |
| 5977 |
* contained in literals, the more text the parser is actually parsing, |
| 5978 |
* and more parsed is better.) |
| 5979 |
*/ |
| 5980 |
rankByLiteralLength: CommandRankingCriterion |
| 5981 |
/* first pass */ |
| 5982 |
comparePass1(a, b) |
| 5983 |
{ |
| 5984 |
/* |
| 5985 |
* Compare our lengths. We want to return >0 if a is shorter and |
| 5986 |
* <0 if a is longer (and, of course, 0 if they're the same |
| 5987 |
* length). So, we can just compute (a-b) and negate the result, |
| 5988 |
* which is the same as computing (b-a). |
| 5989 |
* |
| 5990 |
* The CommandRanking objects keep track of the length of text in |
| 5991 |
* literals in their literalLength properties. |
| 5992 |
*/ |
| 5993 |
return b.literalLength - a.literalLength; |
| 5994 |
} |
| 5995 |
|
| 5996 |
/* |
| 5997 |
* Second pass - we use our full powers of discrimination on the |
| 5998 |
* first pass, so if we make it to the second pass, we couldn't tell |
| 5999 |
* a difference on the first pass and thus can't tell a difference |
| 6000 |
* now. So, just inherit the default implementation, which simply |
| 6001 |
* returns 0 to indicate that there's no difference. |
| 6002 |
*/ |
| 6003 |
; |
| 6004 |
|
| 6005 |
/* |
| 6006 |
* Command ranking by subcommand count: we prefer the match with fewer |
| 6007 |
* subcommands. If one has fewer subcommands than the other, it means |
| 6008 |
* that we were able to interpret ambiguous conjunctions (such as "and") |
| 6009 |
* as noun phrase conjunctions rather than as command conjunctions; other |
| 6010 |
* things being equal, we'd rather take the interpretation that gives us |
| 6011 |
* noun phrases than the one that involves more separate commands. |
| 6012 |
*/ |
| 6013 |
rankBySubcommands: CommandRankingCriterion |
| 6014 |
/* first pass - compare subcommand counts */ |
| 6015 |
comparePass1(a, b) |
| 6016 |
{ |
| 6017 |
/* |
| 6018 |
* if a has fewer subcommands, return <0, and if b has fewer |
| 6019 |
* subcommands, return >0: so we can just return the negative of |
| 6020 |
* (a-b), or (b-a) |
| 6021 |
*/ |
| 6022 |
return b.commandCount - a.commandCount; |
| 6023 |
} |
| 6024 |
|
| 6025 |
/* second pass - do nothing, as we do all of our work on the first pass */ |
| 6026 |
; |
| 6027 |
|
| 6028 |
/* |
| 6029 |
* Rank by token count. Other things being equal, we'd rather pick a |
| 6030 |
* longer match. If one match is shorter than the other in terms of the |
| 6031 |
* number of tokens it encompasses, then it means that the shorter match |
| 6032 |
* left more tokens at the end of the command to be interpreted as |
| 6033 |
* separate commands. If we have an interpretation that can take more of |
| 6034 |
* those tokens and parse them as part of the current command, that |
| 6035 |
* interpretation is probably better. |
| 6036 |
*/ |
| 6037 |
rankByTokenCount: CommandRankingCriterion |
| 6038 |
/* first pass - compare token counts */ |
| 6039 |
comparePass1(a, b) |
| 6040 |
{ |
| 6041 |
/* choose the one that matched more tokens */ |
| 6042 |
return a.tokCount - b.tokCount; |
| 6043 |
} |
| 6044 |
|
| 6045 |
/* first pass - we do all our work on the first pass */ |
| 6046 |
; |
| 6047 |
|
| 6048 |
/* |
| 6049 |
* Rank by "verb structure." This gives more weight to an |
| 6050 |
* interpretation that has more structural noun phrases in the verb. |
| 6051 |
* For example, "DETACH dobj FROM iobj" is given more weight than |
| 6052 |
* "DETACH dobj", because the former has two structural noun phrases |
| 6053 |
* whereas the latter has only one. This will make us prefer to treat |
| 6054 |
* DETACH WIRE FROM BOX as a two-object action, for example, even if we |
| 6055 |
* could treat WIRE FROM BOX as a single "locational" noun phrase. |
| 6056 |
*/ |
| 6057 |
rankByVerbStructure: CommandRankingCriterion |
| 6058 |
comparePass2(a, b) |
| 6059 |
{ |
| 6060 |
/* take the one with more structural noun slots in the verb phrase */ |
| 6061 |
return a.nounSlotCount - b.nounSlotCount; |
| 6062 |
} |
| 6063 |
; |
| 6064 |
|
| 6065 |
/* |
| 6066 |
* Rank by ambiguous noun phrases. We apply this criterion on the second |
| 6067 |
* pass only, because it's a weak test: we might end up narrowing things |
| 6068 |
* down through automatic "logicalness" tests during the noun resolution |
| 6069 |
* process, so ambiguity at this stage in the parsing process doesn't |
| 6070 |
* necessarily indicate that there's real ambiguity in the command. |
| 6071 |
* However, if we can already tell that one interpretation is unambiguous |
| 6072 |
* and another is ambiguous, and the two interpretations are otherwise |
| 6073 |
* equally good, pick the one that's already unambiguous: the ambiguous |
| 6074 |
* interpretation might or might not stay ambiguous, but the unambiguous |
| 6075 |
* interpretation will definitely stay unambiguous. |
| 6076 |
*/ |
| 6077 |
rankByAmbiguity: CommandRankingCriterion |
| 6078 |
/* |
| 6079 |
* Do nothing on the first pass, because we want any first-pass |
| 6080 |
* criterion to prevail over our weak test. Instead, check for a |
| 6081 |
* difference in ambiguity only on the second pass. |
| 6082 |
*/ |
| 6083 |
comparePass2(a, b) |
| 6084 |
{ |
| 6085 |
/* the one with lower ambiguity is better */ |
| 6086 |
return b.ambigCount - a.ambigCount; |
| 6087 |
} |
| 6088 |
; |
| 6089 |
|
| 6090 |
|
| 6091 |
/* |
| 6092 |
* Production match ranking object. We create one of these objects for |
| 6093 |
* each match tree that we wish to rank. |
| 6094 |
* |
| 6095 |
* This class is generally not instantiated by client code - instead, |
| 6096 |
* clients use the sortByRanking() class method to rank a list of |
| 6097 |
* production matches. |
| 6098 |
*/ |
| 6099 |
class CommandRanking: ResolveResults |
| 6100 |
/* |
| 6101 |
* Sort a list of productions, as returned from |
| 6102 |
* GrammarProd.parseTokens(), in descending order of command |
| 6103 |
* strength. We return a list of CommandRanking objects whose first |
| 6104 |
* element is the best command interpretation. |
| 6105 |
* |
| 6106 |
* Note that this can be used as a class-level method. |
| 6107 |
*/ |
| 6108 |
sortByRanking(lst, [resolveArguments]) |
| 6109 |
{ |
| 6110 |
/* |
| 6111 |
* create a vector to hold the ranking information - we |
| 6112 |
* need one ranking item per match |
| 6113 |
*/ |
| 6114 |
local rankings = new Vector(lst.length()); |
| 6115 |
|
| 6116 |
/* get the ranking information for each command */ |
| 6117 |
foreach(local cur in lst) |
| 6118 |
{ |
| 6119 |
local curRank; |
| 6120 |
|
| 6121 |
/* create a ranking item for the entry */ |
| 6122 |
curRank = self.createInstance(cur); |
| 6123 |
|
| 6124 |
/* rank this entry */ |
| 6125 |
curRank.calcRanking(resolveArguments); |
| 6126 |
|
| 6127 |
/* add this to our ranking list */ |
| 6128 |
rankings.append(curRank); |
| 6129 |
} |
| 6130 |
|
| 6131 |
/* sort the entries by descending ranking, and return the results */ |
| 6132 |
return rankings.sort(SortDesc, {x, y: x.compareRanking(y)}); |
| 6133 |
} |
| 6134 |
|
| 6135 |
/* create a new entry */ |
| 6136 |
construct(match) |
| 6137 |
{ |
| 6138 |
/* remember the match object */ |
| 6139 |
self.match = match; |
| 6140 |
|
| 6141 |
/* remember the number of tokens in the match */ |
| 6142 |
tokCount = match.lastTokenIndex - match.firstTokenIndex + 1; |
| 6143 |
} |
| 6144 |
|
| 6145 |
/* calculate my ranking */ |
| 6146 |
calcRanking(resolveArguments) |
| 6147 |
{ |
| 6148 |
/* |
| 6149 |
* Ask the match tree to resolve nouns, using this ranking |
| 6150 |
* object as the resolution results receiver - when an error or |
| 6151 |
* warning occurs during resolution, we'll merely note the |
| 6152 |
* condition rather than say anything about it. |
| 6153 |
* |
| 6154 |
* Note that 'self' is the results object, because the point of |
| 6155 |
* this resolution pass is to gather statistics into this |
| 6156 |
* results object. |
| 6157 |
*/ |
| 6158 |
match.resolveNouns(resolveArguments..., self); |
| 6159 |
} |
| 6160 |
|
| 6161 |
/* |
| 6162 |
* Compare two production list entries for ranking purposes. Returns |
| 6163 |
* a negative number if this one ranks worse than the other, 0 if |
| 6164 |
* they have the same ranking, or a positive number if this one ranks |
| 6165 |
* better than the other one. |
| 6166 |
* |
| 6167 |
* This routine is designed to run entirely off of our |
| 6168 |
* rankingCriteria property. In most cases, subclasses should be |
| 6169 |
* able to customize the ranking system simply by overriding the |
| 6170 |
* rankingCriteria property to provide a customized list of criteria |
| 6171 |
* objects. |
| 6172 |
*/ |
| 6173 |
compareRanking(other) |
| 6174 |
{ |
| 6175 |
local ret; |
| 6176 |
|
| 6177 |
/* |
| 6178 |
* Run through our ranking criteria and apply the first pass to |
| 6179 |
* each one. Return the indication of the first criterion that |
| 6180 |
* can tell a difference. |
| 6181 |
*/ |
| 6182 |
foreach (local cur in rankingCriteria) |
| 6183 |
{ |
| 6184 |
/* if the rankings differ in this criterion, return the result */ |
| 6185 |
if ((ret = cur.comparePass1(self, other)) != 0) |
| 6186 |
return ret; |
| 6187 |
} |
| 6188 |
|
| 6189 |
/* |
| 6190 |
* We couldn't tell any difference on the first pass, so try |
| 6191 |
* again with the finer-grained second pass. |
| 6192 |
*/ |
| 6193 |
foreach (local cur in rankingCriteria) |
| 6194 |
{ |
| 6195 |
/* run the second pass */ |
| 6196 |
if ((ret = cur.comparePass2(self, other)) != 0) |
| 6197 |
return ret; |
| 6198 |
} |
| 6199 |
|
| 6200 |
/* we couldn't tell any difference between the two */ |
| 6201 |
return 0; |
| 6202 |
} |
| 6203 |
|
| 6204 |
/* |
| 6205 |
* Our list of ranking criteria. This is a list of |
| 6206 |
* CommandRankingCriterion objects. The list is given in order of |
| 6207 |
* importance: the first criterion is the most important, so if it |
| 6208 |
* can discriminate the two match trees, we use its result; if the |
| 6209 |
* first criterion can't tell any difference, then we move on to the |
| 6210 |
* second criterion; and so on through the list. |
| 6211 |
* |
| 6212 |
* The most important thing is whether or not we have irresolvable |
| 6213 |
* noun phrases (vocabNonMatchCount). If one of us has a noun phrase |
| 6214 |
* that refers to nothing anywhere in the game, it's not as good as a |
| 6215 |
* phrase that at least matches something somewhere. |
| 6216 |
* |
| 6217 |
* Next, if one of us has noun phrases that cannot be resolved to |
| 6218 |
* something in scope (nonMatchCount), and the other can successfully |
| 6219 |
* resolve its noun phrases, the one that can resolve the phrases is |
| 6220 |
* preferred. |
| 6221 |
* |
| 6222 |
* Next, check for insufficient numbers of matches to counted phrases |
| 6223 |
* (insufficientCount). |
| 6224 |
* |
| 6225 |
* Next, check for noun lists in single-noun-only slots |
| 6226 |
* (listForSingle). |
| 6227 |
* |
| 6228 |
* Next, if we have an empty "but" list in one but not the other, |
| 6229 |
* take the one with the non-empty "but" list (emptyButCount). We |
| 6230 |
* prefer a non-empty "but" list with an empty "all" even to a |
| 6231 |
* non-empty "all" list with an empty "but", because in the latter |
| 6232 |
* case we probably failed to exclude anything because we |
| 6233 |
* misinterpreted the noun phrase to be excluded. |
| 6234 |
* |
| 6235 |
* Next, if we have an empty "all" or "any" phrase due to "but" |
| 6236 |
* exclusion, take the one that's not empty (allExcludedCount). |
| 6237 |
* |
| 6238 |
* Next, prefer a command that addresses an actor |
| 6239 |
* (actorSpecifiedCount) - if the actor name looks like a command (we |
| 6240 |
* have someone named "Open Bob," maybe?), we'd prefer to interpret |
| 6241 |
* the name appearing as a command prefix as an actor name. |
| 6242 |
* |
| 6243 |
* Next, prefer no unstructured word lists as noun phrases |
| 6244 |
* (miscWordList phrases) (miscWordListCount). |
| 6245 |
* |
| 6246 |
* Next, prefer interpretations that treat less text as uninterpreted |
| 6247 |
* literal text. By "less text," we simply mean that one has a |
| 6248 |
* shorter string treated as a literal than the other. |
| 6249 |
* |
| 6250 |
* Prefer no indefinite noun phrases (indefiniteCount). |
| 6251 |
* |
| 6252 |
* Prefer no truncated plurals (pluralTruncCount). |
| 6253 |
* |
| 6254 |
* Prefer no noun phrases ending in adjectives (endAdjCount). |
| 6255 |
* |
| 6256 |
* Prefer no truncated words of any kind (truncCount). |
| 6257 |
* |
| 6258 |
* Prefer fewer pronouns. If we have an interpretation that matches |
| 6259 |
* a word to explicit vocabulary, take it over matching a word as a |
| 6260 |
* pronoun: if a word is given explicitly as vocabulary for an |
| 6261 |
* object, use it if possible. |
| 6262 |
* |
| 6263 |
* Prefer no missing phrases (missingCount). |
| 6264 |
* |
| 6265 |
* Prefer the one with fewer subcommands - if one has fewer |
| 6266 |
* subcommands than the other, it means that we were able to |
| 6267 |
* interpret ambiguous conjunctions (such as "and") as noun phrase |
| 6268 |
* conjunctions rather than as command conjunctions; since we know by |
| 6269 |
* now that we both either have or don't have unresolved noun |
| 6270 |
* phrases, we'd rather take the interpretation that gives us noun |
| 6271 |
* phrases than the one that involves more separate commands. |
| 6272 |
* |
| 6273 |
* Prefer the tree that matches more tokens. |
| 6274 |
* |
| 6275 |
* Prefer the one with more structural noun phrases in the verb. For |
| 6276 |
* example, if we have one interpretation that's DETACH (X FROM Y) |
| 6277 |
* (where X FROM Y is a 'locational' phrase that we treat as the |
| 6278 |
* direct object), and one that's DETACH X FROM Y (where X is the |
| 6279 |
* direct object and Y is in the indirect object), prefer the latter, |
| 6280 |
* because it has both direct and indirect object phrases, whereas |
| 6281 |
* the former has only a direct object phrase. English speakers |
| 6282 |
* almost always try to put prepositions into a structural role in |
| 6283 |
* the verb phrase like this when they could be either in the verb |
| 6284 |
* phrase or part of a noun phrase. |
| 6285 |
* |
| 6286 |
* If all else fails, prefer the one that is initially less |
| 6287 |
* ambiguous. Ambiguity is a weak test at this point, since we might |
| 6288 |
* end up narrowing things down through automatic "logicalness" tests |
| 6289 |
* later, but it's slightly better to have the match be less |
| 6290 |
* ambiguous now, all other things being equal. |
| 6291 |
*/ |
| 6292 |
rankingCriteria = [rankByVocabNonMatch, |
| 6293 |
rankByNonMatch, |
| 6294 |
rankByNonMatchPoss, |
| 6295 |
rankByInsufficient, |
| 6296 |
rankByListForSingle, |
| 6297 |
rankByEmptyBut, |
| 6298 |
rankByAllExcluded, |
| 6299 |
rankByActorSpecified, |
| 6300 |
rankByUnwantedPlural, |
| 6301 |
rankByMiscWordList, |
| 6302 |
rankByWeakness, |
| 6303 |
rankByLiteralLength, |
| 6304 |
rankByIndefinite, |
| 6305 |
rankByPluralTrunc, |
| 6306 |
rankByEndAdj, |
| 6307 |
rankByTrunc, |
| 6308 |
rankByPronoun, |
| 6309 |
rankByMissing, |
| 6310 |
rankBySubcommands, |
| 6311 |
rankByTokenCount, |
| 6312 |
rankByVerbStructure, |
| 6313 |
rankByAmbiguity] |
| 6314 |
|
| 6315 |
/* the match tree I'm ranking */ |
| 6316 |
match = nil |
| 6317 |
|
| 6318 |
/* the number of tokens my match tree consumes */ |
| 6319 |
tokCount = 0 |
| 6320 |
|
| 6321 |
/* |
| 6322 |
* Ranking information. calcRanking() fills in these members, and |
| 6323 |
* compareRanking() uses these to calculate the relative ranking. |
| 6324 |
*/ |
| 6325 |
|
| 6326 |
/* |
| 6327 |
* The number of structural "noun phrase slots" in the verb. An |
| 6328 |
* intransitive verb has no noun phrase slots; a transitive verb |
| 6329 |
* with a direct object has one; a verb with a direct and indirect |
| 6330 |
* object has two slots. |
| 6331 |
*/ |
| 6332 |
nounSlotCount = nil |
| 6333 |
|
| 6334 |
/* number of noun phrases matching nothing anywhere in the game */ |
| 6335 |
vocabNonMatchCount = 0 |
| 6336 |
|
| 6337 |
/* number of noun phrases matching nothing in scope */ |
| 6338 |
nonMatchCount = 0 |
| 6339 |
|
| 6340 |
/* |
| 6341 |
* Number of possessive-qualified noun phrases matching nothing in |
| 6342 |
* scope. For example, "bob's desk" when there's no desk in scope |
| 6343 |
* (Bob's or otherwise). |
| 6344 |
*/ |
| 6345 |
nonMatchPossCount = 0 |
| 6346 |
|
| 6347 |
/* number of phrases requiring quantity higher than can be fulfilled */ |
| 6348 |
insufficientCount = 0 |
| 6349 |
|
| 6350 |
/* number of noun lists in single-noun slots */ |
| 6351 |
listForSingle = 0 |
| 6352 |
|
| 6353 |
/* number of empty "but" lists */ |
| 6354 |
emptyButCount = 0 |
| 6355 |
|
| 6356 |
/* number of "all" or "any" lists totally excluded by "but" */ |
| 6357 |
allExcludedCount = 0 |
| 6358 |
|
| 6359 |
/* missing phrases (structurally omitted, as in "put book") */ |
| 6360 |
missingCount = 0 |
| 6361 |
|
| 6362 |
/* number of truncated plurals */ |
| 6363 |
pluralTruncCount = 0 |
| 6364 |
|
| 6365 |
/* number of phrases ending in adjectives */ |
| 6366 |
endAdjCount = 0 |
| 6367 |
|
| 6368 |
/* number of phrases with indefinite noun phrase structure */ |
| 6369 |
indefiniteCount = 0 |
| 6370 |
|
| 6371 |
/* number of miscellaneous word lists as noun phrases */ |
| 6372 |
miscWordListCount = 0 |
| 6373 |
|
| 6374 |
/* number of truncated words overall */ |
| 6375 |
truncCount = 0 |
| 6376 |
|
| 6377 |
/* number of ambiguous noun phrases */ |
| 6378 |
ambigCount = 0 |
| 6379 |
|
| 6380 |
/* number of subcommands in the command */ |
| 6381 |
commandCount = 0 |
| 6382 |
|
| 6383 |
/* an actor is specified */ |
| 6384 |
actorSpecifiedCount = 0 |
| 6385 |
|
| 6386 |
/* unknown words */ |
| 6387 |
unknownWordCount = 0 |
| 6388 |
|
| 6389 |
/* total character length of literal text phrases */ |
| 6390 |
literalLength = 0 |
| 6391 |
|
| 6392 |
/* number of pronoun phrases */ |
| 6393 |
pronounCount = 0 |
| 6394 |
|
| 6395 |
/* weakness level (for noteWeakPhrasing) */ |
| 6396 |
weaknessLevel = 0 |
| 6397 |
|
| 6398 |
/* number of plural phrases encountered in single-object slots */ |
| 6399 |
unwantedPluralCount = 0 |
| 6400 |
|
| 6401 |
/* -------------------------------------------------------------------- */ |
| 6402 |
/* |
| 6403 |
* ResolveResults implementation. We use this results receiver when |
| 6404 |
* we're comparing the semantic strengths of multiple structural |
| 6405 |
* matches, so we merely note each error condition without showing |
| 6406 |
* any message to the user or asking the user for any input. Once |
| 6407 |
* we've ranked all of the matches, we'll choose the one with the |
| 6408 |
* best attributes and then resolve it for real, at which point if |
| 6409 |
* we chose one with any errors, we'll finally get around to showing |
| 6410 |
* the errors to the user. |
| 6411 |
*/ |
| 6412 |
|
| 6413 |
noVocabMatch(action, txt) |
| 6414 |
{ |
| 6415 |
/* note the unknown phrase */ |
| 6416 |
++vocabNonMatchCount; |
| 6417 |
} |
| 6418 |
|
| 6419 |
noMatch(action, txt) |
| 6420 |
{ |
| 6421 |
/* note that we have a noun phrase that matches nothing */ |
| 6422 |
++nonMatchCount; |
| 6423 |
} |
| 6424 |
|
| 6425 |
noMatchPossessive(action, txt) |
| 6426 |
{ |
| 6427 |
/* note that we have an unmatched possessive-qualified noun phrase */ |
| 6428 |
++nonMatchCount; |
| 6429 |
++nonMatchPossCount; |
| 6430 |
} |
| 6431 |
|
| 6432 |
allNotAllowed() |
| 6433 |
{ |
| 6434 |
/* treat this as a non-matching noun phrase */ |
| 6435 |
++nonMatchCount; |
| 6436 |
} |
| 6437 |
|
| 6438 |
noMatchForAll() |
| 6439 |
{ |
| 6440 |
/* treat this as any other noun phrase that matches nothing */ |
| 6441 |
++nonMatchCount; |
| 6442 |
} |
| 6443 |
|
| 6444 |
noteEmptyBut() |
| 6445 |
{ |
| 6446 |
/* note it */ |
| 6447 |
++emptyButCount; |
| 6448 |
} |
| 6449 |
|
| 6450 |
noMatchForAllBut() |
| 6451 |
{ |
| 6452 |
/* count the total exclusion */ |
| 6453 |
++allExcludedCount; |
| 6454 |
} |
| 6455 |
|
| 6456 |
noMatchForListBut() |
| 6457 |
{ |
| 6458 |
/* treat this as any other noun phrase that matches nothing */ |
| 6459 |
++allExcludedCount; |
| 6460 |
} |
| 6461 |
|
| 6462 |
noMatchForPronoun(typ, txt) |
| 6463 |
{ |
| 6464 |
/* treat this as any other noun phrase that matches nothing */ |
| 6465 |
++nonMatchCount; |
| 6466 |
} |
| 6467 |
|
| 6468 |
reflexiveNotAllowed(typ, txt) |
| 6469 |
{ |
| 6470 |
/* treat this as any other noun phrase that matches nothing */ |
| 6471 |
++nonMatchCount; |
| 6472 |
} |
| 6473 |
|
| 6474 |
wrongReflexive(typ, txt) |
| 6475 |
{ |
| 6476 |
/* treat this as any other noun phrase that matches nothing */ |
| 6477 |
++nonMatchCount; |
| 6478 |
} |
| 6479 |
|
| 6480 |
noMatchForPossessive(owner, txt) |
| 6481 |
{ |
| 6482 |
/* treat this as any other noun phrase that matches nothing */ |
| 6483 |
++nonMatchCount; |
| 6484 |
} |
| 6485 |
|
| 6486 |
noMatchForLocation(loc, txt) |
| 6487 |
{ |
| 6488 |
/* treat this as any other noun phrase that matches nothing */ |
| 6489 |
++nonMatchCount; |
| 6490 |
} |
| 6491 |
|
| 6492 |
noteBadPrep() |
| 6493 |
{ |
| 6494 |
/* don't do anything at this point */ |
| 6495 |
} |
| 6496 |
|
| 6497 |
nothingInLocation(txt) |
| 6498 |
{ |
| 6499 |
/* treat this as any other noun phrase that matches nothing */ |
| 6500 |
++nonMatchCount; |
| 6501 |
} |
| 6502 |
|
| 6503 |
ambiguousNounPhrase(keeper, asker, txt, |
| 6504 |
matchList, fullMatchList, scopeList, |
| 6505 |
requiredNum, resolver) |
| 6506 |
{ |
| 6507 |
local lst; |
| 6508 |
|
| 6509 |
/* note the ambiguity */ |
| 6510 |
++ambigCount; |
| 6511 |
|
| 6512 |
/* |
| 6513 |
* There's no need to disambiguate the list at this stage, since |
| 6514 |
* we're only testing the strength of the structure. |
| 6515 |
* |
| 6516 |
* As a tentative approximation of the results, return a list |
| 6517 |
* consisting of the required number only, but stash away the |
| 6518 |
* remainder of the full list as a property of the first element |
| 6519 |
* of the return list so we can find the full list again later. |
| 6520 |
*/ |
| 6521 |
lst = matchList.sublist(1, requiredNum); |
| 6522 |
if (matchList.length() > requiredNum && lst.length() >= 1) |
| 6523 |
lst[1].extraObjects = matchList.sublist(requiredNum + 1); |
| 6524 |
|
| 6525 |
/* return the abbreviated list */ |
| 6526 |
return lst; |
| 6527 |
} |
| 6528 |
|
| 6529 |
unknownNounPhrase(match, resolver) |
| 6530 |
{ |
| 6531 |
local wordList; |
| 6532 |
local ret; |
| 6533 |
|
| 6534 |
/* |
| 6535 |
* if the resolver can handle this set of unknown words, treat |
| 6536 |
* it as a good noun phrase; otherwise, treat it as an unmatched |
| 6537 |
* noun phrase |
| 6538 |
*/ |
| 6539 |
wordList = match.getOrigTokenList(); |
| 6540 |
if ((ret = resolver.resolveUnknownNounPhrase(wordList)) == nil) |
| 6541 |
{ |
| 6542 |
/* count the unmatchable phrase */ |
| 6543 |
++nonMatchCount; |
| 6544 |
|
| 6545 |
/* count the unknown word */ |
| 6546 |
++unknownWordCount; |
| 6547 |
|
| 6548 |
/* |
| 6549 |
* since this is only a ranking pass, resolve to an empty |
| 6550 |
* list for now |
| 6551 |
*/ |
| 6552 |
ret = []; |
| 6553 |
} |
| 6554 |
|
| 6555 |
/* return the results */ |
| 6556 |
return ret; |
| 6557 |
} |
| 6558 |
|
| 6559 |
getImpliedObject(np, resolver) |
| 6560 |
{ |
| 6561 |
/* count the missing object phrase */ |
| 6562 |
++missingCount; |
| 6563 |
return nil; |
| 6564 |
} |
| 6565 |
|
| 6566 |
askMissingObject(asker, resolver, responseProd) |
| 6567 |
{ |
| 6568 |
/* |
| 6569 |
* no need to do anything here - we'll count the missing object |
| 6570 |
* in getImpliedObject, and we don't want to ask for anything |
| 6571 |
* interactively at this point |
| 6572 |
*/ |
| 6573 |
return nil; |
| 6574 |
} |
| 6575 |
|
| 6576 |
noteLiteral(txt) |
| 6577 |
{ |
| 6578 |
/* add the length of this literal to the total literal length */ |
| 6579 |
literalLength += txt.length(); |
| 6580 |
} |
| 6581 |
|
| 6582 |
emptyNounPhrase(resolver) |
| 6583 |
{ |
| 6584 |
/* treat this as a non-matching noun phrase */ |
| 6585 |
++nonMatchCount; |
| 6586 |
return []; |
| 6587 |
} |
| 6588 |
|
| 6589 |
zeroQuantity(txt) |
| 6590 |
{ |
| 6591 |
/* treat this as a non-matching noun phrase */ |
| 6592 |
++nonMatchCount; |
| 6593 |
} |
| 6594 |
|
| 6595 |
insufficientQuantity(txt, matchList, requiredNum) |
| 6596 |
{ |
| 6597 |
/* treat this as a non-matching noun phrase */ |
| 6598 |
++insufficientCount; |
| 6599 |
} |
| 6600 |
|
| 6601 |
singleObjectRequired(txt) |
| 6602 |
{ |
| 6603 |
/* treat this as a non-matching noun phrase */ |
| 6604 |
++listForSingle; |
| 6605 |
} |
| 6606 |
|
| 6607 |
uniqueObjectRequired(txt, matchList) |
| 6608 |
{ |
| 6609 |
/* |
| 6610 |
* ignore this for now - we might get a unique object via |
| 6611 |
* disambiguation during the execution phase |
| 6612 |
*/ |
| 6613 |
} |
| 6614 |
|
| 6615 |
noteAdjEnding() |
| 6616 |
{ |
| 6617 |
/* count it */ |
| 6618 |
++endAdjCount; |
| 6619 |
} |
| 6620 |
|
| 6621 |
noteIndefinite() |
| 6622 |
{ |
| 6623 |
/* count it */ |
| 6624 |
++indefiniteCount; |
| 6625 |
} |
| 6626 |
|
| 6627 |
noteMiscWordList(txt) |
| 6628 |
{ |
| 6629 |
/* note the presence of an unstructured noun phrase */ |
| 6630 |
++miscWordListCount; |
| 6631 |
|
| 6632 |
/* count this as a literal as well */ |
| 6633 |
noteLiteral(txt); |
| 6634 |
} |
| 6635 |
|
| 6636 |
notePronoun() |
| 6637 |
{ |
| 6638 |
/* note the presence of a pronoun */ |
| 6639 |
++pronounCount; |
| 6640 |
} |
| 6641 |
|
| 6642 |
noteMatches(matchList) |
| 6643 |
{ |
| 6644 |
/* |
| 6645 |
* Run through the match list and note each weak flag. Note |
| 6646 |
* that each element of the match list is a ResolveInfo |
| 6647 |
* instance. |
| 6648 |
*/ |
| 6649 |
foreach (local cur in matchList) |
| 6650 |
{ |
| 6651 |
/* if this object was matched with a truncated word, note it */ |
| 6652 |
if ((cur.flags_ & VocabTruncated) != 0) |
| 6653 |
++truncCount; |
| 6654 |
|
| 6655 |
/* if this object was matched with a truncated plural, note it */ |
| 6656 |
if ((cur.flags_ & PluralTruncated) != 0) |
| 6657 |
++pluralTruncCount; |
| 6658 |
} |
| 6659 |
} |
| 6660 |
|
| 6661 |
beginSingleObjSlot() { ++inSingleObjSlot; } |
| 6662 |
endSingleObjSlot() { --inSingleObjSlot; } |
| 6663 |
inSingleObjSlot = 0 |
| 6664 |
|
| 6665 |
beginTopicSlot() { ++inTopicSlot; } |
| 6666 |
endTopicSlot() { --inTopicSlot; } |
| 6667 |
inTopicSlot = 0 |
| 6668 |
|
| 6669 |
notePlural() |
| 6670 |
{ |
| 6671 |
/* |
| 6672 |
* If we're resolving a single-object slot, we want to avoid |
| 6673 |
* plurals, since they could resolve to multiple objects as |
| 6674 |
* though we'd typed a list of objects here. This isn't a |
| 6675 |
* problem for topics, though, since a topic slot isn't iterated |
| 6676 |
* for execution. |
| 6677 |
*/ |
| 6678 |
if (inSingleObjSlot && !inTopicSlot) |
| 6679 |
++unwantedPluralCount; |
| 6680 |
} |
| 6681 |
|
| 6682 |
incCommandCount() |
| 6683 |
{ |
| 6684 |
/* increase our subcommand counter */ |
| 6685 |
++commandCount; |
| 6686 |
} |
| 6687 |
|
| 6688 |
noteActorSpecified() |
| 6689 |
{ |
| 6690 |
/* note it */ |
| 6691 |
++actorSpecifiedCount; |
| 6692 |
} |
| 6693 |
|
| 6694 |
noteNounSlots(cnt) |
| 6695 |
{ |
| 6696 |
/* |
| 6697 |
* If this is the first noun slot count we've received, remember |
| 6698 |
* it. If we already have a count, ignore the new one - we only |
| 6699 |
* want to consider the first verb phrase if there are multiple |
| 6700 |
* verb phrases, since we'll reconsider the next verb phrase when |
| 6701 |
* we're ready to execute it. |
| 6702 |
*/ |
| 6703 |
if (nounSlotCount == nil) |
| 6704 |
nounSlotCount = cnt; |
| 6705 |
} |
| 6706 |
|
| 6707 |
noteWeakPhrasing(level) |
| 6708 |
{ |
| 6709 |
/* note the weak phrasing level */ |
| 6710 |
weaknessLevel = level; |
| 6711 |
} |
| 6712 |
|
| 6713 |
/* don't allow action remapping while ranking */ |
| 6714 |
allowActionRemapping = nil |
| 6715 |
; |
| 6716 |
|
| 6717 |
/* |
| 6718 |
* Another preliminary results gatherer that does everything the way the |
| 6719 |
* CommandRanking results object does, except that we perform |
| 6720 |
* interactive resolution of unknown words via OOPS. |
| 6721 |
*/ |
| 6722 |
class OopsResults: CommandRanking |
| 6723 |
construct(issuingActor, targetActor) |
| 6724 |
{ |
| 6725 |
/* remember the actors */ |
| 6726 |
issuingActor_ = issuingActor; |
| 6727 |
targetActor_ = targetActor; |
| 6728 |
} |
| 6729 |
|
| 6730 |
/* |
| 6731 |
* handle a phrase with unknown words |
| 6732 |
*/ |
| 6733 |
unknownNounPhrase(match, resolver) |
| 6734 |
{ |
| 6735 |
|
| 6736 |
/* |
| 6737 |
* if the resolver can handle this set of unknown words, treat |
| 6738 |
* it as a good noun phrase |
| 6739 |
*/ |
| 6740 |
local wordList = match.getOrigTokenList(); |
| 6741 |
local ret = resolver.resolveUnknownNounPhrase(wordList); |
| 6742 |
if (ret != nil) |
| 6743 |
return ret; |
| 6744 |
|
| 6745 |
/* |
| 6746 |
* we still can't resolve it; try prompting for a correction of |
| 6747 |
* any misspelled words in the phrase |
| 6748 |
*/ |
| 6749 |
tryOops(wordList, issuingActor_, targetActor_, |
| 6750 |
match.firstTokenIndex, match.tokenList, rmcCommand); |
| 6751 |
|
| 6752 |
/* |
| 6753 |
* if we got this far, we still haven't resolved it; resolve to |
| 6754 |
* an empty phrase |
| 6755 |
*/ |
| 6756 |
return []; |
| 6757 |
} |
| 6758 |
|
| 6759 |
/* the command's issuing actor */ |
| 6760 |
issuingActor_ = nil |
| 6761 |
|
| 6762 |
/* the command's target actor */ |
| 6763 |
targetActor_ = nil |
| 6764 |
; |
| 6765 |
|
| 6766 |
/* ------------------------------------------------------------------------ */ |
| 6767 |
/* |
| 6768 |
* Exception list resolver. We use this type of resolution for noun |
| 6769 |
* phrases in the "but" list of an "all but" construct. |
| 6770 |
* |
| 6771 |
* We scope the "all but" list to the objects in the "all" list, since |
| 6772 |
* there's no point in excluding objects that aren't in the "all" list. |
| 6773 |
* In addition, if a phrase in the exclusion list matches more than one |
| 6774 |
* object in the "all" list, we consider it a match to all of those |
| 6775 |
* objects, even if it's a definite phrase - this means that items in |
| 6776 |
* the "but" list are never ambiguous. |
| 6777 |
*/ |
| 6778 |
class ExceptResolver: ProxyResolver |
| 6779 |
construct(mainList, mainListText, resolver) |
| 6780 |
{ |
| 6781 |
/* invoke the base class constructor */ |
| 6782 |
inherited(resolver); |
| 6783 |
|
| 6784 |
/* remember the main list, from which we're excluding items */ |
| 6785 |
self.mainList = mainList; |
| 6786 |
self.mainListText = mainListText; |
| 6787 |
} |
| 6788 |
|
| 6789 |
/* we're a sub-phrase resolver */ |
| 6790 |
isSubResolver = true |
| 6791 |
|
| 6792 |
/* |
| 6793 |
* match an object's name - we'll use the disambiguation name |
| 6794 |
* resolver, so that they can give us partial names just like in |
| 6795 |
* answer to a disambiguation question |
| 6796 |
*/ |
| 6797 |
matchName(obj, origTokens, adjustedTokens) |
| 6798 |
{ |
| 6799 |
return obj.matchNameDisambig(origTokens, adjustedTokens); |
| 6800 |
} |
| 6801 |
|
| 6802 |
/* |
| 6803 |
* Resolve qualifiers in the enclosing main scope, since qualifier |
| 6804 |
* phrases are not part of the narrowed list - qualifiers apply to |
| 6805 |
* the main phrase from which we're excluding, not to the exclusion |
| 6806 |
* list itself. |
| 6807 |
*/ |
| 6808 |
getQualifierResolver() { return origResolver; } |
| 6809 |
|
| 6810 |
/* |
| 6811 |
* determine if an object is in scope - it's in scope if it's in the |
| 6812 |
* original main list |
| 6813 |
*/ |
| 6814 |
objInScope(obj) |
| 6815 |
{ |
| 6816 |
return mainList.indexWhich({x: x.obj_ == obj}) != nil; |
| 6817 |
} |
| 6818 |
|
| 6819 |
/* for 'all', simply return the whole original list */ |
| 6820 |
getAll(np) |
| 6821 |
{ |
| 6822 |
return mainList; |
| 6823 |
} |
| 6824 |
|
| 6825 |
/* filter ambiguous equivalents */ |
| 6826 |
filterAmbiguousEquivalents(lst, np) |
| 6827 |
{ |
| 6828 |
/* |
| 6829 |
* keep all of the equivalent items in an exception list, |
| 6830 |
* because we want to exclude all of the equivalent items from |
| 6831 |
* the main list |
| 6832 |
*/ |
| 6833 |
return lst; |
| 6834 |
} |
| 6835 |
|
| 6836 |
/* filter an ambiguous noun list */ |
| 6837 |
filterAmbiguousNounPhrase(lst, requiredNum, np) |
| 6838 |
{ |
| 6839 |
/* |
| 6840 |
* noun phrases in an exception list are never ambiguous, |
| 6841 |
* because they implicitly refer to everything they match - |
| 6842 |
* simply return the full matching list |
| 6843 |
*/ |
| 6844 |
return lst; |
| 6845 |
} |
| 6846 |
|
| 6847 |
/* filter a plural noun list */ |
| 6848 |
filterPluralPhrase(lst, np) |
| 6849 |
{ |
| 6850 |
/* return all of the original plural matches */ |
| 6851 |
return lst; |
| 6852 |
} |
| 6853 |
|
| 6854 |
/* the main list from which we're excluding things */ |
| 6855 |
mainList = nil |
| 6856 |
|
| 6857 |
/* the original text for the main list */ |
| 6858 |
mainListText = '' |
| 6859 |
|
| 6860 |
/* the original underlying resolver */ |
| 6861 |
origResolver = nil |
| 6862 |
; |
| 6863 |
|
| 6864 |
/* |
| 6865 |
* Except list results object |
| 6866 |
*/ |
| 6867 |
class ExceptResults: object |
| 6868 |
construct(results) |
| 6869 |
{ |
| 6870 |
/* remember the original results object */ |
| 6871 |
origResults = results; |
| 6872 |
} |
| 6873 |
|
| 6874 |
/* |
| 6875 |
* ignore failed matches in the exception list - if they try to |
| 6876 |
* exclude something that's not in the original list, the object is |
| 6877 |
* excluded to begin with |
| 6878 |
*/ |
| 6879 |
noMatch(action, txt) { } |
| 6880 |
noMatchPoss(action, txt) { } |
| 6881 |
noVocabMatch(action, txt) { } |
| 6882 |
|
| 6883 |
/* ignore failed matches for possessives in the exception list */ |
| 6884 |
noMatchForPossessive(owner, txt) { } |
| 6885 |
|
| 6886 |
/* ignore failed matches for location in the exception list */ |
| 6887 |
noMatchForLocation(loc, txt) { } |
| 6888 |
|
| 6889 |
/* ignore failed matches for location in the exception list */ |
| 6890 |
nothingInLocation(loc) { } |
| 6891 |
|
| 6892 |
/* |
| 6893 |
* in case of ambiguity, simply keep everything and treat it as |
| 6894 |
* unambiguous - if they say "take coin except copper", we simply |
| 6895 |
* want to treat "copper" as unambiguously excluding every copper |
| 6896 |
* coin in the original list |
| 6897 |
*/ |
| 6898 |
ambiguousNounPhrase(keeper, asker, txt, |
| 6899 |
matchList, fullMatchList, scopeList, |
| 6900 |
requiredNum, resolver) |
| 6901 |
{ |
| 6902 |
/* return the full match list - exclude everything that matches */ |
| 6903 |
return fullMatchList; |
| 6904 |
} |
| 6905 |
|
| 6906 |
/* proxy anything we don't override to the underlying results object */ |
| 6907 |
propNotDefined(prop, [args]) |
| 6908 |
{ |
| 6909 |
return origResults.(prop)(args...); |
| 6910 |
} |
| 6911 |
|
| 6912 |
/* my original underlying results object */ |
| 6913 |
origResults = nil |
| 6914 |
; |
| 6915 |
|
| 6916 |
|
| 6917 |
/* ------------------------------------------------------------------------ */ |
| 6918 |
/* |
| 6919 |
* Base class for parser exceptions |
| 6920 |
*/ |
| 6921 |
class ParserException: Exception |
| 6922 |
; |
| 6923 |
|
| 6924 |
/* |
| 6925 |
* Terminate Command exception - when the parser encounters an error |
| 6926 |
* that makes it impossible to go any further processing a command, we |
| 6927 |
* throw this error to abandon the current command and proceed to the |
| 6928 |
* next. This indicates a syntax error or semantic resolution error |
| 6929 |
* that renders the command meaningless or makes it impossible to |
| 6930 |
* proceed. |
| 6931 |
* |
| 6932 |
* When this exception is thrown, all processing of the current command |
| 6933 |
* termintes immediately. No further action processing is performed; we |
| 6934 |
* don't continue iterating the command on any additional objects for a |
| 6935 |
* multi-object command; and we discard any remaining commands on the |
| 6936 |
* same command line. |
| 6937 |
*/ |
| 6938 |
class TerminateCommandException: ParserException |
| 6939 |
; |
| 6940 |
|
| 6941 |
/* |
| 6942 |
* Cancel Command Line exception. This is used to cancel any *remaining* |
| 6943 |
* commands on a command line after finishing execution of one command on |
| 6944 |
* the line. For example, if the player types "TAKE BOX AND GO NORTH", |
| 6945 |
* the handler for TAKE BOX can throw this exception to cancel everything |
| 6946 |
* later on the command line (in this case, the GO NORTH part). |
| 6947 |
* |
| 6948 |
* This is handled almost identically to TerminateCommandException. The |
| 6949 |
* only difference is that some games might want to alert the player with |
| 6950 |
* an explanation that extra commands are being ignored. |
| 6951 |
*/ |
| 6952 |
class CancelCommandLineException: TerminateCommandException |
| 6953 |
; |
| 6954 |
|
| 6955 |
|
| 6956 |
/* |
| 6957 |
* Parsing failure exception. This exception is parameterized with |
| 6958 |
* message information describing the failure, and can be used to route |
| 6959 |
* the failure notification to the issuing actor. |
| 6960 |
*/ |
| 6961 |
class ParseFailureException: ParserException |
| 6962 |
construct(messageProp, [args]) |
| 6963 |
{ |
| 6964 |
/* remember the message property and the parameters */ |
| 6965 |
message_ = messageProp; |
| 6966 |
args_ = args; |
| 6967 |
} |
| 6968 |
|
| 6969 |
/* notify the issuing actor of the problem */ |
| 6970 |
notifyActor(targetActor, issuingActor) |
| 6971 |
{ |
| 6972 |
/* |
| 6973 |
* Tell the target actor to notify the issuing actor. We route |
| 6974 |
* the notification from the target to the issuer in keeping |
| 6975 |
* with conversation we're modelling: the issuer asked the |
| 6976 |
* target to do something, so the target is now replying with |
| 6977 |
* information explaining why the target can't do as asked. |
| 6978 |
*/ |
| 6979 |
targetActor.notifyParseFailure(issuingActor, message_, args_); |
| 6980 |
} |
| 6981 |
|
| 6982 |
displayException() { "Parse failure exception"; } |
| 6983 |
|
| 6984 |
/* the message property ID */ |
| 6985 |
message_ = nil |
| 6986 |
|
| 6987 |
/* the (varargs) parameters to the message */ |
| 6988 |
args_ = nil |
| 6989 |
; |
| 6990 |
|
| 6991 |
/* |
| 6992 |
* Exception: Retry a command with new tokens. In some cases, the |
| 6993 |
* parser processes a command by replacing the command with a new one |
| 6994 |
* and processing the new one instead of the original. When this |
| 6995 |
* happens, the parser will throw this exception, filling in newTokens_ |
| 6996 |
* with the replacement token list. |
| 6997 |
* |
| 6998 |
* Note that this is meant to replace the current command only - this |
| 6999 |
* exception effectively *edits* the current command. Any pending |
| 7000 |
* tokens after the current command should be retained when this |
| 7001 |
* exception is thrown. |
| 7002 |
*/ |
| 7003 |
class RetryCommandTokensException: ParserException |
| 7004 |
construct(lst) |
| 7005 |
{ |
| 7006 |
/* remember the new token list */ |
| 7007 |
newTokens_ = lst; |
| 7008 |
} |
| 7009 |
|
| 7010 |
/* |
| 7011 |
* The replacement token list. Note that this is in the same format |
| 7012 |
* as the token list returned from the tokenizer, so this is a list |
| 7013 |
* consisting of two sublists - one for the token strings, the other |
| 7014 |
* for the corresponding token types. |
| 7015 |
*/ |
| 7016 |
newTokens_ = [] |
| 7017 |
; |
| 7018 |
|
| 7019 |
/* |
| 7020 |
* Replacement command string exception. Abort any current command |
| 7021 |
* line, and start over with a brand new input string. Note that any |
| 7022 |
* pending, unparsed tokens on the previous command line should be |
| 7023 |
* discarded. |
| 7024 |
*/ |
| 7025 |
class ReplacementCommandStringException: ParserException |
| 7026 |
construct(str, issuer, target) |
| 7027 |
{ |
| 7028 |
/* remember the new command string */ |
| 7029 |
newCommand_ = str; |
| 7030 |
|
| 7031 |
/* |
| 7032 |
* note the issuing actor; if the caller specified this as nil, |
| 7033 |
* use the current player character as the default |
| 7034 |
*/ |
| 7035 |
issuingActor_ = (issuer == nil ? libGlobal.playerChar : issuer); |
| 7036 |
|
| 7037 |
/* note the default target actor, defaulting to the player */ |
| 7038 |
targetActor_ = (target == nil ? libGlobal.playerChar : target); |
| 7039 |
} |
| 7040 |
|
| 7041 |
/* the new command string */ |
| 7042 |
newCommand_ = '' |
| 7043 |
|
| 7044 |
/* the actor issuing the command */ |
| 7045 |
issuingActor_ = nil |
| 7046 |
|
| 7047 |
/* the default target actor of the command */ |
| 7048 |
targetActor_ = nil |
| 7049 |
; |
| 7050 |
|
| 7051 |
/* ------------------------------------------------------------------------ */ |
| 7052 |
/* |
| 7053 |
* Parser debugging helpers |
| 7054 |
*/ |
| 7055 |
|
| 7056 |
#ifdef PARSER_DEBUG |
| 7057 |
/* |
| 7058 |
* Show a list of match trees |
| 7059 |
*/ |
| 7060 |
showGrammarList(matchList) |
| 7061 |
{ |
| 7062 |
/* show the list only if we're in debug mode */ |
| 7063 |
if (libGlobal.parserDebugMode) |
| 7064 |
{ |
| 7065 |
/* show each match tree in the list */ |
| 7066 |
foreach (local cur in matchList) |
| 7067 |
{ |
| 7068 |
"\n----------\n"; |
| 7069 |
showGrammar(cur, 0); |
| 7070 |
} |
| 7071 |
} |
| 7072 |
} |
| 7073 |
|
| 7074 |
/* |
| 7075 |
* Show a winning match tree |
| 7076 |
*/ |
| 7077 |
showGrammarWithCaption(headline, match) |
| 7078 |
{ |
| 7079 |
/* show the list only if we're in debug mode */ |
| 7080 |
if (libGlobal.parserDebugMode) |
| 7081 |
{ |
| 7082 |
"\n----- <<headline>> -----\n"; |
| 7083 |
showGrammar(match, 0); |
| 7084 |
} |
| 7085 |
} |
| 7086 |
|
| 7087 |
/* |
| 7088 |
* Show a grammar tree |
| 7089 |
*/ |
| 7090 |
showGrammar(prod, indent) |
| 7091 |
{ |
| 7092 |
local info; |
| 7093 |
|
| 7094 |
/* if we're not in parser debug mode, do nothing */ |
| 7095 |
if (!libGlobal.parserDebugMode) |
| 7096 |
return; |
| 7097 |
|
| 7098 |
/* indent to the requested level */ |
| 7099 |
for (local i = 0 ; i < indent ; ++i) |
| 7100 |
"\ \ "; |
| 7101 |
|
| 7102 |
/* check for non-production objects */ |
| 7103 |
if (prod == nil) |
| 7104 |
{ |
| 7105 |
/* this tree element isn't used - skip it */ |
| 7106 |
return; |
| 7107 |
} |
| 7108 |
else if (dataType(prod) == TypeSString) |
| 7109 |
{ |
| 7110 |
/* show the item literally, and we're done */ |
| 7111 |
"'<<prod>>'\n"; |
| 7112 |
return; |
| 7113 |
} |
| 7114 |
|
| 7115 |
/* get the information for this item */ |
| 7116 |
info = prod.grammarInfo(); |
| 7117 |
|
| 7118 |
/* if it's nil, there's nothing more to do */ |
| 7119 |
if (info == nil) |
| 7120 |
{ |
| 7121 |
"<no information>\n"; |
| 7122 |
return; |
| 7123 |
} |
| 7124 |
|
| 7125 |
/* show the name */ |
| 7126 |
"<<info[1]>> [<<prod.getOrigText()>>]\n"; |
| 7127 |
|
| 7128 |
/* show the subproductions */ |
| 7129 |
for (local i = 2 ; i <= info.length ; ++i) |
| 7130 |
showGrammar(info[i], indent + 1); |
| 7131 |
} |
| 7132 |
#endif /* PARSER_DEBUG */ |
| 7133 |
|