{"id":1152680,"date":"2025-01-13T17:28:57","date_gmt":"2025-01-13T09:28:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152680.html"},"modified":"2025-01-13T17:29:00","modified_gmt":"2025-01-13T09:29:00","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%88%86%e6%9e%90%e6%96%87%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152680.html","title":{"rendered":"\u5982\u4f55\u7528python\u5206\u6790\u6587\u672c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182606\/3938dbb0-dc9f-4076-9554-102c5f2aab8e.webp\" alt=\"\u5982\u4f55\u7528python\u5206\u6790\u6587\u672c\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u5206\u6790\u6587\u672c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u5e93\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u6587\u672c\u6e05\u6d17\u5de5\u5177\u3001\u8bcd\u9891\u5206\u6790\u5de5\u5177\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528NLP\u5e93\uff08\u5982NLTK\u6216spaCy\uff09<\/strong>\u662f\u6700\u4e3a\u5e38\u89c1\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NLTK\u8fdb\u884c\u6587\u672c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001NLTK\u5e93\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>NLTK\uff08Natural Language Toolkit\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u4eba\u7c7b\u8bed\u8a00\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5de5\u5177\u548c\u8d44\u6e90\uff0c\u652f\u6301\u6587\u672c\u5904\u7406\u548cNLP\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5NLTK<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5NLTK\u5e93\u975e\u5e38\u7b80\u5355\uff0c\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install nltk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u52a0\u8f7d\u548c\u4e0b\u8f7d\u5fc5\u8981\u7684\u8d44\u6e90<\/h4>\n<\/p>\n<p><p>NLTK\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u9884\u8bad\u7ec3\u6570\u636e\u548c\u5de5\u5177\u5305\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e0b\u8f7d\u4e00\u4e9b\u57fa\u7840\u6570\u636e\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>nltk.download(&#39;punkt&#39;)  # Tokenizer models<\/p>\n<p>nltk.download(&#39;stopwords&#39;)  # Stop words<\/p>\n<p>nltk.download(&#39;wordnet&#39;)  # WordNet<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6587\u672c\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6587\u672c\u9884\u5904\u7406\u662fNLP\u4e2d\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u4e3b\u8981\u5305\u62ec\u6587\u672c\u6e05\u6d17\u3001\u5206\u8bcd\u3001\u53bb\u9664\u505c\u7528\u8bcd\u3001\u8bcd\u5f62\u8fd8\u539f\u548c\u8bcd\u5e72\u63d0\u53d6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u6587\u672c\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u6587\u672c\u6e05\u6d17\u5305\u62ec\u53bb\u9664\u6807\u70b9\u7b26\u53f7\u3001\u6570\u5b57\u3001HTML\u6807\u7b7e\u7b49\u65e0\u5173\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def clean_text(text):<\/p>\n<p>    text = re.sub(r&#39;&lt;.*?&gt;&#39;, &#39;&#39;, text)  # \u53bb\u9664HTML\u6807\u7b7e<\/p>\n<p>    text = re.sub(r&#39;[^\\w\\s]&#39;, &#39;&#39;, text)  # \u53bb\u9664\u6807\u70b9\u7b26\u53f7<\/p>\n<p>    text = re.sub(r&#39;\\d+&#39;, &#39;&#39;, text)  # \u53bb\u9664\u6570\u5b57<\/p>\n<p>    text = text.lower()  # \u8f6c\u6362\u4e3a\u5c0f\u5199<\/p>\n<p>    return text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u8bcd<\/h4>\n<\/p>\n<p><p>\u5206\u8bcd\u662f\u5c06\u6587\u672c\u62c6\u5206\u6210\u5355\u72ec\u7684\u5355\u8bcd\u6216\u8bcd\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.tokenize import word_tokenize<\/p>\n<p>def tokenize_text(text):<\/p>\n<p>    return word_tokenize(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u53bb\u9664\u505c\u7528\u8bcd<\/h4>\n<\/p>\n<p><p>\u505c\u7528\u8bcd\u662f\u6307\u4e00\u4e9b\u5728\u6587\u672c\u4e2d\u9891\u7e41\u51fa\u73b0\u4f46\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u7684\u8bcd\uff0c\u5982\u201cthe\u201d\u3001\u201cis\u201d\u7b49\u3002NLTK\u63d0\u4f9b\u4e86\u5e38\u89c1\u7684\u505c\u7528\u8bcd\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.corpus import stopwords<\/p>\n<p>def remove_stopwords(tokens):<\/p>\n<p>    stop_words = set(stopwords.words(&#39;english&#39;))<\/p>\n<p>    return [word for word in tokens if word not in stop_words]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u8bcd\u5f62\u8fd8\u539f\u548c\u8bcd\u5e72\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u8bcd\u5f62\u8fd8\u539f\uff08Lemmatization\uff09\u548c\u8bcd\u5e72\u63d0\u53d6\uff08Stemming\uff09\u662f\u5c06\u5355\u8bcd\u8fd8\u539f\u5230\u5176\u57fa\u672c\u5f62\u5f0f\u3002NLTK\u63d0\u4f9b\u4e86WordNetLemmatizer\u548cPorterStemmer\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.stem import WordNetLemmatizer, PorterStemmer<\/p>\n<p>def lemmatize_tokens(tokens):<\/p>\n<p>    lemmatizer = WordNetLemmatizer()<\/p>\n<p>    return [lemmatizer.lemmatize(token) for token in tokens]<\/p>\n<p>def stem_tokens(tokens):<\/p>\n<p>    stemmer = PorterStemmer()<\/p>\n<p>    return [stemmer.stem(token) for token in tokens]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8bcd\u9891\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u8bcd\u9891\u5206\u6790\u662f\u6587\u672c\u5206\u6790\u4e2d\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6587\u672c\u4e2d\u6700\u5e38\u89c1\u7684\u8bcd\u3002<\/p>\n<\/p>\n<p><h4>1. \u8ba1\u7b97\u8bcd\u9891<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>def get_word_frequencies(tokens):<\/p>\n<p>    return Counter(tokens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u53ef\u89c6\u5316\u8bcd\u9891<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528matplotlib\u6216wordcloud\u5e93\u6765\u53ef\u89c6\u5316\u8bcd\u9891\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>from wordcloud import WordCloud<\/p>\n<p>def plot_word_frequencies(word_freq):<\/p>\n<p>    wordcloud = WordCloud(width=800, height=400, background_color=&#39;white&#39;).generate_from_frequencies(word_freq)<\/p>\n<p>    plt.figure(figsize=(10, 5))<\/p>\n<p>    plt.imshow(wordcloud, interpolation=&#39;bilinear&#39;)<\/p>\n<p>    plt.axis(&#39;off&#39;)<\/p>\n<p>    plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u60c5\u611f\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u60c5\u611f\u5206\u6790\u662fNLP\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u5e94\u7528\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6587\u672c\u7684\u60c5\u611f\u503e\u5411\u3002NLTK\u63d0\u4f9b\u4e86VADER\u60c5\u611f\u5206\u6790\u5668\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.sentiment.vader import SentimentIntensityAnalyzer<\/p>\n<p>def analyze_sentiment(text):<\/p>\n<p>    sia = SentimentIntensityAnalyzer()<\/p>\n<p>    sentiment = sia.polarity_scores(text)<\/p>\n<p>    return sentiment<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4e3b\u9898\u5efa\u6a21<\/h3>\n<\/p>\n<p><p>\u4e3b\u9898\u5efa\u6a21\u662f\u4e00\u79cd\u4ece\u6587\u6863\u96c6\u4e2d\u63d0\u53d6\u4e3b\u9898\u7684\u65b9\u6cd5\u3002LDA\uff08Latent Dirichlet Allocation\uff09\u662f\u5e38\u7528\u7684\u4e3b\u9898\u5efa\u6a21\u65b9\u6cd5\u4e4b\u4e00\u3002\u53ef\u4ee5\u4f7f\u7528gensim\u5e93\u6765\u8fdb\u884cLDA\u4e3b\u9898\u5efa\u6a21\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gensim import corpora, models<\/p>\n<p>def topic_modeling(texts, num_topics=5):<\/p>\n<p>    dictionary = corpora.Dictionary(texts)<\/p>\n<p>    corpus = [dictionary.doc2bow(text) for text in texts]<\/p>\n<p>    lda_model = models.LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=15)<\/p>\n<p>    topics = lda_model.print_topics(num_words=4)<\/p>\n<p>    return topics<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5b8c\u6210\u6587\u672c\u7684\u9884\u5904\u7406\u3001\u8bcd\u9891\u5206\u6790\u3001\u60c5\u611f\u5206\u6790\u548c\u4e3b\u9898\u5efa\u6a21\u3002\u8fd9\u4e9b\u6b65\u9aa4\u662f\u6587\u672c\u5206\u6790\u4e2d\u7684\u57fa\u7840\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u6316\u6398\u6587\u672c\u4e2d\u7684\u4fe1\u606f\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u9002\u5408\u7684\u65b9\u6cd5\u548c\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p><strong>\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\u4f7f\u7528NLP\u5e93\u8fdb\u884c\u6587\u672c\u9884\u5904\u7406\u3001\u8bcd\u9891\u5206\u6790\u3001\u60c5\u611f\u5206\u6790\u548c\u4e3b\u9898\u5efa\u6a21<\/strong>\u3002\u6587\u672c\u5206\u6790\u4e0d\u4ec5\u9700\u8981\u638c\u63e1\u5404\u79cd\u5de5\u5177\u548c\u65b9\u6cd5\uff0c\u8fd8\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u8fdb\u884c\u6587\u672c\u5206\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u8fdb\u884c\u6587\u672c\u5206\u6790\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5f3a\u5927\u7684\u5e93\u548c\u5de5\u5177\uff0c\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u8fdb\u884c\u6587\u672c\u5206\u6790\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>NLTK<\/code>\uff08\u81ea\u7136\u8bed\u8a00\u5de5\u5177\u5305\uff09\u548c<code>spaCy<\/code>\u8fd9\u4e24\u4e2a\u5e93\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u5305\u62ec\u5206\u8bcd\u3001\u8bcd\u6027\u6807\u6ce8\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u7b49\u3002\u5b89\u88c5\u8fd9\u4e9b\u5e93\u975e\u5e38\u7b80\u5355\uff0c\u60a8\u53ea\u9700\u4f7f\u7528<code>pip install nltk<\/code>\u6216<code>pip install spacy<\/code>\u5373\u53ef\u3002\u63a5\u7740\uff0c\u60a8\u53ef\u4ee5\u52a0\u8f7d\u6570\u636e\u5e76\u5f00\u59cb\u8fdb\u884c\u57fa\u672c\u7684\u6587\u672c\u5904\u7406\uff0c\u4f8b\u5982\u6e05\u6d17\u6587\u672c\u3001\u53bb\u9664\u505c\u7528\u8bcd\u548c\u8bcd\u5e72\u63d0\u53d6\u3002<\/p>\n<p><strong>\u5728\u6587\u672c\u5206\u6790\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u975e\u7ed3\u6784\u5316\u6570\u636e\uff1f<\/strong><br \/>\u975e\u7ed3\u6784\u5316\u6570\u636e\u5728\u6587\u672c\u5206\u6790\u4e2d\u5f88\u5e38\u89c1\uff0c\u6bd4\u5982\u793e\u4ea4\u5a92\u4f53\u5e16\u5b50\u3001\u65b0\u95fb\u6587\u7ae0\u6216\u5ba2\u6237\u53cd\u9988\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u7c7b\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u63d0\u53d6\u7279\u5b9a\u4fe1\u606f\uff0c\u6216\u8005\u4f7f\u7528Python\u7684<code>pandas<\/code>\u5e93\u5c06\u6587\u672c\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u636e\u6846\u683c\u5f0f\uff0c\u4fbf\u4e8e\u5206\u6790\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u6587\u672c\u5411\u91cf\u5316\u65b9\u6cd5\u5982TF-IDF\u6216Word2Vec\uff0c\u53ef\u4ee5\u5c06\u975e\u7ed3\u6784\u5316\u6587\u672c\u8f6c\u5316\u4e3a\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f7f\u5176\u9002\u5408\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u53ef\u89c6\u5316\u6587\u672c\u5206\u6790\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u6587\u672c\u5206\u6790\u7684\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u53ef\u89c6\u5316\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u6570\u636e\u3002\u4f7f\u7528<code>matplotlib<\/code>\u6216<code>seaborn<\/code>\u5e93\u53ef\u4ee5\u7ed8\u5236\u8bcd\u9891\u56fe\u3001\u60c5\u611f\u5206\u6790\u7ed3\u679c\u7684\u997c\u56fe\u7b49\u3002\u6b64\u5916\uff0c<code>WordCloud<\/code>\u5e93\u5141\u8bb8\u7528\u6237\u751f\u6210\u8bcd\u4e91\u56fe\uff0c\u5c55\u793a\u6587\u672c\u4e2d\u6700\u5e38\u51fa\u73b0\u7684\u8bcd\u6c47\u3002\u53ef\u89c6\u5316\u4e0d\u4ec5\u53ef\u4ee5\u589e\u5f3a\u5206\u6790\u7684\u6548\u679c\uff0c\u8fd8\u80fd\u5e2e\u52a9\u53d1\u73b0\u6f5c\u5728\u7684\u6a21\u5f0f\u548c\u8d8b\u52bf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u5206\u6790\u6587\u672c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u5e93\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u6587\u672c\u6e05\u6d17\u5de5\u5177\u3001\u8bcd\u9891\u5206\u6790\u5de5\u5177\u7b49\u3002 \u5176\u4e2d [&hellip;]","protected":false},"author":3,"featured_media":1152691,"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\/1152680"}],"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=1152680"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152680\/revisions"}],"predecessor-version":[{"id":1152693,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152680\/revisions\/1152693"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152691"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}