{"id":1100234,"date":"2025-01-08T15:38:04","date_gmt":"2025-01-08T07:38:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1100234.html"},"modified":"2025-01-08T15:38:06","modified_gmt":"2025-01-08T07:38:06","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8python%e7%bb%9f%e8%ae%a1%e8%8b%b1%e6%96%87%e6%96%87%e7%ab%a0%e8%af%8d%e9%a2%91-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1100234.html","title":{"rendered":"\u5982\u4f55\u5229\u7528python\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u8bcd\u9891"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063555\/c025f1f1-d9fd-4a5a-b117-cedf8a25399f.webp\" alt=\"\u5982\u4f55\u5229\u7528python\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u8bcd\u9891\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5229\u7528python\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u8bcd\u9891<\/strong><\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Python\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u8bcd\u9891\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u7528\u7684\u5305\u62ec\uff1a\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b\u3001\u4f7f\u7528nltk\u5e93\u8fdb\u884c\u6587\u672c\u5904\u7406\u3001\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u6e05\u6d17\u3002<\/strong>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5229\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u7684\u8bcd\u9891\uff0c\u5e76\u7ed9\u51fa\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b<\/h3>\n<\/p>\n<p><p>collections\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u6a21\u5757\uff0c\u5176\u4e2d\u7684Counter\u7c7b\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u7528\u6765\u7edf\u8ba1\u8bcd\u9891\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        text = file.read()<\/p>\n<p>    return text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6e05\u6d17\u6587\u672c\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def clean_text(text):<\/p>\n<p>    text = text.lower()<\/p>\n<p>    text = re.sub(r&#39;[^a-z\\s]&#39;, &#39;&#39;, text)<\/p>\n<p>    return text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u7edf\u8ba1\u8bcd\u9891<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def count_words(text):<\/p>\n<p>    words = text.split()<\/p>\n<p>    word_counts = Counter(words)<\/p>\n<p>    return word_counts<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u5c06\u7ed3\u679c\u6253\u5370\u51fa\u6765<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;<\/p>\n<p>text = read_file(file_path)<\/p>\n<p>cleaned_text = clean_text(text)<\/p>\n<p>word_counts = count_words(cleaned_text)<\/p>\n<p>for word, count in word_counts.items():<\/p>\n<p>    print(f&#39;{word}: {count}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528nltk\u5e93\u8fdb\u884c\u6587\u672c\u5904\u7406<\/h3>\n<\/p>\n<p><p>nltk\uff08Natural Language Toolkit\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u6587\u672c\u6e05\u6d17\u3001\u5206\u8bcd\u3001\u8bcd\u9891\u7edf\u8ba1\u7b49\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.corpus import stopwords<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4e0b\u8f7d\u5fc5\u8981\u7684\u8d44\u6e90<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">nltk.download(&#39;punkt&#39;)<\/p>\n<p>nltk.download(&#39;stopwords&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;<\/p>\n<p>with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    text = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u6e05\u6d17\u6587\u672c\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = text.lower()<\/p>\n<p>text = re.sub(r&#39;[^a-z\\s]&#39;, &#39;&#39;, text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u5206\u8bcd\u5e76\u53bb\u9664\u505c\u7528\u8bcd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">stop_words = set(stopwords.words(&#39;english&#39;))<\/p>\n<p>words = word_tokenize(text)<\/p>\n<p>filtered_words = [word for word in words if word not in stop_words]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u7edf\u8ba1\u8bcd\u9891<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">word_counts = Counter(filtered_words)<\/p>\n<p>for word, count in word_counts.items():<\/p>\n<p>    print(f&#39;{word}: {count}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5e93\uff0c\u53ef\u4ee5\u7528\u6765\u5904\u7406\u6587\u672c\u6570\u636e\u5e76\u8fdb\u884c\u8bcd\u9891\u7edf\u8ba1\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import re<\/p>\n<p>from collections import Counter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;<\/p>\n<p>with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    text = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6e05\u6d17\u6587\u672c\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = text.lower()<\/p>\n<p>text = re.sub(r&#39;[^a-z\\s]&#39;, &#39;&#39;, text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5206\u8bcd<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">words = text.split()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5. \u7edf\u8ba1\u8bcd\u9891\u5e76\u521b\u5efaDataFrame<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">word_counts = Counter(words)<\/p>\n<p>df = pd.DataFrame(word_counts.items(), columns=[&#39;Word&#39;, &#39;Frequency&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6. \u6392\u5e8f\u5e76\u6253\u5370\u7ed3\u679c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">df = df.sort_values(by=&#39;Frequency&#39;, ascending=False)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u6587\u672c\u6570\u636e\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u6e05\u6d17\u6587\u672c\u5e76\u8fdb\u884c\u5206\u8bcd\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = &#39;example.txt&#39;<\/p>\n<p>with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>    text = file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6e05\u6d17\u6587\u672c\u6570\u636e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">text = text.lower()<\/p>\n<p>text = re.sub(r&#39;[^a-z\\s]&#39;, &#39;&#39;, text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u5206\u8bcd\u5e76\u7edf\u8ba1\u8bcd\u9891<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">words = text.split()<\/p>\n<p>word_counts = Counter(words)<\/p>\n<p>for word, count in word_counts.items():<\/p>\n<p>    print(f&#39;{word}: {count}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u7efc\u5408\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u66f4\u5b8c\u6574\u7684\u8bcd\u9891\u7edf\u8ba1\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from collections import Counter<\/p>\n<p>import pandas as pd<\/p>\n<p>import nltk<\/p>\n<p>from nltk.corpus import stopwords<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<h2><strong>\u4e0b\u8f7d\u5fc5\u8981\u7684\u8d44\u6e90<\/strong><\/h2>\n<p>nltk.download(&#39;punkt&#39;)<\/p>\n<p>nltk.download(&#39;stopwords&#39;)<\/p>\n<p>def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        text = file.read()<\/p>\n<p>    return text<\/p>\n<p>def clean_text(text):<\/p>\n<p>    text = text.lower()<\/p>\n<p>    text = re.sub(r&#39;[^a-z\\s]&#39;, &#39;&#39;, text)<\/p>\n<p>    return text<\/p>\n<p>def remove_stop_words(text):<\/p>\n<p>    stop_words = set(stopwords.words(&#39;english&#39;))<\/p>\n<p>    words = word_tokenize(text)<\/p>\n<p>    filtered_words = [word for word in words if word not in stop_words]<\/p>\n<p>    return filtered_words<\/p>\n<p>def count_words(words):<\/p>\n<p>    word_counts = Counter(words)<\/p>\n<p>    return word_counts<\/p>\n<p>def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n(file_path):<\/p>\n<p>    text = read_file(file_path)<\/p>\n<p>    cleaned_text = clean_text(text)<\/p>\n<p>    filtered_words = remove_stop_words(cleaned_text)<\/p>\n<p>    word_counts = count_words(filtered_words)<\/p>\n<p>    df = pd.DataFrame(word_counts.items(), columns=[&#39;Word&#39;, &#39;Frequency&#39;])<\/p>\n<p>    df = df.sort_values(by=&#39;Frequency&#39;, ascending=False)<\/p>\n<p>    print(df)<\/p>\n<p>file_path = &#39;example.txt&#39;<\/p>\n<p>main(file_path)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u7684\u7efc\u5408\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Python\u7684\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u6765\u8bfb\u53d6\u6587\u672c\u3001\u6e05\u6d17\u6587\u672c\u3001\u5206\u8bcd\u3001\u53bb\u9664\u505c\u7528\u8bcd\u548c\u7edf\u8ba1\u8bcd\u9891\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5206\u6790\u548c\u5904\u7406\u6587\u672c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6269\u5c55\uff1a\u53ef\u89c6\u5316\u8bcd\u9891\u7edf\u8ba1\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u5c55\u793a\u8bcd\u9891\u7edf\u8ba1\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528matplotlib\u5e93\u5c06\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u5fc5\u8981\u7684\u6a21\u5757<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u7ed8\u5236\u8bcd\u9891\u7edf\u8ba1\u7ed3\u679c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def plot_word_counts(word_counts):<\/p>\n<p>    top_words = word_counts.most_common(10)<\/p>\n<p>    words, counts = zip(*top_words)<\/p>\n<p>    plt.figure(figsize=(10, 6))<\/p>\n<p>    plt.bar(words, counts)<\/p>\n<p>    plt.xlabel(&#39;Words&#39;)<\/p>\n<p>    plt.ylabel(&#39;Frequency&#39;)<\/p>\n<p>    plt.title(&#39;Top 10 Words by Frequency&#39;)<\/p>\n<p>    plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5728main\u51fd\u6570\u4e2d\u8c03\u7528\u7ed8\u5236\u51fd\u6570<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">def main(file_path):<\/p>\n<p>    text = read_file(file_path)<\/p>\n<p>    cleaned_text = clean_text(text)<\/p>\n<p>    filtered_words = remove_stop_words(cleaned_text)<\/p>\n<p>    word_counts = count_words(filtered_words)<\/p>\n<p>    df = pd.DataFrame(word_counts.items(), columns=[&#39;Word&#39;, &#39;Frequency&#39;])<\/p>\n<p>    df = df.sort_values(by=&#39;Frequency&#39;, ascending=False)<\/p>\n<p>    print(df)<\/p>\n<p>    plot_word_counts(word_counts)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u76f4\u89c2\u5730\u5c55\u793a\u8bcd\u9891\u7edf\u8ba1\u7ed3\u679c\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6587\u672c\u6570\u636e\u7684\u7279\u70b9\u548c\u89c4\u5f8b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528Python\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u7684\u8bcd\u9891\u3002\u6211\u4eec\u63a2\u8ba8\u4e86\u4f7f\u7528collections\u6a21\u5757\u7684Counter\u7c7b\u3001nltk\u5e93\u3001pandas\u5e93\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6587\u672c\u5904\u7406\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed9\u51fa\u4e86\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd8\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528matplotlib\u5e93\u5c06\u8bcd\u9891\u7edf\u8ba1\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5206\u6790\u548c\u5904\u7406\u6587\u672c\u6570\u636e\uff0c\u63ed\u793a\u6587\u672c\u6570\u636e\u4e2d\u7684\u9690\u85cf\u4fe1\u606f\u548c\u89c4\u5f8b\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u5982\u679c\u4f60\u6709\u4efb\u4f55\u95ee\u9898\u6216\u5efa\u8bae\uff0c\u6b22\u8fce\u5728\u4e0b\u65b9\u7559\u8a00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6765\u8ba1\u7b97\u4e00\u7bc7\u82f1\u6587\u6587\u7ae0\u4e2d\u5404\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u9891\u7387\uff1f<\/strong><br \/>\u4f7f\u7528Python\u8ba1\u7b97\u8bcd\u9891\u901a\u5e38\u9700\u8981\u501f\u52a9\u4e8e\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u5904\u7406\u529f\u80fd\u548c\u4e00\u4e9b\u989d\u5916\u7684\u5e93\uff0c\u5982collections\u4e2d\u7684Counter\u3002\u9996\u5148\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u6216\u76f4\u63a5\u8f93\u5165\u5b57\u7b26\u4e32\u6765\u83b7\u53d6\u6587\u7ae0\u5185\u5bb9\u3002\u63a5\u7740\uff0c\u5229\u7528split()\u65b9\u6cd5\u5c06\u6587\u672c\u62c6\u5206\u4e3a\u5355\u8bcd\uff0c\u6700\u540e\u4f7f\u7528Counter\u6765\u7edf\u8ba1\u6bcf\u4e2a\u5355\u8bcd\u7684\u9891\u7387\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from collections import Counter\nimport re\n\n# \u8bfb\u53d6\u6587\u4ef6\nwith open(&#39;article.txt&#39;, &#39;r&#39;) as file:\n    text = file.read().lower()  # \u8f6c\u4e3a\u5c0f\u5199\u4ee5\u907f\u514d\u91cd\u590d\n\n# \u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u53bb\u9664\u6807\u70b9\u7b26\u53f7\nwords = re.findall(r&#39;\\b\\w+\\b&#39;, text)\n\n# \u7edf\u8ba1\u8bcd\u9891\nword_counts = Counter(words)\n\n# \u8f93\u51fa\u7ed3\u679c\nfor word, count in word_counts.items():\n    print(f&#39;{word}: {count}&#39;)\n<\/code><\/pre>\n<p><strong>\u5728\u7edf\u8ba1\u8bcd\u9891\u65f6\uff0c\u5982\u4f55\u5904\u7406\u5e38\u89c1\u7684\u505c\u7528\u8bcd\uff1f<\/strong><br \/>\u505c\u7528\u8bcd\u662f\u6307\u5728\u6587\u672c\u5206\u6790\u4e2d\u5e38\u5e38\u88ab\u5ffd\u7565\u7684\u5355\u8bcd\uff0c\u5982\u201cthe\u201d\u3001\u201cis\u201d\u3001\u201cin\u201d\u7b49\u3002\u4e3a\u4e86\u63d0\u9ad8\u7edf\u8ba1\u7ed3\u679c\u7684\u76f8\u5173\u6027\uff0c\u53ef\u4ee5\u5728\u7edf\u8ba1\u524d\u5c06\u8fd9\u4e9b\u8bcd\u4ece\u6587\u672c\u4e2d\u79fb\u9664\u3002\u53ef\u4ee5\u4f7f\u7528NLTK\u5e93\u63d0\u4f9b\u7684\u505c\u7528\u8bcd\u5217\u8868\uff0c\u6216\u8005\u81ea\u5b9a\u4e49\u4e00\u4e2a\u505c\u7528\u8bcd\u5217\u8868\u3002\u793a\u4f8b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728\u7edf\u8ba1\u524d\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u8bcd\uff1a  <\/p>\n<pre><code class=\"language-python\">from nltk.corpus import stopwords\n\nstop_words = set(stopwords.words(&#39;english&#39;))\nfiltered_words = [word for word in words if word not in stop_words]\nword_counts = Counter(filtered_words)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5c06\u8bcd\u9891\u7edf\u8ba1\u7ed3\u679c\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u53ef\u89c6\u5316\u8bcd\u9891\u7edf\u8ba1\u7ed3\u679c\u80fd\u591f\u5e2e\u52a9\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216WordCloud\u5e93\u6765\u5b9e\u73b0\u3002Matplotlib\u9002\u5408\u7ed8\u5236\u6761\u5f62\u56fe\uff0c\u800cWordCloud\u53ef\u4ee5\u751f\u6210\u8bcd\u4e91\u56fe\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528WordCloud\u5e93\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">from wordcloud import WordCloud\nimport matplotlib.pyplot as plt\n\nwordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;).generate_from_frequencies(word_counts)\n\nplt.figure(figsize=(10, 5))\nplt.imshow(wordcloud, interpolation=&#39;bilinear&#39;)\nplt.axis(&#39;off&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u60a8\u53ef\u4ee5\u5c06\u8bcd\u9891\u6570\u636e\u4ee5\u66f4\u52a0\u751f\u52a8\u7684\u5f62\u5f0f\u5448\u73b0\u51fa\u6765\uff0c\u589e\u5f3a\u5bf9\u6587\u672c\u5185\u5bb9\u7684\u7406\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5229\u7528python\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u8bcd\u9891 \u4f7f\u7528Python\u7edf\u8ba1\u82f1\u6587\u6587\u7ae0\u8bcd\u9891\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u7528\u7684\u5305\u62ec\uff1a\u4f7f\u7528colle [&hellip;]","protected":false},"author":3,"featured_media":1100239,"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\/1100234"}],"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=1100234"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100234\/revisions"}],"predecessor-version":[{"id":1100243,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1100234\/revisions\/1100243"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100239"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1100234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1100234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1100234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}