from nltk.corpus import wordnet as wn
import re
f = open("ans1.txt","w")
print('(1)找出以下单词的同义词集、查看同义词集中的所有单词、查看 同义词的具体定义及例子:dog, apple, fly',file=f)
print('',file=f)
target = ['dog','apple','fly']
for word in target:
print('*******', file=f)
b = wn.synsets(word)
print(word+'的同义词集',file=f)
print(b,file=f)
print(' ',file=f)
for i in b:
print(str(i)+'同义词集中所有的单词',file=f)
print(i.lemma_names(),file=f)
print('定义',file=f)
print(i.definition(),file = f)
print('例子',file=f)
print(i.examples(),file = f)
print('---', file=f)
print('',file=f)
print('(2)查看以下单词对的语义相似度:good, beautiful;good, bad; dog, cat',file=f)
print('',file=f)
target2 = [ ['good','beautiful'], ['good','bad'], ['dog','cat']]
for i in target2:
print(i[0]+'与'+i[1]+'的相似度为:',file = f)
a = wn.synsets(i[0])
b = wn.synsets(i[1])
ans = a[0].path_similarity(b[0])
print(ans,file = f)
print('',file=f)
print('(3)找出以下单词的蕴含(entailments)关系和反义词:walk, supply, hot',file=f)
print('',file=f)
target3 = ['walk', 'supply', 'hot']
for word in target3:
b = wn.synsets(word)
print(word + '的蕴含:',file=f)
for i in b:
out = re.search(r'\'+.*\'', str(i)).group()
out = out.strip('\'')
out = out + '.' + word
a = i.entailments()
if(a!=[]):
print(a,file=f)
c = wn.lemma(out).antonyms()
if(c != []):
print(word+'的反义词:',file=f)
print(c,file=f)
print('---',file=f)
print('',file=f)
f.close()
wordnet作业
最新推荐文章于 2022-08-23 19:30:47 发布