def findSimilarEntitys(entity, list):
    dictionary = {}  # 用于计算相似度后,把解决方案、对应id、相似度存入字典,根据相似度大小逆序输出
    for desc in list:
        similar = cosin.sentence_resemble(entity, desc)
        if similar > 0.6:
            dictionary.update({desc: round(similar * 100,
                                           2)})  # 将id,描述,解决方法,以及对应的相似度加入字典
    dictionary = sorted(dictionary.items(),
                        key=lambda item: item[1],
                        reverse=True)  # 对字典根据相似度进行降序排序
    #暂且全部输出
    return dictionary
def huida(q_type, zhuyu):
    print(zhuyu, ':')
    daanlist = []
    # 某故障原因 会引起哪些现象
    if q_type == 0:
        relationyuanyins = []
        for yuanyindb in findEntitiesByType('Yuanyin'):
            similar = cosin.sentence_resemble(zhuyu, yuanyindb)
            if similar > 0.8:
                relationyuanyins.append(yuanyindb)
        print('relationyuanyins:', relationyuanyins)
        for relationyuanyin in relationyuanyins:
            daanlist += findEntities2(relationyuanyin, '间接原因')
        templist = []
        for temp in daanlist:
            if temp.isalnum():
                templist.append(temp + '报警')
        daanlist = templist
    # 做什么操作 会遇到什么错误
    if q_type == 1:
        relationcaozuos = []
        for caozuodb in findEntitiesByType('Caozuo'):
            similar = cosin.sentence_resemble(zhuyu, caozuodb)
            if similar > 0.8:
                relationcaozuos.append(caozuodb)
        print('relationcaozuos:', relationcaozuos)
        for caozuo in relationcaozuos:
            daanlist += findEntities(caozuo, '引起')

    # 某部位 常出现哪些故障
    if q_type == 2:
        daanlist = findEntities2(zhuyu, '故障部位')
    # 报警的含义是什么
    if q_type == 3:
        daanlist = findEntities(zhuyu, '直接原因')
    temp = {}
    temp = temp.fromkeys(daanlist)
    daanlist = list(temp.keys())
    return daanlist
