鐢ㄦ満鍣ㄥ涔犳墦閫犺亰澶╂満鍣ㄤ汉(涓�) 璁捐绡�

鏈枃鏄敤鏈哄櫒瀛︿範鎵撻�犺亰澶╂満鍣ㄤ汉绯诲垪鐨勭涓夌瘒锛岄�氳繃闃呰鏈枃浣犲皢瀵硅亰澶╂満鍣ㄤ汉鐨勫疄鐜版湁涓�涓ぇ鑷寸殑鎬濊矾銆�

鎴戜滑鐨勮亰澶╂満鍣ㄤ汉灏嗗叿澶囦粈涔堟牱鐨勭壒鎬э紵

  • 鐢ㄦ埛鍙互浣跨敤浜虹被鑷劧璇█鐨勬柟寮忔潵琛ㄨ揪鑷繁鐨勬剰鍥俱��
  • 鍙互渚濇嵁鐢ㄦ埛鐨勫弽棣堣繘琛屽湪绾垮閲忓涔狅紝浣跨敤鐨勮秺涔咃紝鑳藉洖绛斿緱闂瓒婂銆�
  • 閲囩敤闈炰镜鍏ュ紡璁捐锛岄�氳繃鍑犱釜绠�鍗曠殑API灏卞彲浠ユ帴鍏ャ��
  • 璇枡鏁版嵁浣跨敤绠�鍗曠殑txt鏍煎紡锛屽彧瑕佹洿鎹xt锛屽氨鍙互鏈嶅姟浜庝笉鍚岀殑鐭ヨ瘑棰嗗煙銆�
  • 鎻愪緵鍋氫负Demo鐨刄I绯荤粺锛屽甫鏈夌畝鍗曠殑鐧诲綍璁よ瘉銆�

寮�婧愯亰澶╂満鍣ㄤ汉妗嗘灦ChatterBot绠�浠�

鏈」鐩熀浜巆hatterbot0.8.7鏉ュ紑鍙戯紝浣嗕笉浠呬簬姝ゃ�傝鎴戜滑鍏堝chatterbot鍋氫竴涓畝鍗曠殑浜嗚В銆�

浠�涔堟槸ChatterBot锛�

ChatterBot鏄竴涓熀浜庢満鍣ㄥ涔犵殑鍙h寮忓璇濆紩鎿庯紝鍩轰簬python缂栧啓锛屽彲浠ュ熀浜庡凡鏈夌殑浼氳瘽闆嗗悎杩斿洖鍖归厤闂鐨勫搷搴斻�侰hatterBot鐨勯潪渚靛叆寮忚瑷�璁捐锛屼娇寰楁垜浠彲浠ュ湪鍏朵笂璁粌浠讳綍璇█鐨勫璇濇ā鍨嬨��

鎬庝箞浣跨敤chatterbot锛�

棣栧厛锛屽畨瑁卌hatterbot0.8.7鐗堟湰锛�

pip install ChatterBot==0.8.7

鍒涘缓涓�涓猚hat bot瀹炰緥锛�

from chatterbot import ChatBot
chatbot = ChatBot("Ron Obvious")

璁粌浣犵殑鏈哄櫒浜猴細

from chatterbot.trainers import ListTrainer

# 瀵硅瘽璇枡瀵癸紝涓�闂竴绛�
conversation = [
    "Hello",
    "Hi there!",
    "How are you doing?",
    "I'm doing great.",
    "That is good to hear",
    "Thank you.",
    "You're welcome."]
trainer = ListTrainer(chatbot)
trainer.train(conversation)

鑾峰彇鍝嶅簲锛�"Thanks!"鍦ㄤ笂闈㈢殑璇枡瀵逛腑鏄病鏈夌殑锛屼絾鏄叾榛樿浣跨敤鐨凩evenshtein distance绠楁硶鑳借寮曟搸浠庨棶绛斿涓�夊嚭涓�涓浉杩戠殑鍥炵瓟锛�

response = chatbot.get_response("Thanks!")
print(response) # 灏嗚緭鍑篩ou're welcome.

