{"id":1167667,"date":"2025-01-15T15:47:59","date_gmt":"2025-01-15T07:47:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167667.html"},"modified":"2025-01-15T15:48:01","modified_gmt":"2025-01-15T07:48:01","slug":"python%e5%a6%82%e4%bd%95%e6%9e%84%e9%80%a0%e4%b8%ad%e6%96%87%e8%af%8d%e6%b1%87","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1167667.html","title":{"rendered":"python\u5982\u4f55\u6784\u9020\u4e2d\u6587\u8bcd\u6c47"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26065126\/1bdb8d6f-6074-4d04-abe0-5dea01a8d9d5.webp\" alt=\"python\u5982\u4f55\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\" \/><\/p>\n<p><p> <strong>\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u5df2\u6709\u7684\u4e2d\u6587\u8bcd\u5e93\u3001\u5206\u8bcd\u6280\u672f\u3001\u7ed3\u5408\u8bed\u8a00\u6a21\u578b\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5df2\u6709\u7684\u4e2d\u6587\u8bcd\u5e93\u662f\u6700\u5e38\u89c1\u548c\u7b80\u4fbf\u7684\u65b9\u6cd5\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u73b0\u6709\u4e2d\u6587\u8bcd\u5e93\u6765\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u73b0\u6709\u7684\u4e2d\u6587\u8bcd\u5e93\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u52a0\u8f7d\u5e76\u4f7f\u7528\u5df2\u6709\u7684\u4e2d\u6587\u8bcd\u5e93\uff0c\u53ef\u4ee5\u5feb\u901f\u83b7\u53d6\u5e38\u7528\u7684\u8bcd\u6c47\uff0c\u5e76\u5229\u7528\u8fd9\u4e9b\u8bcd\u5e93\u8fdb\u884c\u6587\u672c\u5206\u6790\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3002Python\u4e2d\u6709\u5f88\u591a\u6210\u719f\u7684\u4e2d\u6587\u5206\u8bcd\u5de5\u5177\uff0c\u5982jieba\u3001THULAC\u3001HanLP\u7b49\uff0c\u8fd9\u4e9b\u5de5\u5177\u81ea\u5e26\u4e86\u4e30\u5bcc\u7684\u4e2d\u6587\u8bcd\u5e93\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5df2\u6709\u7684\u4e2d\u6587\u8bcd\u5e93<\/h3>\n<\/p>\n<p><h4>1\u3001jieba\u5206\u8bcd\u5e93<\/h4>\n<\/p>\n<p><p>jieba\u662fPython\u4e2d\u6700\u4e3a\u5e38\u7528\u7684\u4e2d\u6587\u5206\u8bcd\u5de5\u5177\u4e4b\u4e00\uff0c\u5177\u6709\u7b80\u5355\u6613\u7528\u3001\u5206\u8bcd\u51c6\u786e\u7387\u9ad8\u7684\u7279\u70b9\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528jieba\u5206\u8bcd\u5e93\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<h2><strong>\u52a0\u8f7d\u81ea\u5b9a\u4e49\u8bcd\u5178<\/strong><\/h2>\n<p>jieba.load_userdict(&quot;user_dict.txt&quot;)<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = jieba.cut(text, cut_all=False)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\/&quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u52a0\u8f7d\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8bcd\u5178\uff0c\u7136\u540e\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\uff0c\u5e76\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001THULAC\u5206\u8bcd\u5e93<\/h4>\n<\/p>\n<p><p>THULAC\u662f\u6e05\u534e\u5927\u5b66\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e0e\u793e\u4f1a\u4eba\u6587\u8ba1\u7b97\u5b9e\u9a8c\u5ba4\u63a8\u51fa\u7684\u4e00\u6b3e\u4e2d\u6587\u5206\u8bcd\u5de5\u5177\uff0c\u5177\u6709\u5206\u8bcd\u51c6\u786e\u7387\u9ad8\u3001\u901f\u5ea6\u5feb\u7684\u7279\u70b9\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528THULAC\u5206\u8bcd\u5e93\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import thulac<\/p>\n<h2><strong>\u521d\u59cb\u5316THULAC<\/strong><\/h2>\n<p>thu = thulac.thulac(seg_only=True)<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = thu.cut(text, text=True)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e86THULAC\uff0c\u7136\u540e\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\uff0c\u5e76\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>3\u3001HanLP\u5206\u8bcd\u5e93<\/h4>\n<\/p>\n<p><p>HanLP\u662f\u7531\u4e00\u7cfb\u5217\u6a21\u578b\u548c\u7b97\u6cd5\u7ec4\u6210\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\uff0c\u652f\u6301\u5206\u8bcd\u3001\u8bcd\u6027\u6807\u6ce8\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u7b49\u529f\u80fd\u3002\u4e0b\u9762\u662f\u5982\u4f55\u4f7f\u7528HanLP\u5206\u8bcd\u5e93\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyhanlp import *<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = HanLP.segment(text)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5229\u7528HanLP\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\uff0c\u5e76\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5206\u8bcd\u6280\u672f<\/h3>\n<\/p>\n<p><p>\u5206\u8bcd\u6280\u672f\u662f\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u7684\u91cd\u8981\u624b\u6bb5\u3002\u4e2d\u6587\u6587\u672c\u4e2d\u8bcd\u4e0e\u8bcd\u4e4b\u95f4\u6ca1\u6709\u660e\u663e\u7684\u5206\u9694\u7b26\uff0c\u56e0\u6b64\u9700\u8981\u901a\u8fc7\u5206\u8bcd\u6280\u672f\u5c06\u8fde\u7eed\u7684\u6587\u672c\u5212\u5206\u6210\u4e00\u4e2a\u4e2a\u72ec\u7acb\u7684\u8bcd\u6c47\u3002\u5206\u8bcd\u6280\u672f\u4e3b\u8981\u5206\u4e3a\u57fa\u4e8e\u89c4\u5219\u7684\u65b9\u6cd5\u548c\u57fa\u4e8e\u7edf\u8ba1\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u4e8e\u89c4\u5219\u7684\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u57fa\u4e8e\u89c4\u5219\u7684\u65b9\u6cd5\u4f9d\u8d56\u4e8e\u9884\u5b9a\u4e49\u7684\u8bcd\u5178\u548c\u89c4\u5219\uff0c\u901a\u8fc7\u5339\u914d\u8bcd\u5178\u4e2d\u7684\u8bcd\u6c47\u6765\u5b9e\u73b0\u5206\u8bcd\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u4f46\u5bf9\u4e8e\u8bcd\u5178\u4e2d\u6ca1\u6709\u7684\u8bcd\u6c47\uff08\u5982\u65b0\u8bcd\u3001\u4e13\u6709\u540d\u8bcd\u7b49\uff09\u5219\u65e0\u6cd5\u8bc6\u522b\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u4e8e\u89c4\u5219\u7684\u7b80\u5355\u5206\u8bcd\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bcd\u5178<\/p>\n<p>dictionary = [&quot;\u6211&quot;, &quot;\u7231&quot;, &quot;\u81ea\u7136&quot;, &quot;\u8bed\u8a00&quot;, &quot;\u5904\u7406&quot;]<\/p>\n<h2><strong>\u5206\u8bcd\u51fd\u6570<\/strong><\/h2>\n<p>def rule_based_segment(text, dictionary):<\/p>\n<p>    words = []<\/p>\n<p>    while text:<\/p>\n<p>        for i in range(len(text), 0, -1):<\/p>\n<p>            if text[:i] in dictionary:<\/p>\n<p>                words.append(text[:i])<\/p>\n<p>                text = text[i:]<\/p>\n<p>                break<\/p>\n<p>    return words<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = rule_based_segment(text, dictionary)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\/&quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u8bcd\u5178\uff0c\u7136\u540e\u901a\u8fc7\u904d\u5386\u6587\u672c\u5e76\u5339\u914d\u8bcd\u5178\u4e2d\u7684\u8bcd\u6c47\u6765\u5b9e\u73b0\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u57fa\u4e8e\u7edf\u8ba1\u7684\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u57fa\u4e8e\u7edf\u8ba1\u7684\u65b9\u6cd5\u901a\u8fc7\u8ba1\u7b97\u8bcd\u6c47\u7684\u5171\u73b0\u6982\u7387\u3001\u4e92\u4fe1\u606f\u7b49\u7edf\u8ba1\u7279\u5f81\u6765\u5b9e\u73b0\u5206\u8bcd\uff0c\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u81ea\u52a8\u5b66\u4e60\u548c\u8bc6\u522b\u65b0\u8bcd\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u7075\u6d3b\u6027\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u4e8e\u7edf\u8ba1\u7684\u5206\u8bcd\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<h2><strong>\u6dfb\u52a0\u81ea\u5b9a\u4e49\u8bcd\u6c47<\/strong><\/h2>\n<p>jieba.add_word(&quot;\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;)<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = jieba.cut(text, cut_all=False)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\/&quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7jieba\u7684add_word\u51fd\u6570\u6dfb\u52a0\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8bcd\u6c47\uff0c\u7136\u540e\u8fdb\u884c\u5206\u8bcd\u5e76\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7ed3\u5408\u8bed\u8a00\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u8bed\u8a00\u6a21\u578b\u662f\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u7684\u91cd\u8981\u5de5\u5177\uff0c\u901a\u8fc7\u8bad\u7ec3\u8bed\u8a00\u6a21\u578b\uff0c\u53ef\u4ee5\u6355\u6349\u6587\u672c\u4e2d\u7684\u8bed\u6cd5\u548c\u8bed\u4e49\u4fe1\u606f\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u52a0\u51c6\u786e\u7684\u5206\u8bcd\u3002\u5e38\u7528\u7684\u8bed\u8a00\u6a21\u578b\u5305\u62ecn\u5143\u6a21\u578b\u3001\u795e\u7ecf\u7f51\u7edc\u8bed\u8a00\u6a21\u578b\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1\u3001n\u5143\u6a21\u578b<\/h4>\n<\/p>\n<p><p>n\u5143\u6a21\u578b\u901a\u8fc7\u7edf\u8ba1\u8bcd\u6c47\u5728\u6587\u672c\u4e2d\u7684\u5171\u73b0\u9891\u7387\u6765\u9884\u6d4b\u4e0b\u4e00\u4e2a\u8bcd\u6c47\uff0c\u5e38\u7528\u4e8e\u5206\u8bcd\u3001\u8bcd\u6027\u6807\u6ce8\u7b49\u4efb\u52a1\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u4e8en\u5143\u6a21\u578b\u7684\u5206\u8bcd\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<h2><strong>\u52a0\u8f7d\u81ea\u5b9a\u4e49\u8bcd\u5178<\/strong><\/h2>\n<p>jieba.load_userdict(&quot;user_dict.txt&quot;)<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = jieba.cut(text, cut_all=False)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\/&quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u52a0\u8f7d\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u8bcd\u5178\uff0c\u7136\u540e\u5229\u7528jieba\u8fdb\u884c\u5206\u8bcd\uff0c\u5e76\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u795e\u7ecf\u7f51\u7edc\u8bed\u8a00\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u795e\u7ecf\u7f51\u7edc\u8bed\u8a00\u6a21\u578b\u901a\u8fc7\u6df1\u5ea6\u5b66\u4e60\u65b9\u6cd5\uff0c\u80fd\u591f\u6355\u6349\u6587\u672c\u4e2d\u7684\u590d\u6742\u8bed\u6cd5\u548c\u8bed\u4e49\u4fe1\u606f\uff0c\u5177\u6709\u8f83\u9ad8\u7684\u5206\u8bcd\u51c6\u786e\u7387\u3002\u5e38\u7528\u7684\u795e\u7ecf\u7f51\u7edc\u8bed\u8a00\u6a21\u578b\u5305\u62ecRNN\u3001LSTM\u3001Transformer\u7b49\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u57fa\u4e8e\u795e\u7ecf\u7f51\u7edc\u8bed\u8a00\u6a21\u578b\u7684\u5206\u8bcd\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.preprocessing.text import Tokenizer<\/p>\n<p>from tensorflow.keras.preprocessing.sequence import pad_sequences<\/p>\n<h2><strong>\u8bad\u7ec3\u6570\u636e<\/strong><\/h2>\n<p>texts = [<\/p>\n<p>    &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;,<\/p>\n<p>    &quot;\u81ea\u7136\u8bed\u8a00\u5904\u7406\u662f\u4eba\u5de5\u667a\u80fd\u7684\u4e00\u4e2a\u91cd\u8981\u5206\u652f&quot;,<\/p>\n<p>    &quot;<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6df1\u5ea6\u5b66\u4e60\u662f\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7684\u91cd\u8981\u6280\u672f&quot;<\/p>\n<p>]<\/p>\n<h2><strong>\u521d\u59cb\u5316Tokenizer<\/strong><\/h2>\n<p>tokenizer = Tokenizer()<\/p>\n<p>tokenizer.fit_on_texts(texts)<\/p>\n<h2><strong>\u751f\u6210\u8bcd\u6c47\u7d22\u5f15<\/strong><\/h2>\n<p>sequences = tokenizer.texts_to_sequences(texts)<\/p>\n<p>word_index = tokenizer.word_index<\/p>\n<h2><strong>\u8f93\u51fa\u8bcd\u6c47\u7d22\u5f15<\/strong><\/h2>\n<p>print(word_index)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5229\u7528TensorFlow\u7684Tokenizer\u8fdb\u884c\u6587\u672c\u5904\u7406\uff0c\u751f\u6210\u8bcd\u6c47\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u901a\u8fc7\u5b9a\u4e49\u5339\u914d\u89c4\u5219\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5206\u8bcd\u548c\u8bcd\u6c47\u6784\u9020\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u5206\u8bcd\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u5206\u8bcd\u51fd\u6570<\/strong><\/h2>\n<p>def regex_segment(text):<\/p>\n<p>    pattern = re.compile(r&#39;\\w+|\\s+|[^\\w\\s]+&#39;)<\/p>\n<p>    words = pattern.findall(text)<\/p>\n<p>    return words<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = regex_segment(text)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\/&quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6a21\u5f0f\uff0c\u7136\u540e\u901a\u8fc7findall\u51fd\u6570\u5339\u914d\u6587\u672c\u4e2d\u7684\u8bcd\u6c47\uff0c\u5e76\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5f80\u5f80\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\uff0c\u4ee5\u63d0\u9ad8\u5206\u8bcd\u7684\u51c6\u786e\u7387\u548c\u9c81\u68d2\u6027\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7ed3\u5408\u57fa\u4e8e\u89c4\u5219\u7684\u65b9\u6cd5\u548c\u57fa\u4e8e\u7edf\u8ba1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u89c4\u5219\u5339\u914d\u8fdb\u884c\u521d\u6b65\u5206\u8bcd\uff0c\u518d\u5229\u7528\u7edf\u8ba1\u65b9\u6cd5\u8fdb\u884c\u4f18\u5316\uff1b\u6216\u8005\u7ed3\u5408\u8bed\u8a00\u6a21\u578b\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u901a\u8fc7\u8bed\u8a00\u6a21\u578b\u6355\u6349\u6587\u672c\u4e2d\u7684\u8bed\u6cd5\u548c\u8bed\u4e49\u4fe1\u606f\uff0c\u518d\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u7ec6\u7c92\u5ea6\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u7ed3\u5408\u57fa\u4e8e\u89c4\u5219\u7684\u65b9\u6cd5\u548c\u57fa\u4e8e\u7edf\u8ba1\u7684\u65b9\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<h2><strong>\u8bcd\u5178<\/strong><\/h2>\n<p>dictionary = [&quot;\u6211&quot;, &quot;\u7231&quot;, &quot;\u81ea\u7136&quot;, &quot;\u8bed\u8a00&quot;, &quot;\u5904\u7406&quot;]<\/p>\n<h2><strong>\u5206\u8bcd\u51fd\u6570<\/strong><\/h2>\n<p>def combined_segment(text, dictionary):<\/p>\n<p>    words = []<\/p>\n<p>    while text:<\/p>\n<p>        for i in range(len(text), 0, -1):<\/p>\n<p>            if text[:i] in dictionary:<\/p>\n<p>                words.append(text[:i])<\/p>\n<p>                text = text[i:]<\/p>\n<p>                break<\/p>\n<p>        else:<\/p>\n<p>            words += list(jieba.cut(text, cut_all=False))<\/p>\n<p>            break<\/p>\n<p>    return words<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = combined_segment(text, dictionary)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\/&quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u4e86\u57fa\u4e8e\u89c4\u5219\u7684\u65b9\u6cd5\u548c\u57fa\u4e8e\u7edf\u8ba1\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5339\u914d\u8bcd\u5178\u4e2d\u7684\u8bcd\u6c47\u8fdb\u884c\u521d\u6b65\u5206\u8bcd\uff0c\u7136\u540e\u5229\u7528jieba\u8fdb\u884c\u4f18\u5316\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7ed3\u5408\u8bed\u8a00\u6a21\u578b\u548c\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.preprocessing.text import Tokenizer<\/p>\n<p>from tensorflow.keras.preprocessing.sequence import pad_sequences<\/p>\n<p>import re<\/p>\n<h2><strong>\u8bad\u7ec3\u6570\u636e<\/strong><\/h2>\n<p>texts = [<\/p>\n<p>    &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;,<\/p>\n<p>    &quot;\u81ea\u7136\u8bed\u8a00\u5904\u7406\u662f<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u7684\u4e00\u4e2a\u91cd\u8981\u5206\u652f&quot;,<\/p>\n<p>    &quot;\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u662f\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7684\u91cd\u8981\u6280\u672f&quot;<\/p>\n<p>]<\/p>\n<h2><strong>\u521d\u59cb\u5316Tokenizer<\/strong><\/h2>\n<p>tokenizer = Tokenizer()<\/p>\n<p>tokenizer.fit_on_texts(texts)<\/p>\n<h2><strong>\u751f\u6210\u8bcd\u6c47\u7d22\u5f15<\/strong><\/h2>\n<p>sequences = tokenizer.texts_to_sequences(texts)<\/p>\n<p>word_index = tokenizer.word_index<\/p>\n<h2><strong>\u5206\u8bcd\u51fd\u6570<\/strong><\/h2>\n<p>def combined_segment(text):<\/p>\n<p>    # \u5229\u7528\u8bed\u8a00\u6a21\u578b\u8fdb\u884c\u521d\u6b65\u5206\u8bcd<\/p>\n<p>    sequences = tokenizer.texts_to_sequences([text])<\/p>\n<p>    words = [tokenizer.index_word[idx] for seq in sequences for idx in seq]<\/p>\n<p>    # \u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u7ec6\u7c92\u5ea6\u5206\u8bcd<\/p>\n<p>    pattern = re.compile(r&#39;\\w+|\\s+|[^\\w\\s]+&#39;)<\/p>\n<p>    words = [w for word in words for w in pattern.findall(word)]<\/p>\n<p>    return words<\/p>\n<h2><strong>\u5206\u8bcd<\/strong><\/h2>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406&quot;<\/p>\n<p>words = combined_segment(text)<\/p>\n<h2><strong>\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c<\/strong><\/h2>\n<p>print(&quot;\/&quot;.join(words))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7ed3\u5408\u4e86\u8bed\u8a00\u6a21\u578b\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u901a\u8fc7\u8bed\u8a00\u6a21\u578b\u8fdb\u884c\u521d\u6b65\u5206\u8bcd\uff0c\u7136\u540e\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u7ec6\u7c92\u5ea6\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5df2\u6709\u7684\u4e2d\u6587\u8bcd\u5e93\u3001\u5206\u8bcd\u6280\u672f\u3001\u7ed3\u5408\u8bed\u8a00\u6a21\u578b\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u6216\u8005\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u4ee5\u63d0\u9ad8\u5206\u8bcd\u7684\u51c6\u786e\u7387\u548c\u9c81\u68d2\u6027\u3002\u901a\u8fc7\u5408\u7406\u5229\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\uff0c\u8fdb\u884c\u6587\u672c\u5206\u6790\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5904\u7406\u4e2d\u6587\u6587\u672c\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9\u4e00\u4e9b\u5e93\uff0c\u5982jieba\u8fdb\u884c\u4e2d\u6587\u5206\u8bcd\uff0cpandas\u8fdb\u884c\u6570\u636e\u5904\u7406\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5\u3002\u4e4b\u540e\uff0c\u4f7f\u7528jieba\u7684\u5206\u8bcd\u529f\u80fd\uff0c\u53ef\u4ee5\u5c06\u4e2d\u6587\u53e5\u5b50\u62c6\u5206\u6210\u8bcd\u6c47\uff0c\u4e3a\u540e\u7eed\u5206\u6790\u6216\u5904\u7406\u6253\u4e0b\u57fa\u7840\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0\u4e2d\u6587\u8bcd\u6c47\u7684\u9891\u7387\u7edf\u8ba1\uff1f<\/strong><br \/>\u7edf\u8ba1\u4e2d\u6587\u8bcd\u6c47\u7684\u9891\u7387\u53ef\u4ee5\u4f7f\u7528collections\u5e93\u4e2d\u7684Counter\u7c7b\u3002\u901a\u8fc7\u5206\u8bcd\u540e\uff0c\u5c06\u8bcd\u6c47\u653e\u5165Counter\u4e2d\uff0c\u5373\u53ef\u5feb\u901f\u5f97\u5230\u5404\u4e2a\u8bcd\u6c47\u51fa\u73b0\u7684\u6b21\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u5408\u6587\u672c\u5206\u6790\u548c\u6570\u636e\u6316\u6398\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u63a8\u8350\u7684Python\u5e93\u7528\u4e8e\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528jieba\u3001THULAC\u548cpkuseg\u7b49\u5e93\u6765\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u8868\u3002\u8fd9\u4e9b\u5e93\u652f\u6301\u591a\u79cd\u5206\u8bcd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u9700\u6c42\u6765\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u53d6\u51fa\u6587\u672c\u4e2d\u7684\u5173\u952e\u8bcd\u548c\u8bcd\u6c47\uff0c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u6784\u9020\u4e2d\u6587\u8bcd\u6c47\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\uff1a\u4f7f\u7528\u5df2\u6709\u7684\u4e2d\u6587\u8bcd\u5e93\u3001\u5206\u8bcd\u6280\u672f\u3001\u7ed3\u5408\u8bed\u8a00\u6a21\u578b\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u5df2\u6709\u7684\u4e2d\u6587\u8bcd\u5e93 [&hellip;]","protected":false},"author":3,"featured_media":1167670,"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\/1167667"}],"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=1167667"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167667\/revisions"}],"predecessor-version":[{"id":1167671,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1167667\/revisions\/1167671"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1167670"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1167667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1167667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1167667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}