commited all contents created by participants
This commit is contained in:
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 195 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 19 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
@@ -0,0 +1,19 @@
|
||||
h1. undefined
|
||||
|
||||
h2. Léo Martin
|
||||
|
||||
h3. pattern
|
||||
|
||||
J'ai produit un graphe de ce que propose pattern après analyse d'un texte.
|
||||
|
||||
!images/graphe_pattern.png!
|
||||
|
||||
Et je commence à utiliser le langage dot pour visualiser du texte. Cela pourrait être intéressant de voir comment représenter graphiquement des textes avec cet outil en utilisant l'analyse avec pattern pour isoler les sujets. (Ou possible isolation des sujets pendant l'écriture, l'outil ne visera pas nécéssairement à s'appliquer à n'importe quel texte).
|
||||
|
||||
!images/phrase-vers-dot.png!
|
||||
|
||||
Même chose avec indication des "Part of Speech"
|
||||
|
||||
!images/phrases-vers-dot--2-label-pos-abbr.png!
|
||||
|
||||
!images/phrases-vers-dot--3-label-pos-longs.png!
|
||||
@@ -0,0 +1,26 @@
|
||||
digraph G {
|
||||
graph [ rankdir=LR ];
|
||||
0 [label="José"];
|
||||
1 [label="va"];
|
||||
0 -> 1
|
||||
2 [label="à"];
|
||||
1 -> 2
|
||||
3 [label="la"];
|
||||
2 -> 3
|
||||
4 [label="plage"];
|
||||
3 -> 4
|
||||
5 [label="mais"];
|
||||
4 -> 5
|
||||
6 [label="a"];
|
||||
5 -> 6
|
||||
7 [label="oublié"];
|
||||
6 -> 7
|
||||
8 [label="sa"];
|
||||
7 -> 8
|
||||
9 [label="crème"];
|
||||
8 -> 9
|
||||
10 [label="solaire"];
|
||||
9 -> 10
|
||||
11 [label="."];
|
||||
10 -> 11
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import imports.arguments as ARG
|
||||
import imports.fichiers as fichiers
|
||||
|
||||
fichier = ARG.obtenir( '-f' )
|
||||
|
||||
|
||||
if ( fichier ):
|
||||
fichiers.exporter( 'toto.txt', 'kikoo' )
|
||||
|
||||
|
||||
def main ():
|
||||
kopi = {
|
||||
'data' : ['a','b','c'],
|
||||
}
|
||||
salut( kopi )
|
||||
print kopi
|
||||
|
||||
|
||||
def salut ( a ):
|
||||
print a
|
||||
a['data'] = [ 'c', 'd', 'e' ]
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,3 @@
|
||||
Je suis allé à la plage avec Toto le chien.
|
||||
Toto a mordu des enfants.
|
||||
On a bien rigolé, les enfants moins.
|
||||
@@ -0,0 +1,274 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# outils
|
||||
import imports.arguments as ARG
|
||||
import imports.fichiers as fichiers
|
||||
import imports.dot as dot
|
||||
|
||||
# pattern
|
||||
from pattern.fr import parse, split
|
||||
|
||||
'''
|
||||
Intention :
|
||||
En utilisant le langage dot, créé un graphe du texte donné en entrée. Le programme tente de lier entre elles les phrases en prenant le mot commun le plus signifiant d'une phrase à l'autre, de m'anière à créer un graphe. La contrainte est alors d'écrire un texte avec des mots communs d'une phrase à l'autre afin de permettre le lien graphique dans le graphe.
|
||||
'''
|
||||
|
||||
# on regarde si on à l'option -f spécifiée : fichier à importer
|
||||
fichier = ARG.obtenir( '-f' )
|
||||
# si option présente mais rien de spécifié = False
|
||||
if ( not isinstance( fichier, basestring) ):
|
||||
fichier == False
|
||||
print 'aide : python contrainte.py -f adresse/du/fichier.txt'
|
||||
print 'choisir le nom d\'export : python contrainte.py -f fichier.txt -o fichier_export.txt'
|
||||
export = ARG.obtenir( '-o' )
|
||||
# on regarde si on à l'option -o spécifiée : fichier à exporter
|
||||
export = ARG.obtenir( '-o' )
|
||||
if ( not isinstance( export, basestring) ):
|
||||
export == False
|
||||
|
||||
# si on a un nom de fichier associé à l'option -f
|
||||
if ( fichier ):
|
||||
fichier = fichiers.importer( fichier )
|
||||
|
||||
scores = {
|
||||
"CC" : 10, #u"conjonction de coordination",
|
||||
"CD" : 10, #u"nombre cardinal",
|
||||
"DT" : 10, #u"déterminant",
|
||||
"EX" : 7, #u"Existential there",
|
||||
"FW" : 4, #u"mot étranger",
|
||||
"IN" : 7, #u"préposition ou conjonction de subordination",
|
||||
"JJ" : 5, #u"adjectif",
|
||||
"JJR" : 5, #u"adjectif comparatif",
|
||||
"JJS" : 5, #u"adjectif superlatif",
|
||||
"LS" : 99, #u"marqueur d'élément de liste",
|
||||
"MD" : 6, #u"modal",
|
||||
"NN" : 2, #u"nm, singulier ou mass",
|
||||
"NNS" : 3, #u"nom, pluriel",
|
||||
"NNP" : 0, #u"nom propre, singulier",
|
||||
"NNPS" : 1, #u"nom propre, pluriel",
|
||||
"PDT" : 10, #u"predeterminer",
|
||||
"POS" : 10, #u"possessive ending",
|
||||
"PRP" : 2, #u"pronom personnel",
|
||||
"PRP$" : 2, #u"pronom possessif",
|
||||
"RB" : 4, #u"adverbe",
|
||||
"RBR" : 4, #u"adverbe, comparatif",
|
||||
"RBS" : 4, #u"adverbe, superlatif",
|
||||
"RP" : 10, #u"particle",
|
||||
"SYM" : -1, #u"symbole",
|
||||
}
|
||||
|
||||
|
||||
def traiter_ligne ( ligne ):
|
||||
'''
|
||||
pour chaque ligne :
|
||||
on analyse avec pattern
|
||||
on regarde les éléments communs avec la phrase d'avant, la phrase d'après
|
||||
on prend celui avec le meilleur score
|
||||
|
||||
'''
|
||||
print ligne
|
||||
|
||||
def traiter_phrase ( phrase_1, phrase_2=False, dernier_id_phrase_2=False ):
|
||||
''''''
|
||||
|
||||
# modèle de la sortie
|
||||
sortie = {
|
||||
'sentence': phrase_1,
|
||||
'correspondances': None,
|
||||
}
|
||||
|
||||
# on gère les correspondances
|
||||
if ( phrase_2 != False ):
|
||||
sortie['correspondances'] = index_correspondances ( phrase_1, phrase_2, dernier_id_phrase_2 )
|
||||
|
||||
# on filtre les correspondances (ne garder que la plus importante)
|
||||
sortie = filtrer_correspondances( sortie )
|
||||
return sortie
|
||||
|
||||
def filtrer_correspondances ( data ):
|
||||
''''''
|
||||
# si on a des correspondances
|
||||
if ( data['correspondances'] != None ):
|
||||
precedent = False # false ou index
|
||||
# pour chaque correspondance
|
||||
for i in range( len( data['correspondances']) ):
|
||||
# si on a une correspondance et qu'on en a déjà eu une
|
||||
if ( data['correspondances'][i] != False and precedent != False ):
|
||||
# on compare :
|
||||
resultat = comparer_score ( data['sentence'], i, precedent )
|
||||
# si meilleur score :
|
||||
if ( resultat ):
|
||||
# on supprime la précédente référence
|
||||
data['correspondances'][precedent] = False
|
||||
else:
|
||||
# sinon on supprime cette référence
|
||||
data['correspondances'][i] = False
|
||||
elif ( precedent == False ):
|
||||
precedent = i
|
||||
return data
|
||||
|
||||
def obtenir_score ( type ):
|
||||
''''''
|
||||
try:
|
||||
scores[ type ]
|
||||
except Exception as erreur:
|
||||
print erreur
|
||||
return False
|
||||
|
||||
def comparer_score ( sentence, i, i_precedent=False):
|
||||
''''''
|
||||
try:
|
||||
score = scores[ sentence.words[ i ].type ]
|
||||
if ( i_precedent != False ):
|
||||
score_precedent = scores[ sentence.words[ i_precedent ].type ]
|
||||
# si on a un meilleur score (plus faible)
|
||||
if ( score < score_precedent ):
|
||||
# on retourne vrai
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
except Exception as erreur:
|
||||
print 'type sans score : ' + str( erreur )
|
||||
return False
|
||||
|
||||
def phrase_vers_liste( phrase ):
|
||||
''''''
|
||||
liste = []
|
||||
for mot in phrase:
|
||||
liste.append( mot.string )
|
||||
return liste
|
||||
|
||||
def index_correspondances ( liste_1, liste_2, dernier_id_phrase_2=0 ):
|
||||
'''renvoie un tableau avec pour chaque mot, soit False si pas de correspondance, soit l'index dans la phrase + celle dans le texte'''
|
||||
sortie = len(liste_1) * [False]
|
||||
|
||||
if ( not dernier_id_phrase_2 ):
|
||||
dernier_id_phrase_2 = 0
|
||||
|
||||
for a in range( len(liste_1) ):
|
||||
for b in range( len(liste_2) ):
|
||||
if ( liste_1[a].string == liste_2[b].string ):
|
||||
#print( str(liste_1[a]) + ' = ' + str(liste_2[b]) )
|
||||
sortie[a] = [b, b + dernier_id_phrase_2]
|
||||
#else:
|
||||
#print( str(liste_1[a]) + ' != ' + str(liste_2[b]))
|
||||
return sortie
|
||||
|
||||
def traiter_fichier ( fichier=False ):
|
||||
'''traite le fichier. Retourne une liste contenant des dictionnaires contenant :
|
||||
- sentence : la phrase au format pattern sentence
|
||||
- correspondance : un index indiquant les correspondances avec la phrase précédente'''
|
||||
|
||||
# liste des phrases avant traitement
|
||||
phrases_a_traiter = []
|
||||
# la liste à retourner
|
||||
phrases_traitees = []
|
||||
|
||||
# si on a bien un fichier
|
||||
if ( fichier != False ):
|
||||
|
||||
# on lit les lignes
|
||||
lignes = fichier.readlines()
|
||||
|
||||
# on ferme le fichier
|
||||
fichier.close()
|
||||
|
||||
compteur_mots = 0;
|
||||
|
||||
# pour chaque ligne
|
||||
for ligne in lignes:
|
||||
|
||||
# on parse
|
||||
texte_traite = parse( ligne )
|
||||
|
||||
# on sépare les phrases
|
||||
phrases_dans_la_ligne = split( texte_traite )
|
||||
|
||||
# on ajoute les phrases de la ligne à la liste générale
|
||||
for phrase in phrases_dans_la_ligne:
|
||||
phrases_a_traiter.append ( phrase )
|
||||
|
||||
# pour chaque phrase, on traite
|
||||
for i in range( len( phrases_a_traiter ) ):
|
||||
phrase_precedente = False
|
||||
if ( i > 0 ):
|
||||
phrase_precedente = phrases_a_traiter[ i - 1 ]
|
||||
phrases_traitees.append( traiter_phrase( phrases_a_traiter[i], phrase_precedente, compteur_mots ) )
|
||||
compteur_mots += len( phrases_a_traiter[i] )
|
||||
# for i in range( len( phrases_dans_la_ligne) ) :
|
||||
# compteur_phrases++
|
||||
# phrase_precedente = False
|
||||
# print('---')
|
||||
# print( 'i :' + str(i))
|
||||
# if ( compteur_phrases > 0 ):
|
||||
# phrase_precedente = phrases_dans_la_ligne[ i - 1 ]
|
||||
# phrases.append( traiter_phrase( phrases_dans_la_ligne[i], phrase_precedente ) )
|
||||
#print ('len' + str(len(phrases)))
|
||||
#for i in range( len( phrases) ):
|
||||
#print i
|
||||
#liste[i] = phrase_vers_liste( phrases[i] )
|
||||
#print str(phrases[i].__dict__.keys())
|
||||
#for mot in phrases[i].words:
|
||||
# print mot.string
|
||||
# print str(mot) + str( mot.__dict__.keys() )
|
||||
#print( '---' )
|
||||
#print(liste)
|
||||
#print( index_correspondances( liste[0], liste[1] ))
|
||||
#for lexeme in split( texte_traite )
|
||||
# on ajoute à la liste à retourner
|
||||
#liste_phrases.insert( len(liste_phrases), phrases )
|
||||
|
||||
# on retourne la liste
|
||||
#return liste_phrases
|
||||
# print phrases
|
||||
# mots_des_phrases = len( phrases ) * [None]
|
||||
# for i in range( len( phrases ) ):
|
||||
# mots_des_phrases[i] = phrase_vers_liste( phrases[i] )
|
||||
# print( index_correspondances( mots_des_phrases[0], mots_des_phrases[i] ) )
|
||||
# return sortie
|
||||
return phrases_traitees
|
||||
|
||||
def generer_graphe ( data ):
|
||||
'''génère le graphe à partir des données'''
|
||||
compteur_mots = 0 # pour les id des noeuds
|
||||
for p in range( len( data ) ):
|
||||
for m in range( len( data[p]['sentence'].words ) ):
|
||||
compteur_mots += 1
|
||||
id = compteur_mots
|
||||
a = data[p]['correspondances'][m]
|
||||
print a
|
||||
#if ( data[p]['correspondances'][m] != False ) :
|
||||
# id = data[p]['correspondances'][m][1]
|
||||
#print id
|
||||
# print( str(p) + ':' + str(m) + ' : ' + str( compteur_mots ) + ' mots.' )
|
||||
|
||||
|
||||
def main ():
|
||||
''''''
|
||||
|
||||
elements = traiter_fichier( fichier )
|
||||
print elements
|
||||
generer_graphe( elements )
|
||||
# for e in elements:
|
||||
# print('---')
|
||||
# for i in e:
|
||||
# print('>')
|
||||
# print(i)
|
||||
# construction = {
|
||||
# 'ligne precedente': False,
|
||||
# 'lignes a traiter': False,
|
||||
# 'lignes traitees': False,
|
||||
# }
|
||||
#
|
||||
# if ( fichier != False ):
|
||||
# construction['lignes a traiter'] = fichier.readlines()
|
||||
# fichier.close()
|
||||
# # fichiers.exporter( lignes )
|
||||
# for i in range(len( construction['lignes a traiter'] )):
|
||||
# #traiter_ligne( ligne )
|
||||
# print i
|
||||
# construction['lignes a traiter'][i] = '_'
|
||||
# print construction['lignes a traiter']
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,49 @@
|
||||
digraph G {
|
||||
graph [ rankdir=LR ];
|
||||
Phrase [ label="Phrase", shape = polygon, sides = 4, color=blue, style=filled, fillcolor=blue, fontcolor=white ];
|
||||
phrase__pnp [label="pnp"];
|
||||
Word [ label="Word", shape = polygon, sides = 4, color=blue, style=filled, fillcolor=blue, fontcolor=white ];
|
||||
word__pnp [label="pnp"];
|
||||
word__chunk [label="chunk"];
|
||||
Chunk [ label="Chunk", shape = polygon, sides = 4, color=blue, style=filled, fillcolor=blue, fontcolor=white ];
|
||||
chunk__pnp [label="pnp"];
|
||||
chunk__relations [label="relations"];
|
||||
chunk__sentence [label="sentence"];
|
||||
chunk__words [label="words"];
|
||||
chunk__type [label="type"];
|
||||
"texte" -> Phrase
|
||||
Phrase -> "parent"
|
||||
Phrase -> "language"
|
||||
Phrase -> "_anchors"
|
||||
Phrase -> "text"
|
||||
Phrase -> "_relation"
|
||||
Phrase -> "relations"
|
||||
Phrase -> "token"
|
||||
Phrase -> "words"
|
||||
Phrase -> phrase__pnp
|
||||
Phrase -> "chunks"
|
||||
Phrase -> "attachment"
|
||||
Phrase -> "id"
|
||||
Phrase -> "_previous"
|
||||
"words" -> Word
|
||||
"chunks" -> Chunk
|
||||
Chunk -> "_modifiers"
|
||||
Chunk -> "attachments"
|
||||
Chunk -> chunk__sentence
|
||||
Chunk -> chunk__pnp
|
||||
Chunk -> chunk__relations
|
||||
Chunk -> "_conjunctions"
|
||||
Chunk -> chunk__words
|
||||
Chunk -> chunk__type
|
||||
Chunk -> "anchor"
|
||||
chunk__words -> Word
|
||||
Word -> "index"
|
||||
Word -> "string"
|
||||
Word -> "_custom_tags"
|
||||
Word -> "sentence"
|
||||
Word -> word__pnp
|
||||
Word -> word__chunk
|
||||
Word -> "lemma"
|
||||
Word -> "type"
|
||||
word__chunk -> Chunk
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
|
||||
def obtenir ( option, fallback=False ) :
|
||||
'''String -> String/Boolean
|
||||
Si l'option demandée n'existe pas, renvoie False.
|
||||
Si l'option demandée existe'''
|
||||
# si on a l'option
|
||||
if option in sys.argv:
|
||||
index = sys.argv.index( option ) + 1
|
||||
# si on a quelque chose après notre option
|
||||
if len( sys.argv ) > index :
|
||||
# si ce quelque chose ne commence pas par -
|
||||
if sys.argv[ index ][0] != '-':
|
||||
return sys.argv[ index ]
|
||||
# si c'est le cas on a à priori une option booléenne
|
||||
else:
|
||||
# si pas de fallback
|
||||
if not fallback:
|
||||
return True
|
||||
# sinon on retourne le fallback
|
||||
else:
|
||||
return fallback
|
||||
# si on a l'option mais rien derrière
|
||||
else:
|
||||
# si pas de fallback
|
||||
if not fallback:
|
||||
return True
|
||||
# si fallback
|
||||
else:
|
||||
# on renvoie le fallback
|
||||
return fallback
|
||||
# dans les autres cas on renvoie False ou le fallback si défini
|
||||
return fallback
|
||||
Binary file not shown.
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
def demarrer_graphe ( horizontal=False) :
|
||||
print 'digraph G {'
|
||||
if ( horizontal ):
|
||||
print ' graph [ rankdir=LR ];'
|
||||
|
||||
def ajouter_noeud_mot ( word, id=False, label=False ):
|
||||
print ' '
|
||||
|
||||
def lier( id_a, id_b ):
|
||||
print ' '
|
||||
|
||||
def clore_graphe () :
|
||||
print '}'
|
||||
Binary file not shown.
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import datetime
|
||||
|
||||
'''
|
||||
Ce module facilite l'import/export de fichiers
|
||||
'''
|
||||
|
||||
|
||||
def importer ( chemin ) :
|
||||
'''String -> File
|
||||
tente d'importer le fichier dont le chemin est spécifié'''
|
||||
try:
|
||||
fichier = open( chemin, 'r' )
|
||||
return fichier
|
||||
except Exception as erreur:
|
||||
print erreur
|
||||
return False
|
||||
|
||||
def exporter ( contenu, chemin=False ):
|
||||
''''''
|
||||
if ( chemin == False ):
|
||||
chemin = generer_nom_de_fichier()
|
||||
fichier = open( chemin, 'w' )
|
||||
if ( isinstance( contenu, basestring) ):
|
||||
fichier.write( contenu )
|
||||
elif ( isinstance( contenu, (list, tuple) ) ):
|
||||
traiter_liste ( contenu, fichier )
|
||||
fichier.close()
|
||||
|
||||
def generer_nom_de_fichier ():
|
||||
''''''
|
||||
return datetime.datetime.now().strftime("%Y-%m-%d-%Hh%Mm%Ss.txt")
|
||||
|
||||
def traiter_liste ( liste, fichier ):
|
||||
''''''
|
||||
for item in liste:
|
||||
if ( isinstance( item, basestring) ):
|
||||
fichier.write( item )
|
||||
elif isinstance( item, (list, tuple)):
|
||||
traiter_liste ( item, fichier )
|
||||
Binary file not shown.
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
posse = {
|
||||
"CC" : u"conjonction de coordination",
|
||||
"CD" : u"nombre cardinal",
|
||||
"DT" : u"déterminant",
|
||||
"EX" : u"Existential there",
|
||||
"FW" : u"mot étranger",
|
||||
"IN" : u"préposition ou conjonction de subordination",
|
||||
"JJ" : u"adjectif",
|
||||
"JJR" : u"adjectif comparatif",
|
||||
"JJS" : u"adjectif superlatif",
|
||||
"LS" : u"marqueur d'élément de liste",
|
||||
"MD" : u"modal",
|
||||
"NN" : u"nm, singulier ou mass",
|
||||
"NNS" : u"nom, pluriel",
|
||||
"NNP" : u"nom propre, singulier",
|
||||
"NNPS" : u"nom propre, pluriel",
|
||||
"PDT" : u"predeterminer",
|
||||
"POS" : u"possessive ending",
|
||||
"PRP" : u"pronom personnel",
|
||||
"PRP$" : u"pronom possessif",
|
||||
"RB" : u"adverbe",
|
||||
"RBR" : u"adverbe, comparatif",
|
||||
"RBS" : u"adverbe, superlatif",
|
||||
"RP" : u"particle",
|
||||
"SYM" : u"symbole",
|
||||
}
|
||||
|
||||
def importer :
|
||||
return posse
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from pattern.fr import parse, split
|
||||
|
||||
texte = parse(u"Le chat joue au tennis.")
|
||||
|
||||
def imprimer_structure_mot ( mot ):
|
||||
'''imprime la structure du mot'''
|
||||
print 'index : ' + str( mot.index )
|
||||
print 'string : ' + str( mot.string )
|
||||
print '_custom_tags : ' + str( mot._custom_tags )
|
||||
print 'sentence : ' + str( mot.sentence )
|
||||
print 'pnp : ' + str( mot.pnp )
|
||||
print 'chunk : ' + str( mot.chunk )
|
||||
print 'lemma : ' + str( mot.lemma )
|
||||
print 'type : ' + str( mot.type )
|
||||
|
||||
# pour chaque phrase dans le texte
|
||||
for phrase in split( texte ):
|
||||
# pour chaque mot dans la phrase
|
||||
for mot in phrase.words:
|
||||
print "---"
|
||||
imprimer_structure_mot( mot )
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/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 ()
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/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
|
||||
|
||||
posse = {
|
||||
"CC" : u"conjonction de coordination",
|
||||
"CD" : u"nombre cardinal",
|
||||
"DT" : u"déterminant",
|
||||
"EX" : u"Existential there",
|
||||
"FW" : u"mot étranger",
|
||||
"IN" : u"préposition ou conjonction de subordination",
|
||||
"JJ" : u"adjectif",
|
||||
"JJR" : u"adjectif comparatif",
|
||||
"JJS" : u"adjectif superlatif",
|
||||
"LS" : u"marqueur d'élément de liste",
|
||||
"MD" : u"modal",
|
||||
"NN" : u"nm, singulier ou mass",
|
||||
"NNS" : u"nom, pluriel",
|
||||
"NNP" : u"nom propre, singulier",
|
||||
"NNPS" : u"nom propre, pluriel",
|
||||
"PDT" : u"predeterminer",
|
||||
"POS" : u"possessive ending",
|
||||
"PRP" : u"pronom personnel",
|
||||
"PRP$" : u"pronom possessif",
|
||||
"RB" : u"adverbe",
|
||||
"RBR" : u"adverbe, comparatif",
|
||||
"RBS" : u"adverbe, superlatif",
|
||||
"RP" : u"particle",
|
||||
"SYM" : u"symbole",
|
||||
}
|
||||
|
||||
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 ):
|
||||
if word.type in posse:
|
||||
type_pos_label = posse[word.type]
|
||||
else:
|
||||
type_pos_label = word.type
|
||||
type_pos = word.type.replace( '.', 'point' ).replace( ',', 'virgule' )
|
||||
print ' type__' + str( numero ) + '__' + type_pos + ' [label="' + type_pos_label + '"];'
|
||||
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 ()
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import imports.arguments as ARG
|
||||
from pattern.fr import parse, split
|
||||
|
||||
texte_brut = ARG.obtenir( '-p', u"Le petit chat rugit alors que l'esclave humain tarde a accomplir sa tache de nourrissage du maitre et seigneur." )
|
||||
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 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 )
|
||||
lier_a_noeud_precedent ( numero_mot )
|
||||
|
||||
clore_graphe ()
|
||||
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
from pattern.fr import parse, split
|
||||
|
||||
def imprimer_erreur ( erreur ):
|
||||
print '💩 erreur : ' + str( erreur )
|
||||
|
||||
print len(sys.argv)
|
||||
|
||||
if len(sys.argv) > 1:
|
||||
try:
|
||||
texte_brut = sys.argv[1].encode('utf-8');
|
||||
except Exception as erreur:
|
||||
imprimer_erreur( erreur )
|
||||
quit()
|
||||
else:
|
||||
texte_brut = u"Je mange des chips avec le pape.";
|
||||
|
||||
print "traitement : " + texte_brut
|
||||
|
||||
texte_traite = parse( texte_brut )
|
||||
|
||||
|
||||
for phrase in split( texte_traite ):
|
||||
try:
|
||||
print phrase.pnp
|
||||
except Exception as erreur:
|
||||
imprimer_erreur( erreur )
|
||||
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pattern.fr import parse, split
|
||||
|
||||
texte_brut = u"Le petit chat rugit alors que l'esclave humain tarde a accomplir sa tache de nourrissage du maitre et seigneur.".encode('utf-8')
|
||||
texte_traite = parse( texte_brut )
|
||||
|
||||
def imprimer_erreur ( erreur ):
|
||||
print '💩 erreur : ' + str( erreur )
|
||||
|
||||
def imprimer_structure_mot ( mot ):
|
||||
'''imprime la structure de la phrase'''
|
||||
print 'index : ' + str( mot.index )
|
||||
print 'string : ' + str( mot.string )
|
||||
print '_custom_tags : ' + str( mot._custom_tags )
|
||||
print 'sentence : ' + str( mot.sentence )
|
||||
print 'pnp : ' + str( mot.pnp )
|
||||
print 'chunk : ' + str( mot.chunk )
|
||||
print 'lemma : ' + str( mot.lemma )
|
||||
print 'type : ' + str( mot.type )
|
||||
|
||||
for phrase in split( texte_traite ):
|
||||
print "——————————"
|
||||
print "mots :"
|
||||
try:
|
||||
print phrase.words
|
||||
except Exception as erreur:
|
||||
imprimer_erreur( erreur )
|
||||
print "——————————"
|
||||
print "groupes de mots :"
|
||||
try:
|
||||
print str(phrase.chunks)
|
||||
except Exception as erreur:
|
||||
imprimer_erreur( erreur )
|
||||
print "——————————"
|
||||
print "structure, phrase :"
|
||||
|
||||
try:
|
||||
print phrase.__dict__.keys()
|
||||
except Exception as erreur:
|
||||
imprimer_erreur( erreur )
|
||||
print "——————————"
|
||||
print "structure, mot :"
|
||||
|
||||
try:
|
||||
print phrase.words[0].__dict__.keys()
|
||||
except Exception as erreur:
|
||||
imprimer_erreur( erreur )
|
||||
print "——————————"
|
||||
print "structure, chunk (groupe de mots) :"
|
||||
print phrase.chunks[0].__dict__.keys()
|
||||
Reference in New Issue
Block a user