python 使用 graphviz 可视化 WordNet

from nltk.corpus import wordnet as wn
from graphviz import Digraph

word="genius"

digraph=Digraph("WordNet Graph")

digraph.node(name="root_node",label=word,color="orange",style="filled",fontcolor="white",fontsize="50")

sense_node_id=0
hyponym_node_id=0
hypernym_node_id=0

for sense in wn.synsets(word):
    #词义
    sense_definition="\n".join(sense.definition().split(" "))
    sense_lemma_names=sense.lemma_names()
    sense_label="\n".join(sense_lemma_names)
    sense_node_name="sense_node_"+str(sense_node_id)    
    digraph.node(name=sense_node_name,label=sense_label,style="filled",color="#008080",fontcolor="white",fontsize="25")
    digraph.edge("root_node",sense_node_name,label=sense_definition,color="#008080",fontcolor="#008080")
    sense_node_id+=1    
    
    #下位词
    for hyponym in sense.hyponyms():
        hyponym_definition="\n".join(hyponym.definition().split(" "))
        for hyponym_lemma_name in hyponym.lemma_names():
            hyponym_node_name="hyponym_node_"+str(hyponym_node_id)
            digraph.node(name=hyponym_node_name,label=hyponym_lemma_name,style="filled",color="#ff6666",fontcolor="white",fontsize="15")
            digraph.edge(sense_node_name,hyponym_node_name,label=hyponym_definition,color="#ff6666",fontcolor="#ff6666")
            hyponym_node_id+=1
            
    '''
    #上位词
    for hypernym in sense.hypernyms():
        hypernym_definition="\n".join(hypernym.definition().split(" "))
        for hypernym_lemma_name in hypernym.lemma_names():
            hypernym_node_name="hypernym_node_"+str(hypernym_node_id)
            digraph.node(name=hypernym_node_name,label=hypernym_lemma_name,style="filled",color="#EEAD0E",fontcolor="white",fontsize="15")
            digraph.edge(hypernym_node_name,sense_node_name,label=hypernym_definition,color="#EEAD0E",fontcolor="#EEAD0E")
            hypernym_node_id+=1
    '''

digraph.view()

python 使用 graphviz 可视化 WordNet_第1张图片

 

 

 

python 使用 graphviz 可视化 WordNet_第2张图片

 

from nltk.corpus import wordnet as wn
from graphviz import Digraph

def nws(sentence,n=1):
    words=sentence.split(" ")
    sentence_length=len(words)
    part_number=int(sentence_length/n)
    new_words=[]
    for i in range(part_number):
        new_words.append(" ".join(words[i*n:i*n+n]))
        new_words.append("\n")
    new_words.append(" ".join(words[part_number*n:]))
    return " ".join(new_words)

word="communion"
digraph=Digraph("WordNet Graph")
digraph.rankdir = "LR"
digraph.node(name="root_node",label=word,color="red",style="filled",fontcolor="white",fontsize="30",fontname="Microsoft YaHei")

node_index=0

for s in wn.synsets(word):
    space = "    "
    print(space*0+"- ",s.lemma_names())
    label="\n\n".join(s.lemma_names())
    definition=nws(s.definition(),4)
    node_name=str(node_index)
    node_name_definition=node_name+"definition"    
    
    digraph.node(name=node_name,label=label,style="filled",color="#e45e21",fontcolor="white",fontsize="15",fontname="Microsoft YaHei")
    digraph.edge("root_node",node_name,color="#e45e21",fontcolor="#e45e21")
    
    digraph.node(name=node_name_definition,label=definition,shape="box",color="#e45e21",fontcolor="#e45e21",fontsize="15",fontname="Microsoft YaHei")
    digraph.edge(node_name,node_name_definition,color="#e45e21",fontcolor="#e45e21")
    
    node_index+=1
    
    for h in s.hyponyms():
        print(space*1+"- ",h.lemma_names())
        label1="\n\n".join(h.lemma_names())
        definition1=nws(h.definition(),6)
        node_name1=str(node_index)
        node_name_definition1=node_name1+"definition"   
        
        digraph.node(name=node_name1,label=label1,style="filled",color="#008080",fontcolor="white",fontsize="12",fontname="Microsoft YaHei")
        digraph.edge(node_name_definition,node_name1,color="#008080",fontcolor="#008080")
        
        digraph.node(name=node_name_definition1,label=definition1,shape="box",color="#008080",fontcolor="#008080",fontsize="12",fontname="Microsoft YaHei")
        digraph.edge(node_name1,node_name_definition1,color="#008080",fontcolor="#008080")
        
        node_index+=1

digraph.graph_attr['rankdir'] = 'LR'                
digraph.view()
#digraph.render('d:/graph_test.dot', view=True)

python 使用 graphviz 可视化 WordNet_第3张图片

你可能感兴趣的:(python)