예제 #1
0
def pre_check(condition):
    if len(str(condition['gpa']).strip()) == 0 :
        condition['gpa'] = -1
    if len(str(condition['toefl']['total'])) < 2:
        condition['toefl']['total'] = -1
    if len(str(condition['gre']['total'])) < 2:
        condition['gre']['total'] = -1
    if len(str(condition['current-school']).strip()) == 0 :
        condition['current-school'] = 10

    if type(condition['target_level']) != int:
        target_level = str(condition['target_level']).strip('\n').strip('\r').replace(r'"', '').replace('\'', '')
        try:
            condition['target_level'] = int(target_level)
        except:
            return exit_error_func('target_level:'+str(condition['target_level']), 1)
    if condition['target_level'] > 4 or condition['target_level'] < 1:
        return exit_error_func('target_level:'+str(condition['target_level']), 2)

    if type(condition['grade']) != int:
        grade = str(condition['grade']).strip('\n').strip('\r').replace(r'"', '').replace('\'', '')
        try:
            tmp_grade = get_start_term(int(grade))
            if type(tmp_grade) ==int:
                condition['grade'] = get_start_term(int(grade))
            else:
                return exit_error_func('grade:'+str(condition['grade']), 1)
        except:
            return exit_error_func('grade:'+str(condition['grade']), 1)
    if condition['grade'] > 14 or condition['grade'] < 1:
        return exit_error_func('grade:'+str(condition['grade']), 2)

    return condition
예제 #2
0
def assess_applier(applier_dict):
    rule_type = applier_dict['major']
    rule_type = rule_type.strip('\r').strip('\n').replace(r'"', '').replace('\'', '')

    if len(str(applier_dict['gpa'])) > 5:
        try:
            temp_dict = translateFromFrontToBack(applier_dict)
            if len(temp_dict['mismatch']) == 0:
                temp_dict = temp_dict['result']
            else:
                return exit_error_func(u'转换出错参数列表:'+str(temp_dict['mismatch']), 1)
        except:
            return exit_error_func(u'转换参数出错:'+str(applier_dict), 1)

    else:
        temp_dict = applier_dict.copy()
    if rule_type in RULE_TYPE_DICT:
        rule_type = RULE_TYPE_DICT[rule_type]

    if rule_type not in ASSESS_RULE_DICT:
        rule_type = 'general'

    remove_stop_seg(temp_dict, rule_type)

    for equation in ASSESS_RULE_DICT[rule_type]:
        is_successful = execute_equation(temp_dict, equation)

    try:
        display_score = display_value(temp_dict['result'], int(temp_dict['level']), rule_type)
    except:
        try:
            base_score = int(level_dict[rule_type].split('-')[1])
            display_score = temp_dict['result']/base_score * 100
        except:
            return exit_error_func(applier_dict, 3)
    display_score = 99.0 if display_score > 99.0 else display_score

    score = round(float(temp_dict['result']), 1)
    seg_score = get_seg_score(temp_dict.copy(), rule_type)

    return_dict = {}
    return_dict = {
        'level': get_segment_level(),
        'score': score,
        'result_level': LEVEL_SEGMENT_DICT['level'],
        'display_score': display_score,
        'seg_full_score': FULL_SCORE_DICT[rule_type],
        'seg_score': seg_score,
    }

    try:
        return return_dict
    except:
        return exit_error_func(applier_dict, 3)