Exemple #3
0
def insertPa(pinpai, xinghao, errorid, describe, selectedList, yuanyin,
             answer):
    jieba.load_userdict(os.getcwd() + '\\Shukongdashi\\demo\\fencidian.txt')
    pattern = r'\.|;|。|;|!'
    pattern2 = r',|,'

    # 加载停用词
    stopwords = []
    with open(os.getcwd() + '\\Shukongdashi\\demo\\stopwords.txt',
              'r',
              encoding='utf-8') as f:
        st = f.readlines()
    for line in st:
        line = line.strip().encode('utf-8').decode('utf-8-sig')
        stopwords.append(line)
    # 加载故障部位
    buweizhuyu = []
    with open(os.getcwd() + '\\Shukongdashi\\demo\\zhuyu.txt',
              'r',
              encoding='utf-8') as f:
        st = f.readlines()
    for line in st:
        line = line.strip().encode('utf-8').decode('utf-8-sig')  # 防止BOM现象
        buweizhuyu.append(line)

    miaoshu = describe.replace(' ', '')
    print(u'正在过滤停用词......')
    miaoshu_baocun = []
    miaoshu_cut = jieba.cut(miaoshu)
    for m in miaoshu_cut:
        if m not in stopwords:
            miaoshu_baocun.append(m)
    miaoshu = ''.join(miaoshu_baocun)

    miaoshu_jvzi = re.split(pattern, miaoshu)
    miaoshu_jvzi = list(filter(None, miaoshu_jvzi))
    # 保存描述中描述品牌型号、执行操作、报警现象的内容
    miaoshu_caozuo = []
    miaoshu_xianxiang = []
    for jvzi in miaoshu_jvzi:
        # 把句子进一步拆分
        miaoshu_list = re.split(pattern2, jvzi)
        miaoshu_list = list(filter(None, miaoshu_list))
        # 判断类型,把相同的类型放到一起
        for miaoshu in miaoshu_list:
            miaoshu_type = cnnModel.predict(miaoshu)
            if miaoshu_type == '执行操作':
                miaoshu_caozuo.append(miaoshu)
            elif miaoshu_type == '故障现象':
                miaoshu_xianxiang.append(miaoshu)
    # 到此,不同的描述放到了不同的list里
    print('执行操作:', miaoshu_caozuo)
    print('故障现象:', miaoshu_xianxiang)
    yuanyin_this = ''
    try:
        yuanyin_this = miaoshu_xianxiang[len(miaoshu_xianxiang) - 1] + '的故障'
    except:
        yuanyin_this = '未知原因引起的故障'
    print(yuanyin_this)
    erroridlist = re.split(',|,', errorid)
    buweilist = []
    for sentence in miaoshu_xianxiang:
        # 处理顿号“、”
        sentence_seged = jieba.posseg.cut(sentence.strip())
        for x in sentence_seged:
            if (x.flag == 'n' or x.flag == 'x'):
                if x.word in buweizhuyu:
                    buweilist.append(x.word)
            if '报警' in sentence:
                if x.flag == 'eng' or x.flag == 'm':
                    if not (x.word >= u'\u4e00'
                            and x.word <= u'\u9fa5'):  # 判断是否是汉字
                        erroridlist.append(x.word)
    print(buweilist, erroridlist)
    # 相关的操作
    similarcaozuos = miaoshu_caozuo
    for i, caozuo in enumerate(miaoshu_caozuo):
        for caozuodb in findEntitiesByType('Caozuo'):
            similar = cosin.sentence_resemble(caozuo, caozuodb)
            if similar > 0.8:
                similarcaozuos[i] = caozuodb
    # 相关故障
    relationguzhangs = []
    # 相关操作引起的现象
    for caozuo in similarcaozuos:
        relationguzhangs += findEntities(caozuo, '引起')

    # 根据故障部位和报警信息寻找现象

    for entity2 in buweilist:
        relationguzhangs += findEntities2(entity2, '故障部位')
    for entity2 in erroridlist:
        relationguzhangs += findEntities2(entity2, '报警信息')
    print('relationguzhangs:', relationguzhangs)

    similarxianxiang = miaoshu_xianxiang  # selectedList
    for i, xianxiang in enumerate(miaoshu_xianxiang):
        for relationguzhang in relationguzhangs:
            similar = cosin.sentence_resemble(xianxiang, relationguzhang)
            if similar > 0.8:
                similarxianxiang[i] = relationguzhang
    print('similarxianxiang:', similarxianxiang)
    # similarcaozuos,similarxianxiang,erroridlist,
    # yuanyin,jiejuebanfa

    # 初始化操作和故障,故障和原因之间的关系
    for xianxiang in similarxianxiang:
        for caozuo in similarcaozuos:
            insertTwoNodes(caozuo, '引起', xianxiang, 'Caozuo', 'Xianxiang')
        insertTwoNodes(xianxiang, '间接原因', yuanyin_this, 'Xianxiang', 'Yuanyin')
    # 故障现象之间的关联关系
    zuhe_xianxiaing = list(combinations(similarxianxiang, 2))
    for zuhe in zuhe_xianxiaing:
        insertTwoNodes(zuhe[0], '相关', zuhe[1], 'Xianxiang', 'Xianxiang')
        insertTwoNodes(zuhe[1], '相关', zuhe[0], 'Xianxiang', 'Xianxiang')

    for sentence in similarxianxiang:
        # 处理顿号“、”
        sentence_seged = jieba.posseg.cut(sentence.strip())
        zhuyu = []

        for x in sentence_seged:
            if (x.flag == 'n' or x.flag == 'x'):
                if x.word in buweizhuyu:
                    insertTwoNodes(sentence, '故障部位', x.word, 'Xianxiang',
                                   'GuzhangBuwei')
            if '报警' in sentence:
                if x.flag == 'eng' or x.flag == 'm':
                    if not (x.word >= u'\u4e00'
                            and x.word <= u'\u9fa5'):  # 判断是否是汉字
                        insertTwoNodes(sentence, '报警信息', x.word, 'Xianxiang',
                                       'Errorid')
    # 报警信息和所有现象关联
    # 最好判断一下是否已经存在关系了

    answer = answer.replace("'", "\\\'").replace('"', '\\\"')
    t = (yuanyin_this, describe + ",".join(selectedList), answer)
    sql = "insert into guzhanganli values('%s','%s','%s')" % t
    print(sql)
    # try:
    # 执行SQL语句
    cursor.execute(sql)
    # 获取所有记录列表
    db_mysql.commit()  # 提交,不然无法保存插入或者修改的数据(这个一定不要忘记加上)
    #cursor.close()
    # 关闭数据库连接
    #db_mysql.close()  # 关闭连接
    return True
