{"id":960050,"date":"2024-12-27T03:46:15","date_gmt":"2024-12-26T19:46:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960050.html"},"modified":"2024-12-27T03:46:16","modified_gmt":"2024-12-26T19:46:16","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%a8jieba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960050.html","title":{"rendered":"Python\u4e2d\u5982\u4f55\u7528jieba"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102701\/cf9d4083-87fd-4025-bc4b-4b69931610ca.webp\" alt=\"Python\u4e2d\u5982\u4f55\u7528jieba\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u4f7f\u7528jieba\u5e93\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd<\/strong>\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u4e3b\u8981\u6d89\u53ca<strong>\u5b89\u88c5jieba\u3001\u57fa\u672c\u5206\u8bcd\u529f\u80fd\u3001\u8bcd\u6027\u6807\u6ce8<\/strong>\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u4f7f\u7528\u7ecf\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5JIEBA<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528jieba\u5e93\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u53ef\u4ee5\u901a\u8fc7Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u6765\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install jieba<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728Python\u811a\u672c\u6216\u4ea4\u4e92\u73af\u5883\u4e2d\u5bfc\u5165\u5e76\u4f7f\u7528jieba\u5e93\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u57fa\u672c\u5206\u8bcd\u529f\u80fd<\/h3>\n<\/p>\n<p><p>jieba\u63d0\u4f9b\u4e86\u4e09\u79cd\u5206\u8bcd\u6a21\u5f0f\uff1a<strong>\u7cbe\u786e\u6a21\u5f0f\u3001\u5168\u6a21\u5f0f\u3001\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f<\/strong>\u3002\u6bcf\u79cd\u6a21\u5f0f\u9002\u5408\u4e0d\u540c\u7684\u573a\u666f\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h4>1. \u7cbe\u786e\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u7cbe\u786e\u6a21\u5f0f\u662fjieba\u7684\u9ed8\u8ba4\u5206\u8bcd\u6a21\u5f0f\uff0c\u9002\u5408\u5bf9\u6587\u672c\u8fdb\u884c\u7cbe\u786e\u5206\u6790\uff0c\u8f93\u51fa\u7684\u7ed3\u679c\u662f\u6700\u7b26\u5408\u8bed\u4e49\u7684\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<p>text = &quot;\u6211\u6765\u5230\u5317\u4eac\u6e05\u534e\u5927\u5b66&quot;<\/p>\n<p>words = jieba.cut(text, cut_all=False)<\/p>\n<p>print(&quot;\u7cbe\u786e\u6a21\u5f0f: &quot; + &quot;\/ &quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u7cbe\u786e\u6a21\u5f0f\u4e0b\uff0cjieba\u4f1a\u5c06\u53e5\u5b50\u62c6\u5206\u4e3a\u6700\u5408\u9002\u7684\u8bcd\u8bed\uff0c\u8fd9\u5bf9\u4e8e\u6587\u672c\u5206\u6790\u3001\u4fe1\u606f\u68c0\u7d22\u7b49\u5e94\u7528\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h4>2. \u5168\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u5168\u6a21\u5f0f\u4f1a\u5c06\u53e5\u5b50\u4e2d\u7684\u6240\u6709\u53ef\u80fd\u8bcd\u8bed\u90fd\u5217\u51fa\u6765\uff0c\u56e0\u6b64\u5206\u8bcd\u901f\u5ea6\u5f88\u5feb\uff0c\u4f46\u4f1a\u5b58\u5728\u5197\u4f59\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = jieba.cut(text, cut_all=True)<\/p>\n<p>print(&quot;\u5168\u6a21\u5f0f: &quot; + &quot;\/ &quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5168\u6a21\u5f0f\u9002\u5408\u7528\u4e8e\u641c\u7d22\u5f15\u64ce\u7684\u5206\u8bcd\uff0c\u80fd\u591f\u5c3d\u53ef\u80fd\u591a\u5730\u627e\u5230\u8bcd\u8bed\u3002<\/p>\n<\/p>\n<p><h4>3. \u641c\u7d22\u5f15\u64ce\u6a21\u5f0f<\/h4>\n<\/p>\n<p><p>\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u5728\u7cbe\u786e\u6a21\u5f0f\u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u5bf9\u957f\u8bcd\u8fdb\u884c\u5207\u5206\uff0c\u9002\u5408\u7528\u4e8e\u641c\u7d22\u5f15\u64ce\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">words = jieba.cut_for_search(text)<\/p>\n<p>print(&quot;\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f: &quot; + &quot;\/ &quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u7279\u522b\u9002\u5408\u7528\u4e8e\u9700\u8981\u63d0\u9ad8\u53ec\u56de\u7387\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8bcd\u6027\u6807\u6ce8<\/h3>\n<\/p>\n<p><p>jieba\u9664\u4e86\u63d0\u4f9b\u57fa\u672c\u7684\u5206\u8bcd\u529f\u80fd\u5916\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u5206\u8bcd\u7ed3\u679c\u8fdb\u884c\u8bcd\u6027\u6807\u6ce8\uff0c\u8fd9\u5bf9\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\u6709\u5f88\u5927\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba.posseg as pseg<\/p>\n<p>words = pseg.cut(&quot;\u6211\u7231\u5317\u4eac\u5929\u5b89\u95e8&quot;)<\/p>\n<p>for word, flag in words:<\/p>\n<p>    print(f&#39;{word} {flag}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bcd\u6027\u6807\u6ce8\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6587\u672c\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u8fdb\u800c\u8fdb\u884c\u66f4\u6df1\u5165\u7684\u6587\u672c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7528\u6237\u81ea\u5b9a\u4e49\u8bcd\u5178<\/h3>\n<\/p>\n<p><p>jieba\u5141\u8bb8\u7528\u6237\u901a\u8fc7\u81ea\u5b9a\u4e49\u8bcd\u5178\u6269\u5c55\u5206\u8bcd\u6548\u679c\u3002\u53ef\u4ee5\u901a\u8fc7\u52a0\u8f7d\u7528\u6237\u81ea\u5b9a\u4e49\u8bcd\u5178\u4f7f\u5f97jieba\u5206\u8bcd\u66f4\u52a0\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">jieba.load_userdict(&quot;user_dict.txt&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u9886\u57df\u7279\u5b9a\u7684\u8bcd\u6c47\uff0c\u8fd9\u65f6\u5019\u81ea\u5b9a\u4e49\u8bcd\u5178\u5c31\u663e\u5f97\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8c03\u6574\u8bcd\u9891<\/h3>\n<\/p>\n<p><p>jieba\u901a\u8fc7\u8c03\u6574\u8bcd\u9891\u53ef\u4ee5\u5f71\u54cd\u5206\u8bcd\u7684\u7ed3\u679c\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u67d0\u4e9b\u8bcd\u8bed\u80fd\u591f\u88ab\u4f18\u5148\u8bc6\u522b\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">jieba.suggest_freq((&#39;\u5317\u4eac&#39;, &#39;\u6e05\u534e\u5927\u5b66&#39;), tune=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8c03\u6574\u8bcd\u9891\u53ef\u4ee5\u6709\u6548\u907f\u514d\u67d0\u4e9b\u8bcd\u8bed\u88ab\u9519\u8bef\u5730\u5207\u5206\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3\u4e0e\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>jieba\u5e93\u5728\u4e2d\u6587\u5206\u8bcd\u65b9\u9762\u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u5de5\u5177<\/strong>\uff0c\u9002\u7528\u4e8e\u6587\u672c\u5206\u6790\u3001\u641c\u7d22\u5f15\u64ce\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u591a\u4e2a\u9886\u57df\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u5206\u8bcd\u6a21\u5f0f\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u8bcd\u5178\u548c\u8c03\u6574\u8bcd\u9891\uff0c\u80fd\u591f\u6781\u5927\u63d0\u5347\u5206\u8bcd\u7684\u51c6\u786e\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cjieba\u5e38\u7528\u4e8e\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6587\u672c\u9884\u5904\u7406<\/strong>\uff1a\u5728\u8fdb\u884c\u6587\u672c\u5206\u6790\u4e4b\u524d\uff0c\u5206\u8bcd\u662f\u4e00\u4e2a\u5fc5\u8981\u7684\u6b65\u9aa4\u3002<\/li>\n<li><strong>\u4fe1\u606f\u68c0\u7d22<\/strong>\uff1a\u901a\u8fc7\u5206\u8bcd\u63d0\u9ad8\u641c\u7d22\u7cbe\u5ea6\u548c\u53ec\u56de\u7387\u3002<\/li>\n<li><strong><a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/strong>\uff1a\u4e3a\u6587\u672c\u5206\u7c7b\u3001\u60c5\u611f\u5206\u6790\u7b49\u4efb\u52a1\u63d0\u4f9b\u57fa\u7840\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><p>\u4f7f\u7528jieba\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u5206\u8bcd\u6a21\u5f0f\uff0c\u5e76\u4e0d\u65ad\u8c03\u6574\u548c\u4f18\u5316\u5206\u8bcd\u6548\u679c\uff0c\u4ee5\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\u3002\u5c24\u5176\u662f\u5728\u9700\u8981\u9ad8\u7cbe\u5ea6\u5206\u8bcd\u7684\u5e94\u7528\u4e2d\uff0c\u81ea\u5b9a\u4e49\u8bcd\u5178\u548c\u8bcd\u9891\u8c03\u6574\u5c06\u662f\u5173\u952e\u56e0\u7d20\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5jieba\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528jieba\u5e93\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002\u5728\u7ec8\u7aef\u6216\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a  <\/p>\n<pre><code>pip install jieba\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5373\u53ef\u5728Python\u811a\u672c\u4e2d\u5bfc\u5165jieba\u5e76\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<p><strong>jieba\u5e93\u7684\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>jieba\u5e93\u4e3b\u8981\u7528\u4e8e\u4e2d\u6587\u6587\u672c\u7684\u5206\u8bcd\u3002\u5b83\u5177\u6709\u4e09\u79cd\u5206\u8bcd\u6a21\u5f0f\uff1a\u7cbe\u786e\u6a21\u5f0f\u3001\u5168\u6a21\u5f0f\u548c\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u3002\u7cbe\u786e\u6a21\u5f0f\u8bd5\u56fe\u5c06\u53e5\u5b50\u6700\u7cbe\u786e\u5730\u5207\u5206\u5f00\uff0c\u9002\u5408\u6587\u672c\u5206\u6790\uff1b\u5168\u6a21\u5f0f\u5219\u628a\u53e5\u5b50\u4e2d\u6240\u6709\u7684\u53ef\u80fd\u8bcd\u8bed\u90fd\u5217\u51fa\u6765\uff0c\u9002\u5408\u5bf9\u6bd4\uff1b\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u662f\u5728\u7cbe\u786e\u6a21\u5f0f\u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u4e00\u4e9b\u4f18\u5316\uff0c\u9002\u5408\u7528\u4e8e\u641c\u7d22\u5f15\u64ce\u7684\u5206\u8bcd\u3002<\/p>\n<p><strong>\u5982\u4f55\u81ea\u5b9a\u4e49jieba\u7684\u8bcd\u5178\uff1f<\/strong><br \/>jieba\u652f\u6301\u7528\u6237\u81ea\u5b9a\u4e49\u8bcd\u5178\uff0c\u8fd9\u5bf9\u4e8e\u5904\u7406\u4e00\u4e9b\u4e13\u4e1a\u9886\u57df\u7684\u8bcd\u6c47\u975e\u5e38\u6709\u7528\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528<code>jieba.load_userdict(file_name)<\/code>\u51fd\u6570\u6765\u52a0\u8f7d\u81ea\u5b9a\u4e49\u8bcd\u5178\u3002\u8bcd\u5178\u6587\u4ef6\u5e94\u4e3aUTF-8\u7f16\u7801\uff0c\u6bcf\u884c\u4e00\u4e2a\u8bcd\uff0c\u683c\u5f0f\u4e3a\u201c\u8bcd \u8bcd\u9891 \u8bcd\u6027\u201d\uff0c\u5176\u4e2d\u8bcd\u9891\u548c\u8bcd\u6027\u53ef\u9009\u3002\u5982\u679c\u60f3\u8981\u6dfb\u52a0\u7279\u5b9a\u8bcd\u6c47\uff0c\u53ef\u4f7f\u7528<code>jieba.add_word(word, freq=None, tag=None)<\/code>\u65b9\u6cd5\u6765\u52a8\u6001\u6dfb\u52a0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528jieba\u5e93\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u4e3b\u8981\u6d89\u53ca\u5b89\u88c5jieba\u3001\u57fa\u672c\u5206\u8bcd\u529f\u80fd\u3001\u8bcd\u6027\u6807\u6ce8 [&hellip;]","protected":false},"author":3,"featured_media":960052,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960050"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=960050"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960050\/revisions"}],"predecessor-version":[{"id":960053,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960050\/revisions\/960053"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960052"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}