def recommend_with_scores(self, test_device_events, tips): last_session_events = _get_last_session(test_device_events) event_to_score = {} for (group_id, event_id) in self.event_types: event_to_score[(group_id, event_id)] = 0 for (group_id2, event_id2) in last_session_events.keys(): if (group_id2, event_id2) in self.cp_matrix[(group_id, event_id)].keys(): event_to_score[(group_id, event_id)] += self.cp_matrix[( group_id, event_id)][(group_id2, event_id2)] if (group_id2, event_id2) in self.dp_matrix.keys(): event_to_score[(group_id, event_id)] += self.dp_matrix[( group_id2, event_id2)][(group_id, event_id)] sorted_by_score_events = sorted(event_to_score.items(), key=operator.itemgetter(1), reverse=True) tips_to_recommend = {} for top_event in sorted_by_score_events: event = top_event[0] score = top_event[1] if (event not in test_device_events.keys()) \ and _is_intersection(tips, event_to_tips(event[0], event[1])) > 0: for tip in event_to_tips(event[0], event[1]): if tip in tips: tips_to_recommend[tip] = score if self.is_logging: logging.info("RecommenderCo+Dis:recommend: recommendation made.") return tips_to_recommend
def _generate_recommendation_list(self, recommendations, test_device_events, tips): recommendation_map = {} for event, score in recommendations: if event_to_tips(self.event_types[event][0], self.event_types[event][1]) and \ _is_intersection(tips, event_to_tips(self.event_types[event][0], self.event_types[event][1])) > 0 \ and self.event_types[event] not in test_device_events: for tip in event_to_tips(self.event_types[event][0], self.event_types[event][1]): if tip in tips: recommendation_map[tip] = max(0, score) return recommendation_map
def process_action(self, event): if event.device_id not in self.device_to_done_actions.keys(): self.device_to_done_actions[event.device_id] = {} if (event.event_id, event.ide ) not in self.device_to_done_actions[event.device_id].keys(): self.device_to_done_actions[event.device_id][( event.event_id, event.ide)] = (event.timestamp, event.count) else: _, cnt = self.device_to_done_actions[event.device_id][( event.event_id, event.ide)] self.device_to_done_actions[event.device_id][(event.event_id, event.ide)] = \ (event.timestamp, cnt + event.count) if event.device_id in self.device_to_tips.keys(): for (tip, tip_timestamp, tip_id) in self.device_to_tips[event.device_id]: if tip in event_to_tips(group_id=ACTION_INVOKED_GROUP, event_id=event.event_id): if event.timestamp - tip_timestamp < PREDICTED_TIME_MILLIS: with open( TRAIN_LABELS_POSITIVE_DIR + self.generate_file_name( event.device_id, "csv", tip_id), 'w') as fout: fout.write(tip + "\n") self.tips_done[tip_id] = True
def process_tip(self, event): tip_name = event.event_id.split(";")[0] algo_name = event.event_id.split(";")[1] if algo_name not in self.all_tips_cnt.keys(): self.all_tips_cnt[algo_name] = 0 self.good_tips_cnt[algo_name] = 0 self.devices_good[algo_name] = {} self.devices_all[algo_name] = {} self.all_tips_cnt[algo_name] += 1 self.devices_all[algo_name][event.device_id] = True is_tip_done_before = False if event.device_id in self.device_to_done_actions.keys(): for done_action in self.device_to_done_actions[ event.device_id].keys(): event_id, _ = done_action action_timestamp, _ = self.device_to_done_actions[ event.device_id][done_action] if tip_name in event_to_tips(group_id=ACTION_INVOKED_GROUP, event_id=event_id) \ and event.timestamp - action_timestamp < FORGET_TIME_DAYS * 24 * 60 * 60 * 1000: is_tip_done_before = True break if not is_tip_done_before: if event.device_id not in self.device_to_tips.keys(): self.device_to_tips[event.device_id] = [] self.device_to_tips[event.device_id].append( (tip_name, event.timestamp, algo_name))
def recommend(self, test_device_events, tips): if self.is_logging: logging.info( "RecommenderTopEventWithProbability:recommend: recommendation started." ) test_device_events = self._filter_old_test_device_events( test_device_events) not_done_event_with_prob, all_not_done_top_sum = \ self._get_not_done_events_with_probability(test_device_events, tips) if self.is_logging: logging.info( "RecommenderTopEventWithProbability:recommend: not_done_events computed, " + str(len(not_done_event_with_prob)) + " found.") if len(not_done_event_with_prob) <= 0: return event_to_tips(self.top_events[0][0][0], self.top_events[0][0][1]) probs = [] for event in not_done_event_with_prob: probs.append(event[1] / all_not_done_top_sum) if self.is_logging: logging.info( "RecommenderTopEventWithProbability:recommend: probability for not_done_events computed" ) events_ids = np.random.choice(range(len(not_done_event_with_prob)), len(not_done_event_with_prob), replace=False, p=probs) events = np.array(not_done_event_with_prob)[events_ids] tips_to_recommend = [] for event in events: for tip in event_to_tips(event[0][0], event[0][1]): if tip in tips: tips_to_recommend.append(tip) if self.is_logging: logging.info( "RecommenderTopEventWithProbability:recommend: tip generated.") return tips_to_recommend
def _split_events_by_ide(events): ide_to_events = {} for event in events: if ide not in ide_to_events.keys(): ide_to_events[ide] = [] if event.group_id == ACTION_INVOKED_GROUP and len( event_to_tips(event.group_id, event.event_id)) > 0: ide_to_events[ide].append( (event.group_id, event.event_id, event.device_id, event.count)) return ide_to_events
def recommend_with_scores(self, test_device_events, tips): if self.is_logging: logging.info( "RecommenderTopEvent:recommend: recommendation started.") test_device_events = self._filter_old_test_device_events( test_device_events) tips_to_recommend = {} for top_event in self.top_events: event = top_event[0] score = top_event[1] if (event not in test_device_events.keys())\ and _is_intersection(tips, event_to_tips(event[0], event[1])) > 0: for tip in event_to_tips(event[0], event[1]): if tip in tips: tips_to_recommend[tip] = score if self.is_logging: logging.info("RecommenderTopEvent:recommend: recommendation made.") return tips_to_recommend
def _get_not_done_events_with_probability(self, test_device_events, tips): not_done_event_with_prob = [] all_not_done_top_sum = 0 for top_event in self.top_events: event = top_event[0] probability = top_event[1] if (event not in test_device_events.keys()) \ and _is_intersection(tips, event_to_tips(event[0], event[1])) > 0: not_done_event_with_prob.append(top_event) all_not_done_top_sum += probability return not_done_event_with_prob, all_not_done_top_sum
def process_tip(self, event): data = { "tips": [], "usageInfo": {}, "bucket": event.bucket, 'ideName': event.ide } tip_name = event.event_id.split(";")[0] for t in self.ide_tips[event.ide].keys(): data["tips"].append(t) is_tip_done_before = False for done_action in self.device_to_done_actions[event.device_id].keys(): event_id, ide = done_action max_timestamp, count = self.device_to_done_actions[ event.device_id][done_action] if tip_name in event_to_tips(group_id=ACTION_INVOKED_GROUP, event_id=event_id): is_tip_done_before = True break if ide == event.ide: data["usageInfo"][event_id] = {} data["usageInfo"][event_id]["usageCount"] = count data["usageInfo"][event_id][ "lastUsedTimestamp"] = max_timestamp if not is_tip_done_before: with open( TRAIN_EVENTS_DIR + self.generate_file_name( event.device_id, "json", self.tip_id_cur), 'w') as fout: json.dump(data, fout) if event.device_id not in self.device_to_tips.keys(): self.device_to_tips[event.device_id] = [] self.device_to_tips[event.device_id].append( (tip_name, event.timestamp, str(self.tip_id_cur))) self.tip_id_cur += 1
def process_action(self, event): if event.device_id not in self.device_to_done_actions.keys(): self.device_to_done_actions[event.device_id] = {} if (event.event_id, event.ide ) not in self.device_to_done_actions[event.device_id].keys(): self.device_to_done_actions[event.device_id][( event.event_id, event.ide)] = (event.timestamp, event.count) else: _, cnt = self.device_to_done_actions[event.device_id][( event.event_id, event.ide)] self.device_to_done_actions[event.device_id][(event.event_id, event.ide)] = \ (event.timestamp, cnt + event.count) if event.device_id in self.device_to_tips.keys(): for (tip, tip_timestamp, algo) in self.device_to_tips[event.device_id]: if tip in event_to_tips(group_id=ACTION_INVOKED_GROUP, event_id=event.event_id): if event.timestamp - tip_timestamp < PREDICTED_TIME_MILLIS: self.good_tips_cnt[algo] += 1 self.devices_good[algo][event.device_id] = True