大于00

if type == toy: # 根据请求,前端发过来了请求,就不用自己查了(思路也有)

​ filename= get()

ret['chat'] = filename

可以发消息的时候,另一个人没有接收到. 再发才会接受到

获得消息提醒和收到消息: 一个是存在RET里, 一个是存在数据库里. (分开写就不会混淆)

下载安装Read timed out.问题的解决方法

pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

pip --default-timeout=100 install gensim

安装了第二遍之后成功 安装软件如下

Installing collected packages: numpy, scipy, boto, docutils, jmespath, python-dateutil, botocore, s3transfer, boto3, smart-open, gensim

下载gensim深度学习

下载 jieba 用于分词, 大词量 检测匹配相同的

关于PEP

写了个a='''很多的中文文字''' 然后有了错误

SyntaxError: Non-UTF-8 code starting with '\xce' in file D:/python/python_code/AI/Monstertoy - 副本/my_jieba.py on line 3, but no encoding declared;see http://python.org/dev/peps/pep-0263/ for details

解决方法
:我们在代码的第一行加上

# coding=gbk

utils

from bson import ObjectId
from BaiduAI import text2audio
from setting import MDB


def get_xxtx(to_user, from_user, tx_type= None):
    friend_remark = '未知用户'
    toy_info = MDB.Toys.find_one({'_id': ObjectId(to_user)})
    for friend in toy_info.get('friend_list'):
        if friend.get("friend_id") == from_user:
            friend_remark = friend.get('friend_remark')
            friend_type = friend.get('friend_type')     # 区分不同的toy 和 app 传过去
    if tx_type == 'xxtx':   # 当toy的时候就给他传参 'xxtx' ,提醒一下
        wenjianming = text2audio(f'你有来自{friend_remark}的消息')
        return wenjianming
    else:   
        wenjianming = text2audio(f'以下是来自{friend_remark}的消息')
        return wenjianming, friend_type

get_xxtx接收来自谁的消息, 通过给他传参的形式, 区分一下 toy和app的内容提醒不一样,主要是,toy给他发过来的时候,玩具可以收到提醒,不给app这个判断的机会 当toy的时候就给他传参 'xxtx' ,当app的时候就不给 然后只有玩具通信时使用,app不用提醒(有页面)

def toy_uploader():
  # 消息没法查看信息,所以需要写个消息提醒
    friend_type = request.form.get('friend_type')
    if friend_type == 'toy':    # 在这里判断一下, 
        filename = get_xxtx(to_user, from_user, 'xxtx')
       
   RET["CODE"] = 0
   RET["MSG"] = "上传成功"
   RET["DATA"] = {
        "code":0,
        "filename": filename,
        "friend_type": "toy"
    }
    return jsonify(RET)

redis_chat

def get_redis_toy(to_user, from_user):
    to_user_json = RDB.get(to_user)
    if to_user_json:
        to_user_dict = json.loads(to_user_json)
        count = to_user_dict.pop(from_user, 0)  # { 'from1' : 0 }
        if count == 0:  # count不为零 返回  如果上一个为0 去找下一个 from_user
            for k, v in to_user_dict.items():   # 逻辑是执行好几次这个函数
                if v:
                    from_user = k      # 来回循环 赋值0 指导所有的都成已读为止
                    count = v          # 但是顺序是不是不对,这是给最后的先走了
        to_user_dict[from_user] = 0    # 存取的时候,字典,
        to_user_json = json.dumps(to_user_dict)

    else:
        to_user_json = json.dumps({from_user: 0})
        count = 0

    RDB.set(to_user, to_user_json)
    return count, from_user

get_redis_toy 提供给rec_msg , 点击一下 , 返回一个count ,然后清空这个from_userd对应的redis未读数据, 然后通过for循环 redis的列表, 获得所有的聊天信息

firend.py

def recv_msg():
    count, from_user = get_redis_toy(chat_info.get("to_user"),chat_info.get("from_user"))
    ret = {
        'from_user' : from_user,
        'friend_type': friend_type,
        'chat_list': chat_info_list
    }
    return jsonify(ret)

