Commit 81c69dda authored by Romain Loth's avatar Romain Loth

Annotations: connected ok

parent 1e39ea34
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
}); });
}); });
// FIXME: est-ce qu'on ne pourrait pas directement utiliser lists // FIXME: est-ce qu'on ne pourrait pas directement utiliser lists
// au lieu de recopier dans allListsSelect ? // au lieu de recopier dans allListsSelect ?
$rootScope.$watchCollection('lists', function (newValue, oldValue) { $rootScope.$watchCollection('lists', function (newValue, oldValue) {
...@@ -37,8 +37,8 @@ ...@@ -37,8 +37,8 @@
'id': key, 'id': key,
'label': value 'label': value
}); });
// initialize activeLists with the MiamList by default // initialize activeLists with the MAPLIST by default
if (value == 'MAINLIST') { if (value == 'MAPLIST') {
$rootScope.activeLists = {}; $rootScope.activeLists = {};
$rootScope.activeLists[key] = value; $rootScope.activeLists[key] = value;
} }
......
...@@ -107,9 +107,9 @@ ...@@ -107,9 +107,9 @@
// console.log("toggleMenu with \$scope.selection_text: '" + JSON.stringify($scope.selection_text) +"'") ; // console.log("toggleMenu with \$scope.selection_text: '" + JSON.stringify($scope.selection_text) +"'") ;
if (angular.isObject(annotation) && !$element.hasClass('menu-is-opened')) { if (angular.isObject(annotation) && !$element.hasClass('menu-is-opened')) {
// existing ngram // existing ngram
console.log("toggleMenu.annotation: '" + JSON.stringify(annotation) +"'") console.log("toggleMenu.annotation: '" + JSON.stringify(annotation) +"'")
// Delete from the current list // Delete from the current list
$scope.menuItems = [ $scope.menuItems = [
{ {
...@@ -374,33 +374,37 @@ ...@@ -374,33 +374,37 @@
annotationsAppHighlight.controller('NGramHighlightController', annotationsAppHighlight.controller('NGramHighlightController',
['$scope', '$rootScope', '$compile', ['$scope', '$rootScope', '$compile',
function ($scope, $rootScope, $compile) { function ($scope, $rootScope, $compile) {
/* /*
* Replace the text by an html template for ngram keywords * Replace the any ad hoc anchor by an html template
*/ */
function replaceTextByTemplate(text, ngram, template, pattern) { function replaceAnchorByTemplate(text, ngram, template, pattern) {
// TODO prevent matching inside XML tags // exemple args:
// (now if term=='class' it highlights XML class attrs ><) // =============
// other interfering terms: ng, span, click, keyword, inline... // text ------- "Background Few previous studies have examined
// suggestion by mrodic: take the original text and replace for all act // non-wealth-based inequalities etc etc etc"
// but problem: it's not multiple lists it's previous replacements in t // ngram ----- {uuid: 1846, occurrences: 1, list_id: 3689,
// solution: use a tag ersatz here like <xmlspan> </xmlspan> // text: "previous studies", listName: "MAINLIST"}
// then replace all afterwards with real xml <span ...> tags // template --- "<span ng-controller='TextSelectionController'
// ng-click='onClick($event)'
// class='keyword-inline'></span>"
// pattern ---- RegExp(/#\(#MAINLIST-10007#\)#/gm)
return text.replace(pattern, function(matched) { return text.replace(pattern, function(matched) {
var tpl = angular.element(template); var tpl = angular.element(template);
tpl.append(matched); tpl.append(ngram.text);
tpl.attr('title', "Click to add/remove"); tpl.attr('title', "Click to add/remove");
tpl.attr('uuid', ngram.uuid); tpl.attr('uuid', ngram.uuid);
/* /*
* Add CSS class depending on the list the ngram is into * Add CSS class depending on the list the ngram is into
* FIXME Lists names and css classes are fixed, can do better
*/ */
tpl.addClass(ngram.listName); tpl.addClass(ngram.listName);
return tpl.get(0).outerHTML; return tpl.get(0).outerHTML;
}); });
} }
/* Escape text before it's inserted in regexp (aka quotemeta) /* Escape text before it's inserted in regexp (aka quotemeta)
* ex: "c++" => "c\+\+" * ex: "c++" => "c\+\+"
*   so the '+' won't act as regexp operator *   so the '+' won't act as regexp operator
...@@ -428,13 +432,13 @@ ...@@ -428,13 +432,13 @@
if ($rootScope.activeLists === undefined) return; if ($rootScope.activeLists === undefined) return;
if (_.keys($rootScope.activeLists).length === 0) return; if (_.keys($rootScope.activeLists).length === 0) return;
var templateBegin = "<span ng-controller='TextSelectionController' ng-click='onClick($event)' class='keyword-inline'>"; var templateBegin = "<span ng-controller='TextSelectionController' ng-click='onClick($event)' class='keyword-inline'>";
var templateBeginRegexp = "<span ng-controller='TextSelectionController' ng-click='onClick\(\$event\)' class='keyword-inline'>";
var templateEnd = "</span>"; var templateEnd = "</span>";
var template = templateBegin + templateEnd; var template = templateBegin + templateEnd;
var templateBeginRegexp = "<span ng-controller='TextSelectionController' ng-click='onClick\(\$event\)' class='keyword-inline'>";
var startPattern = "\\b((?:"+templateBeginRegexp+")*"; var startPattern = "\\b((?:"+templateBeginRegexp+")*";
var middlePattern = "(?:<\/span>)*\\s(?:"+templateBeginRegexp+")*"; var middlePattern = "(?:<\/span>)*\\s(?:"+templateBeginRegexp+")*";
var middlePattern = " ";
var endPattern = "(?:<\/span>)*)\\b"; var endPattern = "(?:<\/span>)*)\\b";
console.log("highlight annotations length: " + annotations.length) console.log("highlight annotations length: " + annotations.length)
...@@ -451,8 +455,13 @@ ...@@ -451,8 +455,13 @@
var i = 0 ; var i = 0 ;
var j = 0 ; var j = 0 ;
var k = 0 ; var k = 0 ;
angular.forEach(sortedSizeAnnotations, function (annotation) { var l = 0 ;
// first pass for anchors
// ======================
angular.forEach(sortedSizeAnnotations, function (annotation) {
// ex annotation --- {uuid: 1846, occurrences: 1, list_id: 3689,
// text: "previous studies", listName: "MAINLIST"}
i ++ ; i ++ ;
// console.log('----------------\n') // console.log('----------------\n')
// console.log('sortedSizeAnnotations n° ' + i + ': \n ' + JSON.stringify(annotation) +'\n') // console.log('sortedSizeAnnotations n° ' + i + ': \n ' + JSON.stringify(annotation) +'\n')
...@@ -460,30 +469,94 @@ ...@@ -460,30 +469,94 @@
// exclude ngrams that are into inactive lists // exclude ngrams that are into inactive lists
if ($rootScope.activeLists[annotation.list_id] === undefined) return; if ($rootScope.activeLists[annotation.list_id] === undefined) return;
// count within activ list
j ++ ; j ++ ;
// used to setup css class
// used to setup anchor
annotation.listName = $rootScope.lists[annotation.list_id]; annotation.listName = $rootScope.lists[annotation.list_id];
// used as unique placeholder for str.replace
// (anchor avoids side effects of multiple replacements
// like new results inside old replacement's result)
var myAnchor = '#(#'+annotation.listName+'-'+annotation.uuid+'#)#' ;
// £WIP simpler text regexp
// regexps (with escaped content) // regexps (with escaped content)
var words = annotation.text.split(" ").map(escapeRegExp); // var myPattern = new RegExp("\\b"+escapeRegExp(annotation.text)+"\\b", 'igm');
var pattern = new RegExp(startPattern + words.join(middlePattern) + endPattern, 'gmi'); // previously:
var textRegexp = new RegExp("\\b"+escapeRegExp(annotation.text)+"\\b", 'igm'); var words = annotation.text.split(" ").map(escapeRegExp);
var myPattern = new RegExp(startPattern + words.join(middlePattern) + endPattern, 'gmi');
// highlight text as html
// ----------------------- // -------------------------------------------
// replace in text: matched annots by anchors
// -------------------------------------------
// text content taken in argument textMapping: // text content taken in argument textMapping:
// eltID eltLongtext
// | |
// {'#title': 'some text', // {'#title': 'some text',
// '#abstract-text': 'some text', // '#abstract-text': 'some text',
// '#full-text': 'some text' } // '#full-text': 'some text' }
//
angular.forEach(textMapping, function(eltLongtext, eltId) {
if(eltLongtext) {
// ------------------------------------------------------------
// £dbgcount here unnecessary nbMatches (can go straight to ICI)
var matches = eltLongtext.match(myPattern)
var nbMatches = matches ? eltLongtext.match(myPattern).length : 0
if (nbMatches > 0) {
k += nbMatches ;
l ++ ;
// ------------------------------------------------------------
// ICI we update each time
textMapping[eltId] = eltLongtext.replace(myPattern, myAnchor);
// ex longtext -- "Background Few previous studies have
// examined non-wealth-based inequalities etc"
// ex result ---- "Background Few #(#MAINLIST-1846#)# have
// examined non-wealth-based inequalities etc"
}
}
});
});
// rl: £dbgcount
console.log('---- compileNgramsHtml created '
+ k + ' anchors ('
+ l + ' distinct ngrams) from '
+ j + ' ngrams in activeLists (of ' + i + ' ngrams total) ----\n')
// 2nd pass for result html
// =========================
// first pass for anchors
// ======================
angular.forEach(sortedSizeAnnotations, function (annotation) {
// again exclude ngrams that are into inactive lists
if ($rootScope.activeLists[annotation.list_id] === undefined) return;
// now used to setup css class
annotation.listName = $rootScope.lists[annotation.list_id];
// used as unique placeholder for str.replace
// (anchor avoids side effects of multiple replacements
// like new results inside old replacement's result)
var myAnchor = '#(#'+annotation.listName+'-'+annotation.uuid+'#)#' ;
var anchorPattern = new RegExp(escapeRegExp(myAnchor), 'gm');
// highlight anchors as html spans
// -------------------------------
angular.forEach(textMapping, function(text, eltId) { angular.forEach(textMapping, function(text, eltId) {
// console.log('textMapping for "' + eltId + '" : ' + JSON.stringify(text)) // console.log(anchorPattern)
// £TODO pas besoin de pattern.test avant pattern.sub ! if(text) {
if (pattern.test(text) === true) { textMapping[eltId] = replaceAnchorByTemplate(
k ++ ;
textMapping[eltId] = replaceTextByTemplate(
text, text,
annotation, annotation,
template, template,
pattern); anchorPattern);
} }
}); });
...@@ -492,10 +565,41 @@ ...@@ -492,10 +565,41 @@
extraNgramList[annotation.list_id] = extraNgramList[annotation.list_id].concat(annotation); extraNgramList[annotation.list_id] = extraNgramList[annotation.list_id].concat(annotation);
}); });
// rl: £dbg
console.log('---- compileNgramsHtml created '
+ k + ' series of highlighted spans from '
+ j + ' annotations in activeLists (of ' + i + ' annotations total) ----\n')
// update extraNgramList // update extraNgramList
$rootScope.extraNgramList = angular.forEach(extraNgramList, function(name, id) { $rootScope.extraNgramList = angular.forEach(extraNgramList, function(name, id) {
......
...@@ -10,10 +10,10 @@ ...@@ -10,10 +10,10 @@
/* /*
* DocumentHttpService: Read Document * DocumentHttpService: Read Document
* =================== * ===================
* *
* route: annotations/documents/@d_id * route: annotations/documents/@d_id
* ------ * ------
* *
* exemple: * exemple:
* -------- * --------
* { * {
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
* "journal": "Vistas in Astronomy", * "journal": "Vistas in Astronomy",
* "authors": "A. Thom" * "authors": "A. Thom"
* } * }
* *
*/ */
http.factory('DocumentHttpService', function($resource) { http.factory('DocumentHttpService', function($resource) {
return $resource( return $resource(
...@@ -49,17 +49,17 @@ ...@@ -49,17 +49,17 @@
/* /*
* NgramListHttpService: Read all Ngrams * NgramListHttpService: Read all Ngrams
* ===================== * =====================
* *
* route: annotations/corpora/@c_id/documents/@d_id * route: annotations/corpora/@c_id/documents/@d_id
* ------ * ------
* *
* json return format: * json return format:
* ------------------- * -------------------
* corpus_id : { * corpus_id : {
* lists: {(list_id:name)+} * lists: {(list_id:name)+}
* doc_id : [ngrams_objects]+, * doc_id : [ngrams_objects]+,
* } * }
* *
* exemple: * exemple:
* -------- * --------
* "554": { * "554": {
...@@ -89,19 +89,19 @@ ...@@ -89,19 +89,19 @@
/* /*
* NgramHttpService: Create, modify or delete 1 Ngram * NgramHttpService: Create, modify or delete 1 Ngram
* ================= * =================
* *
* £TODO REACTIVATE IN urls.py * TODO REACTIVATE IN urls.py
* *
* if new ngram: * if new ngram:
* -> ngram_id will be "create" * -> ngram_id will be "create"
* -> route: annotations/lists/@node_id/ngrams/create * -> route: annotations/lists/@node_id/ngrams/create
* -> will land on views.NgramCreate * -> will land on views.NgramCreate
* *
* else: * else:
* -> ngram_id is a real ngram id * -> ngram_id is a real ngram id
* -> route: annotations/lists/@node_id/ngrams/@ngram_id * -> route: annotations/lists/@node_id/ngrams/@ngram_id
* -> will land on views.NgramCreate * -> will land on views.NgramCreate
* *
*/ */
http.factory('NgramHttpService', function ($resource) { http.factory('NgramHttpService', function ($resource) {
return $resource( return $resource(
......
...@@ -591,6 +591,7 @@ function getTopPapers(type){ ...@@ -591,6 +591,7 @@ function getTopPapers(type){
if(pub["title"]) { if(pub["title"]) {
var gquery = "https://searx.laquadrature.net/?categories=general&q="+pub["title"].replace(" "+"+") var gquery = "https://searx.laquadrature.net/?categories=general&q="+pub["title"].replace(" "+"+")
// ex url_elems = ["http:", "", "localhost:8000", "projects", "1", "corpora", "2690", "explorer?field1=ngrams&field2=ngrams&distance=conditional&bridgeness=5"]
var url_elems = window.location.href.split("/") var url_elems = window.location.href.split("/")
var url_mainIDs = {} var url_mainIDs = {}
for(var i=0; i<url_elems.length; i++) { for(var i=0; i<url_elems.length; i++) {
...@@ -598,7 +599,10 @@ function getTopPapers(type){ ...@@ -598,7 +599,10 @@ function getTopPapers(type){
url_mainIDs[url_elems[i-1]] = Number(url_elems[i]); url_mainIDs[url_elems[i-1]] = Number(url_elems[i]);
} }
} }
var getpubAPI = window.location.origin+'/project/'+url_mainIDs["project"]+'/corpus/'+ url_mainIDs["corpus"] + '/document/'+pub["id"] // ex url_mainIDs = {projects: 1, corpora: 2690}
// link to matching document
var getpubAPI = window.location.origin+'/projects/'+url_mainIDs["projects"]+'/corpora/'+ url_mainIDs["corpora"] + '/documents/'+pub["id"]
var ifjournal="",ifauthors="",ifkeywords="",ifdate="",iftitle=""; var ifjournal="",ifauthors="",ifkeywords="",ifdate="",iftitle="";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment