{"id":152,"date":"2023-03-27T16:57:47","date_gmt":"2023-03-27T08:57:47","guid":{"rendered":"https:\/\/mianka.xyz\/?p=151"},"modified":"2023-03-28T10:35:26","modified_gmt":"2023-03-28T02:35:26","slug":"python","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=152","title":{"rendered":"\u7528python\u5199\u4e2a\u81ea\u7136\u8bed\u8a00\u5bf9\u8bdd\u5904\u7406\u6a21\u578b"},"content":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e\u89c4\u5219\u7684\u81ea\u7136\u8bed\u8a00\u5bf9\u8bdd\u5904\u7406\u6a21\u578b\u7684\u4f8b\u5b50\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539\u548c\u6269\u5c55\u3002<\/p>\n<p><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u5fc5\u8981\u7684Python\u5e93\uff0c\u5305\u62ecNLTK\u3001pandas\u548cnumpy\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-bash\">pip&nbsp;install&nbsp;nltk&nbsp;pandas&nbsp;numpy<\/pre>\n<p>\u7136\u540e\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u6784\u5efa\u6a21\u578b\uff1a<\/p>\n<p><\/p>\n<p>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/p>\n<p><\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;nltk\nimport&nbsp;pandas&nbsp;as&nbsp;pd\nimport&nbsp;numpy&nbsp;as&nbsp;np<\/pre>\n<p>\u51c6\u5907\u6570\u636e<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4eceCSV\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5bf9\u8bdd\u6570\u636e\uff0c\u7136\u540e\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\u3002\u5047\u8bbeCSV\u6587\u4ef6\u7684\u683c\u5f0f\u4e3a\u201c\u7528\u6237\u8f93\u5165\uff0c\u673a\u5668\u4eba\u54cd\u5e94\u201d\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">How&nbsp;are&nbsp;you?,I&#39;m&nbsp;fine,&nbsp;thank&nbsp;you!\nWhat&#39;s&nbsp;your&nbsp;name?,My&nbsp;name&nbsp;is&nbsp;ChatBot.<\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u8bfb\u53d6CSV\u6587\u4ef6\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">data&nbsp;=&nbsp;pd.read_csv(&#39;conversation.csv&#39;,&nbsp;header=None)\nconversations&nbsp;=&nbsp;data.values.tolist()<\/pre>\n<p>\u7136\u540e\uff0c\u53ef\u4ee5\u5c06\u5bf9\u8bdd\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u7528\u6237\u8f93\u5165\uff0c\u503c\u662f\u673a\u5668\u4eba\u54cd\u5e94\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<p><\/p>\n<pre class=\"prism-highlight prism-language-python\">convo_dict&nbsp;=&nbsp;{}\nfor&nbsp;conversation&nbsp;in&nbsp;conversations:\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5c06\u7528\u6237\u8f93\u5165\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff0c\u65b9\u4fbf\u5339\u914d\n&nbsp;&nbsp;&nbsp;&nbsp;user_input&nbsp;=&nbsp;conversation[0].lower()\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5c06\u673a\u5668\u4eba\u54cd\u5e94\u6dfb\u52a0\u5230\u5b57\u5178\u4e2d\n&nbsp;&nbsp;&nbsp;&nbsp;convo_dict[user_input]&nbsp;=&nbsp;conversation[1]<\/pre>\n<p>\u5b9a\u4e49\u54cd\u5e94\u51fd\u6570<\/p>\n<p>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6839\u636e\u7528\u6237\u8f93\u5165\u8fd4\u56de\u76f8\u5e94\u7684\u673a\u5668\u4eba\u54cd\u5e94\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">def&nbsp;respond(user_input,&nbsp;convo_dict):\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5c06\u7528\u6237\u8f93\u5165\u8f6c\u6362\u4e3a\u5c0f\u5199\u5b57\u6bcd\uff0c\u65b9\u4fbf\u5339\u914d\n&nbsp;&nbsp;&nbsp;&nbsp;user_input&nbsp;=&nbsp;user_input.lower()\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5982\u679c\u7528\u6237\u8f93\u5165\u5728\u5b57\u5178\u4e2d\uff0c\u5219\u8fd4\u56de\u76f8\u5e94\u7684\u673a\u5668\u4eba\u54cd\u5e94\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;user_input&nbsp;in&nbsp;convo_dict:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;convo_dict[user_input]\n&nbsp;&nbsp;&nbsp;&nbsp;else:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5982\u679c\u6ca1\u6709\u627e\u5230\u76f8\u5e94\u7684\u673a\u5668\u4eba\u54cd\u5e94\uff0c\u5219\u8fd4\u56de\u9ed8\u8ba4\u54cd\u5e94\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;&quot;I&#39;m&nbsp;sorry,&nbsp;I&nbsp;didn&#39;t&nbsp;understand&nbsp;your&nbsp;question.&quot;<\/pre>\n<p>\u8fdb\u884c\u5bf9\u8bdd<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5f00\u59cb\u5bf9\u8bdd\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">while&nbsp;True:\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u83b7\u53d6\u7528\u6237\u8f93\u5165\n&nbsp;&nbsp;&nbsp;&nbsp;user_input&nbsp;=&nbsp;input(&#39;You:&nbsp;&#39;)\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u751f\u6210\u673a\u5668\u4eba\u54cd\u5e94\n&nbsp;&nbsp;&nbsp;&nbsp;bot_response&nbsp;=&nbsp;respond(user_input,&nbsp;convo_dict)\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u8f93\u51fa\u673a\u5668\u4eba\u54cd\u5e94\n&nbsp;&nbsp;&nbsp;&nbsp;print(&#39;Bot:&#39;,&nbsp;bot_response)<\/pre>\n<p>\u8fd9\u4e2a\u57fa\u4e8e\u89c4\u5219\u7684\u6a21\u578b\u5f88\u7b80\u5355\uff0c\u4f46\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u8d77\u70b9\u6765\u6269\u5c55\u548c\u6539\u8fdb\uff0c\u4f8b\u5982\u589e\u52a0\u8bed\u8a00\u6a21\u578b\u3001\u5b9e\u4f53\u8bc6\u522b\u3001\u60c5\u611f\u5206\u6790\u7b49\u529f\u80fd<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u57fa\u4e8e\u89c4\u5219\u7684\u81ea\u7136\u8bed\u8a00\u5bf9\u8bdd\u5904\u7406\u6a21\u578b\u7684\u4f8b\u5b50\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539\u548c\u6269\u5c55\u3002<\/p>\n<p><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u5fc5\u8981\u7684Python\u5e93\uff0c\u5305\u62ecNLTK\u3001pandas\u548cnumpy\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-bash\">pip&nbsp;install&nbsp;nltk&nbsp;pandas&nbsp;numpy<\/pre>\n<p>\u7136\u540e\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u6784\u5efa\u6a21\u578b\uff1a<\/p>\n<p><\/p>\n<p><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=152"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}