Search in sources :

Example 6 with Pojo

use of fr.neamar.kiss.pojo.Pojo in project KISS by Neamar.

the class AppProvider method getResults.

public ArrayList<Pojo> getResults(String query) {
    query = StringNormalizer.normalize(query);
    ArrayList<Pojo> records = new ArrayList<>();
    int relevance;
    // The position inside the query
    int queryPos;
    // The position inside pojo.nameNormalized
    int normalizedAppPos;
    // The position inside pojo.name, updated after we increment normalizedAppPos
    int appPos;
    int beginMatch;
    int matchedWordStarts;
    int totalWordStarts;
    ArrayList<Pair<Integer, Integer>> matchPositions;
    for (AppPojo pojo : pojos) {
        pojo.displayName = pojo.name;
        pojo.displayTags = pojo.tags;
        relevance = 0;
        queryPos = 0;
        normalizedAppPos = 0;
        appPos = pojo.mapPosition(normalizedAppPos);
        beginMatch = 0;
        matchedWordStarts = 0;
        totalWordStarts = 0;
        matchPositions = null;
        boolean match = false;
        int inputLength = pojo.nameNormalized.length();
        while (normalizedAppPos < inputLength) {
            int cApp = pojo.nameNormalized.codePointAt(normalizedAppPos);
            if (queryPos < query.length() && query.codePointAt(queryPos) == cApp) {
                // If we aren't already matching something, let's save the beginning of the match
                if (!match) {
                    beginMatch = normalizedAppPos;
                    match = true;
                }
                // If we are at the beginning of a word, add it to matchedWordStarts
                if (appPos == 0 || normalizedAppPos == 0 || Character.isUpperCase(pojo.name.codePointAt(appPos)) || Character.isWhitespace(pojo.name.codePointBefore(appPos)))
                    matchedWordStarts += 1;
                // Increment the position in the query
                queryPos += Character.charCount(query.codePointAt(queryPos));
            } else if (match) {
                if (matchPositions == null)
                    matchPositions = new ArrayList<>();
                matchPositions.add(Pair.create(beginMatch, normalizedAppPos));
                match = false;
            }
            // If we are at the beginning of a word, add it to totalWordsStarts
            if (appPos == 0 || normalizedAppPos == 0 || Character.isUpperCase(pojo.name.codePointAt(appPos)) || Character.isWhitespace(pojo.name.codePointBefore(appPos)))
                totalWordStarts += 1;
            normalizedAppPos += Character.charCount(cApp);
            appPos = pojo.mapPosition(normalizedAppPos);
        }
        boolean matchedTags = false;
        if (match) {
            if (matchPositions == null)
                matchPositions = new ArrayList<>();
            matchPositions.add(Pair.create(beginMatch, normalizedAppPos));
        }
        int tagStart = 0;
        int tagEnd = 0;
        if (queryPos == query.length() && matchPositions != null) {
            // Add percentage of matched letters, but at a weight of 40
            relevance += (int) (((double) queryPos / pojo.nameNormalized.length()) * 40);
            // Add percentage of matched upper case letters (start of word), but at a weight of 60
            relevance += (int) (((double) matchedWordStarts / totalWordStarts) * 60);
            // The more fragmented the matches are, the less the result is important
            relevance *= (0.2 + 0.8 * (1.0 / matchPositions.size()));
        } else {
            if (pojo.tagsNormalized.startsWith(query)) {
                relevance = 4 + query.length();
            } else if (pojo.tagsNormalized.indexOf(query) >= 0) {
                relevance = 3 + query.length();
            }
            if (relevance > 0) {
                matchedTags = true;
            }
            tagStart = pojo.tagsNormalized.indexOf(query);
            tagEnd = tagStart + query.length();
        }
        if (relevance > 0) {
            if (!matchedTags) {
                pojo.setDisplayNameHighlightRegion(matchPositions);
            } else {
                pojo.setTagHighlight(tagStart, tagEnd);
            }
            pojo.relevance = relevance;
            records.add(pojo);
        }
    }
    return records;
}
Also used : AppPojo(fr.neamar.kiss.pojo.AppPojo) AppPojo(fr.neamar.kiss.pojo.AppPojo) Pojo(fr.neamar.kiss.pojo.Pojo) ArrayList(java.util.ArrayList) Pair(android.util.Pair)

