Commit 331bbfe4 authored by PkSM3's avatar PkSM3

[UPDATE] GET groups as dictionary using DG and G

parent a2acaa19
......@@ -223,7 +223,7 @@ class Group(APIView):
REST API to manage groups of Ngrams
Groups can be synonyms, a cathegory or ngrams groups with stems or lems.
'''
def get_group_id(node_id):
def get_group_id(self , node_id):
node_id = int(node_id)
corpus = session.query(Node).filter(Node.id==node_id).first()
group = get_or_create_node(corpus=corpus, nodetype='Group')
......@@ -231,35 +231,58 @@ class Group(APIView):
def get(self, request, corpus_id):
# query ngrams
group_id = get_group_id(corpus_id)
group_id = self.get_group_id(corpus_id)
#api/node/$corpus_id/ngrams?ngram_id=12
ngram_id = request.GET.get('ngram_id', False)
ngram_id = int(node_id)
# ngram_id = 1 #request.GET.get('ngram_id', False)
# ngram_id = int(node_id)
#api/node/$corpus_id/ngrams?all=True
all_option = request.GET.get('all', False)
all_option = int(all_option)
# #api/node/$corpus_id/ngrams?all=True
# all_option = request.GET.get('all', False)
# all_option = 1 #int(all_option)
if ngram_id > 0 or all_option == 1:
ngrams_ngrams = (session
.query(NodeNgramNgram)
.filter(NodeNgramNgram.node_id==group_id)
)
if ngram_id > 0:
ngrams_ngrams = ngrams_ngrams.filter(NodeNgramNgram.ngramx_id==ngram_id)
else:
raise APIException('Missing parameter: "ngram_id" as Integer', 400)
group = dict(list())
# IMPORTANT: Algorithm for getting the groups:
# 1. pairs_list <- Get all pairs from get_group_id
# 2. G <- Do a non-directed graph of pairs_list
# 3. DG <- Do a directed graph of pairs_list
# 4. cliques_list <- find_cliques of G
# 5. groups <- Iterate in G and set the mainNode per each clique: take the highest max_outdegree-node of each clique, using DG
import networkx as nx
G = nx.Graph()
DG = nx.DiGraph()
ngrams_ngrams = (session
.query(NodeNgramNgram)
.filter(NodeNgramNgram.node_id==group_id)
)
# ngramy_id=476996, score=1.0, node_id=75081, id=1282846, ngramx_id=493431
for ng in ngrams_ngrams:
# n_x = ( session.query(Ngram).filter(Ngram.id==ng.ngramx_id) ).first()
# n_y = ( session.query(Ngram).filter(Ngram.id==ng.ngramy_id) ).first()
G.add_edge( ng.ngramx_id , ng.ngramy_id )
DG.add_edge( ng.ngramx_id , ng.ngramy_id )
# group = dict(list())
sinonims_cliques = nx.find_cliques( G )
# for nn in ngrams_ngrams.all():
# group[nn.ngramx_id] = group.get(nn.ngramx_id, []) + [nn.ngramy_id]
for nn in ngrams_ngrams.all():
group[nn.ngramx_id] = group.get(nn.ngramx_id, []) + [nn.ngramy_id]
groups = {}
for c in sinonims_cliques:
max_deg = -1
mainNode = -1
mainNode_sinonims = []
for node in c:
node_outdeg = DG.out_degree(node)
if node_outdeg>max_deg:
max_deg = node_outdeg
mainNode = node
for node in c:
if mainNode!=node:
mainNode_sinonims.append( node )
groups[ int(mainNode) ] = mainNode_sinonims
return JsonHttpResponse(group)
return JsonHttpResponse(groups)
def post(self, request, node_id):
......
......@@ -209,6 +209,7 @@ function getRecords() {
return MyTable.data('dynatable').settings.dataset.originalRecords;
}
// new
function group_mode ( elem ) {
GState=1
var elem_id = $( elem ).data("stuff")
......@@ -226,6 +227,7 @@ function group_mode ( elem ) {
MyTable.data('dynatable').dom.update();
}
// new
function SaveSinonims( gheader , gcontent) {
console.log("GHEADER:")
$(gheader).children('span').each(function () {
......@@ -237,6 +239,7 @@ function SaveSinonims( gheader , gcontent) {
});
}
// new
$('#group_box_content').bind("DOMSubtreeModified",function(){
console.log( $(this).has( "span" ).length )
var groupdiv = "#group_box"
......@@ -257,6 +260,7 @@ $('#group_box_content').bind("DOMSubtreeModified",function(){
}
})
// new
function add2group ( elem ) {
var elem_id = $( elem ).data("stuff")
......@@ -285,6 +289,7 @@ function add2group ( elem ) {
MyTable.data('dynatable').dom.update();
}
// new
function clickngram_action ( elem ) {
var elem_id = $( elem ).data("stuff")
AjaxRecords[elem_id].state = (AjaxRecords[elem_id].state==(System[0]["states"].length-2))?0:(AjaxRecords[elem_id].state+1);
......@@ -292,6 +297,7 @@ function clickngram_action ( elem ) {
MyTable.data('dynatable').dom.update();
}
// modified
function transformContent(rec_id) {
var elem = AjaxRecords[rec_id];
var result = {}
......@@ -301,7 +307,7 @@ function transformContent(rec_id) {
plus_event = " <a class=\"plusclass\" onclick=\"group_mode(this.parentNode.parentNode)\">(+)</a>"
if(GState==1 ) {
if(elem.state!=1 && elem.state!=3) { // if deleted and already grouped, no Up button
plus_event = " <a class=\"plusclass\" onclick=\"add2group(this.parentNode.parentNode)\">()</a>"
plus_event = " <a class=\"plusclass\" onclick=\"add2group(this.parentNode.parentNode)\">()</a>"
}
}
result["id"] = elem["id"]
......@@ -389,11 +395,6 @@ function Mark_NGram( ngram_id , old_flag , new_flag ) {
return new_flag;
}
function GroupNGrams() {
for (var i in FlagsBuffer["to_group"]){
console.log( AjaxRecords[i] )
}
}
//generic enough
function ulWriter(rowIndex, record, columns, cellWriter) {
......@@ -414,6 +415,7 @@ function ulWriter(rowIndex, record, columns, cellWriter) {
}
function SelectAll( the_checkbox ) {
console.log(the_checkbox)
var current_flag = $("input[type='radio'][name='radios']:checked").val()
$("tbody tr").each(function (i, row) {
var id = $(row).data('stuff')
......@@ -739,34 +741,10 @@ function Main_test( data , initial) {
}
function SearchFilters( elem ) {
var MODE = elem.value;
if( MODE == "filter_all") {
var result = Main_test(AjaxRecords , MODE)
console.log( result )
return ;
}
// if( MODE == "filter_stoplist") {
// }
// if( MODE == "filter_miamlist") {
// }
}
console.log(window.location.href+"/ngrams.json")
$.ajax({
url: window.location.href+"/ngrams.json",
success: function(data){
// Building the Score-Selector
var FirstScore = data.scores.initial
var possible_scores = Object.keys( data.ngrams[0].scores );
......@@ -781,7 +759,6 @@ $.ajax({
var result = Main_test( data , FirstScore )
console.log( result )
// Listener for onchange Score-Selector
scores_div += "<select>"+"\n";
$("#ScoresBox").html(scores_div)
......@@ -791,7 +768,5 @@ $.ajax({
console.log( result )
});
}
});
\ No newline at end of file
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