Thứ nhất, có một sự khác biệt giữa word
và synset
/concept
trong WordNet.
Ở đây chúng ta thấy rằng một từ có thể có nhiều ý nghĩa (tức là các liên kết đến nhiều khái niệm):
>>> 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')]
Và trong trường hợp này 'ô tô' và 'xe' có thể tham khảo với cùng Synset('car.n.01')
và nếu như vậy, sau đó họ không có mối quan hệ hypo/hypernym.
Ngoài ra còn có khái niệm về lemma
sẽ chỉ làm phức tạp mọi thứ, vì vậy, chúng tôi sẽ bỏ qua điều đó ngay bây giờ.
Giả sử bạn không so sánh các từ nhưng các tập hợp, sau đó bạn có thể tìm thấy tất cả các từ đồng nghĩa của đồng bộ và xem liệu tập hợp đồng bộ khác có xảy ra bên trong nó hay không.
Nếu bạn đang so sánh chữ rõ ràng, xem How to get all the hyponyms of a word/synset in python nltk and wordnet?
dưới đây sẽ hiển thị như thế nào để so sánh synsets. Ví dụ sake, tôi sẽ sử dụng 'quả' và 'táo' mà là nhiều hơn logic 'ô tô' và 'xe' vì chỉ có một danh từ synset cho 'ô tô' và 'xe'
>>> 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]
>>>
Dưới , chúng ta thấy rằng táo không có trong hyponyms trực tiếp kết quả của:
>>> 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
Vì vậy, chúng ta phải lặp lại tất cả các hyponyms và xem táo là một trong số họ:
>>> hypofruits = set([i for i in fruit.closure(lambda s:s.hyponyms())])
>>> apple in hypofruits
True
ở đó bạn có nó! Vì lợi ích của sự hoàn chỉnh:
>>> 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
Bạn chưa cung cấp bất kỳ dữ liệu RDF, hoặc liên kết đến bất kỳ dữ liệu RDF, và SPARQL là một ngôn ngữ truy vấn RDF, vì vậy chúng tôi có thể không thực sự đề nghị bất cứ điều gì về bất cứ thắc mắc SPARQL. Có dữ liệu RDF ở đâu đó mà bạn quan tâm không? –