view hip.js @ 18:5fce4c8f3ebd

Preliminary version of date stuff added
author jonathandicarlo@jonathan-dicarlos-macbook-pro.local
date Tue, 15 Jul 2008 14:10:17 -0700
parents a26372118854
children
line wrap: on
line source

function dictDeepCopy( dict ) {
  var newDict = {};
  for (var i in dict ) {
    newDict[i] = dict[i];
  }
  return newDict;
};

function dictKeys( dict ) {
  return [ key for ( key in dict ) ];
}

function getSelection () {
  return $("#fake-selection").attr( "value" );
}

var wordsThatReferToSelection = [ "this", "that", "it", "selection", "him", "her", "them"];

function ParsedSentence( verb, DO, modifiers ) {
  this._init( verb, DO, modifiers );
}
ParsedSentence.prototype = {
  _init: function( verb, DO, modifiers ) {
    /* modifiers is dictionary of preposition: noun */
    this._verb = verb;
    this._DO = DO;
    this._modifiers = modifiers;
  },

  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;
    if ( this._DO ) {
      sentence = sentence + " " + this._DO;
    }
    for ( var x in this._modifiers ) {
      if ( this._modifiers[x] ) {
	sentence = sentence + " " + x + " " + this._modifiers[x];
      }
    }
    return sentence;
  },

  getDisplayText: function() {
    // returns html formatted sentence for display in suggestion list
    var sentence = this._verb._name;
    if ( this._verb._DOType ) {
      if ( this._DO ) {
	sentence = sentence + " " + this._DO;
      } else {
	sentence = sentence + " <span class=\"needarg\">(" + this._verb._DOLabel + ")</span>";
      }
    }

    for ( var x in this._verb._modifiers ) {  // was this._verb._modifiers
      if ( this._modifiers[ x ] ) {
	sentence = sentence + " <b>" +  x + " " + this._modifiers[x] + "</b>";
      } else {
	sentence = sentence + " <span class=\"needarg\">(" + x + " " + this._verb._modifiers[x]._name + ")</span>";
      }
    }
    return sentence;
  },

  getDescription: function() {
    // returns a string describing what the sentence will do if executed
    return this._verb.getDescription( this._DO, this._modifiers );
  }

};


var gQs = new QuerySource([ fly, define, google, go, close, open, translate, email, nuke, encrypt, wiki, calendarize ],
			  [city, language, tab, person, application, DateNounType, AddressNounType]);

function makeSuggestionHtml( tagName, list, hilitedNumber ) {
  var result = "";
  var openingTag = "";
  var closingTag = "</" + tagName + ">";

  for (var i = 0; i < list.length; i++) {
    if ( i == hilitedNumber ) {
      openingTag = "<" + tagName + " class=\"hilited\">";
    } else {
      openingTag = "<" + tagName + ">";
    }
    result += (openingTag + list[i] + closingTag );
  }
  return result;
}

function updateDisplay( number ) {
   var suggestions = gQs.getSuggestionsAsHtml();
   var hilitedSuggestion = gQs.getHilitedSuggestion();
   var description = gQs.getDescriptionText();
   $("#status-line").html( description );
   if ( hilitedSuggestion == -1 ) {
     // TODO set #search-box background to green, not sure how
   } else {
     // TODO set #search-box background to white, not sure how
   }
   var ac = $("#autocomplete-popup");
   ac.html( makeSuggestionHtml( "div", suggestions, hilitedSuggestion ) );
   ac.show();
}

function searchBoxQuery( event ) {
  // 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
  // down arrow is 40
  // enter is 13
  // space is 32
  var input = event.target.value;
  switch( event.which ) {
  case 27: //esc
    event.target.value = "";
    gQs.clear();
    break;
  case 38: // up arrow
    gQs.indicationUp();
    break;
  case 40: // down arrow
    gQs.indicationDown();
    break;
  case 13: // enter
    gQs.execute();
    break;
  case 32: // spacebar
    //input = gQs.autocomplete( input );
    gQs.updateSuggestionList( input );
    break;
  default:
    gQs.updateSuggestionList( input );
    break;
  }

  updateDisplay( event.which );

}

$(document).ready( function() {
    $("#status-line").html( "All you gotta do is rub that lamp." );
    $("#search-box").focus();
    $("#search-box").keyup( searchBoxQuery );
    $("#autocomplete-popup").css(
        "width",
        $("#search-box").css("width")
    );
});

/* Where to go next:

1. Noun-first completions.  If you type "london", it matches no verb, so
the parser looks at the known noun categories, sees that "london" could be
a city, and then looks for verbs that take cities and suggests
fly to london
fly from london
nuke london

3. Allow the possibility of a multiple-word indirect-object.
(Maybe pass the whole rest of the sentence past the preposition as an argument
to the noun class, and as the noun class returns the list of suggestions,
it also returns a count of how many words it has used.)

4. Preview output of topmost or hilited command, as you type.

5. When a command is executed, put its output in a special buffer that forms
the implicit selection for the next command.  In this way commands can be
chained conversationally, without the need for a special word or key to
link them together.

6. More complex datatypes, like dates, that allow for multiple input formats.
( see http://www.datejs.com/ )

7. Make verbs more tolerant of text that doesn't match the direct object.
First of all, if the direct object text is just spaces (and how does that
happen?) it should be discarded.  And then, if there's input that can't
be used, maybe discard it or put it in red or something, rather than
rejecting it.

8. Tweak what text gets autocompleted when you arrow down to something
and hit space.  Right now it's putting in prepositions that you haven't
actually typed.

9. Learn how frequently you use different completions (use the algorithm
   we've already developed) and rank accordingly.

10. Start sorting matches by goodness.

11. Have nounTypes that can learn new entries!!!

12. Make it so when hilite is 0, the literal input is hilited green
    and an appropriate suggestion is displayed!

13. Break this file into multiple files, it's getting really big.

14. Use Tab to mean "copy hilited suggestion to input box"
    (No, we can't do that!  Because tab changes focus away from the input
    box.)

*/