Skip to content
This repository
Browse code

added testcases for Zemanta enhancement; fixed some stanbol testcases

  • Loading branch information...
commit fb1fe3f43a34879cb9867e4e496e117ff7dde2df 1 parent 75c70f7
authored April 20, 2012
1  .gitignore
@@ -7,3 +7,4 @@ docs
7 7
 target
8 8
 node_modules
9 9
 npm-debug.log
  10
+test/service/api_keys.js
200  src/service/ZemantaService.js
@@ -28,7 +28,7 @@ VIE.prototype.ZemantaService = function(options) {
28 28
         name : 'zemanta',
29 29
         /* you can pass an array of URLs which are then tried sequentially */
30 30
         url: ["http://api.zemanta.com/services/rest/0.0/"],
31  
-        timeout : 60000, /* 60 seconds timeout */
  31
+        timeout : 20000, /* 20 seconds timeout */
32 32
         namespaces : {
33 33
         	zemanta: "http://s.zemanta.com/ns#"
34 34
         },
@@ -44,7 +44,8 @@ VIE.prototype.ZemantaService = function(options) {
44 44
                     '?entity zemanta:hasEntityAnnotation ?subject'
45 45
                 ]
46 46
             }
47  
-         ]
  47
+         ],
  48
+         "api_key" : undefined
48 49
     };
49 50
     /* the options are merged with the default options */
50 51
     this.options = jQuery.extend(true, defaults, options ? options : {});
