Hypernym or Hyponym definition using wordnet nltk

I want to check the dependence of the hypernim / hyponym between two words (user-defined), this means that either of them can be a hypernonym of the other, or it can also be that there is no hypernomal relationship between them. Can I use path_similarity for the same. I am trying to do so. If you can offer any best method for this. I also want to know if it is better to check the same from sparql query

first=wn.synset('automobile.n.01') second=wn.synset('car.n.01') first.path_similarity(second) 
+6
source share
1 answer

Firstly, there is a difference between wordnet and synset / concept in wordnet.

Here we see that one word can have several meanings (i.e. links to several concepts):

 >>> from nltk.corpus import wordnet as wn >>> car = 'car' >>> auto = 'automobile' >>> wn.synsets(auto) [Synset('car.n.01'), Synset('automobile.v.01')] >>> wn.synsets(car) [Synset('car.n.01'), Synset('car.n.02'), Synset('car.n.03'), Synset('car.n.04'), Synset('cable_car.n.01')] 

And in this case, β€œcar” and β€œcar” can refer to the same Synset('car.n.01') , and if so, then they do not have a hypo / hypernym relationship.

There is also the concept of lemma , which will simply complicate the situation, so we will skip this now.

Let's say you are not comparing words, but synsets, then you can just find all the syntax hyponyms and see if another synchronization will occur inside it.

If you are comparing simple words, see How to get all hyponyms of the word / synset in python nltk and wordnet?

The following shows how to compare synsets. For example, sake, I will use "fruit" and "apple", which is more logical than "car" and "car", since there is only one noun synset for "car" and "car"

 >>> from nltk.corpus import wordnet as wn >>> >>> fruit = 'fruit' >>> wn.synsets(fruit) [Synset('fruit.n.01'), Synset('yield.n.03'), Synset('fruit.n.03'), Synset('fruit.v.01'), Synset('fruit.v.02')] >>> wn.synsets(fruit)[0].definition() u'the ripened reproductive body of a seed plant' >>> fruit = wn.synsets(fruit)[0] >>> >>> apple = 'apple' >>> wn.synsets(apple) [Synset('apple.n.01'), Synset('apple.n.02')] >>> wn.synsets(apple)[0].definition() u'fruit with red or yellow or green skin and sweet to tart crisp whitish flesh' >>> apple = wn.synsets(apple)[0] >>> 

Below we see that the apple does not contain direct fruit hyponyms:

 >>> fruit.hyponyms() [Synset('accessory_fruit.n.01'), Synset('achene.n.01'), Synset('acorn.n.01'), Synset('aggregate_fruit.n.01'), Synset('berry.n.02'), Synset('buckthorn_berry.n.01'), Synset('buffalo_nut.n.01'), Synset('chokecherry.n.01'), Synset('cubeb.n.01'), Synset('drupe.n.01'), Synset('ear.n.05'), Synset('edible_fruit.n.01'), Synset('fruitlet.n.01'), Synset('gourd.n.02'), Synset('hagberry.n.01'), Synset('hip.n.05'), Synset('juniper_berry.n.01'), Synset('marasca.n.01'), Synset('may_apple.n.01'), Synset('olive.n.01'), Synset('pod.n.02'), Synset('pome.n.01'), Synset('prairie_gourd.n.01'), Synset('pyxidium.n.01'), Synset('quandong.n.02'), Synset('rowanberry.n.01'), Synset('schizocarp.n.01'), Synset('seed.n.01'), Synset('wild_cherry.n.01')] >>> >>> apple in fruit.hyponyms() False 

So, we must iterate all the hyponyms and see if the apple is in one of them:

 >>> hypofruits = set([i for i in fruit.closure(lambda s:s.hyponyms())]) >>> apple in hypofruits True 

Here it is! For completeness:

 >>> hyperapple = set([i for i in apple.closure(lambda s:s.hypernyms())]) >>> fruit in hyperapple True >>> hypoapple = set([i for i in apple.closure(lambda s:s.hyponyms())]) >>> fruit in hypoapple False >>> hyperfruit = set([i for i in fruit.closure(lambda s:s.hypernyms())]) >>> apple in hyperfruit False 
+14
source

Source: https://habr.com/ru/post/976315/


All Articles