OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / packages / apps / QuickSearchBox / src / com / android / quicksearchbox / DefaultCorpusRanker.java
1 /*
2  * Copyright (C) 2010 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
17 package com.android.quicksearchbox;
18
19 import android.util.Log;
20
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;
26 import java.util.Map;
27
28 public class DefaultCorpusRanker extends AbstractCorpusRanker {
29
30     private static final boolean DBG = false;
31     private static final String TAG = "QSB.DefaultCorpusRanker";
32
33     private final ShortcutRepository mShortcuts;
34
35     public DefaultCorpusRanker(Corpora corpora, ShortcutRepository shortcuts) {
36         super(corpora);
37         mShortcuts = shortcuts;
38     }
39
40     private static class CorpusComparator implements Comparator<Corpus> {
41         private final Map<String,Integer> mClickScores;
42
43         public CorpusComparator(Map<String,Integer> clickScores) {
44             mClickScores = clickScores;
45         }
46
47         public int compare(Corpus corpus1, Corpus corpus2) {
48             boolean corpus1IsDefault = corpus1.isCorpusDefaultEnabled();
49             boolean corpus2IsDefault = corpus2.isCorpusDefaultEnabled();
50
51             if (corpus1IsDefault != corpus2IsDefault) {
52                 // Default corpora always come before non-default
53                 return corpus1IsDefault ? -1 : 1;
54             } else {
55                 // Then by descending score
56                 return getCorpusScore(corpus2) - getCorpusScore(corpus1);
57             }
58         }
59
60         /**
61          * Scores a corpus. Higher score is better.
62          */
63         private int getCorpusScore(Corpus corpus) {
64             // Web corpus always comes first
65             if (corpus.isWebCorpus()) {
66                 return Integer.MAX_VALUE;
67             }
68             // Then use click score
69             Integer clickScore = mClickScores.get(corpus.getName());
70             if (clickScore != null) {
71                 return clickScore;
72             }
73             return 0;
74         }
75     }
76
77     @Override
78     public List<Corpus> rankCorpora(Corpora corpora) {
79         Collection<Corpus> enabledCorpora = corpora.getEnabledCorpora();
80         if (DBG) Log.d(TAG, "Ranking: " + enabledCorpora);
81
82         Map<String,Integer> clickScores = mShortcuts.getCorpusScores();
83         ArrayList<Corpus> ordered = new ArrayList<Corpus>(enabledCorpora);
84         Collections.sort(ordered, new CorpusComparator(clickScores));
85
86         if (DBG) Log.d(TAG, "Click scores: " + clickScores);
87         if (DBG) Log.d(TAG, "Ordered: " + ordered);
88         return ordered;
89     }
90
91 }