{"id":1046459,"date":"2024-12-31T13:33:33","date_gmt":"2024-12-31T05:33:33","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1046459.html"},"modified":"2024-12-31T13:33:37","modified_gmt":"2024-12-31T05:33:37","slug":"%e5%a6%82%e4%bd%95%e6%a3%80%e6%b5%8b%e6%96%87%e5%ad%97%e6%98%af%e4%b8%ad%e6%96%87%e8%bf%98%e6%98%af%e5%ad%97%e6%af%8dpython","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1046459.html","title":{"rendered":"\u5982\u4f55\u68c0\u6d4b\u6587\u5b57\u662f\u4e2d\u6587\u8fd8\u662f\u5b57\u6bcdpython"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5e26bf6c-629c-45a8-a0f9-8fddeccbebdb.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u68c0\u6d4b\u6587\u5b57\u662f\u4e2d\u6587\u8fd8\u662f\u5b57\u6bcdpython\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u68c0\u6d4b\u4e00\u6bb5\u6587\u5b57\u662f\u4e2d\u6587\u8fd8\u662f\u5b57\u6bcd\u3002<\/strong> \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u7f16\u7801\u8303\u56f4\u4ee5\u53ca\u5e93\u51fd\u6570\u7b49\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u5e76\u63a2\u8ba8\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\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\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u7279\u5b9a\u7684\u5b57\u7b26\u6a21\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u68c0\u6d4b\u4e00\u6bb5\u6587\u5b57\u4e2d\u662f\u5426\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u6216\u82f1\u6587\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h4>1. \u68c0\u6d4b\u4e2d\u6587\u5b57\u7b26<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7Unicode\u7f16\u7801\u8303\u56f4\u6765\u68c0\u6d4b\u4e2d\u6587\u5b57\u7b26\u3002\u4e2d\u6587\u5b57\u7b26\u7684Unicode\u7f16\u7801\u8303\u56f4\u662f <code>\\u4e00<\/code> \u5230 <code>\\u9fff<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>[\\u4e00-\\u9fff]<\/code> \u6765\u5339\u914d\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns_chinese(text):<\/p>\n<p>    pattern = re.compile(r&#39;[\\u4e00-\\u9fff]+&#39;)<\/p>\n<p>    return bool(pattern.search(text))<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5&quot;<\/p>\n<p>print(contains_chinese(text))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u6d4b\u82f1\u6587\u5b57\u6bcd<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f <code>[A-Za-z]<\/code> \u6765\u5339\u914d\u82f1\u6587\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def contains_english(text):<\/p>\n<p>    pattern = re.compile(r&#39;[A-Za-z]+&#39;)<\/p>\n<p>    return bool(pattern.search(text))<\/p>\n<p>text = &quot;This is a test&quot;<\/p>\n<p>print(contains_english(text))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5b57\u7b26\u7f16\u7801\u8303\u56f4<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u5b57\u7b26\u7684Unicode\u7f16\u7801\u8303\u56f4\u6765\u5224\u65ad\u5b57\u7b26\u662f\u4e2d\u6587\u8fd8\u662f\u82f1\u6587\u5b57\u6bcd\u3002<\/p>\n<\/p>\n<p><h4>1. \u68c0\u6d4b\u4e2d\u6587\u5b57\u7b26<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_chinese(char):<\/p>\n<p>    return &#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fff&#39;<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5&quot;<\/p>\n<p>print(any(is_chinese(char) for char in text))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u68c0\u6d4b\u82f1\u6587\u5b57\u6bcd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def is_english(char):<\/p>\n<p>    return &#39;A&#39; &lt;= char &lt;= &#39;Z&#39; or &#39;a&#39; &lt;= char &lt;= &#39;z&#39;<\/p>\n<p>text = &quot;This is a test&quot;<\/p>\n<p>print(any(is_english(char) for char in text))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u5e93\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u4e9b\u7b2c\u4e09\u65b9\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u68c0\u6d4b\u6587\u672c\u4e2d\u7684\u5b57\u7b26\u7c7b\u578b\uff0c\u4f8b\u5982 <code>unicodedata<\/code> \u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unicodedata<\/p>\n<p>def contains_chinese(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        if &#39;CJK UNIFIED IDEOGRAPH&#39; in unicodedata.name(char):<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5&quot;<\/p>\n<p>print(contains_chinese(text))  # \u8f93\u51fa: True<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u68c0\u6d4b\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u521b\u5efa\u4e00\u4e2a\u7efc\u5408\u7684\u68c0\u6d4b\u51fd\u6570\uff0c\u4ee5\u63d0\u9ad8\u68c0\u6d4b\u7684\u51c6\u786e\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>import unicodedata<\/p>\n<p>def contains_chinese(text):<\/p>\n<p>    pattern = re.compile(r&#39;[\\u4e00-\\u9fff]+&#39;)<\/p>\n<p>    return bool(pattern.search(text))<\/p>\n<p>def contains_english(text):<\/p>\n<p>    pattern = re.compile(r&#39;[A-Za-z]+&#39;)<\/p>\n<p>    return bool(pattern.search(text))<\/p>\n<p>def contains_chinese_unicode(text):<\/p>\n<p>    for char in text:<\/p>\n<p>        if &#39;CJK UNIFIED IDEOGRAPH&#39; in unicodedata.name(char):<\/p>\n<p>            return True<\/p>\n<p>    return False<\/p>\n<p>def detect_text_type(text):<\/p>\n<p>    if contains_chinese(text) or contains_chinese_unicode(text):<\/p>\n<p>        return &quot;Chinese&quot;<\/p>\n<p>    elif contains_english(text):<\/p>\n<p>        return &quot;English&quot;<\/p>\n<p>    else:<\/p>\n<p>        return &quot;Unknown&quot;<\/p>\n<p>text1 = &quot;\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5&quot;<\/p>\n<p>text2 = &quot;This is a test&quot;<\/p>\n<p>text3 = &quot;12345&quot;<\/p>\n<p>print(detect_text_type(text1))  # \u8f93\u51fa: Chinese<\/p>\n<p>print(detect_text_type(text2))  # \u8f93\u51fa: English<\/p>\n<p>print(detect_text_type(text3))  # \u8f93\u51fa: Unknown<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6709\u6548\u5730\u68c0\u6d4b\u4e00\u6bb5\u6587\u5b57\u662f\u4e2d\u6587\u8fd8\u662f\u5b57\u6bcd\u3002<strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u7f16\u7801\u8303\u56f4\u3001\u5e93\u51fd\u6570\u7b49\u65b9\u6cd5\uff0c\u80fd\u591f\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u4e0b\u7684\u9700\u6c42\u3002<\/strong> \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u68c0\u6d4b\u7684\u51c6\u786e\u6027\u548c\u6548\u7387\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u66f4\u590d\u6742\u7684\u6587\u672c\u7c7b\u578b\u68c0\u6d4b\uff0c\u53ef\u4ee5\u8003\u8651\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\uff0c\u751a\u81f3\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u6280\u672f\u8fdb\u884c\u66f4\u6df1\u5165\u7684\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u4e2d\u6587\u5b57\u7b26\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u3002\u901a\u8fc7<code>re<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\uff0c\u5229\u7528Unicode\u8303\u56f4\u6765\u8bc6\u522b\u4e2d\u6587\u5b57\u7b26\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\n\ndef contains_chinese(text):\n    pattern = re.compile(&#39;[\\u4e00-\\u9fa5]&#39;)\n    return bool(pattern.search(text))\n\n# \u793a\u4f8b\u7528\u6cd5\nprint(contains_chinese(&quot;Hello, \u4f60\u597d&quot;))  # \u8f93\u51fa: True\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u4e2d\u6587\u68c0\u6d4b\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u4e2d\u6587\u8fd8\u662f\u5b57\u6bcd\u7684\u6bd4\u4f8b\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u7edf\u8ba1\u4e2d\u6587\u5b57\u7b26\u548c\u5b57\u6bcd\u7684\u6570\u91cf\uff0c\u4ece\u800c\u8ba1\u7b97\u6bd4\u4f8b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">def calculate_ratio(text):\n    chinese_count = sum(1 for char in text if &#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fa5&#39;)\n    letter_count = sum(1 for char in text if char.isalpha() and not (&#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fa5&#39;))\n    \n    total_count = chinese_count + letter_count\n    if total_count == 0:\n        return &quot;\u6ca1\u6709\u4e2d\u6587\u6216\u5b57\u6bcd&quot;\n    \n    return {\n        &quot;\u4e2d\u6587\u6bd4\u4f8b&quot;: chinese_count \/ total_count,\n        &quot;\u5b57\u6bcd\u6bd4\u4f8b&quot;: letter_count \/ total_count\n    }\n\n# \u793a\u4f8b\u7528\u6cd5\nprint(calculate_ratio(&quot;Hello, \u4f60\u597d&quot;))  # \u8f93\u51fa: {&#39;\u4e2d\u6587\u6bd4\u4f8b&#39;: 0.5, &#39;\u5b57\u6bcd\u6bd4\u4f8b&#39;: 0.5}\n<\/code><\/pre>\n<p>\u8be5\u65b9\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u4e86\u89e3\u5b57\u7b26\u4e32\u7684\u8bed\u8a00\u7ec4\u6210\u3002<\/p>\n<p><strong>Python\u4e2d\u662f\u5426\u6709\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u7528\u6765\u68c0\u6d4b\u6587\u5b57\u7c7b\u578b\uff1f<\/strong><br \/>\u786e\u5b9e\u6709\u4e00\u4e9b\u73b0\u6210\u7684\u5e93\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u68c0\u6d4b\u6587\u672c\u7684\u8bed\u8a00\u7c7b\u578b\u3002\u4f8b\u5982\uff0c<code>langdetect<\/code>\u5e93\u53ef\u4ee5\u7528\u6765\u8bc6\u522b\u5b57\u7b26\u4e32\u7684\u8bed\u8a00\u3002\u7528\u6237\u53ea\u9700\u5b89\u88c5\u8be5\u5e93\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u5373\u53ef\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5\u548c\u4f7f\u7528\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install langdetect\n<\/code><\/pre>\n<pre><code class=\"language-python\">from langdetect import detect\n\ntext = &quot;Hello, \u4f60\u597d&quot;\nlanguage = detect(text)\nprint(f&quot;\u68c0\u6d4b\u5230\u7684\u8bed\u8a00\u662f: {language}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u8bc6\u522b\u4e2d\u6587\u548c\u5b57\u6bcd\uff0c\u8fd8\u80fd\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u68c0\u6d4b\uff0c\u9002\u5408\u9700\u8981\u591a\u8bed\u8a00\u5904\u7406\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u68c0\u6d4b\u4e00\u6bb5\u6587\u5b57\u662f\u4e2d\u6587\u8fd8\u662f\u5b57\u6bcd\u3002 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u7f16\u7801\u8303\u56f4\u4ee5\u53ca\u5e93\u51fd\u6570\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1046476,"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\/1046459"}],"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=1046459"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046459\/revisions"}],"predecessor-version":[{"id":1046480,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046459\/revisions\/1046480"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1046476"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1046459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1046459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1046459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}