1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 package com.android.settings.dashboard.suggestions; 17 18 import com.android.settingslib.drawer.Tile; 19 20 import android.support.annotation.VisibleForTesting; 21 22 import java.util.Collections; 23 import java.util.Comparator; 24 import java.util.HashMap; 25 import java.util.List; 26 import java.util.Map; 27 28 public class SuggestionRanker { 29 30 private static final String TAG = "SuggestionRanker"; 31 32 // The following coefficients form a linear model, which mixes the features to obtain a 33 // relevance metric for ranking the suggestion items. This model is learned with off-line data 34 // by training a binary classifier to detect the clicked items. The higher the obtained 35 // relevance metric, the higher chance of getting clicked. 36 private static final Map<String, Double> WEIGHTS = new HashMap<String, Double>() {{ 37 put(SuggestionFeaturizer.FEATURE_IS_SHOWN, 5.05140842519); 38 put(SuggestionFeaturizer.FEATURE_IS_DISMISSED, 2.29641455171); 39 put(SuggestionFeaturizer.FEATURE_IS_CLICKED, -2.98812233623); 40 put(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_SHOWN, 5.02807250202); 41 put(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_DISMISSED, 2.49589700842); 42 put(SuggestionFeaturizer.FEATURE_TIME_FROM_LAST_CLICKED, -4.3377039948); 43 put(SuggestionFeaturizer.FEATURE_SHOWN_COUNT, -2.35993512546); 44 }}; 45 46 private final SuggestionFeaturizer mSuggestionFeaturizer; 47 48 private final Map<Tile, Double> relevanceMetrics; 49 50 Comparator<Tile> suggestionComparator = new Comparator<Tile>() { 51 @Override 52 public int compare(Tile suggestion1, Tile suggestion2) { 53 return relevanceMetrics.get(suggestion1) < relevanceMetrics.get(suggestion2) ? 1 : -1; 54 } 55 }; 56 SuggestionRanker(SuggestionFeaturizer suggestionFeaturizer)57 public SuggestionRanker(SuggestionFeaturizer suggestionFeaturizer) { 58 mSuggestionFeaturizer = suggestionFeaturizer; 59 relevanceMetrics = new HashMap<Tile, Double>(); 60 } 61 rankSuggestions(final List<Tile> suggestions, List<String> suggestionIds)62 public void rankSuggestions(final List<Tile> suggestions, List<String> suggestionIds) { 63 relevanceMetrics.clear(); 64 Map<String, Map<String, Double>> features = mSuggestionFeaturizer.featurize(suggestionIds); 65 for (int i = 0; i < suggestionIds.size(); i++) { 66 relevanceMetrics.put(suggestions.get(i), 67 getRelevanceMetric(features.get(suggestionIds.get(i)))); 68 } 69 Collections.sort(suggestions, suggestionComparator); 70 } 71 72 @VisibleForTesting getRelevanceMetric(Map<String, Double> features)73 double getRelevanceMetric(Map<String, Double> features) { 74 double sum = 0; 75 if (features == null) { 76 return sum; 77 } 78 for (String feature : WEIGHTS.keySet()) { 79 sum += WEIGHTS.get(feature) * features.get(feature); 80 } 81 return sum; 82 } 83 } 84