ok锛屽鏋滀綘涔熷凡缁忓緱鍒颁簡涓婇潰鐨勬纭緭鍑猴紝浣犲彲鑳芥兂璇达紝灏辫繖涔堢畝鍗曪紵鍏跺疄濡傛灉浠庣畝鍗曚綋楠屼竴涓嬬殑瑙掑害鏉ヨ锛屽氨鏄繖涔堢畝鍗曘�備絾鏄鏋滀綘鎯冲仛涓�涓儚閭d箞鍥炰簨鐨勮亰澶╂満鍣ㄤ汉鍑烘潵锛岄偅涔堣繕鏈変竴娈佃矾瑕佽蛋锛屾瘮濡侺evenshtein distance绠楁硶鍏跺疄杩囦簬绠�鍗曚簡锛屼細瀵艰嚧涓�浜涚瓟闈炴墍闂殑鎯呭喌锛岄櫎姝や箣澶栵紝杩樻湁涓�浜涘叾浠栨垜浠渶瑕佸畬鍠勭殑鍦版柟锛屽叿浣撲細鍦ㄤ笅涓�涓瘽棰樹腑璁ㄨ銆�

濡傛绠�鍗曞氨瀹炵幇浜嗕竴涓亰澶╂満鍣ㄤ汉锛屽叾涓湁娌℃湁浠�涔堜笉濡ワ紵

鐨勭‘锛岃繖閲岄潰鏄湁涓�浜涢棶棰樼殑锛�

  • chatterbot鐨勯粯璁ゅ疄鐜帮紝鍙互姝e父澶勭悊1000宸﹀彸鐨勯棶绛斿锛屼絾鏄殢鐫�鏁版嵁閲忕殑缁х画澧炲姞锛屽氨浼氬崄鍑犵鐢氳嚦鍑犲崄绉掓墠鑳借幏鍙栧埌鍥炵瓟锛屼互鍙婃洿闀跨殑鏃堕棿鎵嶈兘璁粌瀹屾垚(杩欓噷鏈変竴鐐归渶瑕佹敞鎰忥紝灏辨槸chatterbot涓彁鍒扮殑璁粌锛屽叾瀹炲氨鏄皢闂瓟璇枡鍐欏叆鏁版嵁搴撲腑),杩欐樉鐒舵槸涓嶈兘鎺ュ彈鐨勩��
  • 榛樿閲囩敤鐨凩evenshtein distance绠楁硶鍦ㄥ彞瀛愮殑鐩镐技搴︽瘮杈冧笂杩囦簬绠�鍗曚簡锛屽畠鍙叧蹇冧竴鍙ヨ瘽鍙樻垚鍙︿竴鍙ヨ瘽闇�瑕佺殑鏈�灏忕殑淇敼姝ラ鏈夊灏戯紝璁や负姝ラ瓒婂皯锛屼袱鍙ヨ瘽瓒婄浉杩戯紝鎵�浠ュ畠骞朵笉浼氳�冭檻鍙ュ瓙涓瘡涓瘝鐨勫叿浣撳惈涔夛紝姣斿涓嶇煡閬撹タ绾㈡熆鍜岀暘鑼勮櫧鐒舵瘡涓瓧閮戒笉鍚岋紝闀垮害涔熶笉鍚岋紝浣嗘槸鍏跺疄鎸囩殑鏄悓涓�涓笢瑗裤��

