移动平台fifa15吧 关注:74,489贴子:6,070,021
  • 7回复贴,共1
import jieba
excludes = {"来到","人马","领兵","将军","却说","荆州","二人","不可","不能","如此"}
txt = open("threekingdom.txt", "rb").read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
elif word == "诸葛亮" or word == "孔明曰":
rword = "孔明"
elif word == "关公" or word == "云长":
rword = "关羽"
elif word == "玄德" or word == "玄德曰":
rword = "刘备"
elif word == "孟德" or word == "丞相":
rword = "曹操"
else:
rword = word
counts[rword] = counts.get(rword,0) + 1
for word in excludes:
del(counts[word])
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(55):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))


IP属地:安徽来自Android客户端1楼2021-12-17 14:56回复
    import jieba
    import wordcloud
    f = open("threekingdom.txt","rb")
    t = f.read()
    f.close()
    ls = jieba.lcut(t)
    txt = " ".join(ls)
    w = wordcloud.WordCloud( font_path = "NotoSerifCJK-Bold.ttc",
    width = 1000,height = 700,background_color = "white",
    )
    w.generate(txt)
    w.to_file("gr.png")


    IP属地:安徽来自Android客户端2楼2021-12-17 14:56
    回复
      # -*- coding: utf-8 -*-
      """
      Created on Mon Apr 6 22:45:36 2020
      @author: 49594
      """
      # coding:utf-8
      from wordcloud import WordCloud
      import 网页链接 as plt
      import jieba
      # 生成词云
      def create_word_cloud(filename):
      text = open("{}.txt".format(filename), 'rb' ).read()
      #结巴分词
      wordlist = jieba.cut(text, cut_all=True) # 结巴分词
      wl = " ".join(wordlist)
      # 设置词云
      wc = WordCloud(
      # 设置背景颜色
      background_color="black",
      # 设置最大显示的词云数
      max_words=2000,
      # 这种字体都在电脑字体中,一般路径
      font_path='simsun.ttf',
      height=1200,
      width=1600,
      # 设置字体最大值
      max_font_size=100,
      # 设置有多少种随机生成状态,即有多少种配色方案
      random_state=100,
      )
      myword = wc.generate(wl) # 生成词云
      # 展示词云图
      plt.imshow(myword)
      plt.axis("off")
      plt.show()
      wc.to_file('py_book.png') # 把词云保存下
      if __name__ == '__main__':
      create_word_cloud('三国演义')


      IP属地:安徽来自Android客户端3楼2021-12-17 15:15
      回复
        # -*- coding: utf-8 -*-
        """
        Created on Mon Apr 6 22:45:36 2020
        @author: 49594
        """
        # coding:utf-8
        from wordcloud import WordCloud
        import 网页链接 as plt
        import jieba
        # 生成词云
        def create_word_cloud(filename):
        text = open("{}.txt".format(filename), 'rb' ).read()
        #结巴分词
        wordlist = jieba.cut(text, cut_all=True) # 结巴分词
        wl = " ".join(wordlist)
        # 设置词云
        wc = WordCloud(
        # 设置背景颜色
        background_color="black",
        # 设置最大显示的词云数
        max_words=2000,
        # 这种字体都在电脑字体中,一般路径
        font_path='simsun.ttf',
        height=1200,
        width=1600,
        # 设置字体最大值
        max_font_size=100,
        # 设置有多少种随机生成状态,即有多少种配色方案
        random_state=100,
        )
        myword = wc.generate(wl) # 生成词云
        # 展示词云图
        plt.imshow(myword)
        plt.axis("off")
        plt.show()
        wc.to_file('py_book.png') # 把词云保存下
        if __name__ == '__main__':
        create_word_cloud('三国演义')


        IP属地:安徽来自Android客户端4楼2021-12-17 15:15
        回复


          IP属地:安徽来自Android客户端6楼2021-12-17 15:26
          回复
            "来到","人马","主公","以为","下文","追赶","一声","分解","分付","粮草","大怒","不得","未知","军中","可以","心中","进兵","引军","大惊","之后","不见","起兵","接应","大败","一军","大事","百姓","因此","一面","然后","原来","令人","忽然","下马","喊声","成都","江东","徐州","正是","先锋","夫人","不如","赶来","何不","忽报","大军","先生","何故","军士","先主","此人","太守","天子","后人","背后","城中","汉中","夏侯","大叫","上马","众将","只见","蜀兵","商议","左右","军马","天下","东吴","魏兵","都督","陛下","领兵","不敢","将军","不知","一人","却说","于是","今日","荆州","次日","大喜","引兵","二人","不可","不能","如何","如此"


            IP属地:安徽7楼2021-12-17 15:59
            回复
              呼叫护身


              IP属地:安徽8楼2021-12-17 16:02
              回复
                "来到","人马","主公","以为","下文","追赶","一声","分解","分付","粮草","大怒","不得","未知","军中","可以","心中","进兵","引军","大惊","之后","不见","起兵","接应","大败","一军","大事","百姓","因此","一面","然后","原来","令人","忽然","下马","喊声","成都","江东","徐州","正是","先锋","夫人","不如","赶来","何不","忽报","大军","先生","何故","军士","先主","此人","太守","天子","后人","背后","城中","汉中","夏侯","大叫","上马","众将","只见","蜀兵","商议","左右","军马","天下","东吴","魏兵","都督","陛下","领兵","不敢","将军","不知","一人","却说","于是","今日","荆州","次日","大喜","引兵","二人","不可","不能","如何","如此"


                IP属地:安徽9楼2021-12-17 16:02
                回复