wordnet作业

由于博客内容为空,无法提供包含关键信息的摘要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值