娌¢敊锛屾垜浠瑙e喅鐨勪富瑕侀棶棰樺氨鏄笂闈㈠垪鍑虹殑2涓棶棰橈紝姒傛嫭鏉ヨ灏辨槸2涓柟闈紝涓�涓槸鎬ц兘锛屽彟涓�涓槸鏅鸿兘銆傝櫧鐒堕棶棰樺彧鏈�2涓紝浣嗘槸瑙e喅璧锋潵纭槸瑕佽姳璐逛竴鐣姛澶殑銆�
棣栧厛锛岃鎴戜滑鏉ュ垎鏋愪竴涓嬫�ц兘鐨勯棶棰橈細
1銆乧hatterbot榛樿閲囩敤sqlite鏁版嵁搴擄紝sqlite鏄竴涓叧绯诲瀷鏁版嵁搴擄紝闈炲父杞婚噺锛屾棤闇�閰嶇疆鍜岄儴缃诧紝浣嗘槸褰撴暟鎹噺姣旇緝澶х殑鏃跺�欙紝鍐欐�ц兘鐩稿mongodb绛塶osql鏁版嵁搴撹繕鏄湁宸窛鐨勶紝瀵逛簬鎴戜滑鐨勫満鏅紝鐢变簬涓嶅瓨鍦ㄥ己浜嬪姟瑕佹眰锛屾墍浠ュ缓璁垏鎹㈠埌mongodb鏁版嵁搴擄紝浣犱細鍙戠幇璁粌閫熷害浼氭湁杈冨ぇ鐨勬彁鍗囷紱
绛栫暐:chatterbot闄や簡鏀寔sqlite锛岃繕鏀寔mongodb锛屾墍浠ュ彲浠ラ�氳繃淇敼閰嶇疆鐨勬柟寮忓垏鎹㈠埌mongodb鏁版嵁搴擄紝绀轰緥濡備笅锛�

chat = ChatBot(
            bot_name,
            database=bot_name,
            database_uri=current_app.config['DATABASE_URI'],
            storage_adapter="chatterbot.storage.MongoDatabaseAdapter")