예제 #3
0
def _load_products_by_tag():
    path_plan_logger.info('[starting] loading product information from mongodb . . . ')
    try:
        for each in open('conf/db.conf', 'r', encoding='utf-8').readlines():
            try:
                each = each.strip('\r').strip('\n')
                if(each.split('=')[0] == 'mongodb.url'):
                    url = each.split('=')[1]
                if(each.split('=')[0] == 'mongodb.port'):
                    port = int(each.split('=')[1])
                if(each.split('=')[0] == 'mongodb.dulishuo.username'):
                    username = each.split('=')[1]
                if(each.split('=')[0] == 'mongodb.dulishuo.password'):
                    password = each.split('=')[1]
            except Exception as e:
                path_plan_logger.error(e)
                path_plan_logger.error('some line is wrong when read .')
                path_plan_logger.info('wrong line : %s ', each)
                return
    except FileNotFoundError:
        path_plan_logger.error('File resource/db.conf not found . . . ')
        return exit_error_func(3)
    except Exception as e:
        path_plan_logger.error(e)
        path_plan_logger.error(3, 'mongodb configuration failed.')
        return exit_error_func(3)
    path_plan_logger.info('[successed] load resource/db.conf')
    mongo_client = MongoDB(host=url, port=port, username=username, password=password, auth=True)
    mongo_client.get_database('dulishuo')
    product_collection = mongo_client.get_collection('product')
    producttag_collection = mongo_client.get_collection('producttag')
    tag_list = ['提升GPA','提升托福成绩','提升雅思成绩','提升GRE成绩','提升GMAT成绩','竞赛','实习','证书','奖学金','活动','推荐信','科研能力提升']
    tag_dict = _get_tag_dict(tag_list,producttag_collection)
    path_plan_logger.info('[successed] get tag dict')
    
    for tag_name in tag_dict:
        products_list = []
        for record in product_collection.find({'tag':{'$regex':tag_dict[tag_name]}},{'id':1,'title':1,'title_pic':1,'tag':1}):
            product = {}
            try:
                product['product_id'] = record['id']
                product['title'] = record['title']
                product['picture'] = record['title_pic']
            except KeyError:
                continue
            products_list.append(product)
        PRODUCT_RECOMMEND[tag_name] = products_list
    product_load_time = time.time()
    try:
        mongo_client.close() # 关闭连接
        path_plan_logger.info('close pymongo connection successed.')
    except Exception as e:
        path_plan_logger.error('close pymongo connection failed.')
예제 #4
0
def schedule(origin_condition):
    return_dict = {}

    try:
        ccondition = translateFromFrontToBack(origin_condition)
        if len(ccondition['mismatch']) == 0:
            condition = ccondition['result']
        else:
            for each in WHITE_PARAM:
                if each in ccondition['mismatch']:
                    return exit_error_func(u'转换出错参数列表:'+str(ccondition['mismatch']), 1)
            condition = condition = ccondition['result']

    except Exception, e:
        return exit_error_func(u'转换参数出错:'+str(origin_condition), 1)
예제 #5
0
파일: search.py 프로젝트: SmartRiver/python
def search_school(condition, major=None, area=None, country=None):
    """ 学校查找"""
    if condition == "":
        return {"status": "success", "result": []}
    try:
        global SCHOOL_TRIE
        search_result = SCHOOL_TRIE.search(SCHOOL_TRIE.root, condition.lower())
    except:
        search_logger.error("no normal result returns .")
        return exit_error_func(3, "condition: %s" % condition)

    # 去重
    search_result_set = set()
    for each in search_result:
        search_result_set.add(each)

    result = _school_filter_by_country(search_result_set, country=country)  # 如果指定国家,则只显示匹配到该国家的院校

    if area != None:
        result = _school_sort_by_area(result, area=area)  # 如果指定地区,则将该地区匹配到的院校优先排列

    result = _sorted_school_weight(result)  # 将匹配到的院校集合按照档次权重值排序

    if len(result) > 10:  # 如果结果院校数量大于10个,则取前10个返回
        result = result[:10]

    if major not in SCHOOL_SPECIAL_MAJOR:
        major = None
    if major != None:
        _school_filter_by_major(result, major=major)  # 如果是该专业的优势学校,并且不是清华北大,则变成'综合前十或者专业优势学校'

    return {"status": "success", "result": result}
예제 #6
0
def _get_hard_condition(student_info, language_type, exam_type):
    if 'data' in student_info:
        user_data = student_info['data']
    else:
        path_plan_logger.error('缺少字段student_info[data]')
        raise Exception('缺少字段student_info[data]')
    temp_hard_condition = {}
    try:
        temp_hard_condition['gpa'] = convert_to_float(user_data['gpa']['score'])
        if language_type == 'ielts':
            temp_hard_condition['ielts'] = convert_to_float(user_data[language_type]['total'])
        elif language_type == 'toefl':
            temp_hard_condition['toefl'] = convert_to_float(user_data[language_type]['total'])
        else:
            temp_hard_condition['ielts'] = 0
            temp_hard_condition['toefl'] = 0
            language_type = 'toefl'
            
        if exam_type == 'gre':
            temp_hard_condition['gre'] = convert_to_float(user_data[exam_type]['total'])
        elif exam_type == 'gmat':
            temp_hard_condition['gmat'] = convert_to_float(user_data[exam_type]['total'])
        else:
            temp_hard_condition['gre'] = convert_to_float(user_data[exam_type]['total'])
            temp_hard_condition['gmat'] = convert_to_float(user_data[exam_type]['total'])
            exam_type = 'both'
            
    except Exception as e:
        path_plan_logger.error('硬性条件比例获取时出错:'+str(e))
        return exit_error_func(6, '硬性条件比例获取时出错:'+str(e))

    return temp_hard_condition, language_type, exam_type
