{"id":952684,"date":"2024-12-27T01:35:04","date_gmt":"2024-12-26T17:35:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/952684.html"},"modified":"2024-12-27T01:35:05","modified_gmt":"2024-12-26T17:35:05","slug":"python%e5%a6%82%e4%bd%95%e5%a6%82%e4%bd%95%e9%80%89%e6%8b%a9%e6%b1%89%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/952684.html","title":{"rendered":"python\u5982\u4f55\u5982\u4f55\u9009\u62e9\u6c49\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090429\/3880a597-ed33-4691-a9be-88be2a8e3394.webp\" alt=\"python\u5982\u4f55\u5982\u4f55\u9009\u62e9\u6c49\u5b57\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u9009\u62e9\u6c49\u5b57\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u4ee5\u53ca\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u5982jieba\u8fdb\u884c\u5206\u8bcd\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u901a\u8fc7\u5339\u914d\u7279\u5b9a\u7684Unicode\u8303\u56f4\u53ef\u4ee5\u6709\u6548\u63d0\u53d6\u6c49\u5b57\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u5219\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u5224\u65ad\u548c\u5207\u7247\u5b9e\u73b0\u7b80\u5355\u7684\u6c49\u5b57\u9009\u62e9\u3001\u800cjieba\u7b49\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u52a0\u667a\u80fd\u7684\u6587\u672c\u5206\u6790\u80fd\u529b\u3002<\/strong>\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6c49\u5b57\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9009\u62e9\u6c49\u5b57\u662f\u4e00\u79cd\u975e\u5e38\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5339\u914d\u5de5\u5177\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u7279\u5b9a\u7684\u6a21\u5f0f\u6765\u63d0\u53d6\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528re\u6a21\u5757\u6765\u5b9e\u73b0\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3002\u8981\u9009\u62e9\u6c49\u5b57\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Unicode\u8303\u56f4\u8fdb\u884c\u5339\u914d\u3002\u6c49\u5b57\u7684Unicode\u8303\u56f4\u4e3b\u8981\u5305\u62ec\uff1a\\u4e00-\\u9fff\uff08\u57fa\u672c\u6c49\u5b57\uff09\u3001\\u3400-\\u4dbf\uff08\u6269\u5c55A\u533a\uff09\u3001\\u20000-\\u2a6df\uff08\u6269\u5c55B\u533a\uff09\u7b49\u3002\u901a\u8fc7\u5b9a\u4e49\u8fd9\u4e9b\u8303\u56f4\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6c49\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u793a\u4f8b\u5b57\u7b26\u4e32<\/strong><\/h2>\n<p>text = &quot;Python\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u7528\u4e8e\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u3002&quot;<\/p>\n<h2><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6c49\u5b57<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;[\\u4e00-\\u9fff]+&#39;)<\/p>\n<p>chinese_characters = pattern.findall(text)<\/p>\n<p>print(chinese_characters)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8f93\u51fa\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u6709\u6c49\u5b57\uff0c\u5229\u7528\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f3a\u5927\u5339\u914d\u80fd\u529b\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u9009\u62e9\u6c49\u5b57<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\u975e\u5e38\u5f3a\u5927\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u4ece\u5927\u91cf\u6587\u672c\u4e2d\u63d0\u53d6\u7279\u5b9a\u6a21\u5f0f\u7684\u5185\u5bb9\u65f6\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u6765\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u6c49\u5b57\u7684Unicode\u8303\u56f4<\/h4>\n<\/p>\n<p><p>\u6c49\u5b57\u5728Unicode\u4e2d\u6709\u591a\u4e2a\u8303\u56f4\uff0c\u6700\u5e38\u7528\u7684\u662f\u57fa\u672c\u6c49\u5b57\u8303\u56f4\uff08\\u4e00-\\u9fff\uff09\u3002\u4f46\u5982\u679c\u9700\u8981\u5339\u914d\u6240\u6709\u6c49\u5b57\uff0c\u5305\u62ec\u6269\u5c55\u5b57\u7b26\uff0c\u8fd8\u5e94\u5305\u62ec\u6269\u5c55A\u533a\uff08\\u3400-\\u4dbf\uff09\u548c\u6269\u5c55B\u533a\uff08\\u20000-\\u2a6df\uff09\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5339\u914d\u6c49\u5b57<\/strong><\/h2>\n<p>def extract_chinese(text):<\/p>\n<p>    pattern = re.compile(r&#39;[\\u4e00-\\u9fff]+&#39;)<\/p>\n<p>    return pattern.findall(text)<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>text = &quot;\u4eca\u5929\u662f2023\u5e7410\u670810\u65e5\uff0c\u5929\u6c14\u6674\u6717\u3002&quot;<\/p>\n<p>chinese = extract_chinese(text)<\/p>\n<p>print(chinese)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6df7\u5408\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u672c\u5f80\u5f80\u662f\u6df7\u5408\u7684\uff0c\u5305\u62ec\u6c49\u5b57\u3001\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u5176\u4ed6\u7b26\u53f7\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u7075\u6d3b\u5730\u9009\u62e9\u53ea\u63d0\u53d6\u6c49\u5b57\uff0c\u4ece\u800c\u5728\u6587\u672c\u9884\u5904\u7406\u4e2d\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python3.9, \u6570\u636e\u5206\u6790\u5f88\u6709\u8da3\uff01&quot;<\/p>\n<p>chinese = extract_chinese(text)<\/p>\n<p>print(&quot;\u63d0\u53d6\u7684\u6c49\u5b57:&quot;, chinese)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u4ece\u6df7\u5408\u6587\u672c\u4e2d\u9ad8\u6548\u5730\u63d0\u53d6\u51fa\u6240\u6709\u7684\u6c49\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u9009\u62e9\u6c49\u5b57<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u9009\u62e9\u6c49\u5b57\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u4e0d\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u7075\u6d3b\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u573a\u666f\u4e0b\uff0c\u5b57\u7b26\u4e32\u64cd\u4f5c\u662f\u975e\u5e38\u6709\u6548\u7684\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9010\u5b57\u7b26\u5224\u65ad<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528\u5b57\u7b26\u7684Unicode\u503c\u6765\u5224\u65ad\u5b83\u662f\u5426\u662f\u6c49\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def is_chinese(character):<\/p>\n<p>    return &#39;\\u4e00&#39; &lt;= character &lt;= &#39;\\u9fff&#39;<\/p>\n<p>def extract_chinese_from_string(text):<\/p>\n<p>    return &#39;&#39;.join([char for char in text if is_chinese(char)])<\/p>\n<p>text = &quot;Python\u7f16\u7a0b\u8bed\u8a00&quot;<\/p>\n<p>chinese = extract_chinese_from_string(text)<\/p>\n<p>print(&quot;\u63d0\u53d6\u7684\u6c49\u5b57:&quot;, chinese)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5207\u7247\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4ece\u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u7279\u5b9a\u4f4d\u7f6e\u7684\u6c49\u5b57\uff0c\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u5207\u7247\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;\u6570\u636e\u5206\u6790\u5f88\u6709\u8da3&quot;<\/p>\n<h2><strong>\u63d0\u53d6\u524d\u4e24\u4e2a\u6c49\u5b57<\/strong><\/h2>\n<p>chinese_slice = text[:2]<\/p>\n<p>print(&quot;\u63d0\u53d6\u7684\u6c49\u5b57\u5207\u7247:&quot;, chinese_slice)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u5df2\u77e5\u4f4d\u7f6e\u7684\u6c49\u5b57\u63d0\u53d6\uff0c\u4f46\u4e0d\u9002\u5408\u590d\u6742\u7684\u6587\u672c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u9009\u62e9\u6c49\u5b57<\/h3>\n<\/p>\n<p><p>Python\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5904\u7406\u4e2d\u6587\u6587\u672c\uff0c\u5982jieba\u3001HanLP\u7b49\u3002\u8fd9\u4e9b\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u9009\u62e9\u6c49\u5b57\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528jieba\u5206\u8bcd<\/h4>\n<\/p>\n<p><p>jieba\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684\u4e2d\u6587\u5206\u8bcd\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5c06\u8fde\u7eed\u7684\u4e2d\u6587\u6587\u672c\u5207\u5206\u6210\u8bcd\u8bed\u3002\u5c3d\u7ba1jieba\u4e3b\u8981\u7528\u4e8e\u5206\u8bcd\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5176\u5206\u8bcd\u7ed3\u679c\u6765\u9009\u62e9\u6c49\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<p>text = &quot;Python\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00&quot;<\/p>\n<h2><strong>\u4f7f\u7528jieba\u5206\u8bcd<\/strong><\/h2>\n<p>words = jieba.lcut(text)<\/p>\n<p>print(&quot;\u5206\u8bcd\u7ed3\u679c:&quot;, words)<\/p>\n<h2><strong>\u63d0\u53d6\u6c49\u5b57<\/strong><\/h2>\n<p>chinese_words = [word for word in words if all(&#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fff&#39; for char in word)]<\/p>\n<p>print(&quot;\u6c49\u5b57\u8bcd\u8bed:&quot;, chinese_words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5229\u7528HanLP\u8fdb\u884c\u9ad8\u7ea7\u6587\u672c\u5206\u6790<\/h4>\n<\/p>\n<p><p>HanLP\u662f\u4e00\u4e2a\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bcd\u6cd5\u5206\u6790\u3001\u53e5\u6cd5\u5206\u6790\u7b49\u529f\u80fd\uff0c\u53ef\u4ee5\u5bf9\u4e2d\u6587\u6587\u672c\u8fdb\u884c\u6df1\u5ea6\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyhanlp import *<\/p>\n<p>text = &quot;Python\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00&quot;<\/p>\n<h2><strong>\u4f7f\u7528HanLP\u5206\u8bcd<\/strong><\/h2>\n<p>segment = HanLP.newSegment()<\/p>\n<p>terms = segment.seg(text)<\/p>\n<p>print(&quot;HanLP\u5206\u8bcd\u7ed3\u679c:&quot;, terms)<\/p>\n<h2><strong>\u63d0\u53d6\u6c49\u5b57<\/strong><\/h2>\n<p>chinese_terms = [term.word for term in terms if all(&#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fff&#39; for char in term.word)]<\/p>\n<p>print(&quot;\u6c49\u5b57\u8bcd\u8bed:&quot;, chinese_terms)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u6c49\u5b57\u7684\u9700\u6c42\u53ef\u80fd\u4f1a\u51fa\u73b0\u5728\u591a\u79cd\u573a\u666f\u4e2d\uff0c\u4f8b\u5982\u6570\u636e\u6e05\u6d17\u3001\u6587\u672c\u5206\u6790\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7b49\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u7ed3\u5408\u5177\u4f53\u5e94\u7528\u573a\u666f\uff0c\u63a2\u8ba8\u9009\u62e9\u6c49\u5b57\u7684\u5b9e\u8df5\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6587\u672c\u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u5c24\u5176\u662f\u4ece\u7f51\u7edc\u6293\u53d6\u7684\u6570\u636e\uff0c\u5f80\u5f80\u9700\u8981\u6e05\u6d17\u6389\u4e0d\u5fc5\u8981\u7684\u5b57\u7b26\uff0c\u53ea\u4fdd\u7559\u6709\u7528\u7684\u4fe1\u606f\u3002\u9009\u62e9\u6c49\u5b57\u662f\u6570\u636e\u6e05\u6d17\u7684\u91cd\u8981\u6b65\u9aa4\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def clean_text(text):<\/p>\n<p>    return extract_chinese_from_string(text)<\/p>\n<p>raw_text = &quot;Hello, \u6b22\u8fce\u4f7f\u7528Python\uff01123&quot;<\/p>\n<p>cleaned_text = clean_text(raw_text)<\/p>\n<p>print(&quot;\u6e05\u6d17\u540e\u7684\u6587\u672c:&quot;, cleaned_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u4e2d\uff0c\u9009\u62e9\u6c49\u5b57\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u66f4\u7cbe\u7ec6\u7684\u6587\u672c\u5206\u6790\uff0c\u4f8b\u5982\u60c5\u611f\u5206\u6790\u3001\u6587\u672c\u5206\u7c7b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def analyze_sentiment(text):<\/p>\n<p>    # \u5047\u8bbe\u8fd9\u91cc\u6709\u4e00\u4e2a\u60c5\u611f\u5206\u6790\u6a21\u578b<\/p>\n<p>    chinese_text = extract_chinese_from_string(text)<\/p>\n<p>    # \u6a21\u578b\u5206\u6790<\/p>\n<p>    sentiment_score = &quot;Positive&quot;  # \u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u793a\u4f8b<\/p>\n<p>    return sentiment_score<\/p>\n<p>text = &quot;Python\u662f\u4e00\u79cd\u975e\u5e38\u53d7\u6b22\u8fce\u7684\u7f16\u7a0b\u8bed\u8a00&quot;<\/p>\n<p>sentiment = analyze_sentiment(text)<\/p>\n<p>print(&quot;\u60c5\u611f\u5206\u6790\u7ed3\u679c:&quot;, sentiment)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e0e\u6570\u636e\u6316\u6398<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u548c\u6570\u636e\u6316\u6398\u4e2d\uff0c\u9009\u62e9\u6c49\u5b57\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6784\u5efa\u66f4\u6709\u6548\u7684\u7279\u5f81\u96c6\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def feature_extraction(text):<\/p>\n<p>    chinese_text = extract_chinese_from_string(text)<\/p>\n<p>    # \u5047\u8bbe\u6211\u4eec\u4f7f\u7528\u6c49\u5b57\u7684\u9891\u7387\u4f5c\u4e3a\u7279\u5f81<\/p>\n<p>    features = {char: chinese_text.count(char) for char in set(chinese_text)}<\/p>\n<p>    return features<\/p>\n<p>text = &quot;\u6570\u636e\u79d1\u5b66\u4e0ePython&quot;<\/p>\n<p>features = feature_extraction(text)<\/p>\n<p>print(&quot;\u63d0\u53d6\u7684\u7279\u5f81:&quot;, features)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5404\u79cd\u573a\u666f\u4e2d\u6709\u6548\u9009\u62e9\u548c\u5904\u7406\u6c49\u5b57\uff0c\u4ece\u800c\u63d0\u9ad8\u6587\u672c\u5904\u7406\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u7684\u5de5\u4f5c\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6c49\u5b57\u7684\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6c49\u5b57\u7684\u7f16\u7801\u901a\u5e38\u4f7f\u7528UTF-8\u6216GBK\u7b49\u7f16\u7801\u683c\u5f0f\u3002\u4e3a\u4e86\u6b63\u786e\u5904\u7406\u6c49\u5b57\uff0c\u786e\u4fdd\u5728\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u65f6\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u7684<code>encoding<\/code>\u53c2\u6570\u6765\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u5982<code>open(&#39;file.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;)<\/code>\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u56e0\u7f16\u7801\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6c49\u5b57\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u5b57\u7b26\u4e32\u4e2d\u7684\u6c49\u5b57\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u975e\u5e38\u5f3a\u5927\uff0c\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u901a\u8fc7\u7f16\u5199\u6b63\u5219\u8868\u8fbe\u5f0f<code>[\\u4e00-\\u9fa5]+<\/code>\uff0c\u53ef\u4ee5\u5339\u914d\u6240\u6709\u6c49\u5b57\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ntext = &quot;Hello, \u4f60\u597d\uff0c\u4e16\u754c\uff01&quot;\nhanzi = re.findall(r&#39;[\\u4e00-\\u9fa5]+&#39;, text)\nprint(hanzi)  # \u8f93\u51fa\uff1a[&#39;\u4f60\u597d&#39;, &#39;\u4e16\u754c&#39;]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6c49\u5b57\uff1f<\/strong><br \/>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u3002\u901a\u8fc7\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5b58\u5728\u7b26\u5408\u6c49\u5b57\u8303\u56f4\u7684\u5b57\u7b26\uff0c\u53ef\u4ee5\u5224\u65ad\u8be5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6c49\u5b57\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ndef cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns_hanzi(text):\n    return bool(re.search(r&#39;[\\u4e00-\\u9fa5]&#39;, text))\n\ntext = &quot;Hello, \u4f60\u597d\uff01&quot;\nprint(contains_hanzi(text))  # \u8f93\u51fa\uff1aTrue\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u6c49\u5b57\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u9009\u62e9\u6c49\u5b57\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u4ee5\u53ca\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u5982jieba\u8fdb\u884c\u5206\u8bcd [&hellip;]","protected":false},"author":3,"featured_media":952685,"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\/952684"}],"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=952684"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952684\/revisions"}],"predecessor-version":[{"id":952686,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/952684\/revisions\/952686"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/952685"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=952684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=952684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=952684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}