comparison hip.js @ 7:aab0d14248f5

All suggested completions now return sensible descriptions of what they would do if executed.
author jonathandicarlo@jonathan-dicarlos-macbook-pro.local
date Wed, 14 May 2008 14:59:24 -0700
parents 98935af12b04
children 11a051a31077
comparison
equal deleted inserted replaced
6:98935af12b04 7:aab0d14248f5
130 // keys are prepositions 130 // keys are prepositions
131 // values are NounTypes. 131 // values are NounTypes.
132 // example: { "from" : City, "to" : City, "on" : Day } 132 // example: { "from" : City, "to" : City, "on" : Day }
133 }, 133 },
134 134
135 getDescription: function() { 135 getDescription: function( directObject, prepositionPhrases ) {
136 // returns a string describing what the sentence will do if executed 136 // returns a string describing what the sentence will do if executed
137 return this._verb.getDescription( this._DO, this._modifiers ); 137 var desc = "Hit enter to do " + this._name + " with direct object " + directObject;
138 } 138 for ( var x in prepositionPhrases ) {
139 desc = desc + ", " + x + " " + prepositionPhrases[x];
140 }
141 return desc;
142 },
139 143
140 recursiveParse: function( unusedWords, filledMods, unfilledMods ) { 144 recursiveParse: function( unusedWords, filledMods, unfilledMods ) {
141 var x; 145 var x;
142 var suggestions = []; 146 var suggestions = [];
143 var completions = []; 147 var completions = [];
192 } 196 }
193 } 197 }
194 if ( matchIndices.length == 0 ) { 198 if ( matchIndices.length == 0 ) {
195 // no match for this preposition. 199 // no match for this preposition.
196 // Leave it blank and try to parse the rest: 200 // Leave it blank and try to parse the rest:
197 filledMods[preposition] = ""; 201 var newFilledMods = dictDeepCopy( filledMods );
202 newFilledMods[preposition] = "";
198 var directObject = unusedWords.join( " " ); 203 var directObject = unusedWords.join( " " );
199 return [ new ParsedSentence( this, directObject, filledMods ) ]; 204 return this.recursiveParse( unusedWords, newFilledMods, newUnfilledMods );
200 //return this.recursiveParse( unusedWords, filledMods, newUnfilledMods );
201 } else { 205 } else {
202 // this is placeholder, destroy it. 206 // this is placeholder, destroy it.
203 for ( x in matchIndices ) { 207 for ( x in matchIndices ) {
204 var noun = unusedWords[ matchIndices[x]+1 ]; 208 var noun = unusedWords[ matchIndices[x]+1 ];
205 var newUnusedWords = unusedWords.slice(); 209 var newUnusedWords = unusedWords.slice();
245 } 249 }
246 } 250 }
247 }; 251 };
248 252
249 var fly = new Verb( "fly", null, null, { "from": city, "to": city } ); 253 var fly = new Verb( "fly", null, null, { "from": city, "to": city } );
254 fly.getDescription = function( directObject, mods ) {
255 var fromCity = mods[ "from" ];
256 var toCity = mods["to"];
257 if ( !fromCity ) {
258 fromCity = "from somewhere";
259 }
260 if ( !toCity ) {
261 toCity = "to somewhere else";
262 }
263 return "Buy airplane tickets from " + fromCity + " to " + toCity;
264 };
250 var define = new Verb( "define", "word", anyWord, {} ); 265 var define = new Verb( "define", "word", anyWord, {} );
266 define.getDescription = function( directObject, mods ) {
267 if (directObject ) {
268 return "Search for definition of the word "" + directObject + """;
269 } else {
270 return "Search for the definition of a word.";
271 }
272 };
251 var google = new Verb( "google", "word", anyWord, {} ); 273 var google = new Verb( "google", "word", anyWord, {} );
274 google.getDescription = function( directObject, mods ) {
275 if (directObject ) {
276 return "Search Google for "" + directObject + """;
277 } else {
278 return "Search Google for a word or phrase.";
279 }
280 };
252 var go = new Verb( "go", "tab", tab, {} ); 281 var go = new Verb( "go", "tab", tab, {} );
282 go.getDescription = function( directObject, mods ) {
283 if (directObject ) {
284 return "Switch to the Firefox tab "" + directObject + """;
285 } else {
286 return "Search to a given Firefox tab.";
287 }
288 };
253 var close = new Verb( "close", null, null, {} ); 289 var close = new Verb( "close", null, null, {} );
290 close.getDescription = function( directObject, mods ) {
291 return "Close the front window or tab.";
292 };
254 var translate = new Verb( "translate", "text", anyWord, { "from": language, "to": language } ); 293 var translate = new Verb( "translate", "text", anyWord, { "from": language, "to": language } );
294 translate.getDescription = function( directObject, mods ) {
295 if (directObject ) {
296 var DO = "the phrase "" + directObject + """;
297 } else {
298 var DO = "a given phrase";
299 }
300 var fromLang = mods["from"];
301 if (!fromLang) {
302 fromLang = "a given language";
303 }
304 var toLang = mods["to"];
305 if (!fromLang) {
306 toLang = "another language.";
307 }
308 return "Translate " + DO + " from " + fromLang + " to " + toLang;
309 };
255 var nuke = new Verb( "nuke", "city", city, {} ); 310 var nuke = new Verb( "nuke", "city", city, {} );
311 nuke.getDescription = function( directObject, mods ) {
312 if (!directObject) {
313 directObject = "a given city";
314 }
315 return "Launch a nuclear missile at " + directObject;
316 };
256 var open = new Verb( "open", "url", anyWord, {} ); 317 var open = new Verb( "open", "url", anyWord, {} );
318 open.getDescription = function( directObject, mods ) {
319 if (directObject ) {
320 return "Open the URL "" + directObject + """;
321 } else {
322 return "Open a given URL.";
323 }
324 };
325
257 var email = new Verb( "email", "text", anyWord, { "to": person, "subject": anyWord } ); 326 var email = new Verb( "email", "text", anyWord, { "to": person, "subject": anyWord } );
327 email.getDescription = function( directObject, mods ) {
328 if (directObject ) {
329 var DO = "the message "" + directObject + "" as an email";
330 } else {
331 var DO = "an email";
332 }
333 var target = mods["to"];
334 if ( !target ) {
335 target = "someone from your address book";
336 }
337 if ( mods["subject"] ) {
338 var subject = ", with the subject " + mods["subject"] + ", ";
339 } else {
340 var subject = " ";
341 }
342 return "Send " + DO + subject + "to " + target;
343 };
344
258 var encrypt = new Verb( "encrypt", "text", anyWord, { "for": person } ); 345 var encrypt = new Verb( "encrypt", "text", anyWord, { "for": person } );
346 encrypt.getDescription = function( directObject, mods ) {
347 if (directObject ) {
348 var DO = "the message "" + directObject + """;
349 } else {
350 var DO = "a secret message";
351 }
352 var target = mods["for"];
353 if ( !target ) {
354 target = "one particular person";
355 }
356 return "Encrypt " + DO + " so it can only be read by " + target;
357 };
358
259 var wiki = new Verb( "wikipedia", "word", anyWord, { "language": language } ); 359 var wiki = new Verb( "wikipedia", "word", anyWord, { "language": language } );
360 wiki.getDescription = function( directObject, mods ) {
361 var desc = "Search ";
362 if ( mods["language"] ) {
363 desc = desc + "the " + mods["language"] + " language version of ";
364 }
365 desc = desc + "Wikipedia for "" + directObject + """;
366 return desc;
367 };
260 368
261 var verbs = [ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ]; 369 var verbs = [ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ];
262 370
263 /* Initial state: no verb determined. 371 /* Initial state: no verb determined.
264 After each keypress, update verb suggestion list. 372 After each keypress, update verb suggestion list.
299 407
300 getSuggestionsAsHtml : function() { 408 getSuggestionsAsHtml : function() {
301 return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ]; 409 return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ];
302 }, 410 },
303 411
412 getDescriptionText: function() {
413 if ( this._suggestionList.length == 0 ) {
414 return "Type some commands!";
415 }
416 var h = this._hilitedSuggestion;
417 if ( h == 0 ) {
418 h = 0;
419 } else {
420 h = h - 1;
421 }
422 var sentence = this._suggestionList[h];
423 return sentence.getDescription();
424 },
425
304 indicationDown: function( ) { 426 indicationDown: function( ) {
305 this._hilitedSuggestion ++; 427 this._hilitedSuggestion ++;
306 if ( this._hilitedSuggestion > this._suggestionList.length ) { 428 if ( this._hilitedSuggestion > this._suggestionList.length ) {
307 this._hilitedSuggestion = 0; 429 this._hilitedSuggestion = 0;
308 } 430 }
356 } 478 }
357 479
358 function updateDisplay( ) { 480 function updateDisplay( ) {
359 var suggestions = gQs.getSuggestionsAsHtml(); 481 var suggestions = gQs.getSuggestionsAsHtml();
360 var hilitedSuggestion = gQs.getHilitedSuggestion(); 482 var hilitedSuggestion = gQs.getHilitedSuggestion();
483 var description = gQs.getDescriptionText();
484 $("#status-line").html( description );
361 var ac = $("#autocomplete-popup"); 485 var ac = $("#autocomplete-popup");
362 ac.html( makeSuggestionHtml( "div", suggestions, hilitedSuggestion ) ); 486 ac.html( makeSuggestionHtml( "div", suggestions, hilitedSuggestion ) );
363 ac.show(); 487 ac.show();
364 } 488 }
365 489
366 function searchBoxQuery( event ) { 490 function searchBoxQuery( event ) {
367 // TODO: if the event is an 'esc' key, clear the input field. 491 // TODO: if the event is an 'esc' key, clear the input field.
368 // If the event is an 'up arrow' or 'down arrow' key, change the 492 // If the event is an 'up arrow' or 'down arrow' key, change the
412 ); 536 );
413 }); 537 });
414 538
415 /* Minor problems: 539 /* Minor problems:
416 2. multiple word direct objects are truncated to single word 540 2. multiple word direct objects are truncated to single word
417 3. prepositional phrases past the first don't get matched? 541 5. Need to include the suggestion where matched prepositions aren't used but
418 4. sentences need to have descriptions 542 are treated as part of the direct object instead.
419 */ 543 */