예제 #7
0
파일: search.py 프로젝트: XuXiaoxue/python
def search_school(condition, major=None, area=None):
    if condition == '':
        return{
            'status': 'success',
            'result': [],
        }
    try:
        global SCHOOL_TRIE
        search_result = SCHOOL_TRIE.search(SCHOOL_TRIE.root, condition)
    except:
        search_logger.error('no normal result returns .')
        return exit_error_func(3, 'condition: %s' % condition)

    # 去重
    search_result_set = set()
    for each in search_result:
        search_result_set.add(each)

    result = __school_sort_by_area(search_result_set, area=area)

    result = __sorted_school_weight(result)

    if len(result) > 10: # 如果结果院校数量大于10个,则取前10个返回
        result = result[:10]
    if major not in SCHOOL_SPECIAL_MAJOR:
        major = None
    if major != None:
        __school_filter_by_major(result, major=major)

    #print('result:%s' % result)
    return{
        'status': 'success',
        'result': result,
    }
예제 #8
0
def get_soft_condition(user_condition):
    if 'dimension' in user_condition:
        dimension = user_condition['dimension']
    else:
        raise Exception('缺少字段dimension')

    if 'dimension_full' in user_condition:
        dimension_full = user_condition['dimension_full']
    else:
        raise Exception('缺少字段dimension_full')
    temp_soft_condition = {}
    try:
        if 'research_dimension' in dimension:
            temp_soft_condition['research'] = convert_to_float(dimension['research_dimension'])/convert_to_float(dimension_full['research_dimension'])
        if 'internship_dimension' in dimension:
            temp_soft_condition['internship'] = convert_to_float(dimension['internship_dimension'])/convert_to_float(dimension_full['internship_dimension'])
        if 'activity_dimension' in dimension:
            temp_soft_condition['activity'] = convert_to_float(dimension['activity_dimension'])/convert_to_float(dimension_full['activity_dimension'])
        if 'scholarship_dimension':
            temp_soft_condition['scholarship'] = convert_to_float(dimension['scholarship_dimension'])/convert_to_float(dimension_full['scholarship_dimension'])
        if 'credential_dimension' in dimension:
            temp_soft_condition['credential'] = convert_to_float(dimension['credential_dimension'])/convert_to_float(dimension_full['credential_dimension'])
        if 'competition_dimension' in dimension:
            temp_soft_condition['competition'] = convert_to_float(dimension['competition_dimension'])/convert_to_float(dimension_full['competition_dimension'])
    except Exception as e:
        return exit_error_func(6, '软性条件比例获取时出错:'+str(e))

    return temp_soft_condition
예제 #9
0
def assess_applier(applier_dict):
    rule_type = applier_dict['major']
    rule_type = rule_type.strip('\r').strip('\n').replace(r'"', '').replace('\'', '')
    print 'before . . . '
    print 'major  ' +rule_type
    print 'current-school: '+ str(applier_dict['current-school'])
    print json.dumps(applier_dict, ensure_ascii=False, indent=4)

    temp_dict = {}
    if len(str(applier_dict['gpa'])) > 5:
        try:
            print 'afrer convert . . . '
            print json.dumps(temp_dict, ensure_ascii=False, indent=4)
            temp_dict = translateFromFrontToBack(applier_dict)
            if len(temp_dict['mismatch']) == 0:
                temp_dict = temp_dict['result']
            else:
                return exit_error_func(u'转换出错参数列表:'+str(temp_dict['mismatch']), 1)
        except Exception, e:
            return exit_error_func(u'转换参数出错:'+str(applier_dict), 1)