2銆乧hatterbot灏嗘墍鏈夐棶绛斿瀛樺偍鍦ㄤ竴璧凤紝姣斿鍦╩ongodb涓紝鏄瓨鍌ㄥ湪涓�涓泦鍚堥噷鐨勶紝杩欐牱鍖归厤闂鐨勬椂鍊欙紝灏辫鍜屾墍鏈夌殑闂瓟瀵规暟鎹瘮杈冧竴閬嶏紝濡傛灉鏁版嵁閲忓緢澶х殑璇濓紝鏁堢巼鑲畾鏄緢鎱㈢殑锛�
绛栫暐:灏嗛棶绛斿鍒嗙被瀛樺偍锛屾瘮濡傚湪mongodb涓紝涓嶅悓绫诲瀷鐨勯棶绛斿瀛樺偍鍦ㄤ笉鍚岀殑闆嗗悎閲岋紝杩欎竴姝ョО涓烘剰鍥惧垎绫伙紝鎵�浠ユ垜浠渶瑕侀�氳繃鍙﹀鐨勭畻娉曟潵纭畾杈撳叆鍙ュ瓙鐨勬剰鍥剧被鍒紝鐒跺悗鍦ㄦ寚瀹氱被鍒笅鍘诲垽鏂彞瀛愬拰鍝簺闂鏇翠负杩戜技锛岀劧鍚庤繑鍥炲搴旂殑鍥炵瓟銆傝繖鏍峰仛鐨勫ソ澶勬槸鏂逛究缁存姢锛屽ぇ浣撻噺鐨勯棶绛斿琚媶鍒嗗埌鍚勪釜瀵瑰簲鐨勭被鍒笅锛屽垎鍒尮閰嶏紝浣撻噺鑷劧浼氬噺灏戝緢澶氾紝骞朵笖鏌愪竴绫诲瀷鐨勯棶绛斿鐨勬暟鎹噺澧炲姞锛屽鍏朵粬绫诲瀷闂瓟瀵圭殑鍝嶅簲閫熷害娌℃湁鐩存帴鐨勫奖鍝嶃��
3銆乧hatterbot鍖哄垎闂鍜岀瓟妗堟槸鏍规嵁鍙ュ瓙鏄惁鍑虹幇鍦╥n_response_to灞炴�т笅鐨則ext灞炴�т腑鏉ュ垽鏂殑锛岃繖瀵艰嚧闇�瑕佸厛鏌ヨ鍑篿n_response_to涓嬬殑鎵�鏈塼ext锛岀劧鍚庢牴鎹畉ext鍐嶆煡鍑烘墍鏈夊睘浜庨棶棰樼殑鍙ュ瓙锛岃繖鏍风殑鏌ヨ鏁堢巼鏄緢浣庣殑锛�
绛栫暐锛氬湪鍑嗗闂瓟瀵硅鏂欑殑鏃跺�欙紝鍒嗗埆瀵归棶棰樺拰绛旀杩涜鏍囪瘑锛屾瘮濡傜敤Q鍜孉鍋氬墠缂�锛岃繖鏍峰瓨鍌ㄥ埌鏁版嵁搴撲腑鍚庯紝鏌ヨ鐨勬椂鍊欏氨鍙互鐢≦鏉ョ洿鎺ュ尮閰嶅嚭闂锛岃�屼笉闇�瑕佸娆℃煡璇㈡暟鎹簱銆�
4銆乧hatterbot榛樿閲囩敤Levenshtein distance绠楁硶灏嗗綋鍓嶈緭鍏ョ殑闂鍜屾暟鎹簱閲屾瘡涓�涓棶绛旇褰曡繘琛屾瘮杈冿紝鍏蜂綋鍋氭硶鏄厛鏌ュ嚭鎵�鏈夌殑闂瓟鍙ュ瓙锛岀劧鍚巉or寰幆杩涜涓�涓�姣旇緝锛岄�夋嫨鍑烘渶鐩镐技鐨勫彞瀛愬仛涓哄搷搴旇繑鍥烇紝鏁堢巼鑷劧涓嶄細楂樸��
绛栫暐锛氭敼涓轰娇鐢ㄨ瘝鍚戦噺杩涜姣旇緝锛屽叿浣撳湪涓嬮潰鐨勬櫤鑳藉害绛栫暐涓湁浠嬬粛銆�
ok锛屾垜浠啀鏉ヨ亰鑱婂浣曟彁鍗嘽hatterbot鐨勬櫤鑳藉害锛�
1銆侀噰鐢ㄤ綑寮︾浉浼煎害绠楁硶浠f浛Levenshtein distance绠楁硶
Levenshtein distance绠楁硶鍙槸鍗曠函鐨勮绠椾竴涓彞瀛愬彉鎴愬彟涓�涓彞瀛愰渶瑕佺粡杩囩殑鏈�灏忕殑缂栬緫姝ラ锛屽苟娌℃湁鑰冭檻鍙ュ瓙涓瘝姹囨湰韬殑鍚箟锛屾墍浠ュ畠骞惰兘璇嗗埆鍑�"鑻规灉"姣旇捣"棣欐按"鏉ヨ鍜�"棣欒晧"鍦ㄨ涔変笂闈犵殑鏇磋繎銆傝�屼綑寮︾浉浼煎害鏄寚姣旇緝涓や釜鍚戦噺涔嬮棿鐨勪綑寮︾浉浼煎害锛屽悜閲忓綋鐒跺垎鍒槸杈撳叆鍙ュ瓙鐨勫彞鍚戦噺鍜屾暟鎹簱涓墍鏈夐棶棰樺彞瀛愮殑鍙ュ悜閲忥紝鑰屽彞瀛愯浆涓哄悜閲忕殑鏂瑰紡鏄噰鐢ㄧ殑word2vec,璇ユ柟娉曞湪鍚庣画璁插師鐞嗙殑閮ㄥ垎浼氬叿浣撲粙缁嶏紝杩欓噷鎴戜滑鍙渶瑕佺煡閬撹瘝鍚戦噺妯″瀷鍙互灏嗚瘝杞负瀵瑰簲鐨勫悜閲忥紝杩欎簺鍚戦噺鍦ㄧ┖闂翠腑鍛堢幇涓�绉嶈涔変笂鐨勫叧绯伙紝姣斿鐢ㄨ瘝鍚戦噺琛ㄧず鎴戜滑鐨勮瘝鐨勬椂鍊欙紝浼氬彂鐜� King鐨勫悜閲�-Man鐨勫悜閲�+Woman鐨勫悜閲�=Queen鐨勫悜閲忋��
閭d箞鍙ュ瓙鍙堟槸鎬庝箞杞垚鍚戦噺鐨勫憿锛熻繖閲屾垜浠噰鐢ㄤ簡骞冲潎鍚戦噺鐨勬柟娉曪紝灏辨槸鍏堝鍙ュ瓙鍒嗚瘝锛岀劧鍚庡皢璇嶅悜閲忕浉鍔犲啀闄や互鍚戦噺鐨勪釜鏁般�傝嚦浜庝负浠�涔堜綑寮﹀�煎彲浠ヨ〃绀轰袱涓悜閲忕殑鐩镐技搴︼紝鎴戜滑鍚屾牱涔熶細鍦ㄥ師鐞嗙殑閮ㄥ垎杩涜浠嬬粛銆�
2銆侀噰鐢ㄥ悜閲忓寲骞惰璁$畻绛栫暐浠f浛for寰幆姣斿
鏈変簡鍙ュ瓙鐨勫悜閲忚〃绀哄悗锛屾垜浠氨鍙互閲囩敤涓�浜涘苟琛岃绠楃殑鏂瑰紡鏉ヤ唬鏇縡or寰幆锛屽叿浣撶殑鎿嶄綔鏄皢鎵�鏈夋暟鎹斁鍒颁竴涓煩闃典腑涓�璧疯绠楋紝CPU铏界劧杩滄瘮涓嶄笂GPU鐨勫苟琛岃绠楅�熷害锛屼絾鏄瘮璧穎or寰幆鐨勬柟寮忎粛鐒跺彲浠ュ甫鏉ュ嚑鍗佸埌鍑犵櫨鍊嶈绠楅�熷害鐨勬彁鍗囥�傚湪姝や篃浣撶幇浜哻hatterbot鐨勪紭绉�璁捐锛屼娇寰楁垜浠彲浠ュ湪涓嶆洿鏀规簮浠g爜鐨勬儏鍐典笅灏辨浛鎹㈡帀鍘熸湁鐨勫尮閰嶇畻娉曪紝鍏蜂綋瑙佷唬鐮佺瘒鐨勪粙缁嶃��

