x**3な人生

基本的にはメモ

Wordnet

wordnetのオブジェクトをいろいろと見てメモしていきます。
(徐々に追加予定)

(1)モジュールをインポートする。

>>> from nltk.wordnet import *

(2)名詞、動詞、形容詞など ('Word' object)

>>> noun = N['dog']     # 名詞
>>> verb = V['run']     # 動詞
>>> adjective = ADJ['large'] # 形容詞
>>> adverb = ADV['largely']  # 副詞

>>> noun.form
'dog'
>>> noun.pos
'noun'

(3)'Synset' object

# synsetを見てみる
>>> N['dog'].synsets()
[{noun: dog, domestic_dog, Canis_familiaris}, {noun: frump, dog}, {noun: dog}, {noun: cad, bounder, blackguard, dog, hound, heel}, {noun: frank, frankfurter, hotdog, hot_dog, dog, wiener, wienerwurst, weenie}, {noun: pawl, detent, click, dog}, {noun: andiron, firedog, dog, dog-iron}]

# synsetの中身にアクセスしてみる
>>> dog_syns = N['dog'].synsets()
>>> dog_syns[0]
{noun: dog, domestic_dog, Canis_familiaris}
>>> dog_syns[0][0]
dog
>>> dog_syns[0][1]
domestic_dog

# hypernym(上位語)
>>> dog_syns[0][HYPERNYM]
[{noun: canine, canid}, {noun: domestic_animal, domesticated_animal}]

# treeで遡って見てみる
>>> from pprint import pprint # 階層構造を見やすく表示するためにpprintを使う
>>> pprint(dog_syns[0].tree(HYPERNYM))
[{noun: dog, domestic_dog, Canis_familiaris},
 [{noun: canine, canid},
  [{noun: carnivore},
   [{noun: placental, placental_mammal, eutherian, eutherian_mammal},
    [{noun: mammal, mammalian},
     [{noun: vertebrate, craniate},
      [{noun: chordate},
       [{noun: animal, animate_being, beast, brute, creature, fauna},
        [{noun: organism, being},
         [{noun: living_thing, animate_thing},
          [{noun: whole, unit},
           [{noun: object, physical_object},
            [{noun: physical_entity}, [{noun: entity}]]]]]]]]]]]]],
 [{noun: domestic_animal, domesticated_animal},
  [{noun: animal, animate_being, beast, brute, creature, fauna},
   [{noun: organism, being},
    [{noun: living_thing, animate_thing},
     [{noun: whole, unit},
      [{noun: object, physical_object},
       [{noun: physical_entity}, [{noun: entity}]]]]]]]]]

今日はここまで