{"id":5168,"date":"2022-11-28T15:31:03","date_gmt":"2022-11-28T07:31:03","guid":{"rendered":"https:\/\/seitlab.com\/?p=5168"},"modified":"2022-11-28T15:32:19","modified_gmt":"2022-11-28T07:32:19","slug":"5-10-%e8%ae%a1%e9%87%8f%e5%88%86%e6%9e%9010%ef%bc%9a%e6%96%87%e6%9c%ac%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.seitlab.com\/?p=5168","title":{"rendered":"5.10 \u8ba1\u91cf\u5206\u679010\uff1a\u6587\u672c\u5206\u6790"},"content":{"rendered":"<h3>\u4f8b\u5b50<\/h3>\n<p>\u5bf9\u53e4\u5178\u6587\u5b66\u540d\u8457\u300a\u7ea2\u697c\u68a6\u300b\u8fdb\u884c\u6587\u672c\u5206\u6790\uff0c\u63d0\u53d6\u5176\u4e2d\u9891\u6570\u5927\u4e8e500\u7684\u8bcd\u8bed\uff0c\u7ed8\u5236\u8bcd\u4e91\u56fe\u8fdb\u884c\u5c55\u793a\u3002<\/p>\n<p><strong>\u6587\u4ef6\u4e0b\u8f7d<\/strong>\uff1a<a href=\"https:\/\/cdn.seitlab.com\/zip\/text.zip\">\u7ea2\u697c\u68a6\u6587\u672c\u5206\u6790\u9644\u4ef6<\/a><\/p>\n<p><strong>\u9644\u4ef6\u5185\u5bb9<\/strong><\/p>\n<pre><code>background.jpg                  # \u81ea\u5b9a\u4e49\u7684\u8bcd\u4e91\u56fe\u80cc\u666f\nDream_of_the_Red_Mansion.txt    # \u300a\u7ea2\u697c\u68a6\u300b\u539f\u6587\nRed_Mansion_Dictionary.txt      # \u81ea\u5b9a\u4e49\u7684\u7ea2\u697c\u68a6\u8bcd\u5178\uff0c\u7528\u4e8e\u63d0\u9ad8\u6587\u672c\u5206\u6790\u7cbe\u786e\u5ea6\nSimHei.ttf                      # \u4e2d\u6587\u5b57\u4f53\u6587\u4ef6\uff0c\u7528\u4e8e\u7ed8\u56fe\u65f6\u663e\u793a\u4e2d\u6587\nstop_words.txt                  # \u505c\u7528\u8bcd\u8868\uff0c\u7528\u4e8e\u5254\u9664\u6587\u672c\u5206\u6790\u4e2d\u7684\u751f\u50fb\u8bcd\u548c\u975e\u8bcd\u8bed\u5b57\u7b26\n<\/code><\/pre>\n<p><strong>\u9644\u4ef6\u4f7f\u7528\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li>\n<p>\u4e0b\u8f7d\u4e0a\u8ff0\u9644\u4ef6<\/p>\n<\/li>\n<li>\n<p>\u89e3\u538b\u9644\u4ef6\uff0c\u786e\u4fdd\u89e3\u538b\u540e\u7684\u9644\u4ef6\u6587\u4ef6\u5939\u201ctext\u201d\u548c\u4ee3\u7801\u5904\u4e8e\u540c\u4e00\u76ee\u5f55\u4e0b<\/p>\n<\/li>\n<li>\n<p>\u8fd0\u884c\u4ee3\u7801(\u5982\u679c\u4ee3\u7801\u8fd0\u884c\u6709\u8bef\uff0c\u8bf7\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u8def\u5f84\u8bbe\u7f6e\u90e8\u5206)\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u4ee3\u7801\u90e8\u5206<\/h3>\n<pre><code># lec10\n# \u8ba1\u91cf\u5b9e\u9a8c\u4e13\u9898\uff1a\u6587\u672c\u5206\u6790-\u4ee3\u7801\u90e8\u5206\n# \u7ea2\u697c\u68a6\u7684\u6587\u672c\u5206\u6790\n\nimport pandas as pd\nimport matplotlib.pyplot as plt  # \u6570\u636e\u53ef\u89c6\u5316\nimport jieba  # \u8bcd\u8bed\u5207\u5272\nfrom wordcloud import WordCloud, ImageColorGenerator  # \u8bcd\u4e91\uff0c\u989c\u8272\u751f\u6210\u5668\uff0c\u505c\u6b62\u8bcd\nimport numpy as np  # \u79d1\u5b66\u8ba1\u7b97\nfrom PIL import Image  # \u5904\u7406\u56fe\u7247\n\n# \u8def\u5f84\u8bbe\u7f6e\npath_content = &quot;.\/text\/Dream_of_the_Red_Mansion.txt&quot;    # \u5f85\u5206\u6790\u6587\u672c\u8def\u5f84\npath_stopword = &quot;.\/text\/stop_words.txt&quot;                 # \u505c\u7528\u8bcd\u8def\u5f84\npath_background_image = &quot;.\/text\/background.jpg&quot;         # \u80cc\u666f\u56fe\u8def\u5f84\npath_font = &quot;.\/text\/SimHei.ttf&quot;                         # \u8bcd\u4e91\u5b57\u4f53\u8def\u5f84\npath_wordcloud_image = &quot;.\/text\/wordcloud.jpg&quot;           # \u8bcd\u4e91\u56fe\u5b58\u50a8\u8def\u5f84\n\n# \u5bfc\u5165\u7528\u6237\u8bcd\u5178\uff08\u53ef\u9009\uff09\njieba.load_userdict(&quot;.\/text\/Red_Mansion_Dictionary.txt&quot;)\n\n# \u505c\u7528\u8bcd\u5217\u8868\nfile = open(path_stopword, &#039;r&#039;, encoding=&#039;utf-8&#039;)   # \u8bfb\u53d6\u505c\u7528\u8bcd\u6587\u4ef6\nstopword = file.readlines()                         # \u8bfb\u53d6\u6240\u6709\u884c\nlist_stopword = []                                  # \u5b9a\u4e49\u7a7a\u5217\u8868\nfor word in stopword:\n    word = word.strip(&#039;\\n&#039;)                         # \u5220\u9664\u505c\u7528\u8bcd\u5c3e\u90e8\u7684\u6362\u884c\u7b26&#039;\\n&#039;\n    list_stopword.append(word)                      # \u7ed3\u679c\u5b58\u5165\u5217\u8868\n\n# \u5206\u8bcd\nfile = open(path_content, &#039;r&#039;, encoding=&#039;utf-8&#039;)     # \u8bfb\u53d6\u5f85\u5206\u6790\u6587\u672c\ntextfile = file.read()                               # \u8bfb\u53d6\u6587\u672c\u5185\u5bb9\ncut_list = jieba.cut(textfile)                       # \u5207\u5272\u8bcd\u8bed\nwordlist = []                                        # \u5b9a\u4e49\u7a7a\u5217\u8868\nfor word in cut_list:\n    if len(word) &gt; 1:                                # \u7b5b\u90092\u5b57\u4ee5\u4e0a\u7684\u8bcd\u8bed\n        wordlist.append(word)                        # \u7b5b\u9009\u7ed3\u679c\u5b58\u5165\u5217\u8868\ntext_str = &#039; &#039;.join(wordlist)                        # \u7528\u7a7a\u683c\u5c06\u8bcd\u8bed\u8fde\u63a5\u4e3a\u5b57\u7b26\u4e32\n\n# \u8bcd\u4e91\u56fe\n# \u5bfc\u5165\u8bcd\u4e91\u56fe\u80cc\u666f\u56fe\u7247\nbackground = np.array(Image.open(path_background_image))\n# \u751f\u6210\u8bcd\u4e91\u56fe\nwc = WordCloud(width=1400, height=2200,\n               background_color=&#039;white&#039;,\n               mode=&#039;RGB&#039;,\n               mask=background,            # \u6dfb\u52a0\u80cc\u666f\n               max_words=500,              # \u6700\u5927\u8bcd\u6570\n               stopwords=list_stopword,    # \u5c4f\u853d\u8bcd\n               font_path=path_font,        # \u8bcd\u4e91\u56fe\u5b57\u4f53\n               max_font_size=150,\n               relative_scaling=0.5,       # \u8bbe\u7f6e\u5b57\u4f53\u5927\u5c0f\u4e0e\u8bcd\u9891\u7684\u5173\u8054\u7a0b\u5ea6\u4e3a0.5\n               random_state=50,\n               scale=2\n               ).generate(text_str)\n# \u81ea\u5b9a\u4e49\u8bcd\u4e91\u56fe\u989c\u8272\uff0c\u989c\u8272\u548c\u5f62\u72b6\u6765\u81ea\u80cc\u666f\uff08\u53ef\u9009\uff09\nimage_color = ImageColorGenerator(background)\nwc.recolor(color_func=image_color)\n# \u663e\u793a\u8bcd\u4e91\nplt.imshow(wc)                       # \u663e\u793a\u8bcd\u4e91\nplt.axis(&#039;off&#039;)                      # \u5173\u95edx,y\u8f74\nplt.show()                           # \u663e\u793a\n\n# \u4fdd\u5b58\u8bcd\u4e91\u56fe\u5230\u672c\u5730\nwc.to_file(path_wordcloud_image)\n\n# \u7edf\u8ba1\u8bcd\u9891\ndf = pd.DataFrame()\ndf[&quot;wordlist&quot;] = wordlist\n\ndf01 = pd.DataFrame(columns=[&quot;word&quot;, &quot;frequency&quot;])\ndf01[&quot;frequency&quot;] = df.groupby(&quot;wordlist&quot;)[&quot;wordlist&quot;].agg(np.size)  # \u5bf9df\u6309\u201cwordlist\u201d\u5206\u7ec4\uff0c\u5e76\u8ba1\u7b97\u9891\u6570\uff0c\u8d4b\u503c\u7ed9df01\ndf01[&quot;word&quot;] = df01.index\ndf01 = df01.reset_index(drop=True)                                # \u91cd\u7f6e\u7d22\u5f15\uff0c\u5220\u9664\u539f\u6765\u7d22\u5f15\ndf01 = df01.sort_values(by=&quot;frequency&quot;)                           # \u6309\u201cfrequency\u201d\uff08\u5347\u5e8f\uff09\u6392\u5e8f\ndf01 = df01[df01[&quot;frequency&quot;] &gt; 500]                              # \u7b5b\u9009\u51fa\u201cfrequency\u201d\u5927\u4e8e500\u7684\u8bcd\u8bed\u548c\u9891\u6570\ndf01 = df01.reset_index(drop=True)                                # \u91cd\u7f6e\u7d22\u5f15\uff0c\u5220\u9664\u539f\u6765\u7d22\u5f15\nprint(df01)\n\n# \u7ed8\u5236\u8bcd\u9891\u56fe\nplt.rcParams[&#039;font.sans-serif&#039;] = [&#039;SimHei&#039;]\nplt.rcParams[&#039;axes.unicode_minus&#039;] = False\ny = df01[&quot;word&quot;]\nx = df01[&quot;frequency&quot;]\nplt.figure(figsize=(8, 10))\nplt.barh(y, x)\nplt.title(&quot;\u7ea2\u697c\u68a6\u8bcd\u9891\u5206\u6790&quot;, fontsize=&quot;xx-large&quot;, fontweight=&quot;bold&quot;)\nplt.ylabel(&quot;\u5173\u952e\u8bcd&quot;, fontsize=&quot;large&quot;, fontweight=&quot;bold&quot;)\nplt.xlabel(&quot;\u9891\u6570&quot;, fontsize=&quot;large&quot;, fontweight=&quot;bold&quot;)\nplt.show()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f8b\u5b50 \u5bf9\u53e4\u5178\u6587\u5b66\u540d\u8457\u300a\u7ea2\u697c\u68a6\u300b\u8fdb\u884c\u6587\u672c\u5206\u6790\uff0c\u63d0\u53d6\u5176\u4e2d\u9891\u6570\u5927\u4e8e500\u7684\u8bcd\u8bed\uff0c\u7ed8\u5236\u8bcd\u4e91\u56fe\u8fdb\u884c\u5c55\u793a\u3002 \u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[29,30],"tags":[],"class_list":["post-5168","post","type-post","status-publish","format-standard","hentry","category-29","category-30"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/5168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5168"}],"version-history":[{"count":0,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=\/wp\/v2\/posts\/5168\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.seitlab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}