{"id":948746,"date":"2024-12-27T00:06:27","date_gmt":"2024-12-26T16:06:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948746.html"},"modified":"2024-12-27T00:06:29","modified_gmt":"2024-12-26T16:06:29","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e9%97%ae%e7%ad%94","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/948746.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u95ee\u7b54"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084005\/01ee39b8-4eb7-4539-805a-41d09c816968.webp\" alt=\"python\u4e2d\u5982\u4f55\u95ee\u7b54\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b9e\u73b0\u95ee\u7b54\u7cfb\u7edf\u901a\u5e38\u6d89\u53ca\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6280\u672f\u3002<strong>\u8981\u5b9e\u73b0Python\u4e2d\u7684\u95ee\u7b54\u7cfb\u7edf\uff0c\u53ef\u4ee5\u4f7f\u7528\u73b0\u6709\u7684NLP\u5e93\u3001\u9884\u8bad\u7ec3\u6a21\u578b\u548c\u81ea\u5b9a\u4e49\u7b97\u6cd5\u3001\u901a\u8fc7\u8bed\u6599\u5e93\u8bad\u7ec3\u6a21\u578b<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u5c55\u5f00\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u95ee\u7b54\u7cfb\u7edf\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b<\/h3>\n<\/p>\n<p><p>Python\u4e2d\u6709\u8bb8\u591a\u5f3a\u5927\u7684\u9884\u8bad\u7ec3\u6a21\u578b\u53ef\u4ee5\u7528\u4e8e\u95ee\u7b54\u7cfb\u7edf\uff0c\u4f8b\u5982BERT\u3001GPT-3\u3001RoBERTa\u7b49\u3002\u8fd9\u4e9b\u6a21\u578b\u5df2\u7ecf\u5728\u5927\u91cf\u7684\u6587\u672c\u6570\u636e\u4e0a\u8fdb\u884c\u8bad\u7ec3\uff0c\u5e76\u80fd\u8fdb\u884c\u8bed\u8a00\u7406\u89e3\u548c\u751f\u6210\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. BERT\u6a21\u578b<\/h4>\n<\/p>\n<p><p>BERT\uff08Bidirectional Encoder Representations from Transformers\uff09\u662fGoogle\u63a8\u51fa\u7684\u4e00\u79cd\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u5e7f\u6cdb\u7528\u4e8e\u95ee\u7b54\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5<code>transformers<\/code>\u5e93\uff0c\u8fd9\u662f\u7531Hugging Face\u63d0\u4f9b\u7684Python\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u4f7f\u7528\u5404\u79cd\u9884\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install transformers<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528BERT\u8fdb\u884c\u95ee\u7b54<\/strong>\uff1a\u901a\u8fc7<code>transformers<\/code>\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u52a0\u8f7dBERT\u6a21\u578b\u5e76\u8fdb\u884c\u95ee\u7b54\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from transformers import pipeline<\/p>\n<h2><strong>\u52a0\u8f7d\u95ee\u7b54\u6a21\u578b<\/strong><\/h2>\n<p>nlp = pipeline(&quot;question-answering&quot;)<\/p>\n<h2><strong>\u95ee\u7b54\u4efb\u52a1<\/strong><\/h2>\n<p>result = nlp(question=&quot;What is Python?&quot;, context=&quot;Python is a programming language that lets you work quickly and integrate systems more effectively.&quot;)<\/p>\n<p>print(result[&#39;answer&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>2. GPT-3\u548c\u5176\u4ed6\u6a21\u578b<\/h4>\n<\/p>\n<p><p>GPT-3\u662fOpen<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>\u63a8\u51fa\u7684\u53e6\u4e00\u79cd\u5f3a\u5927\u7684\u8bed\u8a00\u6a21\u578b\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u95ee\u7b54\u7cfb\u7edf\u3002\u7531\u4e8eGPT-3\u9700\u8981\u901a\u8fc7API\u8bbf\u95ee\uff0c\u6240\u4ee5\u9700\u8981\u6ce8\u518cOpenAI\u8d26\u53f7\u5e76\u83b7\u53d6API\u5bc6\u94a5\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u5236\u5316\u95ee\u7b54\u7cfb\u7edf<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u66f4\u4e3a\u7279\u5b9a\u9886\u57df\u7684\u95ee\u7b54\u7cfb\u7edf\uff0c\u53ef\u80fd\u9700\u8981\u8bad\u7ec3\u81ea\u5df1\u7684\u6a21\u578b\u6216\u4f7f\u7528\u7279\u5b9a\u9886\u57df\u7684\u8bed\u6599\u5e93\u3002<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u51c6\u5907<\/h4>\n<\/p>\n<ul>\n<li><strong>\u8bed\u6599\u5e93<\/strong>\uff1a\u9996\u5148\uff0c\u4f60\u9700\u8981\u6536\u96c6\u548c\u51c6\u5907\u4e00\u4e2a\u4e0e\u7279\u5b9a\u9886\u57df\u76f8\u5173\u7684\u8bed\u6599\u5e93\u3002\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u662f\u6587\u6863\u3001\u7f51\u9875\u3001\u4e66\u7c4d\u7b49\u3002<\/li>\n<\/ul>\n<p><h4>2. \u8bad\u7ec3\u6a21\u578b<\/h4>\n<\/p>\n<ul>\n<li>\n<p><strong>NLP\u5e93<\/strong>\uff1a\u4f7f\u7528<code>nltk<\/code>\u3001<code>spaCy<\/code>\u7b49NLP\u5e93\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\uff0c\u5206\u8bcd\u3001\u53bb\u9664\u505c\u7528\u8bcd\u3001\u8bcd\u5e72\u63d0\u53d6\u7b49\u3002<\/p>\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>nltk.download(&#39;punkt&#39;)<\/p>\n<p>nltk.download(&#39;stopwords&#39;)<\/p>\n<p>text = &quot;Python is a programming language.&quot;<\/p>\n<p>stop_words = set(stopwords.words(&#39;english&#39;))<\/p>\n<p>word_tokens = word_tokenize(text)<\/p>\n<p>filtered_sentence = [w for w in word_tokens if not w.lower() in stop_words]<\/p>\n<p>print(filtered_sentence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u578b\u8bad\u7ec3<\/strong>\uff1a\u4f7f\u7528<code>scikit-learn<\/code>\u3001<code>TensorFlow<\/code>\u3001<code>PyTorch<\/code>\u7b49\u5e93\u8bad\u7ec3\u673a\u5668\u5b66\u4e60\u6216\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h4>3. \u6a21\u578b\u8bc4\u4f30<\/h4>\n<\/p>\n<ul>\n<li><strong>\u8bc4\u4f30\u6307\u6807<\/strong>\uff1a\u4f7f\u7528\u51c6\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1\u5206\u6570\u7b49\u6307\u6807\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\uff0c\u8c03\u6574\u6a21\u578b\u53c2\u6570\u4ee5\u4f18\u5316\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u96c6\u6210\u548c\u90e8\u7f72<\/h3>\n<\/p>\n<p><h4>1. \u90e8\u7f72\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7Flask\u3001Django\u7b49\u6846\u67b6\u5f00\u53d1API\u63a5\u53e3\uff0c\u4f9b\u5916\u90e8\u5e94\u7528\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h4>2. \u524d\u7aef\u5c55\u793a<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7Web\u5e94\u7528\u3001\u804a\u5929\u673a\u5668\u4eba\u7b49\u754c\u9762\u5c55\u793a\u95ee\u7b54\u529f\u80fd\uff0c\u7528\u6237\u53ef\u4ee5\u5728\u754c\u9762\u4e0a\u8f93\u5165\u95ee\u9898\u5e76\u83b7\u53d6\u7b54\u6848\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e38\u89c1\u6311\u6218\u548c\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>1. \u6570\u636e\u4e0d\u8db3<\/h4>\n<\/p>\n<p><p>\u5728\u7279\u5b9a\u9886\u57df\uff0c\u53ef\u80fd\u7f3a\u4e4f\u8db3\u591f\u7684\u6570\u636e\u7528\u4e8e\u8bad\u7ec3\u6a21\u578b\u3002\u89e3\u51b3\u65b9\u6848\u5305\u62ec\u4f7f\u7528\u6570\u636e\u589e\u5f3a\u6280\u672f\u3001\u8fc1\u79fb\u5b66\u4e60\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2. \u6a21\u578b\u7406\u89e3\u80fd\u529b<\/h4>\n<\/p>\n<p><p>\u6709\u4e9b\u590d\u6742\u95ee\u9898\u53ef\u80fd\u96be\u4ee5\u7528\u7b80\u5355\u6a21\u578b\u89e3\u51b3\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u66f4\u590d\u6742\u7684\u6a21\u578b\u6216\u7ed3\u5408\u591a\u79cd\u6280\u672f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>3. \u7cfb\u7edf\u6027\u80fd<\/h4>\n<\/p>\n<p><p>\u95ee\u7b54\u7cfb\u7edf\u9700\u8981\u5feb\u901f\u54cd\u5e94\uff0c\u56e0\u6b64\u5728\u8bbe\u8ba1\u65f6\u9700\u8981\u8003\u8651\u7cfb\u7edf\u6027\u80fd\u4f18\u5316\uff0c\u5982\u4f7f\u7528\u7f13\u5b58\u3001\u8d1f\u8f7d\u5747\u8861\u7b49\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u672a\u6765\u53d1\u5c55\u65b9\u5411<\/h3>\n<\/p>\n<p><p>\u95ee\u7b54\u7cfb\u7edf\u5728<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u9886\u57df\u6709\u7740\u5e7f\u9614\u7684\u53d1\u5c55\u524d\u666f\u3002\u672a\u6765\u53ef\u80fd\u4f1a\u8fdb\u4e00\u6b65\u7ed3\u5408\u8bed\u97f3\u8bc6\u522b\u3001\u56fe\u50cf\u8bc6\u522b\u7b49\u6280\u672f\uff0c\u5b9e\u73b0\u66f4\u4e3a\u667a\u80fd\u548c\u591a\u6837\u5316\u7684\u4ea4\u4e92\u65b9\u5f0f\u3002\u6b64\u5916\uff0c\u95ee\u7b54\u7cfb\u7edf\u5728\u667a\u80fd\u5ba2\u670d\u3001\u6559\u80b2\u3001\u533b\u7597\u7b49\u9886\u57df\u7684\u5e94\u7528\u4e5f\u5c06\u4e0d\u65ad\u6269\u5927\uff0c\u5e26\u6765\u66f4\u591a\u7684\u5546\u4e1a\u673a\u4f1a\u548c\u6280\u672f\u6311\u6218\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u95ee\u7b54\u7cfb\u7edf\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6280\u672f\u6c34\u5e73\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u9884\u8bad\u7ec3\u6a21\u578b\u6216\u5b9a\u5236\u5316\u65b9\u6cd5\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u95ee\u7b54\u7cfb\u7edf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u5178\u6765\u5b58\u50a8\u95ee\u9898\u548c\u7b54\u6848\uff0c\u5229\u7528\u5faa\u73af\u8bed\u53e5\u6765\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\u5e76\u63d0\u4f9b\u76f8\u5e94\u7684\u56de\u7b54\u3002\u9996\u5148\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5b57\u5178\uff0c\u952e\u4e3a\u95ee\u9898\uff0c\u503c\u4e3a\u7b54\u6848\u3002\u63a5\u7740\uff0c\u4f7f\u7528<code>input()<\/code>\u51fd\u6570\u83b7\u53d6\u7528\u6237\u7684\u95ee\u9898\uff0c\u5e76\u901a\u8fc7\u6761\u4ef6\u5224\u65ad\u6765\u5339\u914d\u7b54\u6848\uff0c\u6700\u540e\u8f93\u51fa\u7ed3\u679c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">qa_dict = {\n    &quot;\u4f60\u7684\u540d\u5b57\u662f\u4ec0\u4e48\uff1f&quot;: &quot;\u6211\u662f\u4e00\u4e2aPython\u95ee\u7b54\u7cfb\u7edf\u3002&quot;,\n    &quot;\u4f60\u80fd\u505a\u4ec0\u4e48\uff1f&quot;: &quot;\u6211\u53ef\u4ee5\u56de\u7b54\u4f60\u7684\u95ee\u9898\u3002&quot;,\n}\n\nwhile True:\n    user_question = input(&quot;\u8bf7\u95ee\u6709\u4ec0\u4e48\u95ee\u9898\uff1f&quot;)\n    if user_question in qa_dict:\n        print(qa_dict[user_question])\n    else:\n        print(&quot;\u62b1\u6b49\uff0c\u6211\u4e0d\u77e5\u9053\u7b54\u6848\u3002&quot;)\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u63d0\u5347\u95ee\u7b54\u7cfb\u7edf\u7684\u667a\u80fd\u6027\uff1f<\/strong><br \/>\u901a\u8fc7\u5f15\u5165\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u5e93\uff0c\u5982NLTK\u6216spaCy\uff0c\u53ef\u4ee5\u4f7f\u95ee\u7b54\u7cfb\u7edf\u66f4\u5177\u667a\u80fd\u6027\u3002\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5206\u6790\u548c\u7406\u89e3\u7528\u6237\u8f93\u5165\u7684\u81ea\u7136\u8bed\u8a00\uff0c\u8bc6\u522b\u5173\u952e\u8bcd\u548c\u610f\u56fe\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u51c6\u786e\u7684\u56de\u7b54\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u673a\u5668\u5b66\u4e60\u6a21\u578b\u6765\u8bad\u7ec3\u7cfb\u7edf\uff0c\u4f7f\u5176\u4e0d\u65ad\u5b66\u4e60\u548c\u6539\u8fdb\u3002\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u53ef\u4ee5\u6784\u5efa\u51fa\u66f4\u590d\u6742\u7684\u95ee\u7b54\u7cfb\u7edf\uff0c\u80fd\u591f\u5904\u7406\u66f4\u5e7f\u6cdb\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u95ee\u7b54\u7cfb\u7edf\u4e2d\u5f15\u5165\u5916\u90e8\u77e5\u8bc6\u5e93\uff1f<\/strong><br \/>\u4e3a\u4e86\u589e\u5f3a\u95ee\u7b54\u7cfb\u7edf\u7684\u77e5\u8bc6\u9762\uff0c\u53ef\u4ee5\u901a\u8fc7API\u63a5\u53e3\u6216\u6570\u636e\u5e93\u8fde\u63a5\u5916\u90e8\u77e5\u8bc6\u5e93\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Wikipedia API\u83b7\u53d6\u76f8\u5173\u4fe1\u606f\uff0c\u6216\u8fde\u63a5\u5230\u67d0\u4e2a\u4e13\u4e1a\u77e5\u8bc6\u5e93\u3002\u901a\u8fc7\u7f16\u5199\u76f8\u5e94\u7684\u4ee3\u7801\uff0c\u7cfb\u7edf\u53ef\u4ee5\u6839\u636e\u7528\u6237\u7684\u95ee\u9898\u52a8\u6001\u67e5\u8be2\u5916\u90e8\u6570\u636e\u6e90\u5e76\u8fd4\u56de\u7b54\u6848\u3002\u8fd9\u4e00\u8fc7\u7a0b\u901a\u5e38\u6d89\u53ca\u5230HTTP\u8bf7\u6c42\u548c\u6570\u636e\u89e3\u6790\uff0c\u80fd\u591f\u663e\u8457\u63d0\u5347\u95ee\u7b54\u7684\u51c6\u786e\u6027\u548c\u4e30\u5bcc\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u95ee\u7b54\u7cfb\u7edf\u901a\u5e38\u6d89\u53ca\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u548c\u673a\u5668\u5b66\u4e60\u6280\u672f\u3002\u8981\u5b9e\u73b0Python\u4e2d\u7684\u95ee\u7b54\u7cfb\u7edf\uff0c\u53ef\u4ee5 [&hellip;]","protected":false},"author":3,"featured_media":948748,"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\/948746"}],"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=948746"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948746\/revisions"}],"predecessor-version":[{"id":948750,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948746\/revisions\/948750"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948748"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}