bug 没有count的时候还会 收到谁的信息

    if count == 0:
        # print(chat_info_list)   # [{'from_user': '5d36b6ab06eeb0fa0765c0f7', 'to_user': '5d36b6c306eeb0fa0765c0f9', 'chat': '1563977834352.amr.mp3', 'createTime': 1563977837.0612962}, {'from_user': '5d36b6ab06eeb0fa0765c0f7', 'to_user': '5d36b6c306eeb0fa0765c0f9', 'chat': '1563977838259.amr.mp3', 'createTime': 1563977841.3143363}]
        wenjainming = text2audio(f"你没有消息")
        data = {'from_user': chat_info.get("from_user"), 'to_user': chat_info.get("to_user"), 'chat': wenjainming,
                'createTime': time.time()}
        a = []
        a.append(data)
        chat_info_list = a
        return jsonify({'chat_list': chat_info_list})

这样改了的话,只会报一次,然后就没了.

因为改了数据结构,返回了三条该前端

  $.post(serv + "/recv_msg", {
            from_user: from_user,
            to_user: toy_id,
            friend_type: document.getElementById("from_user_type").innerText
        }, function (data) {
            console.log(data);
            // 更改 返回值的类型
            // {from_user , friend_type,chat_list}
            document.getElementById("from_user_type").innerText = data.friend_type;
            document.getElementById("from_user").innerText = data.from_user;
            var chat_info = data.chat_list.pop();
            document.getElementById("player").src = serv + "/get_chat/" + chat_info.chat;
            document.getElementById("player").onended = function () {
                if (data.chat_list.length == 0) {
                    return
                }
                document.getElementById("player").src = serv + "/get_chat/" + data.chat_list.pop().chat;
            }
        }, "json")
    }

昨日bug

给小绵羊发消息

消息来自:5d36bf7d00c8bf23acf1a3f4

好友类型:toy

播放的音乐却是: 我没听清楚,请再说一次

  if '发消息' in Q:
        toy_info = MDB.Toys.find_one({'_id': ObjectId(toy_id)})
        for friend in toy_info.get('friend_list'):
            if friend.get("friend_remark") in Q or friend.get('friend_nick') in Q:
                filename = text2audio(f"现在可以给{friend.get('friend_remark')}发消息了")
                return {
                        'from_user': friend.get('friend_id'),
                        'chat': filename,
                        'friend_type': friend.get('friend_type')
                }
filename = text2audio('我没听清楚,请再说一次')
return {"from_user": "ai", "chat": filename}

bug 产生地方 :

filename : SendOk.mp3

解决顺序: 我给filename了一个字符串,就不说我没听清楚,请再说一次了

查了一下引用的text2audio(A):

发现生成的是 filename 是第一个 , 我上次生成SendOk的时候,又忘了注释了

# file_name = 'SendOk.mp3'
file_name = f"{uuid4()}.mp3"

看来,print果然可以排除写bug

这个bug 影响了好久 , 早打印的(逻辑没问题,有没有别的地方掺杂着,竟然不会处理差的远呢)

上午上了课,还以为是 接收好友请求那里出错,发现没有错,而且那里也不会去调用ai接口,所以没有可能出错(小)

刚开始的时候一直报 data.pop not a function , 去了缓存,也不行,重启之后,可以收到消息了,看rev_msg 的逻辑, 会一些知道去哪找错,才好些. 但是rec_msg 也写错了,因为我的只收到爸爸的消息,一直是(用老师的可以,不知道哪里逻辑有问题,应该是这个地方)

刚才也是得清除缓存? 标配吗!

bug : 一直收消息的话 一直是爸爸的消息 , 应该没有消息了

"{"5d36b6ab06eeb0fa0765c0f7": 0}"

my_gensim.py

import jieba
import gensim
from gensim import corpora
from gensim import models
from gensim import similarities

