annotate querySource.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
1 function QuerySource(verbList, nounList) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
2 this._init(verbList, nounList);
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
3 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
4 QuerySource.prototype = {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
5 _init: function(verbList, nounList) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
6 this._verbList = verbList; //arrayof Verb objects to use for completions
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
7 this._nounTypeList = nounList;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
8 this._lockedInSentence = null;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
9 this._hilitedSuggestion = 0;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
10 this._suggestionList = []; // a list of ParsedSentences.
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
11 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
12
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
13 updateSuggestionList: function( query ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
14 this._suggestionList = [];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
15 var completions = [];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
16 var x, y;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
17 var nounType, verb;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
18 var words = query.split( " " );
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
19 // verb-first matches
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
20 for ( x in this._verbList ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
21 verb = this._verbList[x];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
22 if ( verb.match( words[0] ) ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
23 completions = verb.getCompletions( words.slice(1) );
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
24 this._suggestionList = this._suggestionList.concat(completions);
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
25 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
26 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
27 // noun-first matches
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
28 if (this._suggestionList.length == 0 ){
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
29 for (x in this._nounTypeList) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
30 nounType = this._nounTypeList[x];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
31 if (nounType.match( words[0] ) ){
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
32 for (y in this._verbList) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
33 verb = this._verbList[y];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
34 var prefix = verb.canPossiblyUseNounType(nounType);
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
35 if (prefix) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
36 var betterSentence = prefix + " " + query;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
37 words = betterSentence.split( " " );
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
38 completions = verb.getCompletions(words.slice(1));
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
39 this._suggestionList = this._suggestionList.concat(completions);
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
40 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
41 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
42 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
43 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
44 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
45
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
46 // TODO sort in order of match quality
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
47 this._hilitedSuggestion = 1; // hilight the first suggestion by default
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
48 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
49
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
50 getSuggestionsAsHtml : function() {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
51 return [ this._suggestionList[x].getDisplayText() for ( x in this._suggestionList ) ];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
52 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
53
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
54 getDescriptionText: function() {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
55 if ( this._suggestionList.length == 0 ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
56 return "You got the magic stick. Type some commands!";
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
57 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
58 var h = this._hilitedSuggestion;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
59 if ( h == 0 ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
60 return "Executes your input literally, with no autocompletion.";
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
61 } else {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
62 h = h - 1;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
63 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
64 var sentence = this._suggestionList[h];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
65 return sentence.getDescription();
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
66 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
67
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
68 indicationDown: function( ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
69 this._hilitedSuggestion ++;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
70 if ( this._hilitedSuggestion > this._suggestionList.length ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
71 this._hilitedSuggestion = 0;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
72 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
73 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
74
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
75 indicationUp: function() {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
76 this._hilitedSuggestion --;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
77 if ( this._hilitedSuggestion < 0 ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
78 this._hilitedSuggestion = this._suggestionList.length;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
79 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
80 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
81
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
82 getHilitedSuggestion: function() {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
83 return this._hilitedSuggestion - 1; // because 0 means no hilite
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
84 // and the suggestion list starts at 1... fencepost!
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
85 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
86
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
87 autocomplete: function( query ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
88 var newText;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
89 var hilited = this.getHilitedSuggestion();
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
90 if ( hilited > -1 ) {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
91 newText = this._suggestionList[ hilited ].getCompletionText() + " ";
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
92 } else {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
93 newText = query;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
94 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
95 return newText;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
96 },
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
97
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
98 clear: function() {
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
99 this._suggestionList = [];
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
100 this._hilitedSuggestion = 0;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
101 this._lockedInSentence = null;
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
102 }
a26372118854 Created a primitive form of noun-first suggestion.
jonathandicarlo@localhost
parents:
diff changeset
103 };