{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# create wordcloud with chinese\n\nWordcloud is a very good tool, but if you want to create\nChinese wordcloud only wordcloud is not enough. The file\nshows how to use wordcloud with Chinese. First, you need a\nChinese word segmentation library jieba, jieba is now the\nmost elegant the most popular Chinese word segmentation tool in python.\nYou can use 'PIP install jieba'. To install it. As you can see,\nat the same time using wordcloud with jieba very convenient\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import jieba\njieba.enable_parallel(4)\n# Setting up parallel processes :4 ,but unable to run on Windows\nfrom os import path\nfrom imageio import imread\nimport matplotlib.pyplot as plt\nimport os\n# jieba.load_userdict(\"txt\\userdict.txt\")\n# add userdict by load_userdict()\nfrom wordcloud import WordCloud, ImageColorGenerator\n\n# get data directory (using getcwd() is needed to support running example in generated IPython notebook)\nd = path.dirname(__file__) if \"__file__\" in locals() else os.getcwd()\n\nstopwords_path = d + '/wc_cn/stopwords_cn_en.txt'\n# Chinese fonts must be set\nfont_path = d + '/fonts/SourceHanSerif/SourceHanSerifK-Light.otf'\n\n# the path to save worldcloud\nimgname1 = d + '/wc_cn/LuXun.jpg'\nimgname2 = d + '/wc_cn/LuXun_colored.jpg'\n# read the mask / color image taken from\nback_coloring = imread(path.join(d, d + '/wc_cn/LuXun_color.jpg'))\n\n# Read the whole text.\ntext = open(path.join(d, d + '/wc_cn/CalltoArms.txt')).read()\n\n# if you want use wordCloud,you need it\n# add userdict by add_word()\nuserdict_list = ['\u963f\uff31', '\u5b54\u4e59\u5df1', '\u5355\u56db\u5ac2\u5b50']\n\n\n# The function for processing text with Jieba\ndef jieba_processing_txt(text):\n    for word in userdict_list:\n        jieba.add_word(word)\n\n    mywordlist = []\n    seg_list = jieba.cut(text, cut_all=False)\n    liststr = \"/ \".join(seg_list)\n\n    with open(stopwords_path, encoding='utf-8') as f_stop:\n        f_stop_text = f_stop.read()\n        f_stop_seg_list = f_stop_text.splitlines()\n\n    for myword in liststr.split('/'):\n        if not (myword.strip() in f_stop_seg_list) and len(myword.strip()) > 1:\n            mywordlist.append(myword)\n    return ' '.join(mywordlist)\n\n\nwc = WordCloud(font_path=font_path, background_color=\"white\", max_words=2000, mask=back_coloring,\n               max_font_size=100, random_state=42, width=1000, height=860, margin=2,)\n\n\nwc.generate(jieba_processing_txt(text))\n\n# create coloring from image\nimage_colors_default = ImageColorGenerator(back_coloring)\n\nplt.figure()\n# recolor wordcloud and show\nplt.imshow(wc, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.show()\n\n# save wordcloud\nwc.to_file(path.join(d, imgname1))\n\n# create coloring from image\nimage_colors_byImg = ImageColorGenerator(back_coloring)\n\n# show\n# we could also give color_func=image_colors directly in the constructor\nplt.imshow(wc.recolor(color_func=image_colors_byImg), interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.figure()\nplt.imshow(back_coloring, interpolation=\"bilinear\")\nplt.axis(\"off\")\nplt.show()\n\n# save wordcloud\nwc.to_file(path.join(d, imgname2))"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.13.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}