l1 = ["你的名字是什么", "你今年几岁了", "你有多高你胸多大", "你胸多大","[祖国祖国]-我们爱你"]
a = "我想听祖国爱你什么"

all_doc_list = []  # 问题库分词结果
for doc in l1:  # 遍历问题库
    doc_list = list(jieba.cut_for_search(doc))
    all_doc_list.append(doc_list)

print(all_doc_list)
doc_test_list = list(jieba.cut_for_search(a)) # 用户表达分词结果

# 制作语料库
dictionary = corpora.Dictionary(all_doc_list)  # 制作词袋
# 词袋的理解
# 词袋就是将很多很多的词,进行排列形成一个 词(key) 与一个 标志位(value) 的字典
# 例如: {'什么': 0, '你': 1, '名字': 2, '是': 3, '的': 4, '了': 5, '今年': 6, '几岁': 7, '多': 8, '有': 9, '胸多大': 10, '高': 11}
# 至于它是做什么用的,带着问题往下看

print("token2id", dictionary.token2id)
print("dictionary", dictionary, type(dictionary))

corpus = [dictionary.doc2bow(doc) for doc in all_doc_list]
# ['你', '的', '名字', '是', '什么']['你', '今年', '几岁', '了']
# doc2bow - 14230 1675
# [你 今年 多 大 了]
# doc2bow - 1685
# 已经可以判断相似度

# 语料库:
# 这里是将all_doc_list 中的每一个列表中的词语 与 dictionary 中的Key进行匹配
# 得到一个匹配后的结果,例如['你', '今年', '几岁', '了']
# 就可以得到 [(1, 1), (5, 1), (6, 1), (7, 1)]
# 1代表的的是 你 1代表出现一次, 5代表的是 了  1代表出现了一次, 以此类推 6 = 今年 , 7 = 几岁
print("corpus", corpus, type(corpus))

# 将需要寻找相似度的分词列表 做成 语料库 doc_test_vec
doc_test_vec = dictionary.doc2bow(doc_test_list)    # doc_test_vec: a的分词 []
# [你 今年 多 大 了]
# doc2bow - [(1,1) 6,1 8,1 5,1]
print("doc_test_vec", doc_test_vec, type(doc_test_vec)  )

# (6,6)

# 100
# 边长 +-1 = +-10
# 非正方形 -20
# 边长之间 相等 +-1 = +-10

# 边长与条件相等 + 100

# [(5,5),(8,8),(8,7),(6,5),(4,5),(4,4)]
# [ 80 , 60 , 40 , 160 , 40 , 60]




# 将corpus语料库(初识语料库) 使用Lsi模型进行训练
lsi = models.LsiModel(corpus) # LSI模型 基于 语料库 问题库而来
# lsi 将 问题 你的名字叫什么 = [(0, 1), (1, 1), (2, 1), (3, 1), (4, 1)] 计算成
# [83405798576837.28642628472 ,83405798576837.28642628472,83405798576837.28642628472]
# 支持向量机 - 向量
# 这里的只是需要学习Lsi模型来了解的,这里不做阐述
print("lsi", lsi, type(lsi))
# 语料库corpus的训练结果
print("lsi[corpus]", lsi[corpus])
# 获得语料库doc_test_vec 在 语料库corpus的训练结果 中的 向量表示
print("lsi[doc_test_vec]", lsi[doc_test_vec])

#[(123123,456456,789789,234234),(123123,456456,789789,234234)]
#[(789789,567567,345345,567567)]



# 文本相似度
# 稀疏矩阵相似度 将 主 语料库corpus的训练结果 作为初始值
index = similarities.SparseMatrixSimilarity(lsi[corpus], num_features=len(dictionary.keys()))
print("index", index, type(index))

# 将 语料库doc_test_vec 在 语料库corpus的训练结果 中的 向量表示 与 语料库corpus的 向量表示 做矩阵相似度计算
sim = index[lsi[doc_test_vec]]

