智能搜索android

时间:2016-03-10 21:26:02

标签: android search android-contacts t9

在android开发者网站的This Retrieving a List of Contacts Tutorial之后,我设法实现了联系人搜索功能。这是我到目前为止的代码

private void retrieveContactRecord(String phoneNo) {
        try {
            Log.e("Info", "Input: " + phoneNo);
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(phoneNo));
            String[] projection = new String[]{ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME};


            String sortOrder = ContactsContract.PhoneLookup.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
            ContentResolver cr = getContentResolver();
            if (cr != null) {
                Cursor resultCur = cr.query(uri, projection, null, null, sortOrder);
                if (resultCur != null) {
                    while (resultCur.moveToNext()) {
                        String contactId = resultCur.getString(resultCur.getColumnIndex(ContactsContract.PhoneLookup._ID));
                        String contactName = resultCur.getString(resultCur.getColumnIndexOrThrow(ContactsContract.PhoneLookup.DISPLAY_NAME));
                        Log.e("Info", "Contact Id : " + contactId);
                        Log.e("Info", "Contact Display Name : " + contactName);
                        break;
                    }
                    resultCur.close();
                }
            }
        } catch (Exception sfg) {
            Log.e("Error", "Error in loadContactRecord : " + sfg.toString());
        }
    }

这是一个问题,这段代码效果非常好,但我需要在这里实现智能搜索。 我希望26268与Amanu以及094 526 2684匹配​​。我相信它被称为T9词典。

我试着寻找其他项目的线索,但我找不到任何东西。任何指针都将不胜感激!

3 个答案:

答案 0 :(得分:3)

可以使用trie data structure实施T9搜索。你可以在这里看到一个例子 - Trie dict。 在实现类似的东西后,您将能够将搜索输入转换为可能的T9解码变体,并比较它是否与名称匹配。

答案 1 :(得分:1)

将所有联系人转储到HashSet

Set<String> contacts = new HashSet<String>();

然后搜索:

List<List<String>> results = new ArrayList<List<String>>();
// start the search, pass empty stack to represent words found so far
search(input, dictionary, new Stack<String>(), results);

搜索方法(from @WhiteFang34

public static void search(String input, Set<String> contacts,
    Stack<String> words, List<List<String>> results) {

    for (int i = 0; i < input.length(); i++) {
        // take the first i characters of the input and see if it is a word
        String substring = input.substring(0, i + 1);

        if (contacts.contains(substring)) {
            // the beginning of the input matches a word, store on stack
            words.push(substring);

            if (i == input.length() - 1) {
                // there's no input left, copy the words stack to results
                results.add(new ArrayList<String>(words));
            } else {
                // there's more input left, search the remaining part
                search(input.substring(i + 1), contacts, words, results);
            }

            // pop the matched word back off so we can move onto the next i
            words.pop();
        }
    }
}

答案 2 :(得分:0)

联系人ContentProvider不支持。所以我做的是将所有联系人转储到List,然后使用RegEx来匹配该名称。

public static String[] values = new String[]{" 0", "1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9"};

/**
 * Get the possible pattern
 * You'll get something like ["2ABC","4GHI"] for input "14"
 */
public static List<String> possibleValues(String in) {

    if (in.length() >= 1) {
        List<String> p = possibleValues(in.substring(1));
        String s = "" + in.charAt(0);
        if (s.matches("[0-9]")) {
            int n = Integer.parseInt(s);

            p.add(0, values[n]);
        } else {
            // It is a character, use it as it is
            p.add(s);
        }

        return p;
    }
    return new ArrayList<>();
}

....然后编译模式。我使用(?i)使其不区分大小写

List<String> values = Utils.possibleValues(query);
StringBuilder sb = new StringBuilder();
for (String value : values) {
    sb.append("[");
    sb.append(value);
    sb.append("]");
    if (values.get(values.size() - 1) != value) {
    sb.append("\\s*");
    }
}

Log.e("Utils", "Pattern = " + sb.toString());

Pattern queryPattern = Pattern.compile("(?i)(" + sb.toString() + ")");

在此之后你会知道该怎么做。