[转载] nltk之查找同义词、反义词、指代关系

参考链接: 如何在Python中从NLTK WordNet获取同义词/反义词

文章目录

 使用nltk中的wordnet语料库找出以下单词的同义词集、查看同义词集中的所有单词、查看同义词的具体定义及例子:dog, apple, fly查看以下单词对的语义相似度:good, beautiful;good, bad; dog, cat找出以下单词的蕴含(entailments)关系和反义词:walk, supply, hot

  判断下列句子中是否存在的共指指代,有的话找出共指链

 

 

使用nltk中的wordnet语料库 

找出以下单词的同义词集、查看同义词集中的所有单词、查看同义词的具体定义及例子:dog, apple, fly 

from nltk.corpus import wordnet as wn

dog_set = wn.synsets('dog')

print('dog的同义词集为:', dog_set)

print('dog的各同义词集包含的单词有:',[dog.lemma_names() for dog in dog_set])

print('dog的各同义词集的具体定义是:',[dog.definition() for dog in dog_set])

print('dog的各同义词集的例子是:',[dog.examples() for dog in dog_set])

 

dog的同义词集为: [Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

dog的各同义词集包含的单词有: [['dog', 'domestic_dog', 'Canis_familiaris'], ['frump', 'dog'], ['dog'], ['cad', 'bounder', 'blackguard', 'dog', 'hound', 'heel'], ['frank', 'frankfurter', 'hotdog', 'hot_dog', 'dog', 'wiener', 'wienerwurst', 'weenie'], ['pawl', 'detent', 'click', 'dog'], ['andiron', 'firedog', 'dog', 'dog-iron'], ['chase', 'chase_after', 'trail', 'tail', 'tag', 'give_chase', 'dog', 'go_after', 'track']]

dog的各同义词集的具体定义是: ['a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds', 'a dull unattractive unpleasant girl or woman', 'informal term for a man', 'someone who is morally reprehensible', 'a smooth-textured sausage of minced beef or pork usually smoked; often served on a bread roll', 'a hinged catch that fits into a notch of a ratchet to move a wheel forward or prevent it from moving backward', 'metal supports for logs in a fireplace', 'go after with the intent to catch']

dog的各同义词集的例子是: [['the dog barked all night'], ['she got a reputation as a frump', "she's a real dog"], ['you lucky dog'], ['you dirty dog'], [], [], ['the andirons were too hot to touch'], ['The policeman chased the mugger down the alley', 'the dog chased the rabbit']]

 

apple_set = wn.synsets('apple')

print('apple的同义词集为:', apple_set)

print('apple的各同义词集包含的单词有:',[apple.lemma_names() for apple in apple_set])

print('apple的各同义词集的具体定义是:',[apple.definition() for apple in apple_set])

print('apple的各同义词集的例子是:',[apple.examples() for apple in apple_set])

 

apple的同义词集为: [Synset('apple.n.01'), Synset('apple.n.02')]

apple的各同义词集包含的单词有: [['apple'], ['apple', 'orchard_apple_tree', 'Malus_pumila']]

apple的各同义词集的具体定义是: ['fruit with red or yellow or green skin and sweet to tart crisp whitish flesh', 'native Eurasian tree widely cultivated in many varieties for its firm rounded edible fruits']

apple的各同义词集的例子是: [[], []]

 

fly_set = wn.synsets('fly')

print('fly的同义词集为:', fly_set)

print('fly的各同义词集包含的单词有:',[fly.lemma_names() for fly in fly_set])

print('fly的各同义词集的具体定义是:',[fly.definition() for fly in fly_set])

print('fly的各同义词集的例子是:',[fly.examples() for fly in fly_set])

 

fly的同义词集为: [Synset('fly.n.01'), Synset('tent-fly.n.01'), Synset('fly.n.03'), Synset('fly.n.04'), Synset('fly.n.05'), Synset('fly.v.01'), Synset('fly.v.02'), Synset('fly.v.03'), Synset('fly.v.04'), Synset('fly.v.05'), Synset('fly.v.06'), Synset('fly.v.07'), Synset('fly.v.08'), Synset('fly.v.09'), Synset('fly.v.10'), Synset('flee.v.01'), Synset('fly.v.12'), Synset('fly.v.13'), Synset('vanish.v.05'), Synset('fly.s.01')]

fly的各同义词集包含的单词有: [['fly'], ['tent-fly', 'rainfly', 'fly_sheet', 'fly', 'tent_flap'], ['fly', 'fly_front'], ['fly', 'fly_ball'], ['fly'], ['fly', 'wing'], ['fly'], ['fly', 'aviate', 'pilot'], ['fly'], ['fly'], ['fly'], ['fly'], ['fly', 'fell', 'vanish'], ['fly'], ['fly'], ['flee', 'fly', 'take_flight'], ['fly'], ['fly'], ['vanish', 'fly', 'vaporize'], ['fly']]

fly的各同义词集的具体定义是: ['two-winged insects characterized by active flight', 'flap consisting of a piece of canvas that can be drawn back to provide entrance to a tent', 'an opening in a garment that is closed by a zipper or by buttons concealed under a fold of cloth', '(baseball) a hit that flies up in the air', "fisherman's lure consisting of a fishhook decorated to look like an insect", 'travel through the air; be airborne', 'move quickly or suddenly', 'operate an airplane', 'transport by aeroplane', 'cause to fly or float', 'be dispersed or disseminated', 'change quickly from one emotional state to another', 'pass away rapidly', 'travel in an airplane', 'display in the air or cause to float', 'run away quickly', 'travel over (an area of land or sea) in an aircraft', 'hit a fly', 'decrease rapidly and disappear', '(British informal) not to be deceived or hoodwinked']

fly的各同义词集的例子是: [[], [], [], [], [], ['Man cannot fly'], ['He flew about the place'], ['The pilot flew to Cuba'], ['We fly flowers from the Caribbean to North America'], ['fly a kite'], ['Rumors and accusations are flying'], ['fly into a rage'], ['Time flies like an arrow', 'Time fleeing beneath him'], ['she is flying to Cincinnati tonight', 'Are we driving or flying?'], ['fly a kite', 'All nations fly their flags in front of the U.N.'], ['He threw down his gun and fled'], ['Lindbergh was the first to fly the Atlantic'], [], ['the money vanished in las Vegas', 'all my stock assets have vaporized'], []]

 

查看以下单词对的语义相似度:good, beautiful;good, bad; dog, cat 

goods = wn.synsets('good')

beautifuls = wn.synsets('beautiful')

bads = wn.synsets('bad')

dogs = wn.synsets('dog')

cats = wn.synsets('cat')

print('good和beautiful的语义相似度为: ', max([0 if good.path_similarity(beautiful) == None else good.path_similarity(beautiful) for good in goods for beautiful in beautifuls]))

print('good和bad的语义相似度为: ', max([0 if good.path_similarity(bad) == None else good.path_similarity(bad) for good in goods for bad in bads]))

print('dog和cat的语义相似度为: ', max([0 if dog.path_similarity(cat) == None else dog.path_similarity(cat) for dog in dogs for cat in cats]))

 

good和beautiful的语义相似度为:  0

good和bad的语义相似度为:  0.3333333333333333

dog和cat的语义相似度为:  0.2

 

找出以下单词的蕴含(entailments)关系和反义词:walk, supply, hot 

walks = wn.synsets('walk')

supplys = wn.synsets('supply')

hots = wn.synsets('hot')

print('walk的蕴含关系包括: ', [walk.entailments() for walk in walks if len(walk.entailments())])

print('supply的蕴含关系包括: ', [supply.entailments() for supply in supplys if len(supply.entailments())])

print('hot的蕴含关系包括: ', [hot.entailments() for hot in hots if len(hot.entailments())])

 

walk的蕴含关系包括:  [[Synset('step.v.01')]]

supply的蕴含关系包括:  []

hot的蕴含关系包括:  []

 

print('walk的反义词包括: ', [walk_lemma.antonyms() for walk_lemmas in [walk.lemmas() for walk in walks] for walk_lemma in walk_lemmas if walk_lemma.antonyms()!=[]])

print('supply的反义词包括: ', [supply_lemma.antonyms() for supply_lemmas in [supply.lemmas() for supply in supplys] for supply_lemma in supply_lemmas if supply_lemma.antonyms()!=[]])

print('hot的反义词包括: ', [hot_lemma.antonyms() for hot_lemmas in [hot.lemmas() for hot in hots] for hot_lemma in hot_lemmas if hot_lemma.antonyms()!=[]])

 

walk的反义词包括:  [[Lemma('ride.v.02.ride')]]

supply的反义词包括:  [[Lemma('demand.n.02.demand')], [Lemma('recall.v.06.recall')]]

hot的反义词包括:  [[Lemma('cold.a.01.cold')], [Lemma('cold.a.02.cold')]]

 

判断下列句子中是否存在的共指指代,有的话找出共指链 

import spacy

nlp = spacy.load('en_core_web_sm')

 

# Add neural coref to SpaCy's pipe

import neuralcoref

neuralcoref.add_to_pipe(nlp)

 

<spacy.lang.en.English at 0x1b772de7550>

 

s1 = nlp(u"My sister has a dog. She loves him.")

s2 = nlp(u"Some like to play football, others are fond of basketball.")

s3 = nlp(u"The more a man knows, the more he feels his ignorance.")

 

if s1._.has_coref:

    print(s1.text+'存在共指指代,共指链为: ' ,s1._.coref_clusters)

else:

    print(s1.text+'不存在共指指代')

 

My sister has a dog. She loves him.存在共指指代,共指链为:  [My sister: [My sister, She], a dog: [a dog, him]]

 

if s2._.has_coref:

    print(s2.text+'存在共指指代,共指链为: ' ,s2._.coref_clusters)

else:

    print(s2.text+'不存在共指指代')

 

Some like to play football, others are fond of basketball.不存在共指指代

 

if s3._.has_coref:

    print(s3.text+'存在共指指代,共指链为: ' ,s3._.coref_clusters)

else:

    print(s3.text+'不存在共指指代')

 

The more a man knows, the more he feels his ignorance.存在共指指代,共指链为:  [The: [The, the], a man: [a man, he, his]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值