Exemple #4
0
def getTuili(pinpai, xinghao, errorid, describe, relationList, ret_dict):
    print( errorid, describe, relationList, ret_dict)
    if errorid!='':
        describe += ',' + errorid + '报警'
    print('目录:',os.path.dirname(os.getcwd()))
    jieba.load_userdict(os.getcwd()+'\\Shukongdashi\\demo\\fencidian.txt')
    pattern = r'\.|;|。|;|!'
    pattern2 = r',|,'

    # 加载停用词
    stopwords = []
    with open(os.getcwd()+'\\Shukongdashi\\demo\\stopwords.txt', 'r', encoding='utf-8') as f:
        st = f.readlines()
    for line in st:
        line = line.strip().encode('utf-8').decode('utf-8-sig')
        stopwords.append(line)
    # 加载故障部位
    buweizhuyu = []
    with open(os.getcwd()+'\\Shukongdashi\\demo\\zhuyu.txt', 'r', encoding='utf-8') as f:
        st = f.readlines()
    for line in st:
        line = line.strip().encode('utf-8').decode('utf-8-sig')  # 防止BOM现象
        buweizhuyu.append(line)
    miaoshu = describe.replace(' ', '')
    print(u'正在过滤停用词......')
    miaoshu_baocun = []
    miaoshu_cut = jieba.cut(miaoshu)
    for m in miaoshu_cut:
        if m not in stopwords:
            miaoshu_baocun.append(m)
    miaoshu = ''.join(miaoshu_baocun)

    miaoshu_jvzi = re.split(pattern, miaoshu)
    miaoshu_jvzi = list(filter(None, miaoshu_jvzi))
    # 保存描述中描述品牌型号、执行操作、报警现象的内容
    miaoshu_xinghao = []
    miaoshu_caozuo = []
    miaoshu_xianxiang = []
    for jvzi in miaoshu_jvzi:
        # 把句子进一步拆分
        miaoshu_list = re.split(pattern2, jvzi)
        miaoshu_list = list(filter(None, miaoshu_list))
        # 判断类型,把相同的类型放到一起
        for miaoshu in miaoshu_list:
            miaoshu_type = cnnModel.predict(miaoshu)
            if miaoshu_type == '机床类型':
                miaoshu_xinghao.append(miaoshu)
            elif miaoshu_type == '执行操作':
                miaoshu_caozuo.append(miaoshu)
            elif miaoshu_type == '故障现象':
                miaoshu_xianxiang.append(miaoshu)
    # 到此,不同的描述放到了不同的list里
    print(miaoshu_caozuo, miaoshu_xianxiang)
    # 处理品牌型号描述
    miaoshu_xinghao = ''.join(miaoshu_xinghao)
    pinpai_xinghao = ''.join(re.findall(u'[0-9a-zA-Z]+', miaoshu_xinghao))
    if pinpai_xinghao != '':
        seg_list = list(jieba.cut(pinpai_xinghao, cut_all=False))
        pinpai = seg_list[0]
        xinghao = ''
        if len(seg_list) > 1:
            xinghao = seg_list[1]
        print(pinpai, xinghao)
    # 处理操作

    # 处理故障描述,寻找部位和故障代码
    buweilist = []
    erroridlist = []
    for sentence in miaoshu_xianxiang:
        # 处理顿号“、”
        sentence_seged = jieba.posseg.cut(sentence.strip())
        for x in sentence_seged:
            if (x.flag == 'n' or x.flag == 'x'):
                if x.word in buweizhuyu:
                    buweilist.append(x.word)
            if '报警' in sentence:
                if x.flag == 'eng' or x.flag == 'm':
                    if not (x.word >= u'\u4e00' and x.word <= u'\u9fa5'):  # 判断是否是汉字
                        erroridlist.append(x.word)
    # print(buweilist, erroridlist)
    # 根据执行了的操作推理导致的现象
    # 相关的操作
    similarcaozuos = []
    for caozuo in miaoshu_xianxiang:
        for caozuodb in findEntitiesByType('Caozuo'):
            similar = cosin.sentence_resemble(caozuo, caozuodb)
            if similar > 0.8:
                similarcaozuos.append(caozuodb)
    # print('similarcaozuos:', similarcaozuos)
    # 相关故障
    relationguzhangs = []
    # 相关操作引起的现象
    for caozuo in similarcaozuos:
        relationguzhangs += findEntities(caozuo, '引起')

    # 根据故障部位和报警信息寻找现象

    for entity2 in buweilist:
        relationguzhangs += findEntities2(entity2, '故障部位')
    for entity2 in erroridlist:
        relationguzhangs += findEntities2(entity2, '报警信息')
    # print('relationguzhangs:', relationguzhangs)
    # 寻找相似现象
    similarxianxiang = miaoshu_xianxiang
    for i,xianxiang in enumerate(miaoshu_xianxiang):
        for relationguzhang in relationguzhangs:
            similar = cosin.sentence_resemble(xianxiang, relationguzhang)
            if similar > 0.8:
                similarxianxiang[i] = relationguzhang
    print('similarxianxiang:', similarxianxiang)
    # 找出所有相似现象
    if len(buweilist) == 0 or len(erroridlist) == 0:
        for i,xianxiang in enumerate(similarxianxiang):
            for xianxiangdb in findEntitiesByType('Xianxiang'):
                similar = cosin.sentence_resemble(xianxiang, xianxiangdb)
                if similar > 0.8:
                    similarxianxiang[i] = xianxiangdb
        print('similar:',similarxianxiang)
        print('relationList:', relationList)
    similarxianxiang += relationList

    # print('similarxianxiang:',similarxianxiang)
    temp = {}
    temp = temp.fromkeys(similarxianxiang)
    selectedlist = list(temp.keys())
    # 推理原因,推理相关现象
    tuilixianxiang = []
    for xianxiang in selectedlist:
        tuilixianxiang += findEntities(xianxiang, '相关')
    # 删除重复
    tuilixianxiangtemp = []
    for chongfu in tuilixianxiang:
        if (chongfu not in selectedlist) and (chongfu not in tuilixianxiangtemp):
            tuilixianxiangtemp.append(chongfu)
    tuilixianxiang = tuilixianxiangtemp
    # 根据这个error_list查找一个和里面元素相关的元素,作数组返回
    ret_dict['selectedlist'] = selectedlist
    ret_dict['hiddenlist'] = tuilixianxiang
    count = {}
    # 根据现象查找可能的原因,将可能的原因作为键,将契合的次数作为值
    for xianxiang in selectedlist:
        jainjieyuanyindb = db.findOtherEntities(xianxiang, "间接原因")
        selected_index = [i for i in range(len(jainjieyuanyindb))]
        for i in selected_index:
            jianjieyuanyin = jainjieyuanyindb[i]['n2']['title']
            count.setdefault(jianjieyuanyin, 0)
            count[jianjieyuanyin] = count[jianjieyuanyin] + 1
    # 根据可能性重新对最终原因进行排序
    for i, j in count.items():
        # print(str(i) + ":", j)
        relathionCount = db.findNumberOfEntities1(i, "间接原因")[0]["relathionCount"]
        count[i] = round(j / relathionCount, 2)
    # 对相关原因按匹配次数排序
    list1 = sorted(count.items(), key=lambda x: x[1], reverse=True)
    for yuanyinItem in list1:
        # 查解决方法
        jiejuelist = []
        db_mysql = pymysql.connect(host='localhost', user='******', password='******', db='sg_faq')
        cursor = db_mysql.cursor()  # 获取指针以操作数据库
        cursor.execute('set names utf8')
        print(yuanyinItem)
        sql = "select guzhangfenxi FROM guzhanganli where guzhangyuanyin = '%s'" % (yuanyinItem[0])
        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                jiejuelist.append(row[0])
        except:
            print("Error: unable to fetch data")
        # 关闭数据库连接
        db_mysql.close()
        # 查找间接导致原因的现象个数#计算可能性
        xianxiangdb = db.findOtherEntities2(yuanyinItem[0], "间接原因")
        relathionCount = len(xianxiangdb)
        selected_index2 = [i for i in range(len(xianxiangdb))]
        tuililist = []
        for i in selected_index2:
            xianxiang = xianxiangdb[i]['n1']['title']
            if xianxiang in selectedlist:
                zhijieyuanyindb = db.findOtherEntities(xianxiang, "直接原因")
                if len(zhijieyuanyindb) > 0:
                    zhijieyuanyi = zhijieyuanyindb[0]['n2']['title']
                    tuililist.append(
                        {"entity1": xianxiang, "rel": "含义", "entity2": zhijieyuanyi, "entity1_type": "故障代码",
                         "entity2_type": "含义"})
                    tuililist.append(
                        {"entity1": zhijieyuanyi, "rel": "间接原因", "entity2": yuanyinItem[0], "entity1_type": "含义",
                         "entity2_type": "最终原因"})
                else:
                    tuililist.append(
                        {"entity1": xianxiang, "rel": "间接原因", "entity2": yuanyinItem[0], "entity1_type": "现象",
                         "entity2_type": "最终原因"})
        for jiejue in jiejuelist:
            tuililist.append({"entity1": yuanyinItem[0], "rel": "解决办法", "entity2": jiejue, "entity1_type": "最终原因",
                              "entity2_type": "解决办法"})
        if (ret_dict.get('list') is None):
            ret_dict['list'] = []
        ret_dict['list'].append(
            {"yuanyin": yuanyinItem[0], "answer": jiejuelist, "possibility": yuanyinItem[1], "list": tuililist})
        print(ret_dict)
    return ret_dict