Example 7 with Pojo

use of fr.neamar.kiss.pojo.Pojo in project KISS by Neamar.

the class AppProvider method findById.

/**
     * Return a Pojo
     *
     * @param id              we're looking for
     * @param allowSideEffect do we allow this function to have potential side effect? Set to false to ensure none.
     * @return an AppPojo, or null
     */
public Pojo findById(String id, Boolean allowSideEffect) {
    for (Pojo pojo : pojos) {
        if (pojo.id.equals(id)) {
            // Reset displayName to default value
            if (allowSideEffect) {
                pojo.displayName = pojo.name;
                if (pojo instanceof AppPojo) {
                    AppPojo appPojo = (AppPojo) pojo;
                    appPojo.displayTags = appPojo.tags;
                }
            }
            return pojo;
        }
    }
    return null;
}
Also used : AppPojo(fr.neamar.kiss.pojo.AppPojo) AppPojo(fr.neamar.kiss.pojo.AppPojo) Pojo(fr.neamar.kiss.pojo.Pojo)

Example 8 with Pojo

use of fr.neamar.kiss.pojo.Pojo in project KISS by Neamar.

the class ContactsProvider method getResults.

public ArrayList<Pojo> getResults(String query) {
    query = StringNormalizer.normalize(query);
    ArrayList<Pojo> results = new ArrayList<>();
    // Search people with composed names, e.g "jean-marie"
    // (not part of the StringNormalizer class, since we want to keep dashes on other providers)
    query = query.replaceAll("-", " ");
    int relevance;
    int matchPositionStart;
    int matchPositionEnd;
    String contactNameNormalized;
    final String queryWithSpace = " " + query;
    for (ContactsPojo contact : pojos) {
        relevance = 0;
        contactNameNormalized = contact.nameNormalized;
        boolean alias = false;
        matchPositionStart = 0;
        matchPositionEnd = 0;
        if (contactNameNormalized.startsWith(query)) {
            relevance = 50;
            matchPositionEnd = matchPositionStart + query.length();
        } else if ((matchPositionStart = contactNameNormalized.indexOf(queryWithSpace)) > -1) {
            relevance = 40;
            matchPositionEnd = matchPositionStart + queryWithSpace.length();
        } else if (contact.nickname.contains(query)) {
            alias = true;
            contact.displayName = contact.name + " <small>(" + contact.nickname.replaceFirst("(?i)(" + Pattern.quote(query) + ")", "{$1}") + ")</small>";
            relevance = 30;
        } else if (query.length() > 2) {
            matchPositionStart = 0;
            matchPositionEnd = 0;
            if (contact.phoneSimplified.startsWith(query)) {
                relevance = 10;
            } else if (contact.phoneSimplified.contains(query)) {
                relevance = 5;
            }
        }
        if (relevance > 0) {
            // Increase relevance according to number of times the contacts
            // was phoned :
            relevance += contact.timesContacted;
            // Increase relevance for starred contacts:
            if (contact.starred)
                relevance += 30;
            // Decrease for home numbers:
            if (contact.homeNumber)
                relevance -= 1;
            if (!alias)
                contact.setDisplayNameHighlightRegion(matchPositionStart, matchPositionEnd);
            contact.relevance = relevance;
            results.add(contact);
            // can return hundreds of results which are then slow to sort and display)
            if (results.size() > 50) {
                break;
            }
        }
    }
    return results;
}
Also used : Pojo(fr.neamar.kiss.pojo.Pojo) ContactsPojo(fr.neamar.kiss.pojo.ContactsPojo) ContactsPojo(fr.neamar.kiss.pojo.ContactsPojo) ArrayList(java.util.ArrayList)

Example 9 with Pojo

use of fr.neamar.kiss.pojo.Pojo in project KISS by Neamar.

the class SearchProvider method getResults.