예제 #10
0
def assess(student_info):
    major = ''
    #学生结果字典
    result = {}
    #满分结果字典
    result_full = {}
    try:
        #检测第一层键是否齐全,不齐全则抛出异常,因为之后的规划没法做了
        if 'major' in student_info.keys():
            major = student_info['major']
        else:
            raise Exception('传入的学生信息没有键值"major",请重新检查学生信息结构')
        if not 'target' in student_info.keys():
            raise Exception('传入的学生信息没有键值"target",请重新检查学生信息结构')
        if not 'grade' in student_info.keys():
            raise Exception('传入的学生信息没有键值"grade",请重新检查学生信息结构')
        if not 'data' in student_info.keys():
            raise Exception('传入的学生信息没有键值"data",请重新检查学生信息结构')
        
        #匹配专业,如果没有具体的评估规则,则将major转为general进行评估
        if not major in MAJOR:
            major = 'general'
        else:
            if not major in WEIGHT.keys():
                major = MAJOR[major]
        if not 'reletter' in student_info['data'].keys():
            student_info['data']['reletter'] = {}
            student_info['data']['reletter']['level'] = ['3','3','3']
        if not 'credential' in student_info['data']:
            student_info['data']['credential'] = {}
            student_info['data']['credential']['level'] = "2"
            
        #将学生信息中的有效项根据权值映射关系替换为为权值,剔除无效项
        student_weight_data = map_weight(student_info, WEIGHT[major], major)

        #根据学生信息的有效项和该专业的运算规则,进行结果计算,并返回结果
        result = exec_rule(student_weight_data, RULE[major])

        #将学生信息中的有效项根据权值映射关系替换为权值最大值
        student_weight_full_data = fill_with_full(copy.deepcopy(student_info['data']), MAX[major])

        #获得根据各项权值最大值计算的结果
        result_full = exec_rule(student_weight_full_data, RULE[major])

    except Exception as e:
        return exit_error_func(1, '接口调用失败,错误信息:\n'+str(e))

    result['dimension_full'] = result_full['dimension']
    result['result_full'] = result_full['result']
    result['student_info'] = student_info
    return {
        'status': 'success',
        'result': result
    }
