{"id":1080628,"date":"2025-01-08T12:31:52","date_gmt":"2025-01-08T04:31:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1080628.html"},"modified":"2025-01-08T12:31:54","modified_gmt":"2025-01-08T04:31:54","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%bb%9f%e8%ae%a1%e5%9b%9b%e7%ba%a7%e8%af%8d%e6%b1%87-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1080628.html","title":{"rendered":"\u5982\u4f55\u7528python\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24183049\/b5e2f730-518b-4274-9c17-cd086339b3b6.webp\" alt=\"\u5982\u4f55\u7528python\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528python\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u6587\u672c\u9884\u5904\u7406\u3001\u5206\u8bcd\u6280\u672f\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u8ba1\u6570\u5668\u7b49\u5de5\u5177\u3002\u9996\u5148\uff0c\u51c6\u5907\u4e00\u4e2a\u5305\u542b\u56db\u7ea7\u8bcd\u6c47\u7684\u8bcd\u5e93\uff0c\u7136\u540e\u8bfb\u53d6\u9700\u8981\u7edf\u8ba1\u7684\u6587\u672c\uff0c\u5bf9\u6587\u672c\u8fdb\u884c\u9884\u5904\u7406\u548c\u5206\u8bcd\uff0c\u6700\u540e\u7edf\u8ba1\u8bcd\u6c47\u7684\u9891\u7387\u3002<strong>\u8bfb\u53d6\u8bcd\u5e93\u3001\u9884\u5904\u7406\u6587\u672c\u3001\u5206\u8bcd\u3001\u7edf\u8ba1\u9891\u7387<\/strong>\u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u7684\u5173\u952e\u6b65\u9aa4\u3002\u4e0b\u9762\u5c06\u91cd\u70b9\u4ecb\u7ecd\u5982\u4f55\u8fdb\u884c\u6587\u672c\u9884\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u8bfb\u53d6\u8bcd\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u56db\u7ea7\u8bcd\u6c47\u7684\u8bcd\u5e93\uff0c\u8fd9\u53ef\u4ee5\u662f\u4e00\u4e2a\u5305\u542b\u6240\u6709\u56db\u7ea7\u8bcd\u6c47\u7684\u6587\u672c\u6587\u4ef6\u6216\u6570\u636e\u5e93\u3002\u6211\u4eec\u53ef\u4ee5\u8bfb\u53d6\u8fd9\u4e2a\u6587\u4ef6\uff0c\u5e76\u5c06\u8bcd\u6c47\u5b58\u50a8\u5728\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u4f8b\u5982\u5217\u8868\u6216\u96c6\u5408\uff0c\u4ee5\u4fbf\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def load_word_list(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        word_list = [line.strip() for line in file.readlines()]<\/p>\n<p>    return word_list<\/p>\n<p>cet4_words = load_word_list(&#39;cet4_word_list.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6\u6587\u672c\u548c\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u8bfb\u53d6\u9700\u8981\u7edf\u8ba1\u7684\u6587\u672c\uff0c\u8fdb\u884c\u9884\u5904\u7406\u3002\u8fd9\u5305\u62ec\u53bb\u9664\u6807\u70b9\u7b26\u53f7\u3001\u8f6c\u6362\u4e3a\u5c0f\u5199\u7b49\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u7edf\u8ba1\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def preprocess_text(text):<\/p>\n<p>    # \u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u5c0f\u5199<\/p>\n<p>    text = text.lower()<\/p>\n<p>    # \u53bb\u9664\u6807\u70b9\u7b26\u53f7<\/p>\n<p>    text = re.sub(r&#39;[^\\w\\s]&#39;, &#39;&#39;, text)<\/p>\n<p>    return text<\/p>\n<p>with open(&#39;sample_text.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    text = file.read()<\/p>\n<p>preprocessed_text = preprocess_text(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5206\u8bcd<\/h3>\n<\/p>\n<p><p>\u5206\u8bcd\u662f\u5c06\u6587\u672c\u62c6\u5206\u6210\u5355\u4e2a\u8bcd\u6c47\u7684\u8fc7\u7a0b\u3002\u5728\u82f1\u6587\u5904\u7406\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684\u7a7a\u683c\u5206\u8bcd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def tokenize(text):<\/p>\n<p>    return text.split()<\/p>\n<p>tokens = tokenize(preprocessed_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u7edf\u8ba1\u8bcd\u9891<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8ba1\u6570\u5668\u7edf\u8ba1\u8bcd\u6c47\u7684\u9891\u7387\uff0c\u5e76\u4e0e\u56db\u7ea7\u8bcd\u6c47\u8868\u8fdb\u884c\u6bd4\u8f83\uff0c\u4ee5\u7edf\u8ba1\u51fa\u56db\u7ea7\u8bcd\u6c47\u5728\u6587\u672c\u4e2d\u7684\u51fa\u73b0\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def count_cet4_words(tokens, cet4_words):<\/p>\n<p>    token_counter = Counter(tokens)<\/p>\n<p>    cet4_word_count = {word: token_counter[word] for word in cet4_words if word in token_counter}<\/p>\n<p>    return cet4_word_count<\/p>\n<p>cet4_word_count = count_cet4_words(tokens, cet4_words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8f93\u51fa\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u8f93\u51fa\u56db\u7ea7\u8bcd\u6c47\u53ca\u5176\u51fa\u73b0\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">for word, count in cet4_word_count.items():<\/p>\n<p>    print(f&#39;{word}: {count}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u5f3a\u5927\u7684\u5206\u8bcd\u548c\u5904\u7406\u80fd\u529b\u3002\u53ef\u4ee5\u4f7f\u7528\u5982nltk\u3001spaCy\u7b49\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u6765\u589e\u5f3a\u6587\u672c\u5904\u7406\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import spacy<\/p>\n<p>nlp = spacy.load(&#39;en_core_web_sm&#39;)<\/p>\n<p>def advanced_tokenize(text):<\/p>\n<p>    doc = nlp(text)<\/p>\n<p>    return [token.text for token in doc]<\/p>\n<p>tokens = advanced_tokenize(preprocessed_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u4e0d\u540c\u5f62\u5f0f\u7684\u8bcd\u6c47<\/h4>\n<\/p>\n<p><p>\u8003\u8651\u5230\u56db\u7ea7\u8bcd\u6c47\u7684\u4e0d\u540c\u5f62\u5f0f\uff0c\u5982\u590d\u6570\u3001\u8fc7\u53bb\u5f0f\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bcd\u5e72\u63d0\u53d6\u548c\u8bcd\u5f62\u8fd8\u539f\u6280\u672f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.stem import PorterStemmer<\/p>\n<p>stemmer = PorterStemmer()<\/p>\n<p>def stem_tokens(tokens):<\/p>\n<p>    return [stemmer.stem(token) for token in tokens]<\/p>\n<p>stemmed_tokens = stem_tokens(tokens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u6587\u672c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u65b9\u6cd5\uff0c\u5982\u751f\u6210\u5668\u548c\u591a\u7ebf\u7a0b\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_large_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        for line in file:<\/p>\n<p>            yield line<\/p>\n<p>def process_large_file(file_path, cet4_words):<\/p>\n<p>    cet4_word_count = Counter()<\/p>\n<p>    for line in read_large_file(file_path):<\/p>\n<p>        preprocessed_line = preprocess_text(line)<\/p>\n<p>        tokens = tokenize(preprocessed_line)<\/p>\n<p>        cet4_word_count.update(count_cet4_words(tokens, cet4_words))<\/p>\n<p>    return cet4_word_count<\/p>\n<p>cet4_word_count = process_large_file(&#39;large_text_file.txt&#39;, cet4_words)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u7ed3\u679c\u5206\u6790\u4e0e\u53ef\u89c6\u5316<\/h3>\n<\/p>\n<p><p>\u5c06\u7edf\u8ba1\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u56db\u7ea7\u8bcd\u6c47\u7684\u5206\u5e03\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>def plot_word_frequency(word_count):<\/p>\n<p>    words = list(word_count.keys())<\/p>\n<p>    counts = list(word_count.values())<\/p>\n<p>    plt.figure(figsize=(10, 5))<\/p>\n<p>    plt.bar(words, counts, color=&#39;skyblue&#39;)<\/p>\n<p>    plt.xlabel(&#39;Words&#39;)<\/p>\n<p>    plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>    plt.title(&#39;CET-4 Word Frequency&#39;)<\/p>\n<p>    plt.xticks(rotation=90)<\/p>\n<p>    plt.show()<\/p>\n<p>plot_word_frequency(cet4_word_count)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u9ad8\u6548\u5730\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47\u5728\u6587\u672c\u4e2d\u7684\u51fa\u73b0\u9891\u7387\u3002\u5173\u952e\u6b65\u9aa4\u5305\u62ec\u8bfb\u53d6\u8bcd\u5e93\u3001\u9884\u5904\u7406\u6587\u672c\u3001\u5206\u8bcd\u3001\u7edf\u8ba1\u8bcd\u9891\u7b49\u3002\u8fdb\u4e00\u6b65\u7684\u4f18\u5316\u548c\u6269\u5c55\u53ef\u4ee5\u63d0\u9ad8\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u6548\u7387\uff0c\u5e76\u589e\u5f3a\u6587\u672c\u5904\u7406\u80fd\u529b\u3002\u901a\u8fc7\u7ed3\u679c\u5206\u6790\u4e0e\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u7edf\u8ba1\u7ed3\u679c\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u5728\u4f7f\u7528Python\u8fdb\u884c\u56db\u7ea7\u8bcd\u6c47\u7edf\u8ba1\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u6765\u8bc6\u522b\u548c\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47\u7684\u9891\u7387\uff1f<\/strong><br \/>\u4f7f\u7528Python\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47\u7684\u9891\u7387\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u5305\u542b\u8fd9\u4e9b\u8bcd\u6c47\u7684\u6587\u4ef6\uff0c\u4f7f\u7528\u5b57\u5178\u6216Counter\u7c7b\u6765\u8bb0\u5f55\u6bcf\u4e2a\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\u3002\u501f\u52a9\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u53d6\u5355\u8bcd\uff0c\u5e76\u4f7f\u7528\u7b80\u5355\u7684\u5faa\u73af\u6765\u8ba1\u7b97\u5b83\u4eec\u7684\u9891\u7387\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f7f\u5f97\u5206\u6790\u8fc7\u7a0b\u9ad8\u6548\u4e14\u6613\u4e8e\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9bPython\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u5904\u7406\u56db\u7ea7\u8bcd\u6c47\u7684\u7edf\u8ba1\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u56db\u7ea7\u8bcd\u6c47\u7edf\u8ba1\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5f3a\u5927\u7684Python\u5e93\uff0c\u4f8b\u5982Pandas\u7528\u4e8e\u6570\u636e\u5904\u7406\u3001NumPy\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\u3001\u4ee5\u53caNLTK\u6216spaCy\u7b49\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u65b9\u4fbf\u5730\u6e05\u6d17\u6570\u636e\u3001\u5206\u6790\u8bcd\u6c47\uff0c\u5e76\u8fdb\u884c\u53ef\u89c6\u5316\u5c55\u793a\u3002<\/p>\n<p><strong>\u5982\u679c\u6211\u60f3\u5c06\u56db\u7ea7\u8bcd\u6c47\u7edf\u8ba1\u7ed3\u679c\u53ef\u89c6\u5316\uff0c\u6709\u54ea\u4e9b\u63a8\u8350\u7684\u56fe\u8868\u5e93\uff1f<\/strong><br \/>\u4e3a\u4e86\u5c06\u56db\u7ea7\u8bcd\u6c47\u7edf\u8ba1\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u548cSeaborn\u7b49\u56fe\u8868\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56fe\u8868\u7c7b\u578b\uff0c\u80fd\u591f\u5e2e\u52a9\u60a8\u76f4\u89c2\u5730\u5c55\u793a\u8bcd\u6c47\u9891\u7387\u5206\u5e03\u3001\u8bcd\u4e91\u7b49\u4fe1\u606f\uff0c\u4f7f\u5f97\u6570\u636e\u5206\u6790\u66f4\u52a0\u751f\u52a8\u6613\u61c2\u3002\u901a\u8fc7\u56fe\u5f62\u5316\u8868\u73b0\uff0c\u60a8\u53ef\u4ee5\u66f4\u6e05\u6670\u5730\u8bc6\u522b\u51fa\u9ad8\u9891\u8bcd\u6c47\u548c\u4f4e\u9891\u8bcd\u6c47\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528python\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47 \u8981\u4f7f\u7528Python\u7edf\u8ba1\u56db\u7ea7\u8bcd\u6c47\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u4f7f\u7528\u6587\u672c\u9884\u5904\u7406\u3001\u5206\u8bcd\u6280\u672f\u3001\u6b63 [&hellip;]","protected":false},"author":3,"featured_media":1080633,"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\/1080628"}],"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=1080628"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080628\/revisions"}],"predecessor-version":[{"id":1080636,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1080628\/revisions\/1080636"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1080633"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1080628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1080628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1080628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}