#!/usr/bin/env python # -*- coding: utf-8 -*- import imports.arguments as ARG from pattern.fr import parse, split texte_brut = ARG.obtenir( '-p', u"Suivez les flèches, attention aux trous." ) texte_traite = parse( texte_brut ) numero_mot = -1 def imprimer_erreur ( erreur ): print '💩 erreur : ' + str( erreur ) def demarrer_graphe () : print 'digraph G {' print ' graph [ rankdir=LR ];' def ajouter_noeud_mot ( word, numero ): print ' ' + str( numero ) + ' [label="' + word.string + '"];' def ajouter_noeud_type ( word, numero ): type_pos = word.type.replace( '.', 'point' ).replace( ',', 'virgule' ) print ' type__' + str( numero ) + '__' + type_pos + ' [label="' + word.type + '"];' print ' ' + str( numero ) +' -> type__' + str( numero ) + '__' + type_pos; def lier_a_noeud_precedent ( numero ): if ( numero_mot > 0 ): print ' ' + str( numero - 1 ) + ' -> ' + str ( numero ) def clore_graphe () : print '}' demarrer_graphe () # pour chaque phrase for phrase in split( texte_traite ): # pour chaque mot for word in phrase.words: # on incrément le numéro du mot numero_mot = numero_mot + 1 ajouter_noeud_mot ( word, numero_mot ) ajouter_noeud_type ( word, numero_mot ) lier_a_noeud_precedent ( numero_mot ) clore_graphe ()