-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.gertrude.js
71 lines (68 loc) · 2.91 KB
/
jquery.gertrude.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*!
* Gertrude plugin for jQuery v0.0.1
* https://github.com/richmarr/gertrude
* Copyright 2011, Richard Marr
* Free to use, modify or redistribute.
*/
(function($) {
// Module libs, exposed with a namespace in case they're unexpectedly useful
$.gertrude = {
// Takes an input string, entity definition, and optional string methods and returns matches
textToEntities : function( entities, tokeniser, normaliser, input ){
var words = tokeniser ? tokeniser(input) : input.split(/[\s\.,]+/ig); // use a custom tokeniser or [\s\.,]
if (!normaliser) normaliser = function(a){return a;}; // default to null normaliser
var found = [];
for ( var i = 0; i < words.length; i++ )
{
// need a clean way to associate adjectives/modifiers with nouns/entities
var entity = entities[normaliser(words[i])];
if ( entity ) found.push(entity);
}
return found;
},
// This just iterates through two arrays testing for equality. Must be a better way to do this.
entitylistequals = function( a, b ){
if ( a.length != b.length ) return false;
for ( var i = 0; i < a.length; i++ )
{
if ( a[i] != b[i] ) return false;
}
return true;
}
};
/*
* This is the public method used to bind DOM elements to the entity recognition
* and downstream event handlers. Usage is (predictably) as follows:
* $("#foo .bar").entitychange( options );
* options takes the form:
* {
* entities : {
* foo : {custom:'any customer properties make this a recognisable entity'},
* bar : {modifier:'this property means this word is attached to a nearby entity'}
* }
* entitychange : function( event, discoveredEntities ) { ... handler code here ... },
* normaliser : function( stringToNormalise ) { ... optional function to stem or depluralise terms ... },
* tokeniser : function( stringToTokenise ) { ... optional function to tokenise a string into an array of strings ... }
* }
*/
$.fn.entitychange = function(opt){
var entities = opt.entities, fn = opt.entitychange, tokeniser = opt.tokeniser, normaliser = opt.normaliser;
// bind the client code's handler to the chosen DOM objects
var $this = $(this);
if ( fn ) $this.bind('entitychange',fn);
// bind the entity analysis to each key action
$this.keyup(function(e){
// currently a brute-force implementation that may suffer poor performance with larger bodies of text
var el = $(this);
var found = $.gertrude.textToEntities( entities, tokeniser, normaliser, el.val() ); // get the current entity set
if ( found.length > 0 && !$.gertrude.entitylistequals(found,el.data("gertrude.entities")) ) {
el.trigger({
type:"entitychange",
found:found,
latest:found[found.length-1] // assumes incorrectly that the latest is the new one?
}); // if there's a new entity then trigger the event on this DOM object
}
el.data("gertrude.entities", found); // save to do a diff later
});
}
})(jQuery);