added a function to break a search string in words and count the number of
occurences of those words in the annotations.
--- a/src/js/serializers/JSONSerializer.js Wed Oct 26 12:40:24 2011 +0200
+++ b/src/js/serializers/JSONSerializer.js Wed Oct 26 12:41:14 2011 +0200
@@ -71,4 +71,36 @@
}
return ret_array;
-}
\ No newline at end of file
+};
+
+/* breaks a string in words and searches each of these words. Returns an array
+ of objects with the id of the annotation and its number of occurences.
+
+ FIXME: optimize ? seems to be n^2 in the worst case.
+*/
+IriSP.JSONSerializer.prototype.searchOccurences = function(searchString) {
+ var ret = { };
+ var keywords = searchString.split(/\s+/);
+
+ for (var i in keywords) {
+ var keyword = keywords[i];
+
+ var found_annotations = []
+ found_annotations = found_annotations.concat(this.searchAnnotations(keyword, "", ""));
+ found_annotations = found_annotations.concat(this.searchAnnotations("", keyword, ""));
+
+ for (var j in found_annotations) {
+ var current_annotation = found_annotations[j];
+
+ if (!ret.hasOwnProperty(current_annotation.id)) {
+ ret[current_annotation.id] = 1;
+ } else {
+ ret[current_annotation.id] += 1;
+ }
+
+ }
+
+ };
+
+ return ret;
+};
\ No newline at end of file
--- a/unittests/tests/JSONSerializer.js Wed Oct 26 12:40:24 2011 +0200
+++ b/unittests/tests/JSONSerializer.js Wed Oct 26 12:41:14 2011 +0200
@@ -98,4 +98,34 @@
});
+
+ test("test occurence count", function() {
+ var ser = new IriSP.JSONSerializer(this.dt, "../test/test.json");
+
+ ser._data = { annotations : [
+ {"content": {
+ "description": "professeur",
+ "title": "garrigou"
+ }, "id" : 1 },
+ { "content": {
+ "description": "interview",
+ "title": "Revue de presse - Hervé Gardette"
+ }, "id" : 2},
+ {"content": {
+ "description": "concept",
+ "title": "idée"
+ }, "id" : 3},
+ { "content": {
+ "description": "",
+ "title": "sans titre"
+ }, "id" : 4}
+ ]};
+
+ // warning : these tests may not work with ie8, safari 4, etc.
+ equal(Object.keys(ser.searchOccurences("garrigou")).length, 1, "first request works");
+ deepEqual(ser.searchOccurences("garrigou"), {1 : 1}, "returned object is correctly defined");
+
+ equal(Object.keys(ser.searchOccurences("garrigou interview")).length, 2, "second request works");
+ equal(Object.keys(ser.searchOccurences("garrigou idée interview")).length, 3, "third request works");
+ });
};
\ No newline at end of file