Commit c70206a2 authored by Mathieu Rodic's avatar Mathieu Rodic

[BUG] adding ngrams to stop- or miamlist now works from the interface

parent 86c021ef
......@@ -65,6 +65,18 @@ class WeightedMatrix:
"""
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:
......@@ -181,7 +193,7 @@ class WeightedList:
for key, value in self.items:
result.items[
other.items.get(key, key)
] = value
] += value
else:
raise TypeError
return result
......
......@@ -204,11 +204,12 @@
// new annotation from selection
NgramHttpService.post(
{
'listId': listId,
'ngramId': 'new'
'listId': listId
},
{'annotation' : {'text': $scope.selection_text.trim()}}
);
).$promise.then(function(data) {
$rootScope.annotations.push(data);
});
}
// hide selection highlighted text and the menu
$(".text-panel").removeClass("selection");
......
......@@ -49,15 +49,15 @@
*/
http.factory('NgramHttpService', function ($resource) {
return $resource(
window.ANNOTATION_API_URL + 'lists/:listId/ngrams/:ngramId/',
window.ANNOTATION_API_URL + 'lists/:listId/ngrams/:ngramId',
{
listId: '@listId',
ngramId: '@ngramId'
ngramId: '@id'
},
{
post: {
method: 'POST',
params: {'listId': '@listId', 'ngramId': '@ngramId'}
params: {'listId': '@listId', 'ngramId': ''}
},
delete: {
method: 'DELETE',
......
......@@ -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>[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):
"""
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
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])
# TODO - if Ngram is in miam-list, and adding it to stop-list,
# then remove it from the previous list
list_id = int(list_id)
# format the ngram's text
ngram_text = request.data.get('annotation', {}).get('text', None)
ngram_text = ngram_text.strip().lower()
ngram_text = ' '.join(ngram_text.split())
# retrieve the ngram's id
ngram = session.query(Ngram).filter(Ngram.terms == ngram_text).first()
if ngram is None:
ngram = Ngram(n=len(ngram_text.split()), terms=ngram_text)
session.add(ngram)
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):
"""
......
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