Commit 3aa44ef1 authored by Administrator's avatar Administrator

Merge branch 'unstable-mat' of ssh://delanoe.org:1979/gargantext into unstable

parents 148fd8ef c70206a2
...@@ -65,6 +65,18 @@ class WeightedMatrix: ...@@ -65,6 +65,18 @@ class WeightedMatrix:
""" """
pass pass
def __mul__(self, other):
if isinstance(other, Translations):
result = WeightedMatrix()
for key1, key2_value in self.items.items():
for key2, value in self.items:
result.items[
other.items.get(key, key)
] = value
else:
raise TypeError
return result
class UnweightedList: class UnweightedList:
...@@ -181,7 +193,7 @@ class WeightedList: ...@@ -181,7 +193,7 @@ class WeightedList:
for key, value in self.items: for key, value in self.items:
result.items[ result.items[
other.items.get(key, key) other.items.get(key, key)
] = value ] += value
else: else:
raise TypeError raise TypeError
return result return result
......
...@@ -204,11 +204,12 @@ ...@@ -204,11 +204,12 @@
// new annotation from selection // new annotation from selection
NgramHttpService.post( NgramHttpService.post(
{ {
'listId': listId, 'listId': listId
'ngramId': 'new'
}, },
{'annotation' : {'text': $scope.selection_text.trim()}} {'annotation' : {'text': $scope.selection_text.trim()}}
); ).$promise.then(function(data) {
$rootScope.annotations.push(data);
});
} }
// hide selection highlighted text and the menu // hide selection highlighted text and the menu
$(".text-panel").removeClass("selection"); $(".text-panel").removeClass("selection");
......
...@@ -49,15 +49,15 @@ ...@@ -49,15 +49,15 @@
*/ */
http.factory('NgramHttpService', function ($resource) { http.factory('NgramHttpService', function ($resource) {
return $resource( return $resource(
window.ANNOTATION_API_URL + 'lists/:listId/ngrams/:ngramId/', window.ANNOTATION_API_URL + 'lists/:listId/ngrams/:ngramId',
{ {
listId: '@listId', listId: '@listId',
ngramId: '@ngramId' ngramId: '@id'
}, },
{ {
post: { post: {
method: 'POST', method: 'POST',
params: {'listId': '@listId', 'ngramId': '@ngramId'} params: {'listId': '@listId', 'ngramId': ''}
}, },
delete: { delete: {
method: 'DELETE', method: 'DELETE',
......
...@@ -5,5 +5,5 @@ from annotations import views ...@@ -5,5 +5,5 @@ from annotations import views
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^document/(?P<doc_id>[0-9]+)$', views.Document.as_view()), # document view 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'^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>[new0-9]+)$', views.NgramEdit.as_view()), # url(r'^lists/(?P<list_id>[0-9]+)/ngrams(?:/(?P<ngram_id>[0-9]+))?$', views.NgramEdit.as_view()), #
) )
...@@ -73,19 +73,34 @@ class NgramEdit(APIView): ...@@ -73,19 +73,34 @@ class NgramEdit(APIView):
""" """
Add a ngram in a list Add a ngram in a list
""" """
# TODO if Ngram is in miam-list, and adding it to stop-list, then remove it from the previous list # TODO - if Ngram is in miam-list, and adding it to stop-list,
if ngram_id == 'new': # then remove it from the previous list
ngram_dict = json.loads(request.POST.get('annotation')) list_id = int(list_id)
results = ngramList('create', list_id, ngram_ids=[ngram_dict['text']]) # format the ngram's text
else: ngram_text = request.data.get('annotation', {}).get('text', None)
results = ngramList('add', list_id, ngram_ids=[ngram_id]) ngram_text = ngram_text.strip().lower()
ngram_text = ' '.join(ngram_text.split())
return Response([{ # retrieve the ngram's id
'uuid': ngram_id, ngram = session.query(Ngram).filter(Ngram.terms == ngram_text).first()
'text': ngram_text, if ngram is None:
'occurrences': ngram_occurrences, ngram = Ngram(n=len(ngram_text.split()), terms=ngram_text)
'list_id': list_id, session.add(ngram)
} for ngram_id, ngram_text, ngram_occurrences in results]) session.commit()
ngram_id = ngram.id
# add the ngram to the list if not already done
node_ngram = session.query(Node_Ngram).filter(Node_Ngram.node_id==list_id).filter(Node_Ngram.ngram_id==ngram_id).first()
if node_ngram is None:
node_ngram = Node_Ngram(node_id=list_id, ngram_id=ngram_id, weight=1.0)
session.add(node_ngram)
session.commit()
ngram_occurrences = node_ngram.weight
# return the response
return Response({
'uuid': ngram_id,
'text': ngram_text,
'occurrences': ngram_occurrences,
'list_id': list_id,
})
def delete(self, request, list_id, ngram_id): def delete(self, request, list_id, ngram_id):
""" """
......
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