public ArrayList<Pojo> getResults(String query) {
    ArrayList<Pojo> pojos = new ArrayList<>();
    Set<String> selectedProviders = new TreeSet<>();
    selectedProviders.addAll(PreferenceManager.getDefaultSharedPreferences(this).getStringSet("search-providers", new HashSet<>(Arrays.asList("Google"))));
    for (String searchProvider : selectedProviders) {
        SearchPojo pojo = new SearchPojo();
        pojo.query = query;
        pojo.relevance = 10;
        pojo.url = searchProviderUrls.get(searchProvider);
        pojo.name = searchProvider;
        pojos.add(pojo);
    }
    Matcher m = p.matcher(query);
    if (m.find()) {
        String guessedUrl = URLUtil.guessUrl(query);
        if (URLUtil.isValidUrl(guessedUrl)) {
            SearchPojo pojo = new SearchPojo();
            pojo.query = "";
            pojo.relevance = 50;
            pojo.name = guessedUrl;
            pojo.url = guessedUrl;
            pojo.direct = true;
            pojos.add(pojo);
        }
    }
    return pojos;
}
Also used : Pojo(fr.neamar.kiss.pojo.Pojo) SearchPojo(fr.neamar.kiss.pojo.SearchPojo) Matcher(java.util.regex.Matcher) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SearchPojo(fr.neamar.kiss.pojo.SearchPojo) HashSet(java.util.HashSet)

Example 10 with Pojo

use of fr.neamar.kiss.pojo.Pojo in project KISS by Neamar.

the class ShortcutsProvider method getResults.

@Override
public ArrayList<Pojo> getResults(String query) {
    ArrayList<Pojo> results = new ArrayList<>();
    int relevance;
    int matchPositionStart;
    int matchPositionEnd;
    String shortcutNameLowerCased;
    final String queryWithSpace = " " + query;
    for (ShortcutsPojo shortcut : pojos) {
        relevance = 0;
        shortcutNameLowerCased = shortcut.nameNormalized;
        matchPositionEnd = 0;
        if (shortcutNameLowerCased.startsWith(query)) {
            relevance = 75;
            matchPositionStart = 0;
            matchPositionEnd = query.length();
        } else if ((matchPositionStart = shortcutNameLowerCased.indexOf(queryWithSpace)) > -1) {
            relevance = 50;
            matchPositionEnd = matchPositionStart + queryWithSpace.length();
        } else if ((matchPositionStart = shortcutNameLowerCased.indexOf(query)) > -1) {
            relevance = 1;
            matchPositionEnd = matchPositionStart + query.length();
        }
        if (relevance > 0) {
            shortcut.setDisplayNameHighlightRegion(matchPositionStart, matchPositionEnd);
            shortcut.relevance = relevance;
            results.add(shortcut);
        }
    }
    return results;
}
Also used : ShortcutsPojo(fr.neamar.kiss.pojo.ShortcutsPojo) Pojo(fr.neamar.kiss.pojo.Pojo) ShortcutsPojo(fr.neamar.kiss.pojo.ShortcutsPojo) ArrayList(java.util.ArrayList)

Aggregations

Pojo (fr.neamar.kiss.pojo.Pojo)14 ArrayList (java.util.ArrayList)10 ShortcutsPojo (fr.neamar.kiss.pojo.ShortcutsPojo)4 SuppressLint (android.annotation.SuppressLint)3 AppPojo (fr.neamar.kiss.pojo.AppPojo)3 Result (fr.neamar.kiss.result.Result)3 ImageView (android.widget.ImageView)2 ValuedHistoryRecord (fr.neamar.kiss.db.ValuedHistoryRecord)2 Drawable (android.graphics.drawable.Drawable)1 Pair (android.util.Pair)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 Toast (android.widget.Toast)1 ContactsPojo (fr.neamar.kiss.pojo.ContactsPojo)1 PojoComparator (fr.neamar.kiss.pojo.PojoComparator)1 SearchPojo (fr.neamar.kiss.pojo.SearchPojo)1 SettingsPojo (fr.neamar.kiss.pojo.SettingsPojo)1 TogglesPojo (fr.neamar.kiss.pojo.TogglesPojo)1