Mercurial > hip
changeset 13:8dcc88f93829
Broke up hip.js into some smaller js files
author | jonathandicarlo@localhost |
---|---|
date | Wed, 02 Jul 2008 12:04:46 -0700 |
parents | aa56c4aa5b11 |
children | a26372118854 |
files | hip.html hip.js nounClasses.js nouns.js verbClasses.js verbs.js |
diffstat | 6 files changed, 383 insertions(+), 372 deletions(-) [+] |
line wrap: on
line diff
--- a/hip.html Thu May 29 14:28:50 2008 -0700 +++ b/hip.html Wed Jul 02 12:04:46 2008 -0700 @@ -2,18 +2,32 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> - <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> - <link rel="stylesheet" type="text/css" media="all" - href="hip.css" /> - <title></title> +<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> +<link rel="stylesheet" type="text/css" media="all" +href="hip.css" /> +<title></title> </head> <body> - <div id="status-line">Ubiquity!</div> - <input id="search-box" type="text" size="40" /> - <div id="autocomplete-popup"></div> - <hr/> - <p>Pretend this is your selection: <input id="selection" type="text" size=30" /></p> +<div id="status-line">Blah!</div> +<input id="search-box" type="text" size="40" /> +<div id="autocomplete-popup"></div> +<hr/> +<p>Pretend this box is your selection, OK? +<input id="fake-selection" type="text" size="40" /></p> +<p>Here is some stuff that you can select on this page!</p> +<p>New York</p> +<p>thunder@mozilla.com</p> +<p>el pollo loco</p> +<p>http://www.evilbrainjono.net</p> +<p>Oku no hosomichi</p> +<p>japanese</p> +<p>opera</p> +<p>mexico city</p> </body> <script type="text/javascript" src="jquery.js"></script> +<script type="text/javascript" src="nounClasses.js"></script> +<script type="text/javascript" src="verbClasses.js"></script> +<script type="text/javascript" src="nouns.js"></script> +<script type="text/javascript" src="verbs.js"></script> <script type="text/javascript" src="hip.js"></script> </html>
--- a/hip.js Thu May 29 14:28:50 2008 -0700 +++ b/hip.js Wed Jul 02 12:04:46 2008 -0700 @@ -11,79 +11,23 @@ } function getSelection () { - return $("#selection").attr( "value" ); + return $("#fake-selection").attr( "value" ); } var wordsThatReferToSelection = [ "this", "that", "it", "selection", "him", "her", "them"]; -function NounType( name, expectedWords ) { - this._init( name, expectedWords ); - } -NounType.prototype = { - _init: function( name, expectedWords ) { - this._name = name; - this._expectedWords = expectedWords; // an array - }, - - match: function( fragment ) { - var suggs = this.suggest( fragment ); - // klugy! - if ( suggs.length > 0 ) { - return true; - } - return false; - }, - - suggest: function( fragment ) { - // returns (ordered) array of suggestions - var suggestions = []; - for ( var x in this._expectedWords ) { - word = this._expectedWords[x]; - if ( word.indexOf( fragment ) > -1 ) { - suggestions.push( word ); - // TODO sort these in order of goodness - // todo if fragment is multiple words, search for each of them - // separately within the expected word. - } - } - return suggestions; - } -}; - -// for example.... -var city = new NounType( "city", [ "new york", "los angeles", "mexico city", "sao paulo", "rio de janeiro", "buenos aires", "london", "paris", "moscow", "cairo", "lagos", "tehran", "karachi", "mumbai", "delhi", "kolkata", "jakarta", "manila", "beijing", "singapore", "shanghai", "hong kong", "seoul", "tokyo", "osaka" ] ); - -var language = new NounType( "language", [ "english", "chinese", "hindi", "japanese", "klingon", "esperanto", "sanskrit", "pig latin", "tagalog", "portugese" ] ); - -var tab = new NounType( "tab", [ "gmail", "mozilla developer connection", "xulplanet", "evilbrainjono.net", "google calendar", "humanized enso forum" ] ); - -var person = new NounType( "person", ["atul@mozilla.com", "aza@mozilla.com", "thunder@mozilla.com", "chris@mozilla.com", "myk@mozilla.com" ] ); - -var application = new NounType( "application", ["firefox", "safari", "opera", "internet explorer", "paint.net", "notepad"] ); - -var anyWord = { - // a singleton object which can be used in place of a NounType. - _name: "text", - match: function( fragment ) { - return true; - }, - suggest: function( fragment ) { - return [ fragment ]; - } -}; - function ParsedSentence( verb, DO, modifiers ) { this._init( verb, DO, modifiers ); } ParsedSentence.prototype = { - _init: function( verb, DO, modifiers ) { + _init: function( verb, DO, modifiers ) { /* modifiers is dictionary of preposition: noun */ this._verb = verb; this._DO = DO; this._modifiers = modifiers; }, - getCompletionText: function() { + getCompletionText: function() { // return plain text that we should set the input box to if user hits // space bar on this sentence. var sentence = this._verb._name; @@ -98,7 +42,7 @@ return sentence; }, - getDisplayText: function() { + getDisplayText: function() { // returns html formatted sentence for display in suggestion list var sentence = this._verb._name; if ( this._verb._DOType ) { @@ -119,311 +63,30 @@ return sentence; }, - getDescription: function() { + getDescription: function() { // returns a string describing what the sentence will do if executed return this._verb.getDescription( this._DO, this._modifiers ); } }; - -function Verb( name, DOLabel, DOType, modifiers ) { - this._init( name, DOLabel, DOType, modifiers ); +function QuerySource(verbList) { + this._init(verbList); } -Verb.prototype = { - _init: function( name, DOLabel, DOType, modifiers ) { - this._name = name; - this._DOLabel = DOLabel; - this._DOType = DOType; // must be a NounType. - this._modifiers = modifiers; - // modifiers should be a dictionary - // keys are prepositions - // values are NounTypes. - // example: { "from" : City, "to" : City, "on" : Day } - }, - - getDescription: function( directObject, prepositionPhrases ) { - // returns a string describing what the sentence will do if executed - 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; - var suggestions = []; - var completions = []; - var newFilledMods = {}; - var directObject = ""; - var newCompletions = []; - if ( dictKeys( unfilledMods ).length == 0 ) { - // Done with modifiers, try to parse direct object. - if ( unusedWords.length == 0 || this._DOType == null ) { - // No direct object, either because there are no words left, - // to use, or because the verb can't take a direct object. - // Try parsing sentence without them. - return [ new ParsedSentence( this, "", filledMods ) ]; - } else { - // Transitive verb, can have direct object. Try to use the - // remaining words in that slot. - directObject = unusedWords.join( " " ); - if ( this._DOType.match( directObject ) ) { - // it's a valid direct object. Make a sentence for each - // possible noun completion based on it; return them all. - suggestions = this._DOType.suggest( directObject ); - for ( var x in suggestions ) { - completions.push( new ParsedSentence( this, suggestions[x], - filledMods ) ); - } - return completions; - } else { - // word is invalid direct object. Fail! - return []; - } - } - } else { - // "pop" a preposition off of the properties of unfilledMods - var preposition = dictKeys( unfilledMods )[0]; - // newUnfilledMods is the same as unfilledMods without preposition - var newUnfilledMods = dictDeepCopy( unfilledMods ); - delete newUnfilledMods[preposition]; - - // Look for a match for this preposition - var nounType = unfilledMods[ preposition ]; - var matchIndices = []; - for ( var x = 0; x < unusedWords.length - 1; x++ ) { - if ( preposition.indexOf( unusedWords[x] ) == 0 ) { - if ( nounType.match( unusedWords[ x + 1 ] ) ) { - // Match for the preposition at index x followed by - // an appropriate noun at index x+1 - matchIndices.push( x ); - } - } - } - if ( matchIndices.length > 0 ) { - // Matches found for this preposition! Add to the completions list - // all sentences that can be formed using these matches for this - // preposition. - for ( x in matchIndices ) { - var noun = unusedWords[ matchIndices[x]+1 ]; - var newUnusedWords = unusedWords.slice(); - newUnusedWords.splice( matchIndices[x], 2 ); - directObject = newUnusedWords.join( " " ); - - suggestions = nounType.suggest( noun ); - for ( var y in suggestions ) { - newFilledMods = dictDeepCopy( filledMods ); - newFilledMods[ preposition ] = suggestions[y]; - newCompletions = this.recursiveParse( newUnusedWords, - newFilledMods, - newUnfilledMods ); - completions = completions.concat( newCompletions ); - } - } - } - // If no match was found, all we'll return is one sentence formed by - // leaving that preposition blank. But even if a match was found, we - // still want to include this sentence as an additional possibility. - newFilledMods = dictDeepCopy( filledMods ); - newFilledMods[preposition] = ""; - directObject = unusedWords.join( " " ); - newCompletions = this.recursiveParse( unusedWords, - newFilledMods, - newUnfilledMods ); - completions = completions.concat( newCompletions ); - - return completions; - } - }, - - getCompletions: function( words ) { - /* returns a list of ParsedSentences. */ - /* words is an array of words that were space-separated. - The first word, which matched this verb, has already been removed. - Everything after that is either: - 1. my direct object - 2. a preposition - 3. a noun following a preposition. - */ - - /* Look for words that refer to selection: */ - var completions = []; - var subbedWords = words.slice(); - var selectionUsed = false; - var selection = getSelection(); - if ( selection ) { - for ( var x in wordsThatReferToSelection ) { - var index = subbedWords.indexOf( wordsThatReferToSelection[x] ); - if ( index > -1 ) { - subbedWords.splice( index, 1, selection ); - // Notice the above line doesn't do what I want if selection - // is more than one word. - selectionUsed = true; - } - } - } - if ( selectionUsed ) { - completions = this.recursiveParse( subbedWords, {}, this._modifiers ); - } - - /* Also parse without that substitution, return both ways: */ - var completionsNoSub = this.recursiveParse( words, {}, this._modifiers ); - completions = completions.concat( completionsNoSub ); - return completions; - }, - - match: function( sentence ) { - // returns a float from 0 to 1 telling how good of a match the input - // is to this verb. - if ( this._name.indexOf( sentence ) == 0 ) { - // verb starts with the sentence, i.e. you may be typing this - // verb but haven't typed the full thing yet. - return sentence.length / this._name.length; - } else { - return 0.0; - } - } -}; - -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, { "with": application } ); -open.getDescription = function( directObject, mods ) { - if (directObject ) { - var desc = "Open the URL "" + directObject + """; - } else { - var desc = "Open a given URL"; - } - if ( mods["with"] ) { - desc += " using the application "" + mods["with"] + """; - } - return desc; -}; -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 ]; - -function QuerySource() { - this._init( ); - } QuerySource.prototype = { - _init: function( ) { + _init: function(verbList) { + this._verbList = verbList; //arrayof Verb objects to use for completions this._lockedInSentence = null; this._hilitedSuggestion = 0; this._suggestionList = []; // a list of ParsedSentences. }, - - updateSuggestionList: function( query ) { + + updateSuggestionList: function( query ) { this._suggestionList = []; var completions = []; var words = query.split( " " ); - for ( var x in verbs ) { - var verb = verbs[x]; + for ( var x in this._verbList ) { + var verb = this._verbList[x]; if ( verb.match( words[0] ) ) { completions = verb.getCompletions( words.slice(1) ); this._suggestionList = this._suggestionList.concat( completions ); @@ -433,13 +96,13 @@ this._hilitedSuggestion = 1; // hilight the first suggestion by default }, - getSuggestionsAsHtml : function() { + getSuggestionsAsHtml : function() { return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ]; }, - getDescriptionText: function() { + getDescriptionText: function() { if ( this._suggestionList.length == 0 ) { - return "Welcome to Ubiquity. Type some commands!"; + return "You got the magic stick. Type some commands!"; } var h = this._hilitedSuggestion; if ( h == 0 ) { @@ -451,43 +114,44 @@ return sentence.getDescription(); }, - indicationDown: function( ) { + indicationDown: function( ) { this._hilitedSuggestion ++; if ( this._hilitedSuggestion > this._suggestionList.length ) { this._hilitedSuggestion = 0; } }, - indicationUp: function() { + indicationUp: function() { this._hilitedSuggestion --; if ( this._hilitedSuggestion < 0 ) { this._hilitedSuggestion = this._suggestionList.length; } }, - getHilitedSuggestion: function() { + getHilitedSuggestion: function() { return this._hilitedSuggestion - 1; // because 0 means no hilite // and the suggestion list starts at 1... fencepost! }, - autocomplete: function( query ) { + autocomplete: function( query ) { + var newText; var hilited = this.getHilitedSuggestion(); if ( hilited > -1 ) { - var newText = this._suggestionList[ hilited ].getCompletionText() + " "; + newText = this._suggestionList[ hilited ].getCompletionText() + " "; } else { newText = query; } return newText; }, - clear: function() { + clear: function() { this._suggestionList = []; this._hilitedSuggestion = 0; - lockedInSentence = null; + this._lockedInSentence = null; } }; -var gQs = new QuerySource(); +var gQs = new QuerySource([ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ]); function makeSuggestionHtml( tagName, list, hilitedNumber ) { var result = ""; @@ -524,7 +188,7 @@ // TODO: if the event is an 'esc' key, clear the input field. // If the event is an 'up arrow' or 'down arrow' key, change the // indication. - + // key is event.which // esc is 27 // up arrow is 38 @@ -560,7 +224,7 @@ } $(document).ready( function() { - $("#status-line").html( "Welcome to Ubiquity." ); + $("#status-line").html( "All you gotta do is rub that lamp." ); $("#search-box").focus(); $("#search-box").keyup( searchBoxQuery ); $("#autocomplete-popup").css(
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nounClasses.js Wed Jul 02 12:04:46 2008 -0700 @@ -0,0 +1,44 @@ +function NounType( name, expectedWords ) { + this._init( name, expectedWords ); +} +NounType.prototype = { + _init: function( name, expectedWords ) { + this._name = name; + this._expectedWords = expectedWords; // an array + }, + + match: function( fragment ) { + var suggs = this.suggest( fragment ); + // klugy! + if ( suggs.length > 0 ) { + return true; + } + return false; + }, + + suggest: function( fragment ) { + // returns (ordered) array of suggestions + var suggestions = []; + for ( var x in this._expectedWords ) { + word = this._expectedWords[x]; + if ( word.indexOf( fragment ) > -1 ) { + suggestions.push( word ); + // TODO sort these in order of goodness + // todo if fragment is multiple words, search for each of them + // separately within the expected word. + } + } + return suggestions; + } +}; + +var anyWord = { + // a singleton object which can be used in place of a NounType. + _name: "text", + match: function( fragment ) { + return true; + }, + suggest: function( fragment ) { + return [ fragment ]; + } +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nouns.js Wed Jul 02 12:04:46 2008 -0700 @@ -0,0 +1,12 @@ + + +// for example.... +var city = new NounType( "city", [ "new york", "los angeles", "mexico city", "sao paulo", "rio de janeiro", "buenos aires", "london", "paris", "moscow", "cairo", "lagos", "tehran", "karachi", "mumbai", "delhi", "kolkata", "jakarta", "manila", "beijing", "singapore", "shanghai", "hong kong", "seoul", "tokyo", "osaka" ] ); + +var language = new NounType( "language", [ "english", "chinese", "hindi", "japanese", "klingon", "esperanto", "sanskrit", "pig latin", "tagalog", "portugese" ] ); + +var tab = new NounType( "tab", [ "gmail", "mozilla developer connection", "xulplanet", "evilbrainjono.net", "google calendar", "humanized enso forum" ] ); + +var person = new NounType( "person", ["atul@mozilla.com", "aza@mozilla.com", "thunder@mozilla.com", "chris@mozilla.com", "myk@mozilla.com" ] ); + +var application = new NounType( "application", ["firefox", "safari", "opera", "internet explorer", "paint.net", "notepad"] );
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/verbClasses.js Wed Jul 02 12:04:46 2008 -0700 @@ -0,0 +1,159 @@ +function Verb( name, DOLabel, DOType, modifiers ) { + this._init( name, DOLabel, DOType, modifiers ); +} +Verb.prototype = { + _init: function( name, DOLabel, DOType, modifiers ) { + this._name = name; + this._DOLabel = DOLabel; + this._DOType = DOType; // must be a NounType. + this._modifiers = modifiers; + // modifiers should be a dictionary + // keys are prepositions + // values are NounTypes. + // example: { "from" : City, "to" : City, "on" : Day } + }, + + getDescription: function( directObject, prepositionPhrases ) { + // returns a string describing what the sentence will do if executed + 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; + var suggestions = []; + var completions = []; + var newFilledMods = {}; + var directObject = ""; + var newCompletions = []; + if ( dictKeys( unfilledMods ).length == 0 ) { + // Done with modifiers, try to parse direct object. + if ( unusedWords.length == 0 || this._DOType == null ) { + // No direct object, either because there are no words left, + // to use, or because the verb can't take a direct object. + // Try parsing sentence without them. + return [ new ParsedSentence( this, "", filledMods ) ]; + } else { + // Transitive verb, can have direct object. Try to use the + // remaining words in that slot. + directObject = unusedWords.join( " " ); + if ( this._DOType.match( directObject ) ) { + // it's a valid direct object. Make a sentence for each + // possible noun completion based on it; return them all. + suggestions = this._DOType.suggest( directObject ); + for ( var x in suggestions ) { + completions.push( new ParsedSentence( this, suggestions[x], + filledMods ) ); + } + return completions; + } else { + // word is invalid direct object. Fail! + return []; + } + } + } else { + // "pop" a preposition off of the properties of unfilledMods + var preposition = dictKeys( unfilledMods )[0]; + // newUnfilledMods is the same as unfilledMods without preposition + var newUnfilledMods = dictDeepCopy( unfilledMods ); + delete newUnfilledMods[preposition]; + + // Look for a match for this preposition + var nounType = unfilledMods[ preposition ]; + var matchIndices = []; + for ( var x = 0; x < unusedWords.length - 1; x++ ) { + if ( preposition.indexOf( unusedWords[x] ) == 0 ) { + if ( nounType.match( unusedWords[ x + 1 ] ) ) { + // Match for the preposition at index x followed by + // an appropriate noun at index x+1 + matchIndices.push( x ); + } + } + } + if ( matchIndices.length > 0 ) { + // Matches found for this preposition! Add to the completions list + // all sentences that can be formed using these matches for this + // preposition. + for ( x in matchIndices ) { + var noun = unusedWords[ matchIndices[x]+1 ]; + var newUnusedWords = unusedWords.slice(); + newUnusedWords.splice( matchIndices[x], 2 ); + directObject = newUnusedWords.join( " " ); + + suggestions = nounType.suggest( noun ); + for ( var y in suggestions ) { + newFilledMods = dictDeepCopy( filledMods ); + newFilledMods[ preposition ] = suggestions[y]; + newCompletions = this.recursiveParse( newUnusedWords, + newFilledMods, + newUnfilledMods ); + completions = completions.concat( newCompletions ); + } + } + } + // If no match was found, all we'll return is one sentence formed by + // leaving that preposition blank. But even if a match was found, we + // still want to include this sentence as an additional possibility. + newFilledMods = dictDeepCopy( filledMods ); + newFilledMods[preposition] = ""; + directObject = unusedWords.join( " " ); + newCompletions = this.recursiveParse( unusedWords, + newFilledMods, + newUnfilledMods ); + completions = completions.concat( newCompletions ); + + return completions; + } + }, + + getCompletions: function( words ) { + /* returns a list of ParsedSentences. */ + /* words is an array of words that were space-separated. + The first word, which matched this verb, has already been removed. + Everything after that is either: + 1. my direct object + 2. a preposition + 3. a noun following a preposition. + */ + + /* Look for words that refer to selection: */ + var completions = []; + var subbedWords = words.slice(); + var selectionUsed = false; + var selection = getSelection(); + if ( selection ) { + for ( var x in wordsThatReferToSelection ) { + var index = subbedWords.indexOf( wordsThatReferToSelection[x] ); + if ( index > -1 ) { + subbedWords.splice( index, 1, selection ); + // Notice the above line doesn't do what I want if selection + // is more than one word. + selectionUsed = true; + } + } + } + if ( selectionUsed ) { + completions = this.recursiveParse( subbedWords, {}, this._modifiers ); + } + + /* Also parse without that substitution, return both ways: */ + var completionsNoSub = this.recursiveParse( words, {}, this._modifiers ); + completions = completions.concat( completionsNoSub ); + return completions; + }, + + match: function( sentence ) { + // returns a float from 0 to 1 telling how good of a match the input + // is to this verb. + if ( this._name.indexOf( sentence ) == 0 ) { + // verb starts with the sentence, i.e. you may be typing this + // verb but haven't typed the full thing yet. + return sentence.length / this._name.length; + } else { + return 0.0; + } + } +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/verbs.js Wed Jul 02 12:04:46 2008 -0700 @@ -0,0 +1,118 @@ +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, { "with": application } ); +open.getDescription = function( directObject, mods ) { + if (directObject ) { + var desc = "Open the URL "" + directObject + """; + } else { + var desc = "Open a given URL"; + } + if ( mods["with"] ) { + desc += " using the application "" + mods["with"] + """; + } + return desc; +}; +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; +};