annotate nounClasses.js @ 15:21bb73c5d9e1

Tiny fix to make nouns never match the empty string, so you don't get that weird thing where your input is empty and it's suggesting a list of cities.
author jonathandicarlo@jonathan-dicarlos-macbook-pro.local
date Mon, 07 Jul 2008 13:00:19 -0700
parents 8dcc88f93829
children 5fce4c8f3ebd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
1 function NounType( name, expectedWords ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
2 this._init( name, expectedWords );
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
3 }
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
4 NounType.prototype = {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
5 _init: function( name, expectedWords ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
6 this._name = name;
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
7 this._expectedWords = expectedWords; // an array
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
8 },
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
9
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
10 match: function( fragment ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
11 var suggs = this.suggest( fragment );
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
12 // klugy!
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
13 if ( suggs.length > 0 ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
14 return true;
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
15 }
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
16 return false;
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
17 },
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
18
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
19 suggest: function( fragment ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
20 // returns (ordered) array of suggestions
15
21bb73c5d9e1 Tiny fix to make nouns never match the empty string, so you don't get that weird thing where your input is empty and it's suggesting a list of cities.
jonathandicarlo@jonathan-dicarlos-macbook-pro.local
parents: 13
diff changeset
21 if (!fragment) {
21bb73c5d9e1 Tiny fix to make nouns never match the empty string, so you don't get that weird thing where your input is empty and it's suggesting a list of cities.
jonathandicarlo@jonathan-dicarlos-macbook-pro.local
parents: 13
diff changeset
22 return [];
21bb73c5d9e1 Tiny fix to make nouns never match the empty string, so you don't get that weird thing where your input is empty and it's suggesting a list of cities.
jonathandicarlo@jonathan-dicarlos-macbook-pro.local
parents: 13
diff changeset
23 }
13
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
24 var suggestions = [];
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
25 for ( var x in this._expectedWords ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
26 word = this._expectedWords[x];
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
27 if ( word.indexOf( fragment ) > -1 ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
28 suggestions.push( word );
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
29 // TODO sort these in order of goodness
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
30 // todo if fragment is multiple words, search for each of them
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
31 // separately within the expected word.
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
32 }
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
33 }
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
34 return suggestions;
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
35 }
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
36 };
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
37
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
38 var anyWord = {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
39 // a singleton object which can be used in place of a NounType.
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
40 _name: "text",
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
41 match: function( fragment ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
42 return true;
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
43 },
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
44 suggest: function( fragment ) {
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
45 return [ fragment ];
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
46 }
8dcc88f93829 Broke up hip.js into some smaller js files
jonathandicarlo@localhost
parents:
diff changeset
47 };