Commit 0348908c authored by Administrator's avatar Administrator

[FEAT] function to add/del ngram from a specific list

	modifié :         ngram/lists.py
Simple function to del/add ngram from list, mainly works for stopList

	modifié :         test-list-management.py
Simple usage test added.

	modifié :         annotations/views.py
Code commented to show example and explains current issues
parent 6b09306c
......@@ -237,5 +237,21 @@ class Ngram(APIView):
doc_id = request.GET.get('docId')
annotationDict = json.loads(request.POST.get("annotation"))
print(annotationDict)
# There is 2 main actions:
# 1) add ngram to the miamList : this step is tricky if the ngram does
# exist yet, it is experimental in this case.
# But according to your function, you have the ngram_id already
# The function is:
ngramList(do='add', ngram_ids=[ngram_id,], list_id=list_id)
#ngramList(do='add', ngram_ids=[ngram_id,], list_id=list_id)
# Note : depending on the list, maybe I should adapt the function to
# delete from a list when added to a specific type of list
# 2) get the list of ngrams of one miamList: for this step see above
# Use the ngramList function in ngram.lists.py for that
# TODO DB query
return Response(annotationDict)
......@@ -136,16 +136,22 @@ def ngramList(do=None, ngram_ids=[], list_id=None) :
)
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()
# Need to be optimized with list of ids
ngram = (session.query(NodeNgram)
node_ngram = (session.query(NodeNgram)
.filter(NodeNgram.ngram_id == ngram_id)
.filter(NodeNgram.node_id == list_id)
.first()
)
if do == 'add':
session.add(ngram)
elif do == 'del':
session.delete(ngram)
if node_ngram is None :
node_ngram = NodeNgram(node_id = list_id,
ngram_id=ngram_id,
weight=1)
if do == 'add' :
session.add(node_ngram)
elif do == 'del' :
session.delete(node_ngram)
session.commit()
return(True)
......
......@@ -73,10 +73,33 @@ for typeList in ['MiamList', 'StopList', 'MainList', 'GroupList']:
#print(n[0][0])
print('Test having list_id')
print(n, listNgramIds(list_id=n[0][0])[:3])
stop_list_id = listIds(user_id=user.id,
corpus_id=corpus.id,
typeList='StopList')[0][0]
miam_list_id = listIds(user_id=user.id,
corpus_id=corpus.id,
typeList='MiamList')[0][0]
print('Stop List', stop_list_id)
print('Miam List', miam_list_id)
ngram_id = listNgramIds(list_id=miam_list_id)[0][0]
print('ngram_id', ngram_id)
ngramList(do='add', ngram_ids=[ngram_id,], list_id=stop_list_id)
#
print('Test having typeList and corpus.id')
print(n, listNgramIds(typeList=typeList, corpus_id=corpus.id, user_id=user.id)[:3])
#
# print('Test having typeList and corpus.id')
# print(n, listNgramIds(typeList=typeList, corpus_id=corpus.id, user_id=user.id)[:3])
##
# print('Test having typeList and corpus.id and doc_id')
# print(n, listNgramIds(typeList=typeList, corpus_id=corpus.id, doc_id=doc_id, user_id=user.id)[:3])
......
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