In [1]:
import requests
import json

In [2]:
url = "http://kodexlab.com/cnrtl/graph/verb/api/play"
request = { "query":"comprendre" }

In [3]:
res = requests.get(url, data=json.dumps(request), headers={"content-type":"application/json"})

In [4]:
data = res.json()
print len( data["results"] )


4

In [5]:
filename = 'data_query.json'
writeFile = open(filename, 'w+')
writeFile.write( json.dumps( data ,   indent = 4 ) )
writeFile.close()

In [6]:
# see http://kodexlab.com/cnrtl/graph/verb/q/partir#

Formatage pour le graph circulaire


In [7]:
myData = {}

myData['links'] = data['results']['graph']['es']
myData['nodes'] = data['results']['graph']['vs']

In [8]:
for d in data['results']['graph']['vs']:
    print d['label'],


admettre apercevoir apprendre apprécier appréhender comporter comprendre compter concevoir connaître consister contenir deviner déchiffrer décoder découvrir démêler embrasser enclore enfermer englober entendre entrevoir envelopper imaginer inclure incorporer interpréter juger lire mélanger mêler penser percer percevoir prendre pénétrer remarquer renfermer repérer révéler s'apercevoir s'expliquer saisir savoir se composer sentir trouver voir y être

In [9]:
# Dico de correspondance vertex -> id du cluster
clusters_id = {}
for id_cluster , c in enumerate( data['results']['clusters']['clusters'] ):
    for i, vertex_id in enumerate( c['vids'] ):
        clusters_id[vertex_id] = id_cluster 
        
print clusters_id
myData['clusters_id'] = clusters_id


{0: 0, 1: 1, 2: 0, 3: 0, 4: 0, 5: 2, 6: 0, 7: 0, 8: 0, 9: 0, 10: 3, 11: 2, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 0, 18: 2, 19: 2, 20: 2, 21: 0, 22: 1, 23: 2, 24: 0, 25: 2, 26: 4, 27: 1, 28: 0, 29: 1, 30: 4, 31: 4, 32: 0, 33: 1, 34: 1, 35: 0, 36: 1, 37: 1, 38: 2, 39: 1, 40: 1, 41: 1, 42: 5, 43: 1, 44: 0, 45: 3, 46: 1, 47: 1, 48: 1, 49: 1}

In [26]:
# Enregistre le cluster_id pour chaque  node

for i, node in enumerate( myData['nodes'] ):
    myData['nodes'][i]['cluster_id'] = clusters_id[ node['id'] ]

In [27]:
print myData['nodes'][40]


{u'gdeg': 96, u'prox': 0.003042702106152673, u'label': u'r\xe9v\xe9ler', u'gid': 5859, 'cluster_id': 1, u'lemme': u'r\xe9v\xe9ler', u'prox_color': [255, 154, 46], 'deg_loc': 9, u'id': 40}

In [28]:
# Retrouve et Enregistre la requête
myData['query'] = None
q = data['results']['query']
for n in myData['nodes']:
    if n['lemme'] == q:
        myData['query'] = n
        print n['lemme']
        
print myData['query']

# --- retrouve l'id du cluster de la requete ---
id_cluster_query = myData['clusters_id'][ myData['query']['id'] ]
print ' id cluster de la requete: ', id_cluster_query


comprendre
{u'gdeg': 60, u'prox': 0.005535226228740178, u'label': u'comprendre', u'gid': 1413, 'cluster_id': 0, u'lemme': u'comprendre', u'prox_color': [255, 152, 50], 'deg_loc': 45, u'id': 6}
 id cluster de la requete:  0

In [29]:
# créer une liste ordonnées des Noeux, en inserrant les espaces vide entre les clusters
# donne l'angle  : theta = iDelta * delta   en fonction de l'id du noeud
# dico{ key= vertex id   , value= iDelta  }
# Place la requete à un bout,    avec son cluster

