日常pytho3練習腳本之--兩個逗比聊天機器人

腳本後續更新及迭代將由kkitDeploy項目代替
https://github.com/luckman666/kkitdeploy_server
請大家持續關注kkitDeploy

這裏首先感謝一下波嫂!

波嫂也非常忙天天加班到晚上8點半以後纔回家,不過依然不辭辛苦的收集資料、頭懸樑、錐刺股的學習如何設計文章樣式。這是什麼精神?這是無產階級無私奉獻的精神!波嫂猶如一把星星之火永遠照亮着波哥前進的方向。

那麼有了波嫂的支持呢,我們就應該做出更多更好的內容了。所以今天波哥用幾十行代碼寫了兩個聊天機器人。之前都是人機交互,那麼兩個機器人相互聊天會是什麼效果呢?

請看下方:

640.webp (1).jpg
是不是有點意思呀!哈哈哈哈!

倉庫位置:

https://github.com/luckman666/mychat.git

由於代碼比較少波哥就直接寫在這裏了:

from time import sleep
import requests


def GenerateWord():

    s=''
    for num in range(5):
        head = random.randint(0xb0, 0xf7)
        body = random.randint(0xa1, 0xf9)
        val = f'{head:x}{body:x}'
        s += bytes.fromhex(val).decode('gb2312')
    return s

def Checkrepetition(chat,chatList):

    if len(chatList) > 5:
        chatList.pop(0)
    if chat in chatList:
        myword = GenerateWord()
        return myword,chatList
    else:
        chatList.append(chat)
        return chat,chatList


def Chitchat():
    zs = input("請主人輸入話題:")
    chatList=[]

    while True:

        resp = requests.get("http://api.qingyunke.com/api.php", {'key': 'free', 'appid':0, 'msg': zs})
        resp.encoding = 'utf8'
        resp = resp.json()
        sleep(1)
        print('趙四:', resp['content'])

        ln,chatList=Checkrepetition(resp['content'],chatList)
        resp = requests.get("http://api.qingyunke.com/api.php", {'key': 'free', 'appid':0, 'msg': ln})
        resp.encoding = 'utf8'
        resp = resp.json()
        sleep(1)
        print('劉能:', resp['content'])
        zs,chatList=Checkrepetition(resp['content'], chatList)

if __name__ == "__main__":
    Chitchat()

簡單介紹一下這個腳本:

這裏爲了使兩個小傢伙聊的開心起勁呢。

1、波哥寫了一個效驗函數(Checkrepetition)來判斷其是否重複。

2、如果有重複呢就隨機生成五個漢字塞給機器人(GenerateWord)

然後就會出現很多意外的回覆。。可能會很污的哦(未成年人禁用)!

記得幫波哥轉發哦!!

PS:這幾天想着要給大家錄一期視頻,結果不是麥克買錯了就是轉接頭不好使。。真是醉了!

在這裏插入圖片描述

發佈了54 篇原創文章 · 獲贊 0 · 訪問量 2628
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章