Commit e8590680 authored by Elias's avatar Elias

adapted ngramList for annocations api

parent 782efa8c
......@@ -27,13 +27,13 @@
border-bottom: none;
}
.miamword {
.miamlist {
color: black;
background-color: rgba(60, 118, 61, 0.5);
cursor: pointer;
}
.stopword {
.stoplist {
color: black;
background-color: rgba(169, 68, 66, 0.2);
cursor: pointer;
......
......@@ -23,6 +23,7 @@
function ($scope, $rootScope, $element, NgramHttpService) {
// TODO use the tooltip ?
$scope.onDeleteClick = function () {
// TODO not the right params
NgramHttpService.delete($scope.keyword, $rootScope);
};
}]);
......@@ -91,9 +92,13 @@
function toggleMenu(context, annotation){
$timeout(function() {
$scope.$apply(function() {
$scope.miamListId = _.invert($rootScope.lists)['MiamList'];
$scope.stopListId = _.invert($rootScope.lists)['StopList'];;
if (angular.isObject(annotation)) {
$scope.level = angular.copy(annotation.level);
$scope.category = angular.copy(annotation.category);
$scope.category = $rootScope.lists[annotation.list_id].toLowerCase();
$scope.listId = angular.copy(annotation.list_id);
// used in onClick
$scope.selection_text = angular.copy(annotation);
......@@ -118,7 +123,7 @@
}
else if (annotation.trim() !== "") {
$scope.selection_text = angular.copy(annotation);
$scope.level = "Create from current selection";
$scope.level = "New Ngram from selection";
$scope.category = null;
$scope.local_miamlist = true;
$scope.local_stoplist = true;
......@@ -166,26 +171,34 @@
$rootScope.$on("positionAnnotationMenu", positionElement);
$rootScope.$on("toggleAnnotationMenu", toggleMenu);
$scope.onClick = function($event, action, category, level) {
$scope.onClick = function($event, action, listId) {
if (angular.isObject($scope.selection_text)) {
// change the status of an existing Ngram
$scope.selection_text.category = category;
// action from the menu of an existing Ngram
$scope.selection_text.category = $rootScope.lists[listId].toLowerCase();
// TODO deprecated
$scope.selection_text.level = level;
NgramHttpService[action](
{
'listId': $rootScope.listId,
// delete from the current list
NgramHttpService.delete({
'listId': $scope.selection_text.list_id,
'ngramId': $scope.selection_text.uuid
},
{'annotation': $scope.selection_text}
);
}).$promise.then(function(data) {
// add to the new list
NgramHttpService.post(
{
'listId': listId,
'ngramId': $scope.selection_text.uuid
}
);
});
} else if ($scope.selection_text.trim() !== "") {
// new annotation from selection
NgramHttpService.post(
{
'listId': $rootScope.listId
'listId': listId,
'ngramId': 'new'
},
{'annotation' : {'text': $scope.selection_text.trim(), 'category': category, 'level': level}}
{'annotation' : {'text': $scope.selection_text.trim()}}
);
}
// hide selection highlighted text and the menu
......@@ -313,16 +326,17 @@
});
});
function submitNewAnnotation($event, inputEltId, category) {
function submitNewAnnotation($event, inputEltId, listId) {
if ($event.keyCode !== undefined && $event.keyCode != 13) return;
var value = $(inputEltId).val().trim();
if (value === "") return;
NgramHttpService.post(
{
'listId': $rootScope.listId
'listId': listId,
'ngramId': 'new'
},
{'annotation' : {'text': value, 'category': category, 'level': 'local'}},
{'annotation' : {'text': value}},
function(data) {
// on success
if (data) {
......
......@@ -47,7 +47,7 @@
window.ANNOTATION_API_URL + 'lists/:listId/ngrams/:ngramId/',
{
listId: '@listId',
ngramId: '@ngramID'
ngramId: '@ngramId'
},
{
post: {
......
<span ng-if="keyword.category == 'miamlist'" ng-click='onDeleteClick()' class="delete-keyword" data-keyword-id="{{keyword.uuid}}" data-keyword-text="{{keyword.text}}" data-keyword-category="miamlist">×</span>
<a ng-if="keyword.category == 'miamlist'" href="#" data-toggle="tooltip" class="keyword miamword">{{keyword.text}}</a>
<span ng-if="keyword.category == 'stoplist'" ng-click='onDeleteClick()' class="delete-keyword" data-keyword-id="{{keyword.uuid}}" data-keyword-text="{{keyword.text}}" data-keyword-category="stoplist">×</span>
<a ng-if="keyword.category == 'stoplist'" href="#" data-toggle="tooltip" class="keyword stopword">{{keyword.text}}</a>
<span class="occurrences" data-keyword-id="{{keyword.uuid}}">{{keyword.occurrences}}</span>
<span ng-if="keyword.category == 'miamlist'" ng-click='onDeleteClick()' class="delete-keyword" data-keyword-id="{[{keyword.uuid}]}" data-keyword-text="{[{keyword.text}]}" data-keyword-category="miamlist">×</span>
<a ng-if="keyword.category == 'miamlist'" href="#" data-toggle="tooltip" class="keyword miamword">{[{keyword.text}]}</a>
<span ng-if="keyword.category == 'stoplist'" ng-click='onDeleteClick()' class="delete-keyword" data-keyword-id="{[{keyword.uuid}]}" data-keyword-text="{[{keyword.text}]}" data-keyword-category="stoplist">×</span>
<a ng-if="keyword.category == 'stoplist'" href="#" data-toggle="tooltip" class="keyword stopword">{[{keyword.text}]}</a>
<span class="occurrences" data-keyword-id="{[{keyword.uuid}]}">{[{keyword.occurrences}]}</span>
<ul class="noselection">
<li>{{level}}<span ng-if="category !== null"> {{category}}</span></li>
<li class="miamword" ng-if="local_miamlist === true" ng-click="onClick($event, 'post', 'miamlist', 'local')">add to miam-list</li>
<li class="miamword" ng-if="local_miamlist === false" ng-click="onClick($event, 'delete', 'miamlist', 'local')">remove from miam-list</li>
<li>{[{level}]}<span ng-if="category !== null"> {[{category}]}</span></li>
<li class="miamword" ng-if="local_miamlist === true" ng-click="onClick($event, 'post', '{[{ miamListId }]}', 'local')">add to miam-list</li>
<li class="miamword" ng-if="local_miamlist === false" ng-click="onClick($event, 'delete', '{[{ miamListId }]}', 'local')">remove from miam-list</li>
<li class="stopword" ng-if="local_stoplist === true" ng-click="onClick($event, 'post', 'stoplist', 'local')">add to local stop-list</li>
<li class="stopword" ng-if="local_stoplist === false" ng-click="onClick($event, 'delete', 'stoplist', 'local')">remove from local stop-list</li>
<li class="stopword" ng-if="local_stoplist === true" ng-click="onClick($event, 'post', '{[{ stopListId }]}', 'local')">add to local stop-list</li>
<li class="stopword" ng-if="local_stoplist === false" ng-click="onClick($event, 'delete', '{[{ stopListId }]}', 'local')">remove from local stop-list</li>
<li class="stopword" ng-if="global_stoplist === true" ng-click="onClick($event, 'post', 'stoplist', 'global')">add to global stop-list</li>
<li class="stopword" ng-if="global_stoplist === false" ng-click="onClick($event, 'delete', 'stoplist', 'global')">remove from global stop-list</li>
<!--<li class="stopword" ng-if="global_stoplist === true" ng-click="onClick($event, 'post', 'stoplist', 'global')">add to global stop-list</li>
<li class="stopword" ng-if="global_stoplist === false" ng-click="onClick($event, 'delete', 'stoplist', 'global')">remove from global stop-list</li>-->
</ul>
......@@ -5,5 +5,5 @@ from annotations import views
urlpatterns = patterns('',
url(r'^document/(?P<doc_id>[0-9]+)$', views.Document.as_view()), # document view
url(r'^corpus/(?P<corpus_id>[0-9]+)/document/(?P<doc_id>[0-9]+)$', views.NgramList.as_view()), # the list associated with an ngram
url(r'^lists/(?P<list_id>[0-9]+)/ngrams(?:/(?P<ngram_id>[0-9]+))?$', views.Ngram.as_view()), #
url(r'^lists/(?P<list_id>[0-9]+)/ngrams/(?P<ngram_id>[0-9]+|new)?$', views.Ngram.as_view()), #
)
......@@ -9,6 +9,7 @@ from django.contrib.auth.decorators import login_required
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework.exceptions import APIException
from node.models import Node
from gargantext_web.db import *
......@@ -40,7 +41,7 @@ class NgramList(APIView):
for list_type in ['MiamList', 'StopList']:
list_id = list()
list_id = listIds(user_id=request.user.id, corpus_id=int(corpus_id), typeList=list_type)
lists[list_type] = int(list_id[0][0])
lists["%s" % list_id[0][0]] = list_type
# ngrams of list_id of corpus_id:
doc_ngram_list = listNgramIds(corpus_id=corpus_id, doc_id=doc_id, user_id=request.user.id)
......@@ -67,13 +68,27 @@ class Ngram(APIView):
"""
Add a ngram in a list
"""
ngramList(do='add', ngram_ids=[ngram_id], list_id=list_id)
ngram_dict = json.loads(request.POST.get('annotation'))
if ngram_id == 'new':
ngram_dict = json.loads(request.POST.get('annotation'))
results = ngramList('create', list_id, ngram_ids=[ngram_dict['text']])
else:
results = ngramList('add', list_id, ngram_ids=[ngram_id])
return Response([{
'uuid': ngram_id,
'text': ngram_text,
'occurrences': ngram_occurrences,
'list_id': list_id,
} for ngram_id, ngram_text, ngram_occurrences in results])
def delete(self, request, list_id, ngram_id):
"""
Remove a ngram from a list
Delete a ngram from a list
"""
ngramList(do='del', ngram_ids=[ngram_id], list_id=list_id)
return Response({ 'delete' : { '%s' % ngram_id :
ngramList(do='del', ngram_ids=[ngram_id], list_id=list_id)}})
class Document(APIView):
......
......@@ -192,7 +192,6 @@ LOGIN_URL = '/auth/'
GRAPPELLI_ADMIN_TITLE = "Gargantext"
if DEBUG is True or 'GARGANTEXT_DEBUG' in os.environ:
DEBUG = True
try:
from gargantext_web.local_settings import *
except ImportError:
......
......@@ -115,7 +115,7 @@ def listNgramIds(list_id=None, typeList=None,
return(query.all())
def ngramList(do=None, ngram_ids=None, list_id=None) :
def ngramList(do, list_id, ngram_ids=None) :
'''
ngramList :: ([Int], Int, String) -> Bool
Do (delete | add) [ngram_id] (from | to) the list_id
......@@ -125,43 +125,45 @@ def ngramList(do=None, ngram_ids=None, list_id=None) :
ngram_id = [Int] : list of Ngrams id (Ngrams.id)
list_id = Int : list id (Node.id)
'''
if ngram_ids is None:
raise Exception('Need at least one ngram id in ngram_ids')
if do is None or list_id is None :
raise Exception('Need more options: do, ngram_id, list_id')
results = []
if do == 'create':
terms = copy(ngram_ids)
ngram_ids = []
for ngram_term in terms:
# TODO set the language correctly
ngram = Ngram.objects.get_or_create(terms=ngram_term, n=len(terms.split()),
language='en')
ngram_ids += [ngram.id]
# TODO there should not be a try/except here, let the code crash as soon as possible
try:
# node_type_id = (session.query(Node.type_id)
# .filter(Node.id == list_id)
# .first()
# )
for ngram_id in ngram_ids:
# First we test to know if ngram exist in database already
#ngram = (session.query(Ngram).filter(Ngram.id == ngram_id).first()
# Fetch the ngram from database
ngram = session.query(Ngram.id, Ngram.terms, func.count()).filter(Ngram.id == ngram_id).first()
# Need to be optimized with list of ids
node_ngram = (session.query(NodeNgram)
.filter(NodeNgram.ngram_id == ngram_id)
.filter(NodeNgram.node_id == list_id)
.first()
)
# create NodeNgram if does not exists
if node_ngram is None :
node_ngram = NodeNgram(node_id = list_id,
ngram_id=ngram_id,
weight=1)
node_ngram = NodeNgram(node_id = list_id, ngram_id=ngram_id,
weight=1)
if do == 'add' :
session.add(node_ngram)
results += [ngram]
elif do == 'del' :
session.delete(node_ngram)
session.commit()
return(True)
return(results)
except:
except Exception as exc:
PrintException()
return(False)
raise exc
# Some functions to manage automatically the lists
......
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