涓�涓棶棰樹粠杈撳叆鍒扮粰鍑哄洖澶嶅皢缁忓巻浠�涔堬紵

鍒版锛屾垜浠В閲婁簡涓轰粈涔堥渶瑕佸熀浜巆hatterbot鍐嶅仛涓�浜涗簨鎯咃紝浠ュ強濡備綍鍋氾紝鐜板湪鎴戜滑鏉ョ湅鐪嬩竴涓棶棰樹粠杈撳叆鍒扮粰鍑哄洖澶嶅叿浣撶粡鍘嗕簡鍝簺姝ラ锛�
鐢ㄦ満鍣ㄥ涔犳墦閫犺亰澶╂満鍣ㄤ汉(涓�) 璁捐绡�_第1张图片

鐢ㄦ埛鎻愰棶鍚庯紝鐢辨剰鍥炬帹娴嬬粍浠舵帴鏀堕棶棰橈紝缁勪欢鍐呴儴杩涜鐗瑰緛鎻� 鍙栧悗锛屼紶缁欐剰鍥惧垎绫诲櫒鍘婚娴嬮棶棰樻墍灞炵殑绫诲埆(姣斿锛氳繖鏄竴涓叧浜� 鈥滅數褰辨紨鍛樷�濈殑闂锛屾垨鍏充簬鈥滅數褰变笂鏄犳椂闂粹�濈殑闂)锛屾帴鐫�闂浼氫紶閫掑埌璇箟 鐞嗚В妯″潡锛屽苟鑷姩瑙﹀彂鍚堥�傜殑璇箟鐞嗚В瀹炰緥鍘诲皾璇曞尮閰嶉棶棰樺搴旂殑 绛旀锛岃繖閲岃鐨勮涔夌悊瑙e氨鏄湪鎸囧畾鐨勬剰鍥惧垎绫讳笅锛屽幓鍖归厤鍏蜂綋鐨勯棶棰橈紝姣斿鈥滀綘瑙夊緱鍔熷か绫荤數褰辫皝婕旂殑濂斤紵鈥濓紝鎴栬�呪�滅埍鎯呯墖浠�涔堟椂闂存涓婃槧姣旇緝鍚堥�傦紵鈥濈瓑銆傛暣涓繃绋嬩富瑕佹槸閲囩敤璇嶅悜閲忔ā鍨嬫瀯閫犻棶棰樺彞瀛愮殑鐗瑰緛鍚戦噺锛岄�氳繃璐濆彾鏂畻娉曡繘琛屾剰鍥惧垎绫伙紝浠ュ強 閲囩敤浣欏鸡鐩镐技搴︾畻娉曡绠楅棶棰樺拰绛旀鐨勫尮閰嶅垎鏁般�傛鏃跺紩鎿庝細鏍规嵁 鍖归厤鍒嗘暟缁撳悎闃堝�艰繘琛屽垎鏋愶紝浠庤�屽喅瀹氭槸鐩存帴杩斿洖绛旀锛岃繕鏄檷绾у鐞嗭紝鎵�浠ユ湁浜涘満鏅笅鍙兘浼氳繑鍥炲涓�欓�夌瓟妗堬紝鍊欓�夌瓟妗堜細鏍规嵁鍒嗘暟闄嶅簭鎺掑垪銆�

