{"id":1146397,"date":"2025-01-08T23:16:35","date_gmt":"2025-01-08T15:16:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1146397.html"},"modified":"2025-01-08T23:16:39","modified_gmt":"2025-01-08T15:16:39","slug":"python%e5%a6%82%e4%bd%95%e6%8f%90%e5%8f%96%e4%b8%80%e5%8f%a5%e8%af%9d%e4%b8%ad%e7%9a%84%e5%85%b3%e9%94%ae%e5%ad%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1146397.html","title":{"rendered":"python\u5982\u4f55\u63d0\u53d6\u4e00\u53e5\u8bdd\u4e2d\u7684\u5173\u952e\u5b57"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24182506\/47638ed8-ca94-457c-a265-c4024434c579.webp\" alt=\"python\u5982\u4f55\u63d0\u53d6\u4e00\u53e5\u8bdd\u4e2d\u7684\u5173\u952e\u5b57\" \/><\/p>\n<p><p> <strong>\u63d0\u53d6\u4e00\u53e5\u8bdd\u4e2d\u7684\u5173\u952e\u5b57\u53ef\u4ee5\u901a\u8fc7<\/strong>\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u6280\u672f\u3001\u4f7f\u7528Python\u7684\u5e93\u5982NLTK\u3001SpaCy\u3001gensim\u7b49\u3001\u4ee5\u53ca\u81ea\u5b9a\u4e49\u89c4\u5219<strong>\u3002\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528Python\u5e93\u8fdb\u884cNLP\u5904\u7406\u662f\u6700\u5e38\u89c1\u548c\u6709\u6548\u7684\u65b9\u6cd5\u3002<\/strong>\u6211\u4eec\u4ee5\u4f7f\u7528SpaCy\u5e93\u4e3a\u4f8b\u6765\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u63d0\u53d6\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6ePython\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548c\u76f8\u5173\u7684\u5e93\u3002\u4ee5\u4e0b\u662f\u5b89\u88c5\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install spacy<\/p>\n<p>python -m spacy download en_core_web_sm<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528SpaCy\u8fdb\u884c\u5173\u952e\u5b57\u63d0\u53d6<\/h3>\n<\/p>\n<p><h4>1\u3001\u52a0\u8f7d\u8bed\u8a00\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u52a0\u8f7dSpaCy\u7684\u8bed\u8a00\u6a21\u578b\u3002\u8fd9\u4e00\u6b65\u662f\u5fc5\u987b\u7684\uff0c\u56e0\u4e3a\u8bed\u8a00\u6a21\u578b\u5305\u542b\u4e86\u8bcd\u6c47\u3001\u8bcd\u6027\u6807\u6ce8\u3001\u4f9d\u5b58\u5173\u7cfb\u89e3\u6790\u7b49\u4fe1\u606f\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><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u52a0\u8f7d\u597d\u7684\u8bed\u8a00\u6a21\u578b\u5bf9\u6587\u672c\u8fdb\u884c\u5904\u7406\u3002SpaCy\u4f1a\u81ea\u52a8\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\u3001\u8bcd\u6027\u6807\u6ce8\u548c\u4f9d\u5b58\u5173\u7cfb\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Python is an interpreted, high-level, general-purpose programming language.&quot;<\/p>\n<p>doc = nlp(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u63d0\u53d6\u540d\u8bcd\u548c\u5f62\u5bb9\u8bcd<\/h4>\n<\/p>\n<p><p>\u5728\u8bb8\u591a\u60c5\u51b5\u4e0b\uff0c\u540d\u8bcd\u548c\u5f62\u5bb9\u8bcd\u662f\u5173\u952e\u5b57\u63d0\u53d6\u7684\u4e3b\u8981\u76ee\u6807\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u904d\u5386<code>doc<\/code>\u5bf9\u8c61\u6765\u63d0\u53d6\u8fd9\u4e9b\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keywords = [token.text for token in doc if token.pos_ in [&#39;NOUN&#39;, &#39;ADJ&#39;]]<\/p>\n<p>print(keywords)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528TF-IDF\u8fdb\u884c\u5173\u952e\u5b57\u63d0\u53d6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528SpaCy\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528TF-IDF\uff08Term Frequency-Inverse Document Frequency\uff09\u6765\u63d0\u53d6\u5173\u952e\u5b57\u3002TF-IDF\u662f\u4e00\u79cd\u7edf\u8ba1\u65b9\u6cd5\uff0c\u7528\u4e8e\u8bc4\u4f30\u4e00\u4e2a\u8bcd\u5728\u6587\u6863\u96c6\u5408\u4e2d\u7684\u91cd\u8981\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Scikit-learn<\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scikit-learn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8ba1\u7b97TF-IDF<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import TfidfVectorizer<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u672c<\/strong><\/h2>\n<p>texts = [&quot;Python is an interpreted, high-level, general-purpose programming language.&quot;,<\/p>\n<p>         &quot;Python has a design philosophy that emphasizes code readability.&quot;]<\/p>\n<p>vectorizer = TfidfVectorizer()<\/p>\n<p>tfidf_matrix = vectorizer.fit_transform(texts)<\/p>\n<p>feature_names = vectorizer.get_feature_names_out()<\/p>\n<h2><strong>\u63d0\u53d6\u6bcf\u4e2a\u8bcd\u7684TF-IDF\u503c<\/strong><\/h2>\n<p>for doc in range(len(texts)):<\/p>\n<p>    feature_index = tfidf_matrix[doc,:].nonzero()[1]<\/p>\n<p>    tfidf_scores = zip(feature_index, [tfidf_matrix[doc, x] for x in feature_index])<\/p>\n<p>    print(f&quot;Document {doc}&quot;)<\/p>\n<p>    for w, s in tfidf_scores:<\/p>\n<p>        print(f&quot;{feature_names[w]}: {s}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f18\u5316\u5173\u952e\u5b57\u63d0\u53d6<\/h3>\n<\/p>\n<p><h4>1\u3001\u53bb\u9664\u505c\u7528\u8bcd<\/h4>\n<\/p>\n<p><p>\u505c\u7528\u8bcd\u662f\u4e00\u4e9b\u5728\u6587\u672c\u4e2d\u9891\u7e41\u51fa\u73b0\u4f46\u5bf9\u5173\u952e\u5b57\u63d0\u53d6\u6ca1\u6709\u5e2e\u52a9\u7684\u8bcd\uff0c\u5982\u201cthe\u201d\u3001\u201cis\u201d\u7b49\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528SpaCy\u5185\u7f6e\u7684\u505c\u7528\u8bcd\u5217\u8868\u6765\u53bb\u9664\u8fd9\u4e9b\u8bcd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keywords = [token.text for token in doc if token.pos_ in [&#39;NOUN&#39;, &#39;ADJ&#39;] and not token.is_stop]<\/p>\n<p>print(keywords)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u8bcd\u5f62\u8fd8\u539f<\/h4>\n<\/p>\n<p><p>\u8bcd\u5f62\u8fd8\u539f\uff08Lemmatization\uff09\u662f\u5c06\u8bcd\u8bed\u8fd8\u539f\u5230\u5176\u539f\u5f62\uff0c\u5982\u5c06\u201crunning\u201d\u8fd8\u539f\u4e3a\u201crun\u201d\u3002\u8fd9\u6709\u52a9\u4e8e\u51cf\u5c11\u5197\u4f59\u7684\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">keywords = [token.lemma_ for token in doc if token.pos_ in [&#39;NOUN&#39;, &#39;ADJ&#39;] and not token.is_stop]<\/p>\n<p>print(keywords)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u63d0\u9ad8\u5173\u952e\u5b57\u63d0\u53d6\u7684\u51c6\u786e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5148\u4f7f\u7528\u8bcd\u6027\u6807\u6ce8\u548c\u505c\u7528\u8bcd\u8fc7\u6ee4\uff0c\u7136\u540e\u518d\u4f7f\u7528TF-IDF\u8fdb\u884c\u52a0\u6743\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import spacy<\/p>\n<p>from sklearn.feature_extraction.text import TfidfVectorizer<\/p>\n<p>nlp = spacy.load(&#39;en_core_web_sm&#39;)<\/p>\n<p>text = &quot;Python is an interpreted, high-level, general-purpose programming language.&quot;<\/p>\n<p>doc = nlp(text)<\/p>\n<h2><strong>\u8fc7\u6ee4\u540d\u8bcd\u548c\u5f62\u5bb9\u8bcd<\/strong><\/h2>\n<p>tokens = [token.text for token in doc if token.pos_ in [&#39;NOUN&#39;, &#39;ADJ&#39;] and not token.is_stop]<\/p>\n<h2><strong>\u4f7f\u7528TF-IDF\u8fdb\u884c\u52a0\u6743<\/strong><\/h2>\n<p>vectorizer = TfidfVectorizer(vocabulary=tokens)<\/p>\n<p>tfidf_matrix = vectorizer.fit_transform([text])<\/p>\n<p>feature_names = vectorizer.get_feature_names_out()<\/p>\n<p>tfidf_scores = zip(tfidf_matrix[0,:].nonzero()[1], [tfidf_matrix[0, x] for x in tfidf_matrix[0,:].nonzero()[1]])<\/p>\n<p>for w, s in tfidf_scores:<\/p>\n<p>    print(f&quot;{feature_names[w]}: {s}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5df2\u7ecf\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u548c\u4e0d\u540c\u7684NLP\u6280\u672f\u6765\u63d0\u53d6\u4e00\u53e5\u8bdd\u4e2d\u7684\u5173\u952e\u5b57\u3002<strong>\u4f7f\u7528SpaCy\u8fdb\u884c\u8bcd\u6027\u6807\u6ce8\u548c\u505c\u7528\u8bcd\u8fc7\u6ee4\u3001\u7ed3\u5408TF-IDF\u8fdb\u884c\u52a0\u6743<\/strong>\uff0c\u662f\u63d0\u53d6\u5173\u952e\u5b57\u7684\u6709\u6548\u65b9\u6cd5\u3002\u4e3a\u4e86\u8fdb\u4e00\u6b65\u63d0\u9ad8\u51c6\u786e\u6027\uff0c\u4f60\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u5176\u4ed6NLP\u5e93\uff0c\u5982NLTK\u3001gensim\u7b49\uff0c\u6216\u8005\u7ed3\u5408<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u6765\u4f18\u5316\u5173\u952e\u5b57\u63d0\u53d6\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u5355\u53e5\u5173\u952e\u5b57\u63d0\u53d6\uff0c\u4e5f\u53ef\u4ee5\u6269\u5c55\u5230\u66f4\u5927\u7684\u6587\u672c\u5206\u6790\u4efb\u52a1\u4e2d\u3002\u901a\u8fc7\u4e0d\u65ad\u4f18\u5316\u548c\u8c03\u6574\uff0c\u4f60\u53ef\u4ee5\u63d0\u53d6\u51fa\u66f4\u7b26\u5408\u5b9e\u9645\u9700\u6c42\u7684\u5173\u952e\u5b57\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u548c\u63d0\u53d6\u5173\u952e\u5b57\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u5e93\u6765\u63d0\u53d6\u53e5\u5b50\u4e2d\u7684\u5173\u952e\u5b57\uff0c\u4f8b\u5982NLTK\u3001spaCy\u548cjieba\u7b49\u3002\u901a\u8fc7\u5206\u8bcd\u548c\u8bcd\u9891\u7edf\u8ba1\uff0c\u7ed3\u5408TF-IDF\u7b49\u7b97\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u8bc6\u522b\u51fa\u53e5\u5b50\u4e2d\u7684\u91cd\u8981\u8bcd\u6c47\u3002\u5b9e\u73b0\u7684\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u6587\u672c\u9884\u5904\u7406\u3001\u5206\u8bcd\u3001\u53bb\u505c\u7528\u8bcd\u3001\u7edf\u8ba1\u8bcd\u9891\u548c\u5e94\u7528\u6743\u91cd\u7b97\u6cd5\u3002<\/p>\n<p><strong>\u63d0\u53d6\u5173\u952e\u5b57\u65f6\uff0c\u5e94\u8be5\u8003\u8651\u54ea\u4e9b\u6587\u672c\u9884\u5904\u7406\u6b65\u9aa4\uff1f<\/strong><br \/>\u6587\u672c\u9884\u5904\u7406\u662f\u63d0\u53d6\u5173\u952e\u5b57\u7684\u5173\u952e\u73af\u8282\uff0c\u5305\u62ec\u53bb\u9664\u6807\u70b9\u7b26\u53f7\u3001\u7edf\u4e00\u5927\u5c0f\u5199\u3001\u53bb\u9664\u505c\u7528\u8bcd\u4ee5\u53ca\u8bcd\u5f62\u8fd8\u539f\u7b49\u3002\u8fd9\u4e9b\u6b65\u9aa4\u80fd\u5e2e\u52a9\u63d0\u5347\u63d0\u53d6\u7ed3\u679c\u7684\u51c6\u786e\u6027\uff0c\u786e\u4fdd\u6700\u7ec8\u5f97\u5230\u7684\u5173\u952e\u5b57\u66f4\u5177\u4ee3\u8868\u6027\u548c\u4fe1\u606f\u91cf\u3002<\/p>\n<p><strong>\u5728\u63d0\u53d6\u5173\u952e\u5b57\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6587\u672c\u7684\u7279\u6027\u548c\u63d0\u53d6\u76ee\u7684\u3002\u5bf9\u4e8e\u77ed\u6587\u672c\uff0cTF-IDF\u53ef\u80fd\u66f4\u4e3a\u6709\u6548\uff0c\u800c\u5728\u5904\u7406\u957f\u6587\u672c\u65f6\uff0c\u4e3b\u9898\u6a21\u578b\uff08\u5982LDA\uff09\u6216\u8005\u56fe\u6a21\u578b\uff08\u5982TextRank\uff09\u53ef\u80fd\u66f4\u80fd\u6355\u6349\u5173\u952e\u8bcd\u7684\u8bed\u5883\u548c\u91cd\u8981\u6027\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u8bc4\u4f30\u4e0d\u540c\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\uff0c\u5c06\u6709\u52a9\u4e8e\u83b7\u5f97\u66f4\u597d\u7684\u63d0\u53d6\u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u63d0\u53d6\u4e00\u53e5\u8bdd\u4e2d\u7684\u5173\u952e\u5b57\u53ef\u4ee5\u901a\u8fc7\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u6280\u672f\u3001\u4f7f\u7528Python\u7684\u5e93\u5982NLTK\u3001SpaCy\u3001gensi [&hellip;]","protected":false},"author":3,"featured_media":1146411,"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\/1146397"}],"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=1146397"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146397\/revisions"}],"predecessor-version":[{"id":1146412,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1146397\/revisions\/1146412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1146411"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1146397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1146397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1146397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}