# on place le cluster de la requete au debut :
c_list = data["results"]['clusters']['clusters']
c_list.insert( 0, c_list.pop( id_cluster_query ) )

In [30]:
# on place la requete en haut de son cluster ...
c_list[0]['vids'].append(  c_list[0]['vids'].pop( c_list[0]['vids'].index(myData['query']['id']) ) )
print c_list[0]['vids']


[0, 2, 3, 4, 7, 8, 9, 17, 21, 24, 28, 32, 35, 44, 6]

In [31]:
iDelta = {}
i = 0

for c in  c_list :
    for vId in c['vids']:
        #if vId == myData['query']['id']:
        #    continue
        iDelta[ vId ] = i
        i = i + 1
    i = i + 1 # espace vide entre cluster
    
nNodes = i - 1
print nNodes

myData['iDelta'] = iDelta
myData['nNodes'] = nNodes


55

In [32]:
# --- Manage les types de liens ---
for i, edge in enumerate( myData['links'] ):
    if edge['s']==myData['query']['id'] or edge['t']==myData['query']['id']:
        myData['links'][i]['type'] = 'query'
    elif clusters_id[ edge['s'] ] == clusters_id[ edge['t'] ]:
        myData['links'][i]['type'] = 'intra'
    else:
        myData['links'][i]['type'] = 'inter'

#print myData['links']

In [33]:
# --- Compte les liens sur le graph 'local' ---
degreeDic = {}
for link in myData['links']:
    
    degreeDic[ link['s'] ] = degreeDic.get( link['s'], 0 ) + 1
    degreeDic[ link['t'] ] = degreeDic.get( link['t'], 0 ) + 1

print degreeDic

# enregistre les valeurs dans le dico Nodes
for v in myData['nodes']:
    v['deg_loc'] = degreeDic[  v['id'] ]
    
print myData['nodes'][:2]


{0: 8, 1: 17, 2: 5, 3: 9, 4: 9, 5: 10, 6: 45, 7: 8, 8: 14, 9: 18, 10: 3, 11: 9, 12: 17, 13: 12, 14: 3, 15: 21, 16: 4, 17: 13, 18: 6, 19: 6, 20: 8, 21: 15, 22: 7, 23: 3, 24: 10, 25: 10, 26: 4, 27: 8, 28: 9, 29: 9, 30: 3, 31: 3, 32: 9, 33: 10, 34: 14, 35: 10, 36: 18, 37: 10, 38: 7, 39: 13, 40: 9, 41: 7, 42: 1, 43: 21, 44: 5, 45: 3, 46: 12, 47: 14, 48: 23, 49: 2}
[{u'gdeg': 52, u'prox': 0.012102238789356276, u'label': u'admettre', u'gid': 99, 'cluster_id': 0, u'lemme': u'admettre', u'prox_color': [255, 154, 63], 'deg_loc': 8, u'id': 0}, {u'gdeg': 30, u'prox': 0.007512577574227055, u'label': u'apercevoir', u'gid': 339, 'cluster_id': 1, u'lemme': u'apercevoir', u'prox_color': [255, 157, 49], 'deg_loc': 17, u'id': 1}]

In [34]:
# -- SAVE --
filename = 'myData_'+request['query']+'.json'
writeFile = open(filename, 'w+')
writeFile.write( json.dumps( myData ,   indent = 4 ) )
writeFile.close()

 Brouillon


In [24]:
print myData['nodes']


