猫史档案馆


【Python作品分享】少年,诗落谁家,敢来挑战吗?!?【作业帖】

用户:WMoSo岛同号WMoSo岛同号查看:0 回复:1 评论:0 创建时间:2021-07-14T11:57:55


【作品展示】

center_image

 

【作品介绍】

输入一句苏轼或陆游的诗,系统会自动将可能性绘制成饼图。(还会把结巴分词后的结果打印出来,需要安装库:jieba/pyecharts/nltk)

 

【作品源代码】

import jieba
from pyecharts import Pie
import webbrowser
import os
from nltk.classify import NaiveBayesClassifier
with open('sushi.txt','r',encoding='utf-8-sig') as txt:
    text1=txt.read()
    list1=jieba.lcut(text1)
result1=' '.join(list1)

with open('luyou.txt', 'r', encoding='utf-8-sig') as txt:
    text2 = txt.read()
    list2 = jieba.lcut(text2)
result2 = ' '.join(list2)

def word_feeds(words):
    return dict([(word,True)for word in words])

ss_feedures=[(word_feeds(ss),'ss')for ss in result1]
ly_feedures = [(word_feeds(ly), 'ly')for ly in result2]
train_set=ss_feedures+ly_feedures

classifier=NaiveBayesClassifier.train(train_set)
print(classifier.classify({'竹':True}))
print('少年,诗落谁家,敢来挑战吗?!?')
print('你输一句苏轼&陆游的诗,给你猜作者。')
print('-----------------------------------------------------------------------------------')
sentence=input('请输入一句苏轼&陆游の诗(瞎扯)')
words=jieba.lcut(sentence)
print(words)
ss=0
ly=0
for word in words:
    print(word)
    classResult=classifier.classify(word_feeds(word))
    if classResult=='ss':
        ss+=1
    if classResult=='ly':
        ly+=1
su=ss/len(words)
lu=ly/len(words)
print('苏轼的可能性:',su)
print('陆游的可能性:', lu)
attr=['苏轼','陆游']
v1=[]
v1.append(su)
v1.append(lu)
pie = Pie('少年,诗落谁家,敢来挑战吗?!?')
pie.add(
    '',
    attr,v1,
    radius=[34,55],
    is_random=True
)
pie.render()
webbrowser.open('file://'+os.path.realpath('render.html'))

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
ZNS言语xyZNS言语xy

作业帖还要发?

点赞0


评论