예제 #11
0
def get_hard_condition(user_condition, language_type, exam_type):
    if 'student_info' in user_condition:
        student_info = user_condition['student_info']
    else:
        raise Exception('缺少字段student_info')
    if 'data' in student_info:
        user_data = student_info['data']
    else:
        raise Exception('缺少字段student_info[data]')
    temp_hard_condition = {}
    try:
        temp_hard_condition['gpa'] = convert_to_float(user_data['gpa']['score'])
        if language_type == 'ielts':
            temp_hard_condition['ielts'] = convert_to_float(user_data[language_type]['total'])
        if language_type == 'toefl':
            temp_hard_condition['toefl'] = convert_to_float(user_data[language_type]['total'])
        if exam_type == 'gre':
            temp_hard_condition['gre'] = convert_to_float(user_data[exam_type]['total'])
        if exam_type == 'gmat':
            temp_hard_condition['gmat'] = convert_to_float(user_data[exam_type]['total'])
    except Exception as e:
        return exit_error_func(6, '硬性条件比例获取时出错:'+str(e)')

    return temp_hard_condition
예제 #12
0
파일: search.py 프로젝트: ja150/python
def init(dict_from='mongodb'):
    '''初始化'''

    start_time = time.time()
    _logging_conf()
    search_logger.info('----------initializing----------')
    search_logger.info('type of reload : %s' % dict_from)
    try:
        for each in open('resource/school/special_school.txt', 'r', encoding='utf-8').readlines():
            try:
                each = each.strip('\r').strip('\n').strip()
                if each[:1] == '#':
                    major_key = each[1:]
                    SCHOOL_SPECIAL_MAJOR[major_key] = []
                else:
                    major_list = SCHOOL_SPECIAL_MAJOR[major_key]
                    major_list.append(each)
                    SCHOOL_SPECIAL_MAJOR[major_key] = major_list
            except:
                search_logger.error('some line is wrong when reading special school.')
                search_logger.info('wrong line : %s ', each)
                return
    except FileNotFoundError:
        search_logger.error('File resource/school/special_school.txt not found . . . ')
    # 学校库来自配置的文本文件
    if dict_from == 'conf':
        search_logger.info('school configuration from conf.txt.')
        try:
            for each in open('resource/university_dict.txt', 'r', encoding='utf-8').readlines():
                try:
                    each = each.strip('\r').strip('\n').lower()
                    UNIVERSITY_LIST.append(each)
                except:
                    search_logger.error('some line is wrong when convent to dict .')
                    search_logger.info('wrong line : %s ', each)
                    return
        except FileNotFoundError:
            search_logger.error('File resource/university_dict.txt not found . . . ')

    # 学校库来自mongodb库
    elif dict_from == 'mongodb':
        search_logger.info('school configuration from mongodb.')
        try:
            for each in open('conf/db.conf', 'r', encoding='utf-8').readlines():
                try:
                    each = each.strip('\r').strip('\n')
                    if(each.split('=')[0] == 'mongodb.url'):
                        url = each.split('=')[1]
                    if(each.split('=')[0] == 'mongodb.port'):
                        port = int(each.split('=')[1])
                    if(each.split('=')[0] == 'mongodb.dulishuo.username'):
                        username = each.split('=')[1]
                    if(each.split('=')[0] == 'mongodb.dulishuo.password'):
                        password = each.split('=')[1]
                except Exception as e:
                    search_logger.error(e)
                    search_logger.error('some line is wrong when read .')
                    search_logger.info('wrong line : %s ', each)
                    return
        except FileNotFoundError:
            search_logger.error('File resource/db.conf not found . . . ')
            return exit_error_func(3)
        except Exception as e:
            search_logger.error(e)
            search_logger.error('configuration failed.')

        mongo_client = MongoDB(host=url, port=port, username=username, password=password, auth=True)
        school_search_collection = mongo_client.get_collection('school', 'dulishuo')
        for each in school_search_collection.find():
            try:
                UNIVERSITY_LIST.append(each['display_name'].lower()+'|'+each['origin_name']+'|'+each['area'].lower()+'|'+str(each['type']).replace('.0','')+'|'+str(each['weight']))
            except TypeError as e:
                search_logger.error(e)
                search_logger.error('wrong line when converting:%s' % each)

        school_load_time = time.time()
        search_logger.info('ending reading data from mongodb,use time %f s.' % (school_load_time-start_time))
        try:
            mongo_client.close() # 关闭连接
            search_logger.info('close pymongo connection successed.')
        except Exception as e:
            search_logger.error('close pymongo connection failed.')
        
    else:
        search_logger.error('initializing failed,wrong params: %s' % dict_from)
        return exit_error_func(2, dict_from)

    # 构建学校库的前缀树
    global SCHOOL_TRIE
    SCHOOL_TRIE.setWords(UNIVERSITY_LIST)
    dict_tree_time = time.time()
    search_logger.info('completing building trie,use time %f s.' % (dict_tree_time-start_time))
    search_logger.info('----------initializing successed----------')
예제 #13
0
파일: search.py 프로젝트: SmartRiver/python
def init(dict_from="mongodb"):
    """初始化"""

    start_time = time.time()
    _logging_conf()
    search_logger.info("----------initializing----------")
    search_logger.info("type of reload : %s" % dict_from)
    try:
        for each in open("resource/school/special_school.txt", "r", encoding="utf-8").readlines():
            try:
                each = each.strip("\r").strip("\n").strip()
                if each[:1] == "#":
                    major_key = each[1:]
                    SCHOOL_SPECIAL_MAJOR[major_key] = []
                else:
                    major_list = SCHOOL_SPECIAL_MAJOR[major_key]
                    major_list.append(each)
                    SCHOOL_SPECIAL_MAJOR[major_key] = major_list
            except:
                search_logger.error("some line is wrong when reading special school.")
                search_logger.info("wrong line : %s ", each)
                return
    except FileNotFoundError:
        search_logger.error("File resource/school/special_school.txt not found . . . ")
    # 学校库来自配置的文本文件
    if dict_from == "conf":
        search_logger.info("school configuration from conf.txt.")
        try:
            for each in open("resource/university_dict.txt", "r", encoding="utf-8").readlines():
                try:
                    each = each.strip("\r").strip("\n").lower()
                    UNIVERSITY_LIST.append(each)
                except:
                    search_logger.error("some line is wrong when convent to dict .")
                    search_logger.info("wrong line : %s ", each)
                    return
        except FileNotFoundError:
            search_logger.error("File resource/university_dict.txt not found . . . ")

    # 学校库来自mongodb库
    elif dict_from == "mongodb":
        search_logger.info("school configuration from mongodb.")
        try:
            for each in open("conf/db.conf", "r", encoding="utf-8").readlines():
                try:
                    each = each.strip("\r").strip("\n")
                    if each.split("=")[0] == "mongodb.url":
                        url = each.split("=")[1]
                    if each.split("=")[0] == "mongodb.port":
                        port = int(each.split("=")[1])
                    if each.split("=")[0] == "mongodb.dulishuo.username":
                        username = each.split("=")[1]
                    if each.split("=")[0] == "mongodb.dulishuo.password":
                        password = each.split("=")[1]
                except Exception as e:
                    search_logger.error(e)
                    search_logger.error("some line is wrong when read .")
                    search_logger.info("wrong line : %s ", each)
                    return
        except FileNotFoundError:
            search_logger.error("File resource/db.conf not found . . . ")
            return exit_error_func(3)
        except Exception as e:
            search_logger.error(e)
            search_logger.error("configuration failed.")

        mongo_client = MongoDB(host=url, port=port, username=username, password=password, auth=True)
        school_search_collection = mongo_client.get_collection("school", "dulishuo")
        for each in school_search_collection.find():
            try:
                UNIVERSITY_LIST.append(
                    each["display_name"].lower()
                    + "|"
                    + each["origin_name"]
                    + "|"
                    + each["area"].lower()
                    + "|"
                    + str(each["type"]).replace(".0", "")
                    + "|"
                    + str(each["weight"])
                )

            except TypeError as e:
                search_logger.error(e)
                search_logger.error("wrong line when converting:%s" % each)

        school_load_time = time.time()
        search_logger.info("ending reading data from mongodb,use time %f s." % (school_load_time - start_time))
        try:
            mongo_client.close()  # 关闭连接
            search_logger.info("close pymongo connection successed.")
        except Exception as e:
            search_logger.error("close pymongo connection failed.")

    else:
        search_logger.error("initializing failed,wrong params: %s" % dict_from)
        return exit_error_func(2, dict_from)

    # 构建学校库的前缀树
    global SCHOOL_TRIE
    SCHOOL_TRIE.setWords(UNIVERSITY_LIST)
    dict_tree_time = time.time()
    search_logger.info("completing building trie,use time %f s." % (dict_tree_time - start_time))
    search_logger.info("----------initializing successed----------")
예제 #14
0
def schedule(condition, size=None):
    part_score_dict = {}
    try:
        #size 校对是否为大于0的整数
     
        size = _check_schedule_size(size)
   
        # 调用assess_student模块的assess(), 提取出所需要的用户信息
        user_condition = _get_user_condition(condition) 
    
        # 确定用户的语言类型(toefl or ielts)、(gre or gmat)
        language_type, exam_type = _get_language_exam_type(user_condition)
   
        #确定'student_info'字段是否存在
        if 'student_info' in user_condition:
            student_info = user_condition['student_info']
        else:
            path_plan_logger.error('缺少字段student_info')
            raise Exception('缺少字段student_info')

        #提取出用户的申请属性(major、 grade、 target)
        part_score_dict.update(_get_mgt(student_info))
        path_plan_logger.info('[successed] _get_mgt()')

        # 提取出用户的硬性指标(gpa、 toefl/ielts、 gre/gmat)
        temp_dict, language_type, exam_type = _get_hard_condition(student_info, language_type, exam_type)
        part_score_dict.update(temp_dict)
        path_plan_logger.info('[successed] _get_hard_condition()')

        #提取用户的软性指标(activity、 scholarship、 internship、 research、 credential、 competition)
        part_score_dict.update(_get_soft_condition(user_condition))
        path_plan_logger.info('[successed] _get_soft_condition()')
        # 为学习提升任务结点关联相应的机会产品
        finished_nodes, unfinished_nodes_products = _get_nodes_products(part_score_dict, language_type, exam_type, size)

        path_plan_logger.info('[successed] _get_nodes_products()')

        return_target = _get_return_target(part_score_dict['target'], language_type, exam_type)
        path_plan_logger.info('[successed] _get_return_target()')
        
    except Exception as e:
        return exit_error_func(1, '接口调用失败,错误信息:'+str(e)+', 异常类型:'+str(type(e)))

    
    #for index,item in enumerate(finished_node):
    #    finished_node[index] = {'node_id': item, 'products': []}
    #    if item in NODE_PRODUCT:
    #        finished_node[index] = {'node_id': item, 'products': NODE_PRODUCT[item]}
    
    # for index,item in enumerate(return_node):
    #     return_node[index] = {item: []}
    #     if item in NODE_PRODUCT:
    #         return_node[index] = {item: NODE_PRODUCT[item]}

    return {
        'status': 'success',
        'result': {
            'user_condition': user_condition,
            'target': return_target,
            'finished': finished_nodes,
            'unfinished': unfinished_nodes_products,
        },
    }
예제 #15
0
def load_products():
    path_plan_logger.info('[starting] loading product information from mongodb . . . ')
    try:
        for each in open('conf/db.conf', 'r', encoding='utf-8').readlines():
            try:
                each = each.strip('\r').strip('\n')
                if(each.split('=')[0] == 'mongodb.url'):
                    url = each.split('=')[1]
                if(each.split('=')[0] == 'mongodb.port'):
                    port = int(each.split('=')[1])
                if(each.split('=')[0] == 'mongodb.dulishuo.username'):
                    username = each.split('=')[1]
                if(each.split('=')[0] == 'mongodb.dulishuo.password'):
                    password = each.split('=')[1]
            except Exception as e:
                path_plan_logger.error(e)
                path_plan_logger.error('some line is wrong when read .')
                path_plan_logger.info('wrong line : %s ', each)
                return
    except FileNotFoundError:
        path_plan_logger.error('File resource/db.conf not found . . . ')
        return exit_error_func(3)
    except Exception as e:
        path_plan_logger.error(e)
        path_plan_logger.error(3, 'mongodb configuration failed.')
        return exit_error_func(3)

    mongo_client = MongoDB(host=url, port=port, username=username, password=password, is_auth=True)
    product_collection = mongo_client.get_collection('product', 'dulishuo')
    for each in product_collection.find():
        try:
            if 'nodeParent' in each:
                node_parent = convert_to_int(each['nodeParent'])
                product_id = convert_to_int(each['id'])
                product_title = each['title']
                if 'title_pic' in each:
                    product_picture = each['title_pic']
                else:
                    product_picture = ''
                temp_product = {
                    'product_id': product_id,
                    'title': product_title,
                    'picture': product_picture
                }
                if node_parent == False:
                    continue
                if node_parent in NODE_PRODUCT:
                    temp_product_list = NODE_PRODUCT[node_parent]
                    temp_product_list.append(temp_product)
                    NODE_PRODUCT.update({node_parent: temp_product_list})
                else:
                    temp_product_list = [temp_product]
                    NODE_PRODUCT[node_parent] = temp_product_list
        except TypeError as e:
            path_plan_logger.error(e)
            path_plan_logger.error('wrong record when converting')
        except Exception as e:
            path_plan_logger.error(e)
            path_plan_logger.error('wrong record when converting')

    product_load_time = time.time()
    path_plan_logger.info('[successed] ending reading data from mongodb.')
    try:
        mongo_client.close() # 关闭连接
        path_plan_logger.info('close pymongo connection successed.')
    except Exception as e:
        path_plan_logger.error('close pymongo connection failed.')
예제 #16
0
def schedule(user_input):
    part_score_dict = {}
    try:
        # 调用assess_student模块的assess(), 提取出所需要的用户信息
        user_condition = get_user_condition(user_input) 

        # 确定用户的语言类型(toefl or ielts)、(gre or gmat)
        language_type, exam_type = get_language_exam_type(user_condition)

        #提取出用户的申请属性(major、 grade、 target)
        part_score_dict.update(get_mgt(student_info))

        # 提取出用户的硬性指标(gpa、 toefl/ielts、 gre/gmat)
        part_score_dict.update(get_hard_condition(user_condition))

        #提取用户的软性指标(activity、 scholarship、 internship、 research、 credential、 competition)
        part_score_dict.update(get_soft_condition(user_condition))
        
    except Exception as e:
        return exit_error_func(1, '接口调用失败,错误信息:'+str(e))
    

    # 获取不同专业不同学期的初始化的各任务(节点)权重
    weight_dict = copy.deepcopy(PATH_PLAN_DICT[major][grade])
    #如果用户是toefl为主,则过滤ielts, 如果用户是gre,则过滤掉gmat, 反之过滤掉相反的,如果用户都没有,则全部保留
    filter_weight_field(weight_dict, language_type, exam_type)

    target_dict = TARGET_DICT[target]

    finished_node = []
    unfinished_node = []

    for each in weight_dict:
        if each in part_score_dict:
            if part_score_dict[each] > target_dict[each]:
                finished_node.append(NODE_DICT[each])
            else:
                ratio = (target_dict[each] - part_score_dict[each]) / target_dict[each]
                weight_dict[each] = weight_dict[each] * (1+ratio)
                unfinished_node.append(each)
    # 按照计算后的权重值对各个学习提升任务排序
    result_weight = sorted(weight_dict.items(), key=lambda x:x[1], reverse=True)

    return_node = [79,80,81,94]

    return_unfinished_node = []
    for each in result_weight:
        if each[0] in NODE_DICT:
            return_node.insert(0, NODE_DICT[each[0]])
        if each[0] in unfinished_node:
            return_unfinished_node.append(NODE_DICT[each[0]])

    # add ID of product to related NODE
    #for index,item in enumerate(finished_node):
    #    finished_node[index] = {'node_id': item, 'products': []}
    #    if item in NODE_PRODUCT:
    #        finished_node[index] = {'node_id': item, 'products': NODE_PRODUCT[item]}
    for index,item in enumerate(return_unfinished_node):
        return_unfinished_node[index] = {'node_id': item, 'products': []}
        if item in NODE_PRODUCT:
            return_unfinished_node[index] = {'node_id': item, 'products': NODE_PRODUCT[item]}
    for index,item in enumerate(return_node):
        return_node[index] = {item: []}
        if item in NODE_PRODUCT:
            return_node[index] = {item: NODE_PRODUCT[item]}

    return {
        'status': 'success',
        'result': {
            'finished': finished_node,
            'unfinished': return_unfinished_node,
        },
    }
예제 #17
0
    def get(self, request_type):
        service_logger.info('request uri: %s' % urllib.parse.unquote(self.request.uri))
        service_logger.info('request IP: %s' % self.request.remote_ip)

        self.set_header('Access-Control-Allow-Origin','*')
        flag = True
        error_msg = ''
        # token验证
        if 'token' in self.request.query_arguments:

            token_requset = convert_to_str(self.request.query_arguments['token'][0])
            service_logger.info('request token :%s' % token_requset)
            token_server = md5_token(token_requset)
            if not token_server: # 发送的token跟服务器生成的token不一致
                service_logger.info('invalid token')
                self.finish(json.dumps(exit_error_func(4, 'token验证失败'), ensure_ascii=False, indent=4))

        if request_type == 'reload':
            try:
                if 'search_flag' in self.request.query_arguments:
                    search_flag = self.request.query_arguments['search_flag'][0]
                    search.init(convert_to_str(search_flag))
                    assess_student.init()
                    path_planning.init()
                else:
                    _init()
                service_logger.info('service reload success.')
                self.finish('reloaded.')
            except:
                service_logger.info('service reload success.')
                self.finish('failed.')
        elif request_type == 'school_search':
            if 'condition' in self.request.query_arguments:
                try:
                    condition = self.request.query_arguments['condition'][0]
                    condition = convert_to_str(condition)
                    condition = condition.replace(' ', '')
                except Exception as e:
                    service_logger.error(e)
                    error_msg = exit_error_func(1, 'condition')
                    flag = False

            else:
                error_msg = exit_error_func(5, 'condition是必选参数')
                flag = False
            if 'area' in self.request.query_arguments:
                try:
                    area = self.request.query_arguments['area'][0]
                    area = convert_to_str(area)
                    area = area.strip()
                    if len(area) < 1:
                        area = None
                except Exception as e:
                    service_logger.error(e)
                    error_msg = exit_error_func(1, 'area')
                    flag = False
                    area = None
            else:
                area = None
            if 'country' in self.request.query_arguments:
                try:
                    country = self.request.query_arguments['country'][0]
                    country = convert_to_str(country)
                    country = country.strip()
                    if len(country) < 1:
                        country = None
                except Exception as e:
                    service_logger.error(e)
                    error_msg = exit_error_func(1, 'country')
                    flag = False
                    country = None
            else:
                country = None
            if 'major' in self.request.query_arguments:
                try:
                    major = self.request.query_arguments['major'][0]
                    major = convert_to_str(major)
                    major = major.strip()
                    if len(major) < 1:
                        major = None
                except Exception as e:
                    service_logger.error(e)
                    error_msg = exit_error_func(1, 'major')
                    flag = False
                    major = None
            else:
                major= None
            if flag:
                self.finish(json.dumps(search.search_school(condition=condition, major=major, area=area, country=country), ensure_ascii=False, indent=4))
            else:
                self.finish(error_msg)
        elif request_type == 'assess_student':
            if 'condition' in self.request.query_arguments:
                try:
                    condition = json.loads(convert_to_str(self.request.query_arguments['condition'][0]))
                except Exception as e:
                    service_logger.error(e)
                    error_msg = exit_error_func(1, 'condition格式转换出错')
                    flag = False
            else:
                error_msg = exit_error_func(5, 'condition为必选参数')
                flag = False
            if flag:
                assess_student.init()
                self.finish(json.dumps(assess_student.assess(condition), ensure_ascii=False, indent=4))
            else:
                self.finish(error_msg)
        elif request_type == 'path_planning':
            if 'condition' in self.request.query_arguments:
                try:
                    condition = json.loads(convert_to_str(self.request.query_arguments['condition'][0]))
                except Exception as e:
                    service_logger.error(e)
                    error_msg = exit_error_func(1, 'condition格式转换出错')
                    flag = False
            else:
                error_msg = exit_error_func(5, 'condition为必选参数')
                flag = False
            if 'size' in self.request.query_arguments:
                size = self.request.query_arguments['size'][0]
            else:
                size = None
            if flag:
                self.finish(json.dumps(path_planning.schedule(condition=condition, size=size), ensure_ascii=False, indent=4))
            else:
                self.finish(error_msg)
        else:
            service_logger.info('invalid request method.')
            self.finish(exit_error_func(7, request_type))