print("sim", sim, type(sim))

# 对下标和相似度结果进行一个排序,拿出相似度最高的结果
# cc = sorted(enumerate(sim), key=lambda item: item[1],reverse=True)
cc = sorted(enumerate(sim), key=lambda item: -item[1])
print(cc)

text = l1[cc[0][0]]

print(a, text)

结果:

[['你', '的', '名字', '是', '什么'], ['你', '今年', '几岁', '了'], ['你', '有', '多', '高', '你', '胸多大'], ['你', '胸多大'], ['[', '祖国', '祖国', ']', '-', '我们', '爱', '你']]
token2id {'什么': 0, '你': 1, '名字': 2, '是': 3, '的': 4, '了': 5, '今年': 6, '几岁': 7, '多': 8, '有': 9, '胸多大': 10, '高': 11, '-': 12, '[': 13, ']': 14, '我们': 15, '爱': 16, '祖国': 17}
dictionary Dictionary(18 unique tokens: ['什么', '你', '名字', '是', '的']...) <class 'gensim.corpora.dictionary.Dictionary'>
corpus [[(0, 1), (1, 1), (2, 1), (3, 1), (4, 1)], [(1, 1), (5, 1), (6, 1), (7, 1)], [(1, 2), (8, 1), (9, 1), (10, 1), (11, 1)], [(1, 1), (10, 1)], [(1, 1), (12, 1), (13, 1), (14, 1), (15, 1), (16, 1), (17, 2)]] <class 'list'>
doc_test_vec [(0, 1), (1, 1), (16, 1), (17, 1)] <class 'list'>
lsi LsiModel(num_terms=18, num_topics=200, decay=1.0, chunksize=20000) <class 'gensim.models.lsimodel.LsiModel'>
lsi[corpus] <gensim.interfaces.TransformedCorpus object at 0x000000000446D780>
lsi[doc_test_vec] [(0, 1.3396458504129864), (1, -0.42361658265951974), (2, -0.46191849530731355), (3, 0.05664504741032271), (4, -0.04451043835732695)]
index <gensim.similarities.docsim.SparseMatrixSimilarity object at 0x000000000446D780> <class 'gensim.similarities.docsim.SparseMatrixSimilarity'>
sim [0.6040311  0.33766365 0.47752848 0.4775285  0.85422903] <class 'numpy.ndarray'>
[(4, 0.85422903), (0, 0.6040311), (3, 0.4775285), (2, 0.47752848), (1, 0.33766365)]
我想听祖国爱你什么 [祖国祖国]-我们爱你

my_jieba

# coding=gbk
import jieba
a = '我要听洗澡歌'
# res = jieba.cut(a)
# res1 = jieba.cut_for_search(a)
# print('res', res, list(res))   # <generator  生成器  生成器用列表循环很慢
# print('res1', res1, list(res1))
# Loading model cost 1.499 seconds. 费时, 为了大数据使用(使用大数据时很快), 小数据的时候依然要载入计算大数据的时候,所以很慢
# 设计到计算 io 就费时, 不涉及到不   # Loading model cost 1.387 seconds.   1000行1600个汉字 费时和小数据费时相同
# 但是用list取值时较为费时 且会打印jieba日志

# print(jieba.cut_for_search(b))   # <generator object Tokenizer.cut_for_search at 0x00000000039A8A40>

print(list(jieba.cut_for_search(b)))    # ['法医', '鉴定', '法医鉴定',  分词更清晰 把认为可能出现的情况都分出来,增大识别的几率

my_pinyin

from pypinyin import lazy_pinyin, TONE, TONE2, TONE3
a = '我要给小狐狸发消息'
res1 = lazy_pinyin(a, style=TONE)
res2 = lazy_pinyin(a, style=TONE2)
res3 = lazy_pinyin(a, style=TONE3)
print(res1)  # ['wǒ', 'yào', 'gěi', 'xiǎo', 'hú', 'lí', 'fā', 'xiāo', 'xī'] 效率最低 下面两个还可以 还有音调的
print(res2)  # ['wo3', 'ya4o', 'ge3i', 'xia3o', 'hu2', 'li2', 'fa1', 'xia1o', 'xi1']
print(res3)  # ['wo3', 'yao4', 'gei3', 'xiao3', 'hu2', 'li2', 'fa1', 'xiao1', 'xi1']