@@ -70,7 +71,7 @@ VIE.prototype.ZemantaService.prototype = {
70 71
 // **Throws**:  
71 72
 // *nothing*  
72 73
 // **Returns**:  
73  
-// *{VIE.StanbolService}* : The VIE.StanbolService instance itself.  
  74
+// *{VIE.ZemantaService}* : The VIE.ZemantaService instance itself.  
74 75
 // **Example usage**:  
75 76
 //
76 77
 //     var service = new vie.ZemantaService({<some-configuration>});
@@ -86,9 +87,9 @@ VIE.prototype.ZemantaService.prototype = {
86 87
         this.rules = jQuery.merge(this.rules, (this.options.rules) ? this.options.rules : []);
87 88
         
88 89
         this.connector = new this.vie.ZemantaConnector(this.options);
89  
-        
  90
+
90 91
         /* adding these entity types to VIE helps later the querying */
91  
-        this.vie.types.addOrOverwrite('zemanta:EntityAnnotation', [
  92
+        this.vie.types.addOrOverwrite('zemanta:Recognition', [
92 93
             /*TODO: add attributes */
93 94
         ]).inherit("owl:Thing");
94 95
     },
@@ -100,7 +101,7 @@ VIE.prototype.ZemantaService.prototype = {
100 101
 // **Throws**:  
101 102
 // *{Error}* if an invalid VIE.Findable is passed.  
102 103
 // **Returns**:  
103  
-// *{VIE.ZemantaService}* : The VIE.ZemantaService instance itself.  
  104
+// *{VIE.StanbolService}* : The VIE.ZemantaService instance itself.  
104 105
 // **Example usage**:  
105 106
 //
106 107
 //     var service = new vie.ZemantaService({<some-configuration>});
@@ -118,7 +119,6 @@ VIE.prototype.ZemantaService.prototype = {
118 119
         var text = service._extractText(element);
119 120
 
120 121
         if (text.length > 0) {
121  
-            /* query enhancer with extracted text */
122 122
             var success = function (results) {
123 123
                 _.defer(function(){
124 124
                     var entities = VIE.Util.rdf2Entities(service, results);
@@ -128,8 +128,10 @@ VIE.prototype.ZemantaService.prototype = {
128 128
             var error = function (e) {
129 129
                 analyzable.reject(e);
130 130
             };
  131
+            
  132
+            var options = {};
131 133
 
132  
-            this.connector.analyze(text, success, error);
  134
+            this.connector.analyze(text, success, error, options);
133 135
 
134 136
         } else {
135 137
             console.warn("No text found in element.");
@@ -138,21 +140,9 @@ VIE.prototype.ZemantaService.prototype = {
138 140
 
139 141
     },
140 142
 
141  
-    // this private method extracts text from a jQuery element
  143
+    /* this private method extracts the outerHTML from a jQuery element */
142 144
     _extractText: function (element) {
143  
-        if (element.get(0) &&
144  
-            element.get(0).tagName &&
145  
-            (element.get(0).tagName == 'TEXTAREA' ||
146  
-            element.get(0).tagName == 'INPUT' && element.attr('type', 'text'))) {
147  
-            return element.get(0).val();
148  
-        }
149  
-        else {
150  
-            var res = element
151  
-                .text()    /* get the text of element */
152  
-                .replace(/\s+/g, ' ') /* collapse multiple whitespaces */
153  
-                .replace(/\0\b\n\r\f\t/g, ''); /* remove non-letter symbols */
154  
-            return jQuery.trim(res);
155  
-        }
  145
+        return jQuery(element).wrap("<div>").parent().html();
156 146
     }
157 147
 };
158 148
 
@@ -164,17 +154,86 @@ VIE.prototype.ZemantaService.prototype = {
164 154
 // **Throws**:  
165 155
 // *nothing*  
166 156
 // **Returns**:  
167  
-// *{VIE.ZemantaService}* : The **new** VIE.ZemantaService instance.  
  157
+// *{VIE.ZemantaConnector}* : The **new** VIE.ZemantaConnector instance.  
168 158
 // **Example usage**:  
169 159
 //
170 160
 //     var conn = new vie.ZemantaConnector({<some-configuration>});
171 161
 VIE.prototype.ZemantaConnector = function (options) {
172  
-    this.options = options;
  162
+    
  163
+    var defaults =  {
  164
+		/* you can pass an array of URLs which are then tried sequentially */
  165
+	    url: ["http://api.zemanta.com/services/rest/0.0/"],
  166
+	    timeout : 20000, /* 20 seconds timeout */
  167
+        "api_key" : undefined
  168
+    };
  169
+
  170
+    /* the options are merged with the default options */
  171
+    this.options = jQuery.extend(true, defaults, options ? options : {});
  172
+    this.options.url = (_.isArray(this.options.url))? this.options.url : [ this.options.url ];
  173
+    
  174
+    this._init();
  175
+
173 176
     this.baseUrl = (_.isArray(options.url))? options.url : [ options.url ];
174  
-    this.enhancerUrlPrefix = "/";
175 177
 };
176 178
 
177 179
 VIE.prototype.ZemantaConnector.prototype = {
  180
+		
  181
+// ### _init()
  182
+// Basic setup of the Zemanta connector.  This is called internally by the constructor!
  183
+// **Parameters**:  
  184
+// *nothing*
  185
+// **Throws**:  
  186
+// *nothing*  
  187
+// **Returns**:  
  188
+// *{VIE.ZemantaConnector}* : The VIE.ZemantaConnector instance itself. 
  189
+	_init : function () {
  190
+		var connector = this;
  191
+		
  192
+	    /* basic setup for the ajax connection */
  193
+	    jQuery.ajaxSetup({
  194
+	        converters: {"text application/rdf+json": function(s){return JSON.parse(s);}},
  195
+	        timeout: connector.options.timeout
  196
+	    });
  197
+	    
  198
+	    return this;
  199
+	},
  200
+	
  201
+	_iterate : function (params) {
  202
+        if (!params) { return; }
  203
+        
  204
+        if (params.urlIndex >= this.options.url.length) {
  205
+        	params.error.call(this, "Could not connect to the given Zemanta endpoints! Please check for their setup!");
  206
+            return;
  207
+        }
  208
+        
  209
+        var retryErrorCb = function (c, p) {
  210
+            /* in case a Zemanta backend is not responding and
  211
+             * multiple URLs have been registered
  212
+             */
  213
+            return function () {
  214
+                console.log("Zemanta connection error", arguments);
  215
+                p.urlIndex = p.urlIndex+1;
  216
+                c._iterate(p);
  217
+            };
  218
+        }(this, params);
  219
+
  220
+        if (typeof exports !== "undefined" && typeof process !== "undefined") {
  221
+            /* We're on Node.js, don't use jQuery.ajax */
  222
+            return params.methodNode.call(
  223
+            		this, 
  224
+            		params.url.call(this, params.urlIndex, params.args.options),
  225
+            		params.args,
  226
+            		params.success,
  227
+            		retryErrorCb);
  228
+        }
  229
+        
  230
+        return params.method.call(
  231
+        		this, 
  232
+        		params.url.call(this, params.urlIndex, params.args.options),
  233
+        		params.args,
  234
+        		params.success,
  235
+        		retryErrorCb);
  236
+	},
178 237
 
179 238
 // ### analyze(text, success, error, options)
180 239
 // This method sends the given text to Zemanta returns the result by the success callback.  
@@ -182,7 +241,7 @@ VIE.prototype.ZemantaConnector.prototype = {
182 241
 // *{string}* **text** The text to be analyzed.  
183 242
 // *{function}* **success** The success callback.  
184 243
 // *{function}* **error** The error callback.  
185  
-// *{object}* **options** Options, like the ```format```.  
  244
+// *{object}* **options** Options, like the ```format```, or the ```chain``` to be used.  
186 245
 // **Throws**:  
187 246
 // *nothing*  
188 247
 // **Returns**:  
@@ -190,78 +249,71 @@ VIE.prototype.ZemantaConnector.prototype = {
190 249
 // **Example usage**:  
191 250
 //
192 251
 //     var conn = new vie.ZemantaConnector(opts);
193  
-//     conn.analyze("This is some text.",
  252
+//     conn.analyze("<p>This is some HTML text.</p>",
194 253
 //                 function (res) { ... },
195 254
 //                 function (err) { ... });
196 255
     analyze: function(text, success, error, options) {
197  
-        if (!options) { options = { urlIndex : 0}; }
198  
-        if (options.urlIndex >= this.baseUrl.length) {
199  
-            error("Could not connect to the given Zemanta endpoints! Please check for their setup!");
200  
-            return;
201  
-        }
  256
+    	options = (options)? options :  {};
  257
+    	var connector = this;
202 258
         
203  
-        var enhancerUrl = this.baseUrl[options.urlIndex].replace(/\/$/, '');
204  
-        enhancerUrl += this.enhancerUrlPrefix;
205  
-        
206  
-        var format = options.format || "application/rdf+json";
207  
-        
208  
-        var retryErrorCb = function (c, t, s, e, o) {
209  
-            /* in case a Zemanta backend is not responding and
210  
-             * multiple URLs have been registered
211  
-             */
212  
-            return  function () {
213  
-                console.error("Zemanta connection error", arguments);
214  
-                c.analyze(t, s, e, _.extend(o, {urlIndex : o.urlIndex+1}));
215  
-            };
216  
-        }(this, text, success, error, options);
217  
-        
218  
-        var data = this._prepareData(text);
219  
-
220  
-        if (typeof exports !== "undefined" && typeof process !== "undefined") {
221  
-            /* We're on Node.js, don't use jQuery.ajax */
222  
-            return this._analyzeNode(enhancerUrl, data, success, retryErrorCb, options, format);
223  
-        }
224  
-
225  
-        jQuery.ajax({
  259
+    	connector._iterate({
  260
+        	method : connector._analyze,
  261
+        	methodNode : connector._analyzeNode,
  262
+        	success : success,
  263
+        	error : error,
  264
+        	url : function (idx, opts) {
  265
+        		var u = this.options.url[idx].replace(/\/$/, '');
  266
+        		return u;
  267
+        	},
  268
+        	args : {
  269
+    			text : text,
  270
+        		format : options.format || "rdfxml",
  271
+        		options : options
  272
+        	},
  273
+        	urlIndex : 0
  274
+        });
  275
+    },
  276
+    
  277
+    _analyze : function (url, args, success, error) {
  278
+    	jQuery.ajax({
226 279
             success: function(a, b, c){
227 280
 	        	var responseData = c.responseText.replace(/<z:signature>.*?<\/z:signature>/, '');
228 281
 	        	success(responseData);
229 282
             },
230  
-            error: retryErrorCb,
  283
+            error: error,
  284
+            url: url,
231 285
             type: "POST",
232  
-            url: enhancerUrl,
233  
-            data: data
  286
+            dataType: "xml",
  287
+            data: {
  288
+            	method : "zemanta.suggest",
  289
+            	text : args.text,
  290
+            	format : args.format,
  291
+            	api_key : this.options.api_key,
  292
+            	return_rdf_links : args.options.return_rdf_links
  293
+            },
  294
+            contentType: "text/plain",
  295
+            accepts: {"application/rdf+json": "application/rdf+json"}
234 296
         });
235 297
     },
236 298
 
237  
-    _analyzeNode: function(url, text, success, errorCB, options, format) {
  299
+    _analyzeNode: function(url, args, success, error) {
238 300
         var request = require('request');
239 301
         var r = request({
240 302
             method: "POST",
241 303
             uri: url,
242  
-            body: text,
  304
+            body: args.text,
243 305
             headers: {
244  
-                Accept: format
  306
+                Accept: args.format,
  307
+                'Content-Type': 'text/plain'
245 308
             }
246  
-        }, function(error, response, body) {
  309
+        }, function(err, response, body) {
247 310
             try {
248 311
                 success({results: JSON.parse(body)});
249 312
             } catch (e) {
250  
-                errorCB(e);
  313
+                error(e);
251 314
             }
252 315
         });
253 316
         r.end();
254  
-    },
255  
-    
256  
-    _prepareData : function (text) {
257  
-        return {
258  
-            method: 'zemanta.suggest_markup',
259  
-            format: 'rdfxml',
260  
-            api_key: this.options.api_key,
261  
-            text: text,
262  
-            return_rdf_links: 1
263  
-            // for more options check http://developer.zemanta.com/docs/suggest/
264  
-        };
265 317
     }
266 318
 };
267 319
 })();
4  test/index.html
@@ -73,6 +73,10 @@
73 73
         <script type="text/javascript" src="service/rdfa.js"></script>
74 74
         <script type="text/javascript" src="service/stanbol.js"></script>
75 75
         <script type="text/javascript" src="service/dbpedia.js"></script>
  76
+        <!-- a file that is ignored by git that stores every developer's own API keys to access several backend systems (e.g., Zemanta, OpenCalais) -->
  77
+        <!-- Please have a look at each test-file to find out how such a file should look like -->
  78
+        <script type="text/javascript" src="service/api_keys.js"></script>
  79
+        <script type="text/javascript" src="service/zemanta.js"></script>
76 80
 
77 81
         <!-- STANDARDS -->
78 82
         <script type="text/javascript" src="standard/rdfa11-testharness.js"></script>
50  test/service/stanbol.js
@@ -51,13 +51,61 @@ module("vie.js - Apache Stanbol Service");
51 51
 
52 52
 
53 53
 var stanbolRootUrl = [/*"http://134.96.189.108:1025", */"http://dev.iks-project.eu/stanbolfull", "http://dev.iks-project.eu:8080"];
54  
-test("Test stanbol connection", function() {
  54
+test("VIE.js StanbolService - Registration", function() {
55 55
     var z = new VIE();
56 56
     ok(z.StanbolService, "Checking if the Stanbol Service exists.'");
57 57
     z.use(new z.StanbolService);
58 58
     ok(z.service('stanbol'));
59 59
 });
60 60
 
  61
+test("VIE.js StanbolService - API", function() {
  62
+    var z = new VIE();
  63
+    z.use(new z.StanbolService);
  64
+
  65
+    ok(z.service('stanbol').init);
  66
+    equals(typeof z.service('stanbol').init, "function");
  67
+    ok(z.service('stanbol').analyze);
  68
+    equals(typeof z.service('stanbol').analyze, "function");
  69
+    ok(z.service('stanbol').find);
  70
+    equals(typeof z.service('stanbol').find, "function");
  71
+    ok(z.service('stanbol').load);
  72
+    equals(typeof z.service('stanbol').load, "function");
  73
+    ok(z.service('stanbol').connector);
  74
+    ok(z.service('stanbol').connector instanceof z.StanbolConnector);
  75
+    ok(z.service('stanbol').rules);
  76
+    equals(typeof z.service('stanbol').rules, "object");
  77
+});
  78
+
  79
+test("VIE.js StanbolConnector - API", function() {
  80
+	var z = new VIE();
  81
+    var stanbol = new z.StanbolService({url : stanbolRootUrl});
  82
+    z.use(stanbol);
  83
+    
  84
+    //API
  85
+    ok(stanbol.connector.analyze);
  86
+    equals(typeof stanbol.connector.analyze, "function");
  87
+    ok(stanbol.connector.load);
  88
+    equals(typeof stanbol.connector.load, "function");
  89
+    ok(stanbol.connector.find);
  90
+    equals(typeof stanbol.connector.find, "function");
  91
+    ok(stanbol.connector.lookup);
  92
+    equals(typeof stanbol.connector.lookup, "function");
  93
+    ok(stanbol.connector.referenced);
  94
+    equals(typeof stanbol.connector.referenced, "function");
  95
+    ok(stanbol.connector.sparql);
  96
+    equals(typeof stanbol.connector.sparql, "function");
  97
+    ok(stanbol.connector.ldpath);
  98
+    equals(typeof stanbol.connector.ldpath, "function");
  99
+    ok(stanbol.connector.uploadContent);
  100
+    equals(typeof stanbol.connector.uploadContent, "function");
  101
+    ok(stanbol.connector.createFactSchema);
  102
+    equals(typeof stanbol.connector.createFactSchema, "function");
  103
+    ok(stanbol.connector.createFact);
  104
+    equals(typeof stanbol.connector.createFact, "function");
  105
+    ok(stanbol.connector.queryFact);
  106
+    equals(typeof stanbol.connector.queryFact, "function");
  107
+});
  108
+
61 109
 test("VIE.js StanbolService - Analyze", function () {
62 110
     if (navigator.userAgent === 'Zombie') {
63 111
        return;
62  test/service/zemanta.js
... ...
@@ -0,0 +1,62 @@
  1
+module("vie.js - Zemanta Service");
  2
+
  3
+// set-up: window.ZEMANTA_API_KEY = "your-api-key-here";
  4
+
  5
+test("VIE.js ZemantaService - Registration", function() {
  6
+    var z = new VIE();
  7
+    ok(z.ZemantaService, "Checking if the Zemanta Service exists.'");
  8
+    z.use(new z.ZemantaService);
  9
+    ok(z.service('zemanta'));
  10
+});
  11
+
  12
+test("VIE.js ZemantaService - API", function() {
  13
+    var z = new VIE();
  14
+    z.use(new z.ZemantaService);    
  15
+    //API
  16
+    ok(z.service('zemanta').analyze);
  17
+    equals(typeof z.service('zemanta').analyze, "function");
  18
+});
  19
+
  20
+test("VIE.js ZemantaService - Analyze", function () {
  21
+    if (navigator.userAgent === 'Zombie') {
  22
+       return;
  23
+    } 
  24
+    if (window.ZEMANTA_API_KEY === undefined) {
  25
+    	console.warn("Please provide an API key for Zemanta.");
  26
+    	ok(true, "Skipped tests, as no API key is available!");
  27
+    	return;
  28
+    }
  29
+    // Sending a an example with double quotation marks.
  30
+    var text = "<p>This is a small test, where Steve Jobs sings the song \"We want to live forever!\" song.</p>";    
  31
+    var elem = $(text);
  32
+    var z = new VIE();
  33
+    ok (z.ZemantaService);
  34
+    equal(typeof z.ZemantaService, "function");
  35
+    z.use(new z.ZemantaService({"api_key" : window.ZEMANTA_API_KEY}));
  36
+    stop();
  37
+    z.analyze({element: elem}).using('zemanta').execute().done(function(entities) {
  38
+        ok(entities);
  39
+        ok(entities.length > 0, "At least one entity returned");
  40
+        ok(entities instanceof Array);
  41
+        var allEntities = true;
  42
+        for(var i=0; i<entities.length; i++){
  43
+            var entity = entities[i];
  44
+            if (! (entity instanceof Backbone.Model)){
  45
+                allEntities = false;
  46
+                ok(false, "VIE.js ZemantaService - Analyze: Entity is not a Backbone model!");
  47
+                console.error("VIE.js ZemantaService - Analyze: ", entity, "is not a Backbone model!");
  48
+            }
  49
+        }
  50
+        ok(allEntities);
  51
+        var firstTextAnnotation = _(entities).filter(function(e){return e.isof("zemanta:Document") && e.get("zemanta:text");})[0];
  52
+        var s = firstTextAnnotation.get("zemanta:text").toString();
  53
+        
  54
+    	equals(text, s, "This should return the same text that has been sent to Zemanta.");
  55
+        ok(s.substring(s.length-4, s.length-2) != "\"@", "Selected text should be converted into a normal string.");
  56
+        start();
  57
+    })
  58
+    .fail(function(f){
  59
+        ok(false, f.statusText);
  60
+        start();
  61
+    });
  62
+});

0 notes on commit fb1fe3f

Please sign in to comment.
Something went wrong with that request. Please try again.