[{u'gdeg': 66, u'prox': 0.01664019432587594, u'label': u'abuser', u'gid': 36, u'lemme': u'abuser', u'prox_color': [255, 132, 58], u'id': 0}, {u'gdeg': 90, u'prox': 0.008082890797141922, u'label': u'attraper', u'gid': 492, u'lemme': u'attraper', u'prox_color': [255, 143, 61], u'id': 1}, {u'gdeg': 57, u'prox': 0.0057810499120329135, u'label': u'avoir', u'gid': 528, u'lemme': u'avoir', u'prox_color': [255, 146, 54], u'id': 2}, {u'gdeg': 28, u'prox': 0.005446489210753513, u'label': u'baiser', u'gid': 688, u'lemme': u'baiser', u'prox_color': [255, 132, 49], u'id': 3}, {u'gdeg': 83, u'prox': 0.0050830751752408395, u'label': u'balancer', u'gid': 697, u'lemme': u'balancer', u'prox_color': [255, 183, 79], u'id': 4}, {u'gdeg': 182, u'prox': 0.004677962748059719, u'label': u'battre', u'gid': 742, u'lemme': u'battre', u'prox_color': [255, 210, 37], u'id': 5}, {u'gdeg': 46, u'prox': 0.004337501557391182, u'label': u'berner', u'gid': 772, u'lemme': u'berner', u'prox_color': [255, 129, 62], u'id': 6}, {u'gdeg': 17, u'prox': 0.004294696542897934, u'label': u'bouler', u'gid': 887, u'lemme': u'bouler', u'prox_color': [255, 192, 141], u'id': 7}, {u'gdeg': 29, u'prox': 0.004143258277441094, u'label': u'charrier', u'gid': 1214, u'lemme': u'charrier', u'prox_color': [255, 114, 63], u'id': 8}, {u'gdeg': 91, u'prox': 0.004087509489403487, u'label': u'couler', u'gid': 1590, u'lemme': u'couler', u'prox_color': [156, 129, 255], u'id': 9}, {u'gdeg': 39, u'prox': 0.003985702880493705, u'label': u'duper', u'gid': 1903, u'lemme': u'duper', u'prox_color': [255, 143, 62], u'id': 10}, {u'gdeg': 13, u'prox': 0.0037754638311474223, u'label': u'd\xe9bouler', u'gid': 1939, u'lemme': u'd\xe9bouler', u'prox_color': [133, 113, 255], u'id': 11}, {u'gdeg': 26, u'prox': 0.003728056262987331, u'label': u'd\xe9gringoler', u'gid': 2154, u'lemme': u'd\xe9gringoler', u'prox_color': [238, 222, 255], u'id': 12}, {u'gdeg': 16, u'prox': 0.0036613042768758267, u'label': u'empaumer', u'gid': 2577, u'lemme': u'empaumer', u'prox_color': [255, 128, 59], u'id': 13}, {u'gdeg': 18, u'prox': 0.003606016500305007, u'label': u'enrouler', u'gid': 2767, u'lemme': u'enrouler', u'prox_color': [255, 174, 104], u'id': 14}, {u'gdeg': 33, u'prox': 0.0036036931109181473, u'label': u'entortiller', u'gid': 2797, u'lemme': u'entortiller', u'prox_color': [255, 133, 38], u'id': 15}, {u'gdeg': 59, u'prox': 0.003507213376822531, u'label': u'envelopper', u'gid': 2828, u'lemme': u'envelopper', u'prox_color': [255, 142, 43], u'id': 16}, {u'gdeg': 28, u'prox': 0.003408340872123967, u'label': u'escroquer', u'gid': 2854, u'lemme': u'escroquer', u'prox_color': [255, 167, 112], u'id': 17}, {u'gdeg': 29, u'prox': 0.003374181349118949, u'label': u'exploiter', u'gid': 2926, u'lemme': u'exploiter', u'prox_color': [255, 179, 71], u'id': 18}, {u'gdeg': 14, u'prox': 0.0033449397028059687, u'label': u'flouer', u'gid': 3285, u'lemme': u'flouer', u'prox_color': [255, 145, 80], u'id': 19}, {u'gdeg': 54, u'prox': 0.0032103116746420514, u'label': u'glisser', u'gid': 3480, u'lemme': u'glisser', u'prox_color': [255, 177, 250], u'id': 20}, {u'gdeg': 2, u'prox': 0.003060468208060746, u'label': u'houler', u'gid': 3659, u'lemme': u'houler', u'prox_color': [255, 145, 89], u'id': 21}, {u'gdeg': 88, u'prox': 0.003034908409883537, u'label': u'jouer', u'gid': 3915, u'lemme': u'jouer', u'prox_color': [255, 136, 77], u'id': 22}, {u'gdeg': 24, u'prox': 0.0030186730383668058, u'label': u'leurrer', u'gid': 3987, u'lemme': u'leurrer', u'prox_color': [255, 123, 54], u'id': 23}, {u'gdeg': 2, u'prox': 0.003004821449247714, u'label': u'lover', u'gid': 4043, u'lemme': u'lover', u'prox_color': [255, 170, 106], u'id': 24}, {u'gdeg': 65, u'prox': 0.002967712449813907, u'label': u'marcher', u'gid': 4116, u'lemme': u'marcher', u'prox_color': [255, 160, 236], u'id': 25}, {u'gdeg': 22, u'prox': 0.0028694208928252596, u'label': u'mystifier', u'gid': 4432, u'lemme': u'mystifier', u'prox_color': [255, 123, 63], u'id': 26}, {u'gdeg': 23, u'prox': 0.0027787376963121137, u'label': u'naviguer', u'gid': 4487, u'lemme': u'naviguer', u'prox_color': [255, 188, 170], u'id': 27}, {u'gdeg': 20, u'prox': 0.002777390365322618, u'label': u'peloter', u'gid': 4740, u'lemme': u'peloter', u'prox_color': [255, 103, 61], u'id': 28}, {u'gdeg': 66, u'prox': 0.00276955745626538, u'label': u'penser', u'gid': 4748, u'lemme': u'penser', u'prox_color': [255, 149, 56], u'id': 29}, {u'gdeg': 13, u'prox': 0.0026694796563730464, u'label': u'pigeonner', u'gid': 4806, u'lemme': u'pigeonner', u'prox_color': [255, 136, 65], u'id': 30}, {u'gdeg': 107, u'prox': 0.0026598848818107884, u'label': u'porter', u'gid': 4912, u'lemme': u'porter', u'prox_color': [255, 119, 47], u'id': 31}, {u'gdeg': 42, u'prox': 0.0026220206910846757, u'label': u'poss\xe9der', u'gid': 4932, u'lemme': u'poss\xe9der', u'prox_color': [255, 148, 56], u'id': 32}, {u'gdeg': 210, u'prox': 0.002619127045781754, u'label': u'prendre', u'gid': 4962, u'lemme': u'prendre', u'prox_color': [255, 145, 54], u'id': 33}, {u'gdeg': 99, u'prox': 0.002613219402228744, u'label': u'pr\xe9parer', u'gid': 5095, u'lemme': u'pr\xe9parer', u'prox_color': [255, 141, 38], u'id': 34}, {u'gdeg': 61, u'prox': 0.0026111666330914, u'label': u'refaire', u'gid': 5383, u'lemme': u'refaire', u'prox_color': [255, 118, 57], u'id': 35}, {u'gdeg': 102, u'prox': 0.002607635138608011, u'label': u'remuer', u'gid': 5473, u'lemme': u'remuer', u'prox_color': [255, 148, 78], u'id': 36}, {u'gdeg': 88, u'prox': 0.0025313822055838763, u'label': u'rouler', u'gid': 5704, u'lemme': u'rouler', u'prox_color': [255, 159, 106], u'id': 37}, {u'gdeg': 22, u'prox': 0.0024407593231683595, u'label': u'se balancer', u'gid': 6595, u'lemme': u'se balancer', u'prox_color': [255, 149, 81], u'id': 38}, {u'gdeg': 53, u'prox': 0.002439254849193109, u'label': u'se moquer', u'gid': 7285, u'lemme': u'se moquer', u'prox_color': [255, 145, 84], u'id': 39}, {u'gdeg': 92, u'prox': 0.002425009926172933, u'label': u's\xe9duire', u'gid': 8072, u'lemme': u's\xe9duire', u'prox_color': [255, 110, 51], u'id': 40}, {u'gdeg': 19, u'prox': 0.002424839850526305, u'label': u'tanguer', u'gid': 8097, u'lemme': u'tanguer', u'prox_color': [255, 152, 84], u'id': 41}, {u'gdeg': 133, u'prox': 0.0023579419707219005, u'label': u'tomber', u'gid': 8205, u'lemme': u'tomber', u'prox_color': [240, 215, 255], u'id': 42}, {u'gdeg': 116, u'prox': 0.0023537195705727425, u'label': u'toucher', u'gid': 8231, u'lemme': u'toucher', u'prox_color': [255, 152, 57], u'id': 43}, {u'gdeg': 95, u'prox': 0.0023341625219371162, u'label': u'tourner', u'gid': 8243, u'lemme': u'tourner', u'prox_color': [255, 161, 178], u'id': 44}, {u'gdeg': 73, u'prox': 0.0023077036531447405, u'label': u'traiter', u'gid': 8271, u'lemme': u'traiter', u'prox_color': [255, 141, 45], u'id': 45}, {u'gdeg': 79, u'prox': 0.002300094090666082, u'label': u'transporter', u'gid': 8300, u'lemme': u'transporter', u'prox_color': [255, 106, 66], u'id': 46}, {u'gdeg': 101, u'prox': 0.0022971547337453714, u'label': u'tromper', u'gid': 8344, u'lemme': u'tromper', u'prox_color': [255, 139, 67], u'id': 47}, {u'gdeg': 118, u'prox': 0.002286751524734353, u'label': u'voler', u'gid': 8533, u'lemme': u'voler', u'prox_color': [255, 167, 201], u'id': 48}, {u'gdeg': 2, u'prox': 0.0022867165308585132, u'label': u'\xe9motter', u'gid': 8727, u'lemme': u'\xe9motter', u'prox_color': [255, 169, 73], u'id': 49}]