# 如果有许多同名的怎么办?
# 给不同的用户编写序号

封装成函数版的my_nlp

import jieba
import gensim
from gensim import corpora
from gensim import models
from gensim import similarities
from setting import MDB
l1 = list(MDB.Content.find({}))  # 别忘了list是啥
all_doc_list = []  # 问题库分词结果
for doc in l1:  # 遍历问题库
    doc_list = list(jieba.cut_for_search(doc.get('title')))  # 获得歌曲 不只有doc  照抄都超不好啊
    all_doc_list.append(doc_list)

dictionary = corpora.Dictionary(all_doc_list)  # 制作词袋
corpus = [dictionary.doc2bow(doc) for doc in all_doc_list]
lsi = models.LsiModel(corpus)  # LSI模型 基于 语料库 问题库而来
index = similarities.SparseMatrixSimilarity(lsi[corpus], num_features=len(dictionary.keys()))

def my_nlp(a):

    doc_test_list = list(jieba.cut_for_search(a))  # 用户表达分词结果
    doc_test_vec = dictionary.doc2bow(doc_test_list)  # doc_test_vec: a的分词 []
    sim = index[lsi[doc_test_vec]]
    cc = sorted(enumerate(sim), key=lambda item: -item[1])
    if cc[0][1] >= 0.78:    # 防止出现不识别也会匹配的情况    # [(4, 0.85422903),
        text = l1[cc[0][0]]
    print(text)
    return text.get('cover')



封装了函数 歌曲的功能, title

自己我要听什么歌的时候 返回, 还有给谁发消息的时候, 怎么处理

原先的my_nlp_low

def my_nlp_low(Q, toy_id):  #
    # Q = '我要听/ 我想听 / 请播放 +  洗澡歌'
    print(Q)
    if '我要听' in Q or '我想听' in Q or '请播放' in Q:    # 也可以列表 for 循环  in
        for content in MDB.Content.find({}):    # 生成器是不占用内存的 内存?内核?
            if content.get('title') in Q:            # 牺牲效率, 节省内存空间
                return {"from_user": "ai", "music": content.get('music')}

    if '发消息' in Q:
        toy_info = MDB.Toys.find_one({'_id': ObjectId(toy_id)})
        for friend in toy_info.get('friend_list'):
            if friend.get("friend_remark") in Q or friend.get('friend_nick') in Q:
                filename = text2audio(f"现在可以给{friend.get('friend_remark')}发消息了")
                # filename = text2audio("现在可以给 'friend_remark' 发消息了")

                # print('小绵羊',  friend.get('friend_nick'))
                # print(filename)
                return {
                        'from_user': friend.get('friend_id'),
                        'chat': filename,
                        'friend_type': friend.get('friend_type')
                }
    # 对接图灵机器人
    if '苹果' in Q:
        A = go_tl(Q)
        filename = text2audio(A)
        return {
            'from_user': 'xiaoai',
            'chat': filename,
        }

    filename = text2audio('我没听清楚,请再说一次')
    print('我没听清楚不走的把,上面return了', filename)
    return {"from_user": "ai", "chat": filename}

调用自己的my_nlp 库

def my_nlp_low(Q, toy_id):  #
    # Q = '我要听/ 我想听 / 请播放 +  洗澡歌'
    print(Q)
    if '我要听' in Q or '我想听' in Q or '请播放' in Q:    # 也可以列表 for 循环  in
        content = my_nlp(Q)
        print(content)
        if content:
            return {"from_user": "ai", "music": content.get('music')}

猜你喜欢

转载自www.cnblogs.com/Doner/p/11253396.html