如何在图表中的列表等列表中表示嵌套列表?

时间:2015-10-05 20:41:28

标签: python nltk

目前,我正试图使用​​networkx在图表中显示部分wordnet。 nltk.wordnet.synsets('girl')[0].tree(lambda s:s.hypernyms())返回的输出如下所示:

[Synset('girl.n.01'),
 [Synset('woman.n.01'),
  [Synset('adult.n.01'),
   [Synset('person.n.01'),
    [Synset('causal_agent.n.01'),
     [Synset('physical_entity.n.01'), [Synset('entity.n.01')]]],
    [Synset('organism.n.01'),
     [Synset('living_thing.n.01'),
      [Synset('whole.n.02'),
       [Synset('object.n.01'),
        [Synset('physical_entity.n.01'), [Synset('entity.n.01')]]]]]]]],
  [Synset('female.n.02'),
   [Synset('person.n.01'),
    [Synset('causal_agent.n.01'),
     [Synset('physical_entity.n.01'), [Synset('entity.n.01')]]],
    [Synset('organism.n.01'),
     [Synset('living_thing.n.01'),
      [Synset('whole.n.02'),
       [Synset('object.n.01'),
        [Synset('physical_entity.n.01'), [Synset('entity.n.01')]]]]]]]]]]

我的目标是在有向图中表示此输出。 关键是Synset('entity.n.01')应该是根节点,并且应该有从Synset('girl.n.01')Synset('entity.n.01')以及从Synset('organism.n.01')Synset('entity.n.01')等的路径。

这应该通过递归来解决吗?有没有人知道从嵌套的嵌套嵌套列表等到以Synset('entity.n.01')为根的图形的解决方案?

1 个答案:

答案 0 :(得分:0)

最后,我解决了这个问题,不是在同义词集上使用nltk树方法,而是使用递归函数迭代单词的同义词

from nltk.corpus import wordnet as wn

def extract_hypernyms(sense):
    global data_list
    if(len(sense.hypernyms()) > 0):
        for hypernym in sense.hypernyms():
            data_list.append(sense)
            extract_hypernyms(hypernym)
    else:
        hypernym_tree.append([data for data in data_list])
        del data_list[:]

if __name__ =='__main__':
    hypernym_tree = list()
    data_list = []
    word = wn.synsets('avocado')[0]
    extract_hypernyms(word)
    create_path_from_nodes(hypernym_tree)

这将返回您迭代的列表列表。

[[Synset('avocado.n.01'),
  Synset('edible_fruit.n.01'),
  Synset('fruit.n.01'),
  Synset('reproductive_structure.n.01'),
  Synset('plant_organ.n.01'),
  Synset('plant_part.n.01'),
  Synset('natural_object.n.01'),
  Synset('whole.n.02'),
  Synset('object.n.01'),
  Synset('physical_entity.n.01')],
 [Synset('edible_fruit.n.01'),
  Synset('produce.n.01'),
  Synset('food.n.02'),
  Synset('solid.n.01'),
  Synset('matter.n.03'),
  Synset('physical_entity.n.01')]]