濡備綍璁╂満鍣ㄤ汉璇存垜鎯冲惉鐨勮瘽锛�

鍓嶉潰璇寸殑閮芥槸濡備綍鏍规嵁杈撳叆鐨勯棶棰樼粰涓庡悎閫傜殑鍥炲锛屾湰绡囦富瑕佽璁哄浣曡皟鏁欐満鍣ㄤ汉璇翠綘鎯冲惉鐨勫洖澶嶏紝鍏蜂綋娴佺▼濡備笅锛�
鐢ㄦ満鍣ㄥ涔犳墦閫犺亰澶╂満鍣ㄤ汉(涓�) 璁捐绡�_第2张图片

鐢ㄦ埛鎻愰棶鍚庯紝濡傛灉绯荤粺娌¤兘缁欏嚭婊℃剰鐨勭瓟妗堬紝鐢ㄦ埛鍙互閫氳繃鏂板闂瓟瀵广�佷慨璁㈢瓟妗� 2 绉嶆柟寮忔潵杩涜鍙嶉锛屽綋绯荤粺缁欏嚭澶氫釜鍊欓�夌瓟 妗堬紝浣嗘槸姝g‘绛旀娌℃湁鎺掑湪棣栦綅鏃讹紝鐢ㄦ埛鍙互閫氳繃鏍囨敞鏈�浣崇瓟妗堟潵 杩涜鍙嶉銆傚彲浠ュ畾鏈熻闂瓟寮曟搸鑷富瀛︿範鐢ㄦ埛鐨勫弽棣堬紝閲嶆柊璁粌鎰� 鍥惧垎绫诲櫒骞舵洿鏂伴棶绛旇鏂欏簱锛屽綋鐢ㄦ埛鑷繁鎴栧叾浠栫敤鎴峰啀娆¢棶鍒扮浉鍚� 鍚箟鐨勯棶棰樻椂鍗冲彲寰楀埌鐩稿簲鐨勭瓟妗堛��
鐢变簬鎴戜滑鍙互鑷繁璋冩暀鏈哄櫒浜猴紝鎵�浠ヤ綘鍙互灏嗗叾璋冩暀鎴愪粎灞炰簬浣犺嚜宸辩殑鐙竴鏃犱簩鐨勬�ф牸馃槒銆�

涓嬩竴绡�銆婄敤鏈哄櫒瀛︿範鎵撻�犺亰澶╂満鍣ㄤ汉(鍥�) 浠g爜绡囥��灏嗗睍绀烘墦閫犺亰澶╂満鍣ㄤ汉鐨勭浉鍏充唬鐮佸疄鐜板強璇存槑銆�

ok,鏈瘒灏辫繖涔堝鍐呭鍟锛屾劅璋㈤槄璇籓(鈭鈭�)O銆�

鏈崥瀹㈠唴瀹规潵鑷叕浼楀彿鈥滅▼搴忓憳涓�涓�娑ょ敓鈥濓紝娆㈣繋鎵爜鍏虫敞 o(鈭鈭�)o

鐢ㄦ満鍣ㄥ涔犳墦閫犺亰澶╂満鍣ㄤ汉(涓�) 璁捐绡�_第3张图片

你可能感兴趣的:(鐢ㄦ満鍣ㄥ涔犳墦閫犺亰澶╂満鍣ㄤ汉(涓�) 璁捐绡�)