<commit_before>package org.commcare.adapters;

import android.content.Context;

import org.commcare.utils.StringUtils;

/**
 * Created by amstone326 on 1/11/17.
 */

public class FuzzyMatchComboboxAdapter extends CustomFilterComboboxAdapter {

    public FuzzyMatchComboboxAdapter(final Context context, final int textViewResourceId,
                                     final String[] objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public boolean shouldRestrictTyping() {
        return false;
    }

    @Override
    public boolean choiceShouldBeShown(String choice, CharSequence textEntered) {
        if ("".equals(textEntered) || textEntered == null) {
            return true;
        }

        String textEnteredLowerCase = textEntered.toString().toLowerCase();
        String choiceLowerCase = choice.toLowerCase();

        // Try the easy cases first
        if (choiceLowerCase.startsWith(textEnteredLowerCase) ||
                StringUtils.fuzzyMatch(textEnteredLowerCase, choiceLowerCase).first) {
            return true;
        }

        return allEnteredWordsHaveFuzzyMatch(choiceLowerCase, textEnteredLowerCase);
    }

    private static boolean allEnteredWordsHaveFuzzyMatch(String choiceLowerCase,
                                                  String textEnteredLowerCase) {
        String[] enteredWords = textEnteredLowerCase.split(" ");<commit_msg>add explanatory comment<commit_after>package org.commcare.adapters;

import android.content.Context;

import org.commcare.utils.StringUtils;

/**
 * Created by amstone326 on 1/11/17.
 */

public class FuzzyMatchComboboxAdapter extends CustomFilterComboboxAdapter {

    public FuzzyMatchComboboxAdapter(final Context context, final int textViewResourceId,
                                     final String[] objects) {
        super(context, textViewResourceId, objects);
    }

    @Override
    public boolean shouldRestrictTyping() {
        // Since fuzzy match only works once the number of typed letters reaches a certain
        // threshold and is close to the number of letters in the comparison string, it doesn't
        // make any sense to restrict typing here
        return false;
    }

    @Override
    public boolean choiceShouldBeShown(String choice, CharSequence textEntered) {
        if ("".equals(textEntered) || textEntered == null) {
            return true;
        }

        String textEnteredLowerCase = textEntered.toString().toLowerCase();
        String choiceLowerCase = choice.toLowerCase();

        // Try the easy cases first
        if (choiceLowerCase.startsWith(textEnteredLowerCase) ||
                StringUtils.fuzzyMatch(textEnteredLowerCase, choiceLowerCase).first) {
            return true;
        }

        return allEnteredWordsHaveFuzzyMatch(choiceLowerCase, textEnteredLowerCase);
    }

    private static boolean allEnteredWordsHaveFuzzyMatch(String choiceLowerCase,
                                                  String textEnteredLowerCase) {
        String[] enteredWords = textEnteredLowerCase.split(" ");
{'fraction_non_alphanumeric': 0.07311549660304109, 'fraction_numerical': 0.00517631834357813, 'mean_word_length': 2.6635071090047395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5105552', 'n_tokens_mistral': 782, 'n_tokens_neox': 755, 'n_words': 194}