Commit 9cf629cd authored by delanoe's avatar delanoe

[GRAPH EXPLORER] Better types to check parameters and give user some usefull...

[GRAPH EXPLORER] Better types to check parameters and give user some usefull informations. I extensively use the Either type of Haskell that is needed to easyly understand the logic of the process.
parent a79375ae
......@@ -27,6 +27,15 @@ def get_graph( request=None , corpus=None
):
'''
Get_graph : main steps:
0) Check the parameters
get_graph :: GraphParameters -> Either (Dic Nodes Links) (Dic State Length)
where type Length = Int
get_graph first checks the parameters and return either graph data or a dic with
state "type" with an integer to indicate the size of the parameter
(maybe we could add a String in that step to factor and give here the error message)
1) count Cooccurrences (function countCooccurrences)
main parameters: threshold
......@@ -54,11 +63,11 @@ def get_graph( request=None , corpus=None
if mapList_id is None:
mapList_id = session.query(Node.id).filter(Node.typename == "MAPLIST").first()[0]
mapList_size = session.query(NodeNgram).filter(NodeNgram.node_id == mapList_id)
if mapList_size.count() < graph_constraints['mapList']:
mapList_size_query = session.query(NodeNgram).filter(NodeNgram.node_id == mapList_id)
mapList_size = mapList_size_query.count()
if mapList_size < graph_constraints['mapList']:
# Do not compute the graph if mapList is not big enough
return {'nodes':[mapList_size.count()], 'links':[0,0,0]}
return {'state': "mapListError", "length" : mapList_size}
# case of corpus not big enough
......@@ -103,7 +112,8 @@ def get_graph( request=None , corpus=None
# Finally test if the size of the corpora is big enough
# --------------------------------
if corpus_size_query.count() > graph_constraints['corpusMax']:
corpus_size = corpus_size_query.count()
if corpus_size > graph_constraints['corpusMax']:
# Then compute cooc asynchronously with celery
scheduled(countCooccurrences)( corpus_id=corpus.id
#, field1="ngrams", field2="ngrams"
......@@ -113,13 +123,13 @@ def get_graph( request=None , corpus=None
, save_on_db = True
#, limit=size
)
# Dic hack to inform user that graph is computed asynchronously
# (Impossible graph: no nodes with one link)
return {'nodes':[], 'links':[0]}
# Dic to inform user that corpus maximum is reached then
# graph is computed asynchronously
return {"state" : "corpusMax", "length" : corpus_size}
elif corpus_size_query.count() <= graph_constraints['corpusMin']:
elif corpus_size <= graph_constraints['corpusMin']:
# Do not compute the graph if corpus is not big enough
return {'nodes':[corpus_size_query.count()], 'links':[0,0]}
return {"state" : "corpusMin", "length" : corpus_size}
else:
# If graph_constraints are ok then compute the graph in live
......
......@@ -142,10 +142,12 @@ class Graph(APIView):
)
# Test data length
# A graph needs more than 3 nodes and 3 links
# Others graphs are used to check the errors
if len(data['nodes']) > 3 and len(data['links']) > 3 :
# data :: Either (Dic Nodes Links) (Dic State Length)
# data_test :: Either String Bool
data_test = data.get("state", True)
if data_test is True:
# normal case --------------------------------
if format_ == 'json':
return JsonHttpResponse(
......@@ -154,64 +156,72 @@ class Graph(APIView):
)
# --------------------------------------------
elif len(data['nodes']) == 1 and len(data['links']) == 2 :
# async data case
return JsonHttpResponse({
'msg': '''Problem: your corpus is too small (only %d documents).
Solution: Add more documents (more than %d documents)
in order to get a graph.
You can manage your corpus here:
http://%sgargantext.org/projects/%d/
''' % ( data['nodes'][0]
, graph_constraints['corpusMin']
, "dev."
, corpus.parent_id
),
}, status=400)
elif len(data['nodes']) == 1 and len(data['links']) == 3 :
# async data case
return JsonHttpResponse({
'msg': '''Problem: your map list is too small (currently %d terms).
Solution: Add some terms (more than %d terms)
in order to get a graph.
You can manage your map terms here:
http://%sgargantext.org/projects/%d/corpora/%d/terms
''' % ( data['nodes'][0]
, graph_constraints['mapList']
, "dev."
, corpus.parent_id
, corpus.id
),
}, status=400)
elif len(data['nodes']) == 0 and len(data['links']) == 0 :
# async data case
return JsonHttpResponse({
'msg': '''Warning: Async graph generation.
Wait a while and discover your graph very soon.
Click on the link and see your current graph
processing on top of the list:
http://%sgargantext.org/projects/%d/corpora/%d/myGraph
''' % ("dev.", corpus.parent_id, corpus.id),
}, status=400)
else:
# empty data case
return JsonHttpResponse({
'msg': '''Empty graph warning
No cooccurences found in this corpus for the words of this maplist
(maybe add more terms to the maplist or increase the size of your
corpus ?)''',
}, status=400)
# All other cases (more probable are higher in the if list)
if data["state"] == "corpusMin":
# async data case
return JsonHttpResponse({
'msg': '''Problem: your corpus is too small (only %d documents).
Solution: Add more documents (more than %d documents)
in order to get a graph.
You can manage your corpus here:
http://%sgargantext.org/projects/%d/
''' % ( data["length"]
, graph_constraints['corpusMin']
, "dev."
, corpus.parent_id
),
}, status=400)
elif data["state"] == "mapListError":
# async data case
return JsonHttpResponse({
'msg': '''Problem: your map list is too small (currently %d terms).
Solution: Add some terms (more than %d terms)
in order to get a graph.
You can manage your map terms here:
http://%sgargantext.org/projects/%d/corpora/%d/terms
''' % ( data["length"]
, graph_constraints['mapList']
, "dev."
, corpus.parent_id
, corpus.id
),
}, status=400)
elif data["state"] == "corpusMax":
# async data case
return JsonHttpResponse({
'msg': '''Warning: Async graph generation since your corpus is
big (about %d documents).
Wait a while and discover your graph very soon.
Click on the link below and see your current graph
processing on top of the list:
http://%sgargantext.org/projects/%d/corpora/%d/myGraphs
''' % (data["length"], "dev.", corpus.parent_id, corpus.id),
}, status=400)
else :
return JsonHttpResponse({
'msg': '''Programming error.''',
}, status=400)
elif len(data["nodes"]) < 2 and len(data["links"]) < 2:
# empty data case
return JsonHttpResponse({
'msg': '''Empty graph warning
No cooccurences found in this corpus for the words of this maplist
(maybe add more terms to the maplist or increase the size of your
corpus ?)''',
}, status=400)
else:
# parameters error case
......
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