Mercurial > hip
changeset 14:a26372118854
Created a primitive form of noun-first suggestion.
author | jonathandicarlo@localhost |
---|---|
date | Wed, 02 Jul 2008 16:50:15 -0700 |
parents | 8dcc88f93829 |
children | 21bb73c5d9e1 |
files | hip.html hip.js nouns.js querySource.js verbClasses.js |
diffstat | 5 files changed, 129 insertions(+), 81 deletions(-) [+] |
line wrap: on
line diff
--- a/hip.html Wed Jul 02 12:04:46 2008 -0700 +++ b/hip.html Wed Jul 02 16:50:15 2008 -0700 @@ -29,5 +29,6 @@ <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="querySource.js"></script> <script type="text/javascript" src="hip.js"></script> </html>
--- a/hip.js Wed Jul 02 12:04:46 2008 -0700 +++ b/hip.js Wed Jul 02 16:50:15 2008 -0700 @@ -70,88 +70,9 @@ }; -function QuerySource(verbList) { - this._init(verbList); -} -QuerySource.prototype = { - _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 ) { - this._suggestionList = []; - var completions = []; - var words = query.split( " " ); - 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 ); - } - } - // TODO sort in order of match quality - this._hilitedSuggestion = 1; // hilight the first suggestion by default - }, - - getSuggestionsAsHtml : function() { - return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ]; - }, - - getDescriptionText: function() { - if ( this._suggestionList.length == 0 ) { - return "You got the magic stick. Type some commands!"; - } - var h = this._hilitedSuggestion; - if ( h == 0 ) { - return "Executes your input literally, with no autocompletion."; - } else { - h = h - 1; - } - var sentence = this._suggestionList[h]; - return sentence.getDescription(); - }, - - indicationDown: function( ) { - this._hilitedSuggestion ++; - if ( this._hilitedSuggestion > this._suggestionList.length ) { - this._hilitedSuggestion = 0; - } - }, - - indicationUp: function() { - this._hilitedSuggestion --; - if ( this._hilitedSuggestion < 0 ) { - this._hilitedSuggestion = this._suggestionList.length; - } - }, - - getHilitedSuggestion: function() { - return this._hilitedSuggestion - 1; // because 0 means no hilite - // and the suggestion list starts at 1... fencepost! - }, - - autocomplete: function( query ) { - var newText; - var hilited = this.getHilitedSuggestion(); - if ( hilited > -1 ) { - newText = this._suggestionList[ hilited ].getCompletionText() + " "; - } else { - newText = query; - } - return newText; - }, - - clear: function() { - this._suggestionList = []; - this._hilitedSuggestion = 0; - this._lockedInSentence = null; - } -}; - -var gQs = new QuerySource([ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ]); +var gQs = new QuerySource([ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki ], + [city, language, tab, person, application]); function makeSuggestionHtml( tagName, list, hilitedNumber ) { var result = "";
--- a/nouns.js Wed Jul 02 12:04:46 2008 -0700 +++ b/nouns.js Wed Jul 02 16:50:15 2008 -0700 @@ -10,3 +10,10 @@ 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"] ); + +/*todo nouns are gonna need to be able to fuzzymatch + * dates (for add-to-calendar) + * addresses (for map) + * email addreses; names associated with email addresses + * + */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/querySource.js Wed Jul 02 16:50:15 2008 -0700 @@ -0,0 +1,103 @@ +function QuerySource(verbList, nounList) { + this._init(verbList, nounList); +} +QuerySource.prototype = { + _init: function(verbList, nounList) { + this._verbList = verbList; //arrayof Verb objects to use for completions + this._nounTypeList = nounList; + this._lockedInSentence = null; + this._hilitedSuggestion = 0; + this._suggestionList = []; // a list of ParsedSentences. + }, + + updateSuggestionList: function( query ) { + this._suggestionList = []; + var completions = []; + var x, y; + var nounType, verb; + var words = query.split( " " ); + // verb-first matches + for ( x in this._verbList ) { + verb = this._verbList[x]; + if ( verb.match( words[0] ) ) { + completions = verb.getCompletions( words.slice(1) ); + this._suggestionList = this._suggestionList.concat(completions); + } + } + // noun-first matches + if (this._suggestionList.length == 0 ){ + for (x in this._nounTypeList) { + nounType = this._nounTypeList[x]; + if (nounType.match( words[0] ) ){ + for (y in this._verbList) { + verb = this._verbList[y]; + var prefix = verb.canPossiblyUseNounType(nounType); + if (prefix) { + var betterSentence = prefix + " " + query; + words = betterSentence.split( " " ); + completions = verb.getCompletions(words.slice(1)); + this._suggestionList = this._suggestionList.concat(completions); + } + } + } + } + } + + // TODO sort in order of match quality + this._hilitedSuggestion = 1; // hilight the first suggestion by default + }, + + getSuggestionsAsHtml : function() { + return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ]; + }, + + getDescriptionText: function() { + if ( this._suggestionList.length == 0 ) { + return "You got the magic stick. Type some commands!"; + } + var h = this._hilitedSuggestion; + if ( h == 0 ) { + return "Executes your input literally, with no autocompletion."; + } else { + h = h - 1; + } + var sentence = this._suggestionList[h]; + return sentence.getDescription(); + }, + + indicationDown: function( ) { + this._hilitedSuggestion ++; + if ( this._hilitedSuggestion > this._suggestionList.length ) { + this._hilitedSuggestion = 0; + } + }, + + indicationUp: function() { + this._hilitedSuggestion --; + if ( this._hilitedSuggestion < 0 ) { + this._hilitedSuggestion = this._suggestionList.length; + } + }, + + getHilitedSuggestion: function() { + return this._hilitedSuggestion - 1; // because 0 means no hilite + // and the suggestion list starts at 1... fencepost! + }, + + autocomplete: function( query ) { + var newText; + var hilited = this.getHilitedSuggestion(); + if ( hilited > -1 ) { + newText = this._suggestionList[ hilited ].getCompletionText() + " "; + } else { + newText = query; + } + return newText; + }, + + clear: function() { + this._suggestionList = []; + this._hilitedSuggestion = 0; + this._lockedInSentence = null; + } +};
--- a/verbClasses.js Wed Jul 02 12:04:46 2008 -0700 +++ b/verbClasses.js Wed Jul 02 16:50:15 2008 -0700 @@ -145,6 +145,22 @@ return completions; }, + canPossiblyUseNounType: function(nounType){ + //returns the words that would be implied before the noun could makes sense, + //i.e. put these words before the noun and try again. + if (this._DOType == nounType ) { + return this._name; + } + for( var prep in this._modifiers ) { + if (this._modifiers[prep] == nounType) { + return this._name + " " + prep; + // TODO returning multiple preps when more than one could use the + // nountype + } + } + return false; + }, + match: function( sentence ) { // returns a float from 0 to 1 telling how good of a match the input // is to this verb.