2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.quicksearchbox;
19 import android.util.Log;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
28 public class DefaultCorpusRanker extends AbstractCorpusRanker {
30 private static final boolean DBG = false;
31 private static final String TAG = "QSB.DefaultCorpusRanker";
33 private final ShortcutRepository mShortcuts;
35 public DefaultCorpusRanker(Corpora corpora, ShortcutRepository shortcuts) {
37 mShortcuts = shortcuts;
40 private static class CorpusComparator implements Comparator<Corpus> {
41 private final Map<String,Integer> mClickScores;
43 public CorpusComparator(Map<String,Integer> clickScores) {
44 mClickScores = clickScores;
47 public int compare(Corpus corpus1, Corpus corpus2) {
48 boolean corpus1IsDefault = corpus1.isCorpusDefaultEnabled();
49 boolean corpus2IsDefault = corpus2.isCorpusDefaultEnabled();
51 if (corpus1IsDefault != corpus2IsDefault) {
52 // Default corpora always come before non-default
53 return corpus1IsDefault ? -1 : 1;
55 // Then by descending score
56 return getCorpusScore(corpus2) - getCorpusScore(corpus1);
61 * Scores a corpus. Higher score is better.
63 private int getCorpusScore(Corpus corpus) {
64 // Web corpus always comes first
65 if (corpus.isWebCorpus()) {
66 return Integer.MAX_VALUE;
68 // Then use click score
69 Integer clickScore = mClickScores.get(corpus.getName());
70 if (clickScore != null) {
78 public List<Corpus> rankCorpora(Corpora corpora) {
79 Collection<Corpus> enabledCorpora = corpora.getEnabledCorpora();
80 if (DBG) Log.d(TAG, "Ranking: " + enabledCorpora);
82 Map<String,Integer> clickScores = mShortcuts.getCorpusScores();
83 ArrayList<Corpus> ordered = new ArrayList<Corpus>(enabledCorpora);
84 Collections.sort(ordered, new CorpusComparator(clickScores));
86 if (DBG) Log.d(TAG, "Click scores: " + clickScores);
87 if (DBG) Log.d(TAG, "Ordered: " + ordered);