Commit 35c3de76 authored by Romain Loth's avatar Romain Loth

Annotations: connected ok

parent 327ac68d
......@@ -24,7 +24,7 @@
});
});
// FIXME: est-ce qu'on ne pourrait pas directement utiliser lists
// au lieu de recopier dans allListsSelect ?
$rootScope.$watchCollection('lists', function (newValue, oldValue) {
......@@ -37,8 +37,8 @@
'id': key,
'label': value
});
// initialize activeLists with the MiamList by default
if (value == 'MAINLIST') {
// initialize activeLists with the MAPLIST by default
if (value == 'MAPLIST') {
$rootScope.activeLists = {};
$rootScope.activeLists[key] = value;
}
......
......@@ -107,9 +107,9 @@
// console.log("toggleMenu with \$scope.selection_text: '" + JSON.stringify($scope.selection_text) +"'") ;
if (angular.isObject(annotation) && !$element.hasClass('menu-is-opened')) {
// existing ngram
console.log("toggleMenu.annotation: '" + JSON.stringify(annotation) +"'")
// Delete from the current list
$scope.menuItems = [
{
......@@ -374,33 +374,37 @@
annotationsAppHighlight.controller('NGramHighlightController',
['$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) {
// TODO prevent matching inside XML tags
// (now if term=='class' it highlights XML class attrs ><)
// other interfering terms: ng, span, click, keyword, inline...
// suggestion by mrodic: take the original text and replace for all act
// but problem: it's not multiple lists it's previous replacements in t
// solution: use a tag ersatz here like <xmlspan> </xmlspan>
// then replace all afterwards with real xml <span ...> tags
function replaceAnchorByTemplate(text, ngram, template, pattern) {
// exemple args:
// =============
// text ------- "Background Few previous studies have examined
// non-wealth-based inequalities etc etc etc"
// ngram ----- {uuid: 1846, occurrences: 1, list_id: 3689,
// text: "previous studies", listName: "MAINLIST"}
// template --- "<span ng-controller='TextSelectionController'
// ng-click='onClick($event)'
// class='keyword-inline'></span>"
// pattern ---- RegExp(/#\(#MAINLIST-10007#\)#/gm)
return text.replace(pattern, function(matched) {
var tpl = angular.element(template);
tpl.append(matched);
tpl.append(ngram.text);
tpl.attr('title', "Click to add/remove");
tpl.attr('uuid', ngram.uuid);
/*
* 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);
return tpl.get(0).outerHTML;
});
}
/* Escape text before it's inserted in regexp (aka quotemeta)
* ex: "c++" => "c\+\+"
*   so the '+' won't act as regexp operator
......@@ -428,13 +432,13 @@
if ($rootScope.activeLists === undefined) return;
if (_.keys($rootScope.activeLists).length === 0) return;
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 template = templateBegin + templateEnd;
var templateBeginRegexp = "<span ng-controller='TextSelectionController' ng-click='onClick\(\$event\)' class='keyword-inline'>";
var startPattern = "\\b((?:"+templateBeginRegexp+")*";
var middlePattern = "(?:<\/span>)*\\s(?:"+templateBeginRegexp+")*";
var middlePattern = " ";
var endPattern = "(?:<\/span>)*)\\b";
console.log("highlight annotations length: " + annotations.length)
......@@ -451,8 +455,13 @@
var i = 0 ;
var j = 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 ++ ;
// console.log('----------------\n')
// console.log('sortedSizeAnnotations n° ' + i + ': \n ' + JSON.stringify(annotation) +'\n')
......@@ -460,30 +469,94 @@
// exclude ngrams that are into inactive lists
if ($rootScope.activeLists[annotation.list_id] === undefined) return;
// count within activ list
j ++ ;
// used to setup css class
// used to setup anchor
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)
var words = annotation.text.split(" ").map(escapeRegExp);
var pattern = new RegExp(startPattern + words.join(middlePattern) + endPattern, 'gmi');
var textRegexp = new RegExp("\\b"+escapeRegExp(annotation.text)+"\\b", 'igm');
// var myPattern = new RegExp("\\b"+escapeRegExp(annotation.text)+"\\b", 'igm');
// previously:
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:
// eltID eltLongtext
// | |
// {'#title': 'some text',
// '#abstract-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) {
// console.log('textMapping for "' + eltId + '" : ' + JSON.stringify(text))
// £TODO pas besoin de pattern.test avant pattern.sub !
if (pattern.test(text) === true) {
k ++ ;
textMapping[eltId] = replaceTextByTemplate(
// console.log(anchorPattern)
if(text) {
textMapping[eltId] = replaceAnchorByTemplate(
text,
annotation,
template,
pattern);
anchorPattern);
}
});
......@@ -492,10 +565,41 @@
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
$rootScope.extraNgramList = angular.forEach(extraNgramList, function(name, id) {
......
......@@ -10,10 +10,10 @@
/*
* DocumentHttpService: Read Document
* ===================
*
*
* route: annotations/documents/@d_id
* ------
*
*
* exemple:
* --------
* {
......@@ -29,7 +29,7 @@
* "journal": "Vistas in Astronomy",
* "authors": "A. Thom"
* }
*
*
*/
http.factory('DocumentHttpService', function($resource) {
return $resource(
......@@ -49,17 +49,17 @@
/*
* NgramListHttpService: Read all Ngrams
* =====================
*
*
* route: annotations/corpora/@c_id/documents/@d_id
* ------
*
*
* json return format:
* -------------------
* corpus_id : {
* lists: {(list_id:name)+}
* doc_id : [ngrams_objects]+,
* doc_id : [ngrams_objects]+,
* }
*
*
* exemple:
* --------
* "554": {
......@@ -89,19 +89,19 @@
/*
* NgramHttpService: Create, modify or delete 1 Ngram
* =================
*
* £TODO REACTIVATE IN urls.py
*
* if new ngram:
*
* TODO REACTIVATE IN urls.py
*
* if new ngram:
* -> ngram_id will be "create"
* -> route: annotations/lists/@node_id/ngrams/create
* -> will land on views.NgramCreate
*
*
* else:
* -> ngram_id is a real ngram id
* -> route: annotations/lists/@node_id/ngrams/@ngram_id
* -> will land on views.NgramCreate
*
*
*/
http.factory('NgramHttpService', function ($resource) {
return $resource(
......
......@@ -591,6 +591,7 @@ function getTopPapers(type){
if(pub["title"]) {
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_mainIDs = {}
for(var i=0; i<url_elems.length; i++) {
......@@ -598,7 +599,10 @@ function getTopPapers(type){
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="";
......
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