Mercurial > hip
changeset 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 |
files | hip.js |
diffstat | 1 files changed, 133 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/hip.js Wed May 14 13:25:17 2008 -0700 +++ b/hip.js Wed May 14 14:59:24 2008 -0700 @@ -132,10 +132,14 @@ // example: { "from" : City, "to" : City, "on" : Day } }, - getDescription: function() { + getDescription: function( directObject, prepositionPhrases ) { // returns a string describing what the sentence will do if executed - return this._verb.getDescription( this._DO, this._modifiers ); - } + var desc = "Hit enter to do " + this._name + " with direct object " + directObject; + for ( var x in prepositionPhrases ) { + desc = desc + ", " + x + " " + prepositionPhrases[x]; + } + return desc; + }, recursiveParse: function( unusedWords, filledMods, unfilledMods ) { var x; @@ -194,10 +198,10 @@ if ( matchIndices.length == 0 ) { // no match for this preposition. // Leave it blank and try to parse the rest: - filledMods[preposition] = ""; + var newFilledMods = dictDeepCopy( filledMods ); + newFilledMods[preposition] = ""; var directObject = unusedWords.join( " " ); - return [ new ParsedSentence( this, directObject, filledMods ) ]; - //return this.recursiveParse( unusedWords, filledMods, newUnfilledMods ); + return this.recursiveParse( unusedWords, newFilledMods, newUnfilledMods ); } else { // this is placeholder, destroy it. for ( x in matchIndices ) { @@ -247,16 +251,120 @@ }; var fly = new Verb( "fly", null, null, { "from": city, "to": city } ); +fly.getDescription = function( directObject, mods ) { + var fromCity = mods[ "from" ]; + var toCity = mods["to"]; + if ( !fromCity ) { + fromCity = "from somewhere"; + } + if ( !toCity ) { + toCity = "to somewhere else"; + } + return "Buy airplane tickets from " + fromCity + " to " + toCity; +}; var define = new Verb( "define", "word", anyWord, {} ); +define.getDescription = function( directObject, mods ) { + if (directObject ) { + return "Search for definition of the word "" + directObject + """; + } else { + return "Search for the definition of a word."; + } +}; var google = new Verb( "google", "word", anyWord, {} ); +google.getDescription = function( directObject, mods ) { + if (directObject ) { + return "Search Google for "" + directObject + """; + } else { + return "Search Google for a word or phrase."; + } +}; var go = new Verb( "go", "tab", tab, {} ); +go.getDescription = function( directObject, mods ) { + if (directObject ) { + return "Switch to the Firefox tab "" + directObject + """; + } else { + return "Search to a given Firefox tab."; + } +}; var close = new Verb( "close", null, null, {} ); +close.getDescription = function( directObject, mods ) { + return "Close the front window or tab."; +}; var translate = new Verb( "translate", "text", anyWord, { "from": language, "to": language } ); +translate.getDescription = function( directObject, mods ) { + if (directObject ) { + var DO = "the phrase "" + directObject + """; + } else { + var DO = "a given phrase"; + } + var fromLang = mods["from"]; + if (!fromLang) { + fromLang = "a given language"; + } + var toLang = mods["to"]; + if (!fromLang) { + toLang = "another language."; + } + return "Translate " + DO + " from " + fromLang + " to " + toLang; +}; var nuke = new Verb( "nuke", "city", city, {} ); +nuke.getDescription = function( directObject, mods ) { + if (!directObject) { + directObject = "a given city"; + } + return "Launch a nuclear missile at " + directObject; +}; var open = new Verb( "open", "url", anyWord, {} ); +open.getDescription = function( directObject, mods ) { + if (directObject ) { + return "Open the URL "" + directObject + """; + } else { + return "Open a given URL."; + } +}; + var email = new Verb( "email", "text", anyWord, { "to": person, "subject": anyWord } ); +email.getDescription = function( directObject, mods ) { + if (directObject ) { + var DO = "the message "" + directObject + "" as an email"; + } else { + var DO = "an email"; + } + var target = mods["to"]; + if ( !target ) { + target = "someone from your address book"; + } + if ( mods["subject"] ) { + var subject = ", with the subject " + mods["subject"] + ", "; + } else { + var subject = " "; + } + return "Send " + DO + subject + "to " + target; +}; + var encrypt = new Verb( "encrypt", "text", anyWord, { "for": person } ); +encrypt.getDescription = function( directObject, mods ) { + if (directObject ) { + var DO = "the message "" + directObject + """; + } else { + var DO = "a secret message"; + } + var target = mods["for"]; + if ( !target ) { + target = "one particular person"; + } + return "Encrypt " + DO + " so it can only be read by " + target; +}; + var wiki = new Verb( "wikipedia", "word", anyWord, { "language": language } ); +wiki.getDescription = function( directObject, mods ) { + var desc = "Search "; + if ( mods["language"] ) { + desc = desc + "the " + mods["language"] + " language version of "; + } + desc = desc + "Wikipedia for "" + directObject + """; + return desc; +}; var verbs = [ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ]; @@ -301,6 +409,20 @@ return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ]; }, + getDescriptionText: function() { + if ( this._suggestionList.length == 0 ) { + return "Type some commands!"; + } + var h = this._hilitedSuggestion; + if ( h == 0 ) { + h = 0; + } else { + h = h - 1; + } + var sentence = this._suggestionList[h]; + return sentence.getDescription(); + }, + indicationDown: function( ) { this._hilitedSuggestion ++; if ( this._hilitedSuggestion > this._suggestionList.length ) { @@ -358,9 +480,11 @@ function updateDisplay( ) { var suggestions = gQs.getSuggestionsAsHtml(); var hilitedSuggestion = gQs.getHilitedSuggestion(); + var description = gQs.getDescriptionText(); + $("#status-line").html( description ); var ac = $("#autocomplete-popup"); ac.html( makeSuggestionHtml( "div", suggestions, hilitedSuggestion ) ); - ac.show(); + ac.show(); } function searchBoxQuery( event ) { @@ -414,6 +538,6 @@ /* Minor problems: 2. multiple word direct objects are truncated to single word -3. prepositional phrases past the first don't get matched? -4. sentences need to have descriptions +5. Need to include the suggestion where matched prepositions aren't used but + are treated as part of the direct object instead. */