In [45]:
# print myData['links']

In [46]:
for k in data['results'].iterkeys():
    print k


clusters
query
layout
graph

In [47]:
print data['results']['clusters']


{u'clusters': [{u'vids': [0, 1, 3, 6, 8, 10, 13, 17, 18, 19, 23, 26, 30, 32, 35, 39, 47, 48], u'docnums': [], u'labels': [{u'score': 66, u'role': u'default', u'label': u'abuser'}, {u'score': 90, u'role': u'default', u'label': u'attraper'}, {u'score': 28, u'role': u'default', u'label': u'baiser'}, {u'score': 46, u'role': u'default', u'label': u'berner'}, {u'score': 29, u'role': u'default', u'label': u'charrier'}, {u'score': 39, u'role': u'default', u'label': u'duper'}, {u'score': 16, u'role': u'default', u'label': u'empaumer'}, {u'score': 28, u'role': u'default', u'label': u'escroquer'}, {u'score': 29, u'role': u'default', u'label': u'exploiter'}, {u'score': 14, u'role': u'default', u'label': u'flouer'}, {u'score': 24, u'role': u'default', u'label': u'leurrer'}, {u'score': 22, u'role': u'default', u'label': u'mystifier'}, {u'score': 13, u'role': u'default', u'label': u'pigeonner'}, {u'score': 42, u'role': u'default', u'label': u'poss\xe9der'}, {u'score': 61, u'role': u'default', u'label': u'refaire'}, {u'score': 53, u'role': u'default', u'label': u'se moquer'}, {u'score': 101, u'role': u'default', u'label': u'tromper'}, {u'score': 118, u'role': u'default', u'label': u'voler'}]}, {u'vids': [2, 15, 16, 22, 25, 27, 31, 33, 37, 40, 43, 45, 46], u'docnums': [], u'labels': [{u'score': 57, u'role': u'default', u'label': u'avoir'}, {u'score': 33, u'role': u'default', u'label': u'entortiller'}, {u'score': 59, u'role': u'default', u'label': u'envelopper'}, {u'score': 88, u'role': u'default', u'label': u'jouer'}, {u'score': 65, u'role': u'default', u'label': u'marcher'}, {u'score': 23, u'role': u'default', u'label': u'naviguer'}, {u'score': 107, u'role': u'default', u'label': u'porter'}, {u'score': 210, u'role': u'default', u'label': u'prendre'}, {u'score': 88, u'role': u'default', u'label': u'rouler'}, {u'score': 92, u'role': u'default', u'label': u's\xe9duire'}, {u'score': 116, u'role': u'default', u'label': u'toucher'}, {u'score': 73, u'role': u'default', u'label': u'traiter'}, {u'score': 79, u'role': u'default', u'label': u'transporter'}]}, {u'vids': [4, 5, 21, 36, 38, 41, 44], u'docnums': [], u'labels': [{u'score': 83, u'role': u'default', u'label': u'balancer'}, {u'score': 182, u'role': u'default', u'label': u'battre'}, {u'score': 2, u'role': u'default', u'label': u'houler'}, {u'score': 102, u'role': u'default', u'label': u'remuer'}, {u'score': 22, u'role': u'default', u'label': u'se balancer'}, {u'score': 19, u'role': u'default', u'label': u'tanguer'}, {u'score': 95, u'role': u'default', u'label': u'tourner'}]}, {u'vids': [7, 9, 11, 12, 20, 42], u'docnums': [], u'labels': [{u'score': 17, u'role': u'default', u'label': u'bouler'}, {u'score': 91, u'role': u'default', u'label': u'couler'}, {u'score': 13, u'role': u'default', u'label': u'd\xe9bouler'}, {u'score': 26, u'role': u'default', u'label': u'd\xe9gringoler'}, {u'score': 54, u'role': u'default', u'label': u'glisser'}, {u'score': 133, u'role': u'default', u'label': u'tomber'}]}, {u'vids': [14, 24, 28], u'docnums': [], u'labels': [{u'score': 18, u'role': u'default', u'label': u'enrouler'}, {u'score': 2, u'role': u'default', u'label': u'lover'}, {u'score': 20, u'role': u'default', u'label': u'peloter'}]}, {u'vids': [29], u'docnums': [], u'labels': [{u'score': 66, u'role': u'default', u'label': u'penser'}]}, {u'vids': [34], u'docnums': [], u'labels': [{u'score': 99, u'role': u'default', u'label': u'pr\xe9parer'}]}, {u'vids': [49], u'docnums': [], u'labels': [{u'score': 2, u'role': u'default', u'label': u'\xe9motter'}]}], u'misc': -1}

In [48]:
clusters_id = {}
for id_cluster , c in enumerate( data['results']['clusters']['clusters'] ):
    for i, vertex_id in enumerate( c['vids'] ):
        clusters_id[vertex_id] = id_cluster 
        
print clusters_id


{0: 0, 1: 0, 2: 1, 3: 0, 4: 2, 5: 2, 6: 0, 7: 3, 8: 0, 9: 3, 10: 0, 11: 3, 12: 3, 13: 0, 14: 4, 15: 1, 16: 1, 17: 0, 18: 0, 19: 0, 20: 3, 21: 2, 22: 1, 23: 0, 24: 4, 25: 1, 26: 0, 27: 1, 28: 4, 29: 5, 30: 0, 31: 1, 32: 0, 33: 1, 34: 6, 35: 0, 36: 2, 37: 1, 38: 2, 39: 0, 40: 1, 41: 2, 42: 3, 43: 1, 44: 2, 45: 1, 46: 1, 47: 0, 48: 0, 49: 7}

Points

  • mettre en avant la requete ... mais multi requete ..?
  • montrer le degrée / taille de texte / tout ou rien
  • couleur
  • selection d'un lien - affichage des voisins

In [ ]: