12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556 |
- import copy
- import json
- import random
- import numpy
- from typing import Dict
- from typing import Set
- from log import Log
- from config import set_config
- from video_recall import PoolRecall
- from db_helper import RedisHelper
- from utils import FilterVideos, send_msg_to_feishu
- from rank_service import get_featurs, get_tf_serving_sores
- from parameter_update import param_update_rule
- log_ = Log()
- config_ = set_config()
- def video_rank(data, size, top_K, flow_pool_P, flow_pool_recall_process=None):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :return: rank_result
- """
-
- if flow_pool_recall_process is None:
- flow_pool_recall_process = {}
- if not data['rov_pool_recall'] and not data['flow_pool_recall']:
-
- return [], flow_pool_recall_process
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- day_30_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_30day']]
- day_30_recall_rank = sorted(day_30_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
- region_h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_region_h']]
- region_h_recall_rank = sorted(region_h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
- rule_h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_h_h']]
- rule_h_recall_rank = sorted(rule_h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
- region_24h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_region_24h']]
- region_24h_recall_rank = sorted(region_24h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
-
-
-
-
- rule_24h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_24h']]
- rule_24h_recall_rank = sorted(rule_24h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
- rule_24h_dup_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_24h_dup']]
- rule_24h_dup_recall_rank = sorted(rule_24h_dup_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
- rule_48h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_48h']]
- rule_48h_recall_rank = sorted(rule_48h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
- rule_48h_dup_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_48h_dup']]
- rule_48h_dup_recall_rank = sorted(rule_48h_dup_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- rov_recall_rank = day_30_recall_rank + \
- region_h_recall_rank + rule_h_recall_rank + region_24h_recall_rank + \
- rule_24h_recall_rank + rule_24h_dup_recall_rank + \
- rule_48h_recall_rank + rule_48h_dup_recall_rank
-
- flow_recall_rank = sorted(data['flow_pool_recall'], key=lambda k: k.get('rovScore', 0), reverse=True)
-
- rov_recall_rank, flow_recall_rank = remove_duplicate(rov_recall=rov_recall_rank, flow_recall=flow_recall_rank,
- top_K=top_K)
-
-
-
- rank_result = []
-
- flow_pool_recall_process['recall_duplicate_res'] = {'rov_recall_rank': rov_recall_rank,
- 'flow_recall_rank': copy.deepcopy(flow_recall_rank)}
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(flow_recall_rank[:top_K])
- flow_recall_rank = flow_recall_rank[top_K:]
-
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- flow_pool_recall_process['flow_pool_P'] = flow_pool_P
- flow_pool_recall_process[f'{i}_rand'] = rand
-
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_pool_recall_process
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_pool_recall_process
- i += 1
- return rank_result[:size], flow_pool_recall_process
- def video_new_rank(videoIds, fast_flow_set, flow_set, size, top_K, flow_pool_P):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :return: rank_result
- """
- add_flow_set = set('')
- if not videoIds or len(videoIds)==0:
- return [], add_flow_set
- redisObj = RedisHelper()
- vidKeys = []
- for vid in videoIds:
- vidKeys.append("k_p:"+str(vid))
-
- video_scores = redisObj.get_batch_key(vidKeys)
-
- video_items = []
- for i in range(len(video_scores)):
- try:
-
- if video_scores[i] is None:
- video_items.append((videoIds[i], 0.0))
- else:
- video_score_str = json.loads(video_scores[i])
-
- video_items.append((videoIds[i], video_score_str[0]))
- except Exception:
- video_items.append((videoIds[i], 0.0))
- sort_items = sorted(video_items, key=lambda k: k[1], reverse=True)
-
- rov_recall_rank = sort_items
- fast_flow_recall_rank = []
- flow_recall_rank = []
- for item in sort_items:
- if item[0] in fast_flow_set:
- fast_flow_recall_rank.append(item)
- elif item[0] in flow_set:
- flow_recall_rank.append(item)
-
- all_flow_recall_rank = fast_flow_recall_rank+flow_recall_rank
- rank_result = []
- rank_set = set('')
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(all_flow_recall_rank[:top_K])
- all_flow_recall_rank = all_flow_recall_rank[top_K:]
- for rank_item in rank_result:
- rank_set.add(rank_item[0])
-
-
- i = 0
- left_quato = size - top_K
- j = 0
- jj = 0
- while i < left_quato and (j<len(all_flow_recall_rank) or jj<len(rov_recall_rank)):
-
- rand = random.random()
-
- if rand < flow_pool_P:
- for flow_item in all_flow_recall_rank:
- j+=1
- if flow_item[0] in rank_set:
- continue
- else:
- rank_result.append(flow_item)
- rank_set.add(flow_item[0])
- add_flow_set.add(flow_item[0])
- i += 1
- if i>= left_quato:
- break
-
- else:
- for recall_item in rov_recall_rank:
- jj+=1
- if recall_item[0] in rank_set:
- continue
- else:
- rank_result.append(recall_item)
- rank_set.add(recall_item[0])
- i += 1
- if i>= left_quato:
- break
-
-
- return rank_result[:size], add_flow_set
- def refactor_video_rank(rov_recall_rank, fast_flow_set, flow_set, size, top_K, flow_pool_P):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :return: rank_result
- """
- if not rov_recall_rank or len(rov_recall_rank) == 0:
- return []
- fast_flow_recall_rank = []
- flow_recall_rank = []
- for item in rov_recall_rank:
- vid = item.get('videoId', 0)
-
- if vid in fast_flow_set:
- fast_flow_recall_rank.append(item)
- elif vid in flow_set:
- flow_recall_rank.append(item)
-
- all_flow_recall_rank = fast_flow_recall_rank + flow_recall_rank
- rank_result = []
- rank_set = set('')
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(all_flow_recall_rank[:top_K])
- all_flow_recall_rank = all_flow_recall_rank[top_K:]
-
- for rank_item in rank_result:
- rank_set.add(rank_item.get('videoId', 0))
-
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- if rand < flow_pool_P:
- for flow_item in all_flow_recall_rank:
- flow_vid = flow_item.get('videoId', 0)
- if flow_vid in rank_set:
- continue
- else:
- rank_result.append(flow_item)
- rank_set.add(flow_vid)
- else:
- for recall_item in rov_recall_rank:
- flow_vid = recall_item.get('videoId', 0)
- if flow_vid in rank_set:
- continue
- else:
- rank_result.append(recall_item)
- rank_set.add(flow_vid)
- i += 1
- return rank_result[:size]
- def remove_duplicate(rov_recall, flow_recall, top_K):
- """
- 对多路召回的视频去重
- 去重原则:
- 如果视频在ROV召回池topK,则保留ROV召回池,否则保留流量池
- :param rov_recall: ROV召回池-已排序
- :param flow_recall: 流量池-已排序
- :param top_K: 保证topK为召回池视频 type-int
- :return:
- """
- flow_recall_result = []
- rov_recall_remove = []
- flow_recall_video_ids = [item['videoId'] for item in flow_recall]
-
- for item in rov_recall[:top_K]:
- if item['videoId'] in flow_recall_video_ids:
- flow_recall_video_ids.remove(item['videoId'])
-
- for item in rov_recall[top_K:]:
- if item['videoId'] in flow_recall_video_ids:
- rov_recall_remove.append(item)
-
- for item in rov_recall_remove:
- rov_recall.remove(item)
-
- for item in flow_recall:
- if item['videoId'] in flow_recall_video_ids:
- flow_recall_result.append(item)
- return rov_recall, flow_recall_result
- def bottom_strategy(request_id, size, app_type, ab_code, params):
- """
- 兜底策略: 从ROV召回池中获取top1000,进行状态过滤后的视频
- :param request_id: request_id
- :param size: 需要获取的视频数
- :param app_type: 产品标识 type-int
- :param ab_code: abCode
- :param params:
- :return:
- """
- pool_recall = PoolRecall(request_id=request_id, app_type=app_type, ab_code=ab_code)
- key_name, _ = pool_recall.get_pool_redis_key(pool_type='rov')
- redis_helper = RedisHelper(params=params)
- data = redis_helper.get_data_zset_with_index(key_name=key_name, start=0, end=1000)
- if not data:
- log_.info('{} —— ROV推荐进入了二次兜底, data = {}'.format(config_.ENV_TEXT, data))
- send_msg_to_feishu('{} —— ROV推荐进入了二次兜底,请查看是否有数据更新失败问题。'.format(config_.ENV_TEXT))
-
- bottom_data = bottom_strategy_last(size=size, app_type=app_type, ab_code=ab_code, params=params)
- return bottom_data
-
-
-
-
- if len(data) > size:
- random_data = numpy.random.choice(data, size, False)
- else:
- random_data = data
- bottom_data = [{'videoId': int(item), 'pushFrom': config_.PUSH_FROM['bottom'], 'abCode': ab_code}
- for item in random_data]
- return bottom_data
- def bottom_strategy_last(size, app_type, ab_code, params):
- """
- 兜底策略: 从兜底视频中随机获取视频,进行状态过滤后的视频
- :param size: 需要获取的视频数
- :param app_type: 产品标识 type-int
- :param ab_code: abCode
- :param params:
- :return:
- """
- redis_helper = RedisHelper(params=params)
- bottom_data = redis_helper.get_data_zset_with_index(key_name=config_.BOTTOM_KEY_NAME, start=0, end=-1)
- random_data = numpy.random.choice(bottom_data, size * 30, False)
-
-
-
-
- bottom_data = [{'videoId': int(video_id), 'pushFrom': config_.PUSH_FROM['bottom_last'], 'abCode': ab_code}
- for video_id in random_data[:size]]
- return bottom_data
- def bottom_strategy2(size, app_type, mid, uid, ab_code, client_info, params):
- """
- 兜底策略: 从兜底视频中随机获取视频,进行过滤后的视频
- :param size: 需要获取的视频数
- :param app_type: 产品标识 type-int
- :param mid: mid
- :param uid: uid
- :param ab_code: abCode
- :param client_info: 地域信息
- :param params:
- :return:
- """
-
- city_code_list = [code for _, code in config_.CITY_CODE.items()]
-
- province_code = client_info.get('provinceCode', '-1')
-
- city_code = client_info.get('cityCode', '-1')
- if city_code in city_code_list:
-
- region_code = city_code
- else:
- region_code = province_code
- if region_code == '':
- region_code = '-1'
- redis_helper = RedisHelper(params=params)
- bottom_data = redis_helper.get_data_from_set(key_name=config_.BOTTOM2_KEY_NAME)
- bottom_result = []
- if bottom_data is None:
- return bottom_result
- if len(bottom_data) > 0:
- try:
- random_data = numpy.random.choice(bottom_data, size * 5, False)
- except Exception as e:
- random_data = bottom_data
- video_ids = [int(item) for item in random_data]
-
- filter_ = FilterVideos(request_id=params.request_id, app_type=app_type, mid=mid, uid=uid, video_ids=video_ids)
- filtered_data = filter_.filter_videos(pool_type='flow', region_code=region_code)
- if filtered_data:
- bottom_result = [{'videoId': int(video_id), 'pushFrom': config_.PUSH_FROM['bottom2'], 'abCode': ab_code}
- for video_id in filtered_data[:size]]
- return bottom_result
- def video_rank_by_w_h_rate(videos):
- """
- 视频宽高比实验(每组的前两个视频调整为横屏视频),根据视频宽高比信息对视频进行重排
- :param videos:
- :return:
- """
- redis_helper = RedisHelper()
-
- top_2_push_from_flag = [False, False]
- for i, video in enumerate(videos[:2]):
- if video['pushFrom'] in [config_.PUSH_FROM['top'], config_.PUSH_FROM['flow_recall']]:
- top_2_push_from_flag[i] = True
- if top_2_push_from_flag[0] and top_2_push_from_flag[1]:
- return videos
-
- top_2_w_h_rate_flag = [False, False]
- for i, video in enumerate(videos[:2]):
- if video['pushFrom'] in [config_.PUSH_FROM['top'], config_.PUSH_FROM['flow_recall']]:
-
- top_2_w_h_rate_flag[i] = True
- elif video['pushFrom'] in [config_.PUSH_FROM['rov_recall'], config_.PUSH_FROM['bottom']]:
-
- w_h_rate = redis_helper.get_score_with_value(
- key_name=config_.W_H_RATE_UP_1_VIDEO_LIST_KEY_NAME['rov_recall'], value=video['videoId'])
- if w_h_rate is not None:
- top_2_w_h_rate_flag[i] = True
- elif video['pushFrom'] == config_.PUSH_FROM['bottom_last']:
-
- w_h_rate = redis_helper.get_score_with_value(
- key_name=config_.W_H_RATE_UP_1_VIDEO_LIST_KEY_NAME['bottom_last'], value=video['videoId'])
- if w_h_rate is not None:
- top_2_w_h_rate_flag[i] = True
- if top_2_w_h_rate_flag[0] and top_2_w_h_rate_flag[1]:
- return videos
-
-
- horizontal_video_index = []
-
- flow_video_index = []
-
- top_video_index = []
- for i, video in enumerate(videos):
-
- if video['pushFrom'] == config_.PUSH_FROM['top']:
- top_video_index.append(i)
-
- elif video['pushFrom'] == config_.PUSH_FROM['flow_recall']:
- flow_video_index.append(i)
-
- elif video['pushFrom'] in [config_.PUSH_FROM['rov_recall'], config_.PUSH_FROM['bottom']]:
- w_h_rate = redis_helper.get_score_with_value(
- key_name=config_.W_H_RATE_UP_1_VIDEO_LIST_KEY_NAME['rov_recall'], value=video['videoId'])
- if w_h_rate is not None:
- horizontal_video_index.append(i)
- else:
- continue
-
- elif video['pushFrom'] == config_.PUSH_FROM['bottom_last']:
- w_h_rate = redis_helper.get_score_with_value(
- key_name=config_.W_H_RATE_UP_1_VIDEO_LIST_KEY_NAME['bottom_last'], value=video['videoId'])
- if w_h_rate is not None:
- horizontal_video_index.append(i)
- else:
- continue
-
- top2_index = []
- for i in range(2):
- if i in top_video_index:
- top2_index.append(i)
- elif i in flow_video_index:
- top2_index.append(i)
- flow_video_index.remove(i)
- elif i in horizontal_video_index:
- top2_index.append(i)
- horizontal_video_index.remove(i)
- elif len(horizontal_video_index) > 0:
-
- top2_index.append(horizontal_video_index[0])
-
- horizontal_video_index.pop(0)
- elif i == 0:
- return videos
-
- flow_result = [videos[i] for i in flow_video_index]
- other_result = [videos[i] for i in range(len(videos)) if i not in top2_index and i not in flow_video_index]
- top2_result = []
- for i, j in enumerate(top2_index):
- item = videos[j]
- if i != j:
-
- item['abCode'] = config_.AB_CODE['w_h_rate']
- top2_result.append(item)
- new_rank_result = top2_result
- for i in range(len(top2_index), len(videos)):
- if i in flow_video_index:
- new_rank_result.append(flow_result[0])
- flow_result.pop(0)
- else:
- new_rank_result.append(other_result[0])
- other_result.pop(0)
- return new_rank_result
- def video_rank_with_old_video(rank_result, old_video_recall, size, top_K, old_video_index=2):
- """
- 视频分发排序 - 包含老视频, 老视频插入固定位置
- :param rank_result: 排序后的结果
- :param size: 请求数
- :param old_video_index: 老视频插入的位置索引,默认为2
- :return: new_rank_result
- """
- if not old_video_recall:
- return rank_result
- if not rank_result:
- return old_video_recall[:size]
-
- rank_video_ids = [item['videoId'] for item in rank_result]
- old_video_remove = []
- for old_video in old_video_recall:
- if old_video['videoId'] in rank_video_ids:
- old_video_remove.append(old_video)
- for item in old_video_remove:
- old_video_recall.remove(item)
- if not old_video_recall:
- return rank_result
-
-
- ind = random.randint(0, len(old_video_recall) - 1)
- old_video = old_video_recall[ind]
-
- if len(rank_result) < top_K:
- new_rank_result = rank_result + [old_video]
- else:
- new_rank_result = rank_result[:old_video_index] + [old_video] + rank_result[old_video_index:]
- if len(new_rank_result) > size:
-
- push_from_1 = new_rank_result[-1]['pushFrom']
- push_from_2 = new_rank_result[-2]['pushFrom']
- if push_from_2 == config_.PUSH_FROM['rov_recall'] and push_from_1 == config_.PUSH_FROM['flow_recall']:
- new_rank_result = new_rank_result[:-2] + new_rank_result[-1:]
- return new_rank_result[:size]
- def video_new_rank2(data, size, top_K, flow_pool_P, ab_code, mid, exp_config=None, env_dict=None):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :return: rank_result
- """
- if not data['rov_pool_recall'] and not data['flow_pool_recall']:
- return [], 0
-
-
- pre_str = "k_p2:"
- rov_recall_rank = data['rov_pool_recall']
-
-
-
- sort_index = 0
- if exp_config and "sort_flag" in exp_config:
- sort_index = exp_config["sort_flag"]
-
- redisObj = RedisHelper()
- vidKeys = []
- rec_recall_item_list = []
- rec_recall_vid_list = []
- day_vidKeys = []
- hour_vidKeys = []
- pre_day_str = "v_ctr:"
- pre_hour_str = "v_hour_ctr:"
- for recall_item in data['rov_pool_recall']:
- try:
- vid = int(recall_item.get("videoId", 0))
- rec_recall_vid_list.append(vid)
- rec_recall_item_list.append(recall_item)
- vidKeys.append(pre_str + str(vid))
- day_vidKeys.append(pre_day_str+str(vid))
- hour_vidKeys.append(pre_hour_str+str(vid))
- except:
- continue
- video_scores = redisObj.get_batch_key(vidKeys)
-
- if (ab_code == 60066 or ab_code == 60069 or ab_code == 60070 or ab_code == 60071) and len(rec_recall_vid_list)>0:
- video_static_info = redisObj.get_batch_key(day_vidKeys)
- video_hour_static_info = redisObj.get_batch_key(hour_vidKeys)
-
- feature_dict = get_featurs(mid, data, size, top_K, flow_pool_P, rec_recall_vid_list,env_dict, video_static_info, video_hour_static_info)
- score_result = get_tf_serving_sores(feature_dict)
-
- if video_scores and len(video_scores)>0 and rec_recall_item_list and score_result and len(score_result) > 0\
- and len(score_result) == len(rec_recall_item_list) and len(video_scores)== len(score_result):
- for i in range(len(score_result)):
- try:
- if video_scores[i] is None and len(score_result[i])>0:
- return_score = 0.000000001
-
- if sort_index == 10:
- total_score = score_result[i][0]
- else:
- total_score = return_score * score_result[i][0]
- rec_recall_item_list[i]['sort_score'] = total_score
- rec_recall_item_list[i]['base_rov_score'] = 0.0
- rec_recall_item_list[i]['share_score'] = return_score
- rec_recall_item_list[i]['model_score'] = score_result[i][0]
- else:
- video_score_str = json.loads(video_scores[i])
-
- return_score = 0.000000001
- if sort_index == 10:
- total_score = score_result[i][0]
- else:
- if len(video_score_str)>= sort_index and len(video_score_str)>0:
- return_score = video_score_str[sort_index]
- total_score = return_score * score_result[i][0]
-
-
- rec_recall_item_list[i]['sort_score'] = total_score
- rec_recall_item_list[i]['base_rov_score'] = video_score_str[0]
- rec_recall_item_list[i]['share_score'] = return_score
- rec_recall_item_list[i]['model_score'] = score_result[i][0]
- except Exception as e:
-
- return_score = 0.000000001
- if sort_index == 10:
- total_score = 0.00000001
- else:
- total_score = return_score * 0.00000001
- rec_recall_item_list[i]['sort_score'] = total_score
- rec_recall_item_list[i]['base_rov_score'] = 0
- rec_recall_item_list[i]['share_score'] = return_score
- rec_recall_item_list[i]['model_score'] = 0.00000001
- rec_recall_item_list[i]['flag_call_service'] = 1
- rov_recall_rank = sorted(rec_recall_item_list, key=lambda k: k.get('sort_score', 0), reverse=True)
- else:
- rov_recall_rank = sup_rank(video_scores, rec_recall_item_list)
- else:
- if video_scores and len(rec_recall_item_list) > 0 and len(video_scores)>0:
- for i in range(len(video_scores)):
- try:
- if video_scores[i] is None:
- rec_recall_item_list[i]['sort_score'] = 0.0
- else:
- video_score_str = json.loads(video_scores[i])
-
- rec_recall_item_list[i]['sort_score'] = video_score_str[0]
- except Exception:
- rec_recall_item_list[i]['sort_score'] = 0.0
- rov_recall_rank = sorted(rec_recall_item_list, key=lambda k: k.get('sort_score', 0), reverse=True)
-
- flow_recall_rank = sorted(data['flow_pool_recall'], key=lambda k: k.get('rovScore', 0), reverse=True)
- rov_recall_rank, flow_recall_rank = remove_duplicate(rov_recall=rov_recall_rank, flow_recall=flow_recall_rank,
- top_K=top_K)
- rank_result = []
- rank_set = set('')
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(flow_recall_rank[:top_K])
- flow_recall_rank = flow_recall_rank[top_K:]
-
- flow_num = 0
- flowConfig = 0
-
- if flowConfig == 1 and len(rov_recall_rank) > 0:
- for recall_item in rank_result:
- flow_recall_name = recall_item.get("flowPool", '')
- flow_num = flow_num + 1
- all_recall_rank = rov_recall_rank + flow_recall_rank
- if flow_num > 0:
- rank_result.extend(all_recall_rank[:size - top_K])
- return rank_result, flow_num
- else:
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- i += 1
- else:
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- i += 1
- return rank_result[:size], flow_num
- def video_new_rank3(data, size, top_K, flow_pool_P, rank_key_prefix='rank:score1:', flow_pool_recall_process=None):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :param rank_key_prefix:
- :return: rank_result
- """
- redis_helper = RedisHelper()
- if flow_pool_recall_process is None:
- flow_pool_recall_process = {}
- if not data['rov_pool_recall'] and not data['flow_pool_recall']:
- return [], 0, flow_pool_recall_process
- rov_recall_rank = data['rov_pool_recall']
- vid_keys = []
- rec_recall_item_list = []
- rec_recall_vid_list = []
- for recall_item in data['rov_pool_recall']:
- try:
- vid = int(recall_item.get("videoId", 0))
- rec_recall_vid_list.append(vid)
- rec_recall_item_list.append(recall_item)
- vid_keys.append(f"{rank_key_prefix}{vid}")
- except:
- continue
- video_scores = redis_helper.get_batch_key(vid_keys)
- if video_scores and len(rec_recall_item_list) > 0 and len(rec_recall_item_list) == len(video_scores):
- for i in range(len(video_scores)):
- try:
- if video_scores[i] is None:
- rec_recall_item_list[i]['sort_score'] = 0.0
- else:
- rec_recall_item_list[i]['sort_score'] = float(video_scores[i])
- except Exception:
- rec_recall_item_list[i]['sort_score'] = 0.0
- rov_recall_rank = sorted(rec_recall_item_list, key=lambda k: k.get('sort_score', 0), reverse=True)
- flow_recall_rank = sorted(data['flow_pool_recall'], key=lambda k: k.get('rovScore', 0), reverse=True)
- rov_recall_rank, flow_recall_rank = remove_duplicate(
- rov_recall=rov_recall_rank, flow_recall=flow_recall_rank, top_K=top_K
- )
- rank_result = []
- flow_pool_recall_process['recall_duplicate_res'] = {'rov_recall_rank': rov_recall_rank,
- 'flow_recall_rank': copy.deepcopy(flow_recall_rank)}
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(flow_recall_rank[:top_K])
- flow_recall_rank = flow_recall_rank[top_K:]
-
- flow_num = 0
- i = 0
- while i < size - top_K:
-
- rand = random.random()
- flow_pool_recall_process['flow_pool_P'] = flow_pool_P
- flow_pool_recall_process[f'{i}_rand'] = rand
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num, flow_pool_recall_process
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num, flow_pool_recall_process
- i += 1
- return rank_result[:size], flow_num, flow_pool_recall_process
- def video_new_rank3_4density(data, size, top_K, flow_pool_P, rank_key_prefix='rank:score1:',
- flow_pool_recall_process=None,
- app_type=None, rule_key_str = None):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :param rank_key_prefix:
- :return: rank_result
- """
-
- redis_helper = RedisHelper()
- density_rules = {}
- rules_all = param_update_rule(redis_helper, rule_key_str)
- try:
- if len(rules_all) != 0:
- for k, v in rules_all.items():
- if str(app_type) in v.keys():
- app_type_rule = v[str(app_type)]
- if "density" in app_type_rule.keys():
- density_rules[k] = app_type_rule["density"]
- except Exception as e:
- log_.error("something is wrong in parsing density_rules:{}".format(e))
- density_rules = {}
- if flow_pool_recall_process is None:
- flow_pool_recall_process = {}
- if not data['rov_pool_recall'] and not data['flow_pool_recall']:
- return [], 0, flow_pool_recall_process
- rov_recall_rank = data['rov_pool_recall']
- vid_keys = []
- rec_recall_item_list = []
- rec_recall_vid_list = []
- for recall_item in data['rov_pool_recall']:
- try:
- vid = int(recall_item.get("videoId", 0))
- rec_recall_vid_list.append(vid)
- rec_recall_item_list.append(recall_item)
- vid_keys.append(f"{rank_key_prefix}{vid}")
- except:
- continue
- video_scores = redis_helper.get_batch_key(vid_keys)
- if video_scores and len(rec_recall_item_list) > 0 and len(rec_recall_item_list) == len(video_scores):
- for i in range(len(video_scores)):
- try:
- if video_scores[i] is None:
- rec_recall_item_list[i]['sort_score'] = 0.0
- else:
- rec_recall_item_list[i]['sort_score'] = float(video_scores[i])
- except Exception:
- rec_recall_item_list[i]['sort_score'] = 0.0
- rov_recall_rank = sorted(rec_recall_item_list, key=lambda k: k.get('sort_score', 0), reverse=True)
- flow_recall_rank = sorted(data['flow_pool_recall'], key=lambda k: k.get('rovScore', 0), reverse=True)
- rov_recall_rank, flow_recall_rank = remove_duplicate(
- rov_recall=rov_recall_rank, flow_recall=flow_recall_rank, top_K=top_K
- )
-
- video_ids = []
- video_ids.extend([v["videoId"] for v in rov_recall_rank])
- video_ids.extend([v["videoId"] for v in flow_recall_rank])
- video_ids = list(set(video_ids))
- video_tag_dict = get_video_tags(redis_helper, video_ids)
- for v in rov_recall_rank:
- v["tags"] = video_tag_dict.get(v["videoId"], [])
- for v in flow_recall_rank:
- v["tags"] = video_tag_dict.get(v["videoId"], [])
- rank_result = []
- flow_pool_recall_process['recall_duplicate_res'] = {'rov_recall_rank': rov_recall_rank,
- 'flow_recall_rank': copy.deepcopy(flow_recall_rank)}
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(flow_recall_rank[:top_K])
- flow_recall_rank = flow_recall_rank[top_K:]
-
- flow_num = 0
- i = 0
-
-
-
-
- while i < size - top_K:
-
- rand = random.random()
- flow_pool_recall_process['flow_pool_P'] = flow_pool_P
- flow_pool_recall_process[f'{i}_rand'] = rand
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
-
- result = merge_density_control(
- rank_result[:size],
- [i for i in rov_recall_rank if i["videoId"] not in [j["videoId"] for j in rank_result[:size]]],
- [i for i in flow_recall_rank if i["videoId"] not in [j["videoId"] for j in rank_result[:size]]],
- density_rules
- )
- return result, flow_num, flow_pool_recall_process
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
-
- result = merge_density_control(
- rank_result[:size],
- [i for i in rov_recall_rank if i["videoId"] not in [j["videoId"] for j in rank_result[:size]]],
- [i for i in flow_recall_rank if i["videoId"] not in [j["videoId"] for j in rank_result[:size]]],
- density_rules
- )
- return result, flow_num, flow_pool_recall_process
- i += 1
-
- result = merge_density_control(
- rank_result[:size],
- [i for i in rov_recall_rank if i["videoId"] not in [j["videoId"] for j in rank_result[:size]]],
- [i for i in flow_recall_rank if i["videoId"] not in [j["videoId"] for j in rank_result[:size]]],
- density_rules
- )
- return result, flow_num, flow_pool_recall_process
- def sup_rank(video_scores, recall_list):
- if video_scores and len(recall_list) > 0:
- for i in range(len(video_scores)):
- try:
- if video_scores[i] is None:
- recall_list[i]['sort_score'] = 0.0
- else:
- video_score_str = json.loads(video_scores[i])
- recall_list[i]['flag_call_service'] = 0
- recall_list[i]['sort_score'] = video_score_str[0]
- except Exception:
- recall_list[i]['sort_score'] = 0.0
- rov_recall_rank = sorted(recall_list, key=lambda k: k.get('sort_score', 0), reverse=True)
-
- else:
- rov_recall_rank = recall_list
- return rov_recall_rank
- def video_sanke_rank(data, size, top_K, flow_pool_P, ab_Code='', exp_config=None):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :return: rank_result
- """
- if not data['rov_pool_recall'] and not data['flow_pool_recall'] \
- and len(data['u2i_recall'])==0 and len(data['w2v_recall'])==0 \
- and len(data['sim_recall']) == 0 and len(data['u2u2i_recall']) == 0 :
- return [], 0
-
- recall_dict = {}
- region_h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_region_h']]
- region_h_recall_rank = sorted(region_h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_region_h'] = region_h_recall_rank
-
- region_24h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_region_24h']]
- region_24h_recall_rank = sorted(region_24h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_region_24h'] = region_24h_recall_rank
-
- rule_24h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_24h']]
- rule_24h_recall_rank = sorted(rule_24h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_24h'] = rule_24h_recall_rank
-
- rule_24h_dup_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_24h_dup']]
- rule_24h_dup_recall_rank = sorted(rule_24h_dup_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_24h_dup'] = rule_24h_dup_recall_rank
- hot_recall = []
- w2v_recall =[]
- sim_recall = []
- u2u2i_recall = []
- if ab_Code==60058:
- if len(data['u2i_recall'])>0:
- hot_recall = sorted(data['u2i_recall'], key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['u2i_recall'] = hot_recall
- elif ab_Code==60059:
- if len(data['w2v_recall'])>0:
- recall_dict['w2v_recall'] = data['w2v_recall']
- else:
- recall_dict['w2v_recall'] = w2v_recall
- elif ab_Code==60061 or ab_Code==60063:
- if len(data['sim_recall'])>0:
- recall_dict['sim_recall'] = data['sim_recall']
- else:
- recall_dict['sim_recall'] = sim_recall
- elif ab_Code==60062:
- if len(data['u2u2i_recall'])>0:
- recall_dict['u2u2i_recall'] = data['u2u2i_recall']
- else:
- recall_dict['u2u2i_recall'] = u2u2i_recall
- recall_list = [('rov_recall_region_h',1, 1),('rov_recall_region_h',0.5, 1),('rov_recall_region_24h',1,1),
- ('u2i_recall',0.5,1), ('w2v_recall',0.5,1),('rov_recall_24h',1,1), ('rov_recall_24h_dup',0.5,1)]
- if exp_config and exp_config['recall_list']:
- recall_list = exp_config['recall_list']
-
- rov_recall_rank = []
- select_ids = set('')
- for i in range(3):
- if len(rov_recall_rank)>8:
- break
- for per_recall_item in recall_list:
- per_recall_name = per_recall_item[0]
- per_recall_freq = per_recall_item[1]
- per_limt_num = per_recall_item[2]
- rand_num = random.random()
-
- if rand_num<per_recall_freq and per_recall_name in recall_dict:
- per_recall = recall_dict[per_recall_name]
-
- cur_recall_num = 0
- for recall_item in per_recall:
- vid = recall_item['videoId']
- if vid in select_ids:
- continue
- rov_recall_rank.append(recall_item)
- select_ids.add(vid)
- cur_recall_num+=1
- if cur_recall_num>=per_limt_num:
- break
-
-
-
-
-
- flow_recall_rank = sorted(data['flow_pool_recall'], key=lambda k: k.get('rovScore', 0), reverse=True)
-
- rov_recall_rank, flow_recall_rank = remove_duplicate(rov_recall=rov_recall_rank, flow_recall=flow_recall_rank,
- top_K=top_K)
-
-
-
- rank_result = []
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(flow_recall_rank[:top_K])
- flow_recall_rank = flow_recall_rank[top_K:]
- flow_num = 0
- flowConfig =0
- if exp_config and exp_config['flowConfig']:
- flowConfig = exp_config['flowConfig']
- if flowConfig == 1 and len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- for recall_item in rank_result:
- flow_recall_name = recall_item.get("flowPool", '')
- if flow_recall_name is not None and flow_recall_name.find("#") > -1:
- flow_num = flow_num + 1
- all_recall_rank = rov_recall_rank + flow_recall_rank
- if flow_num > 0:
- rank_result.extend(all_recall_rank[:size - top_K])
- return rank_result[:size], flow_num
- else:
-
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- i += 1
- else:
-
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size],flow_num
- i += 1
- return rank_result[:size], flow_num
- def video_sank_pos_rank(data, size, top_K, flow_pool_P, ab_Code='', exp_config=None):
- """
- 视频分发排序
- :param data: 各路召回的视频 type-dict {'rov_pool_recall': [], 'flow_pool_recall': []}
- :param size: 请求数
- :param top_K: 保证topK为召回池视频 type-int
- :param flow_pool_P: size-top_K视频为流量池视频的概率 type-float
- :return: rank_result
- """
- if not data['rov_pool_recall'] and not data['flow_pool_recall'] \
- and len(data['u2i_recall'])==0 and len(data['w2v_recall'])==0 \
- and len(data['sim_recall']) == 0 and len(data['u2u2i_recall']) == 0 :
- return [], 0
-
- recall_dict = {}
- region_h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_region_h']]
- region_h_recall_rank = sorted(region_h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_region_h'] = region_h_recall_rank
-
- region_24h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_region_24h']]
- region_24h_recall_rank = sorted(region_24h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_region_24h'] = region_24h_recall_rank
-
- rule_24h_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_24h']]
- rule_24h_recall_rank = sorted(rule_24h_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_24h'] = rule_24h_recall_rank
-
- rule_24h_dup_recall = [item for item in data['rov_pool_recall']
- if item.get('pushFrom') == config_.PUSH_FROM['rov_recall_24h_dup']]
- rule_24h_dup_recall_rank = sorted(rule_24h_dup_recall, key=lambda k: k.get('rovScore', 0), reverse=True)
- recall_dict['rov_recall_24h_dup'] = rule_24h_dup_recall_rank
- u2i_recall = []
- u2i_play_recall = []
- w2v_recall =[]
- sim_recall = []
- u2u2i_recall = []
- return_video_recall = []
-
- if ab_Code==60058:
- if len(data['u2i_recall'])>0:
- recall_dict['u2i_recall'] = data['u2i_recall']
- else:
- recall_dict['u2i_recall'] = u2i_recall
- if len(data['u2i_play_recall']) > 0:
- recall_dict['u2i_play_recall'] = data['u2i_play_recall']
- else:
- recall_dict['u2i_play_recall'] = u2i_play_recall
- elif ab_Code==60059:
- if len(data['w2v_recall'])>0:
- recall_dict['w2v_recall'] = data['w2v_recall']
- else:
- recall_dict['w2v_recall'] = w2v_recall
- elif ab_Code==60061 or ab_Code==60063:
- if len(data['sim_recall'])>0:
- recall_dict['sim_recall'] = data['sim_recall']
- else:
- recall_dict['sim_recall'] = sim_recall
- elif ab_Code==60062:
- if len(data['u2u2i_recall'])>0:
- recall_dict['u2u2i_recall'] = data['u2u2i_recall']
- else:
- recall_dict['u2u2i_recall'] = u2u2i_recall
- elif ab_Code==60064:
- if len(data['return_video_recall'])>0:
- recall_dict['return_video_recall'] = data['return_video_recall']
- else:
- recall_dict['return_video_recall'] = return_video_recall
- recall_pos1 = [('rov_recall_region_h',0, 0.98),('rov_recall_24h',0.98, 1),('rov_recall_region_24h',0,1),
- ('rov_recall_24h',0,1), ('rov_recall_24h_dup',0,1)]
- recall_pos2 = [('rov_recall_region_h',0,0.98),('rov_recall_24h',0.98,1),('rov_recall_region_24h',0,1),
- ('rov_recall_24h',0,1),('rov_recall_24h_dup',0,1)]
- recall_pos3 = [('rov_recall_region_h', 0,0.98), ('rov_recall_24h', 0.98,1), ('rov_recall_region_24h', 0,1),
- ('rov_recall_24h', 0,1), ('rov_recall_24h_dup', 0,1)]
- recall_pos4 = [('rov_recall_region_h', 0,0.98), ('rov_recall_24h', 0,0.02), ('rov_recall_region_24h', 0,1),
- ('rov_recall_24h', 0,1), ('rov_recall_24h_dup', 0,1)]
- if exp_config and 'recall_pos1' in exp_config \
- and 'recall_pos2' in exp_config \
- and 'recall_pos3' in exp_config \
- and 'recall_pos4' in exp_config :
- recall_pos1 = exp_config['recall_pos1']
- recall_pos2 = exp_config['recall_pos2']
- recall_pos3 = exp_config['recall_pos3']
- recall_pos4 = exp_config['recall_pos4']
-
- rov_recall_rank = []
- recall_list = []
- recall_list.append(recall_pos1)
- recall_list.append(recall_pos2)
- recall_list.append(recall_pos3)
- recall_list.append(recall_pos4)
- select_ids = set('')
- recall_num_limit_dict = {}
- if exp_config and 'recall_num_limit' in exp_config:
- recall_num_limit_dict = exp_config['recall_num_limit']
- exp_recall_dict = {}
-
- for j in range(3):
- if len(rov_recall_rank)>12:
- break
-
- for recall_pos_config in recall_list:
- rand_num = random.random()
- index_pos = 0
-
- for per_recall_item in recall_pos_config:
- if index_pos == 1:
- break
- if len(per_recall_item)<3:
- continue
- per_recall_name = per_recall_item[0]
- per_recall_min = float(per_recall_item[1])
- per_recall_max = float(per_recall_item[2])
- per_recall_num = exp_recall_dict.get(per_recall_name, 0)
- per_recall_total_num = recall_num_limit_dict.get(per_recall_name, 0)
-
- if len(recall_num_limit_dict)>0 and per_recall_total_num>0 and per_recall_num>= per_recall_total_num:
- continue
- if rand_num >= per_recall_min and rand_num < per_recall_max and per_recall_name in recall_dict:
- per_recall = recall_dict[per_recall_name]
- for recall_item in per_recall:
- vid = recall_item['videoId']
- if vid in select_ids:
- continue
- recall_item['rand'] = rand_num
- rov_recall_rank.append(recall_item)
- select_ids.add(vid)
- if per_recall_name in exp_recall_dict:
- exp_recall_dict[per_recall_name] +=1
- else:
- exp_recall_dict[per_recall_name] = 1
- index_pos = 1
- break
-
- if len(rov_recall_rank)<4:
- rov_doudi_rank = region_h_recall_rank + sim_recall + u2i_recall + u2u2i_recall + w2v_recall +return_video_recall+u2i_play_recall+ region_24h_recall_rank + rule_24h_recall_rank + rule_24h_dup_recall_rank
- for recall_item in rov_doudi_rank:
- vid = recall_item['videoId']
- if vid in select_ids:
- continue
- rov_recall_rank.append(recall_item)
- select_ids.add(vid)
- if len(rov_recall_rank)>12:
- break
-
-
-
- flow_recall_rank = sorted(data['flow_pool_recall'], key=lambda k: k.get('rovScore', 0), reverse=True)
-
- rov_recall_rank, flow_recall_rank = remove_duplicate(rov_recall=rov_recall_rank, flow_recall=flow_recall_rank,
- top_K=top_K)
-
-
-
- rank_result = []
-
- if len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- rov_recall_rank = rov_recall_rank[top_K:]
- else:
- rank_result.extend(flow_recall_rank[:top_K])
- flow_recall_rank = flow_recall_rank[top_K:]
- flow_num = 0
- flowConfig =0
- if exp_config and exp_config['flowConfig']:
- flowConfig = exp_config['flowConfig']
- if flowConfig == 1 and len(rov_recall_rank) > 0:
- rank_result.extend(rov_recall_rank[:top_K])
- for recall_item in rank_result:
- flow_recall_name = recall_item.get("flowPool", '')
- if flow_recall_name is not None and flow_recall_name.find("#") > -1:
- flow_num = flow_num + 1
- all_recall_rank = rov_recall_rank + flow_recall_rank
- if flow_num > 0:
- rank_result.extend(all_recall_rank[:size - top_K])
- return rank_result[:size], flow_num
- else:
-
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- i += 1
- else:
-
- i = 0
- while i < size - top_K:
-
- rand = random.random()
-
- if rand < flow_pool_P:
- if flow_recall_rank:
- rank_result.append(flow_recall_rank[0])
- flow_recall_rank.remove(flow_recall_rank[0])
- else:
- rank_result.extend(rov_recall_rank[:size - top_K - i])
- return rank_result[:size], flow_num
- else:
- if rov_recall_rank:
- rank_result.append(rov_recall_rank[0])
- rov_recall_rank.remove(rov_recall_rank[0])
- else:
- rank_result.extend(flow_recall_rank[:size - top_K - i])
- return rank_result[:size],flow_num
- i += 1
- return rank_result[:size], flow_num
- def merge_density_control(
- data: list, rov: list, flow: list, rule: dict
- ) -> list:
-
- status_cur: Dict[str, int] = {}
- for d in data:
- if "tags" in d.keys() and len(d["tags"]) != 0:
- for t in d["tags"]:
- if t in rule.keys():
- status_cur[t] = 1 + status_cur[t] if t in status_cur.keys() else 1
- status_cur_illegal: Dict[str, int] = {}
- for k, v in status_cur.items():
- if k in rule.keys() and rule[k] < v:
- status_cur_illegal[k] = v - rule[k]
- if len(status_cur_illegal) == 0:
- return data
-
- indexes = []
- pushes = []
- var1 = len(data)
- for i in range(var1-1, -1, -1):
- d = data[i]
- tags = d["tags"] if "tags" in d.keys() else []
- inters = set(tags) & set(status_cur_illegal.keys())
- if len(inters) == 0:
- continue
- indexes.append(i)
- pushes.append(d["flowPool"] if "flowPool" in d.keys() and len(d["flowPool"]) != 0 else "")
- for inter in inters:
- status_cur_illegal[inter] = status_cur_illegal[inter] - 1
- if status_cur_illegal[inter] == 0:
- status_cur_illegal.pop(inter)
- status_cur[inter] = status_cur[inter] - 1
- if status_cur[inter] == 0:
- status_cur.pop(inter)
-
- for index, push in zip(reversed(indexes), reversed(pushes)):
- if len(push) > 0:
-
- candidate = flow
- else:
-
- candidate = rov
- for i, d in enumerate(candidate):
- judge_rule_set = judge_rule(rule=rule, status=status_cur)
- tags = set(d["tags"] if "tags" in d.keys() else [])
- if len(judge_rule_set & tags) != 0:
- continue
-
- tmp = copy.deepcopy(data[index])
- data[index] = d
- candidate[i] = tmp
-
- for tag in tags:
- status_cur[tag] = status_cur[tag] + 1 if tag in status_cur.keys() else 1
- break
- if len(push) > 0:
- flow = candidate
- else:
- rov = candidate
- return data
- def judge_rule(rule: dict, status: dict) -> Set:
- result = set()
- for k, v in status.items():
- if k in rule.keys() and rule[k] <= v:
- result.add(k)
- return result
- def get_video_tags(redis_helper, video_ids) -> dict:
- REDIS_PREFIX = "alg_recsys_video_tags_"
- redis_keys = [REDIS_PREFIX + str(i) for i in video_ids]
- video_tags = redis_helper.get_batch_key(redis_keys)
- video_tag_dict = {}
- if video_tags is not None:
- for i, tags_str in enumerate(video_tags):
- tags = []
- if tags_str is not None and len(tags_str) != 0:
- tags = str(tags_str).split(",")
- video_tag_dict[video_ids[i]] = tags
- return video_tag_dict
- if __name__ == '__main__':
- data: list = [
- {'videoId': 1, 'flowPool': '', 'tags': ['下午好','元旦','祝福']},
- {'videoId': 2, 'flowPool': '', 'tags': ['下午好','祝福']},
- {'videoId': 3, 'flowPool': '', 'tags': ['早上好']},
- {'videoId': 4, 'flowPool': 'flow', 'tags': ['下午好','元旦','祝福']},
- ]
- rov = [
- {'videoId': 10, 'flowPool': '', 'tags': ['下午好']},
- {'videoId': 11, 'flowPool': '', 'tags': ['祝福']},
- {'videoId': 12, 'flowPool': '', 'tags': ['下午好','元旦','祝福']},
- {'videoId': 13, 'flowPool': '', 'tags': ['元旦', "下午好"]},
-
-
- ]
- flow = [
- {'videoId': 20, 'flowPool': 'flow', 'tags': ['下午好']},
- {'videoId': 21, 'flowPool': 'flow', 'tags': ['下午好']},
- {'videoId': 22, 'flowPool': 'flow', 'tags': []},
- {'videoId': 23, 'flowPool': 'flow', 'tags': []}
- ]
- rule = {
- '下午好': 2,
- '早上好': 1,
- '祝福': 1
- }
- result = merge_density_control(data, rov, flow, rule)
- print(result)
-
-
-
-
-
-
-
-
-
-
-
-
-
- pass
|