2

我向文本应用程序阿拉伯语发表了演讲。语音文本的结果将与数组中的现有文本进行比较。使用字符串算法 macthing Jaro-Winkler 距离我一直在计算所有文本输入的手册以及数组中的文本,结果是 1.0。但是当应用程序按照比较的结果运行时是不计算的。

此文本数组数据:

int [] soal={R.raw.alikhlas,R.raw.alfalaq,R.raw.alkafirun};
String [] jawaban={"لم يلدولم يولد","ومن شرغاسق اذاوقب","ولاانتم عبدون مااعبد"};

将数组和输入语音转换为文本

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case REQ_CODE_SPEECH_INPUT: {
            if (resultCode == RESULT_OK && null != data) {

                ArrayList<String> result = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
         String hasil = result.get(0);
                JaroWinkler jw = new JaroWinkler();
            String angka = String.valueOf(jw.similarity(hasil,jawaban[0]));

             txtSpeechInput.setText(result.get(0) + "\n"+angka);

            }

这个算法 Jaro Winkler 距离

public final double similarity(final String s1, final String s2) {
    if (s1 == null) {
        throw new NullPointerException("s1 must not be null");
    }

    if (s2 == null) {
        throw new NullPointerException("s2 must not be null");
    }

    if (s1.equals(s2)) {
        return 1;
    }

    int[] mtp = matches(s1, s2);
    float m = mtp[0];
    if (m == 0) {
        return 0f;
    }
    double j = ((m /  s1.length() + m / s2.length() + (m - mtp[1]) / m))
            / THREE;

    AppLog.logString("Nilai m = " +m);
    AppLog.logString("Nilai s1 = " +s1.length());
    AppLog.logString("Nilai s2 = " +s2.length());
    AppLog.logString("Nilai mtp = " +mtp[1]);
    AppLog.logString("Nilai J = " +j);

    double jw = j;

    if (j > getThreshold()) {
        jw = j + Math.min(JW_COEF, 1.0 / mtp[THREE]) * mtp[2] * (1 - j);

        AppLog.logString("Nilai mtp = " +mtp[THREE]);
        AppLog.logString("Nilai mtp2 = " +mtp[2]);
        AppLog.logString("Nilai Jw = " +jw);
    }
    return jw;
}
public final double distance(final String s1, final String s2) {
    return 1.0 - similarity(s1, s2);
}

private int[] matches(final String s1, final String s2) {
    String max, min;
    if (s1.length() > s2.length()) {
        max = s1;
        min = s2;
    } else {
        max = s2;
        min = s1;
    }
    int range = Math.max(max.length() / 2 - 1, 0);

    int[] matchIndexes = new int[min.length()];
    Arrays.fill(matchIndexes, -1);
    boolean[] matchFlags = new boolean[max.length()];
    int matches = 0;
    for (int mi = 0; mi < min.length(); mi++) {
        char c1 = min.charAt(mi); //ntuk mengambil karakter pada sebuah String sesuai index
        for (int xi = Math.max(mi - range, 0),
             xn = Math.min(mi + range + 1, max.length()); xi < xn; xi++) {
            if (!matchFlags[xi] && c1 == max.charAt(xi)) {
                matchIndexes[mi] = xi;
                matchFlags[xi] = true;
                matches++;
                break;
            }
        }
    }
    char[] ms1 = new char[matches];
    char[] ms2 = new char[matches];
    for (int i = 0, si = 0; i < min.length(); i++) {
        if (matchIndexes[i] != -1) {
            ms1[si] = min.charAt(i);
            si++;
        }
    }
    for (int i = 0, si = 0; i < max.length(); i++) {
        if (matchFlags[i]) {
            ms2[si] = max.charAt(i);
            si++;
        }
    }
    int transpositions = 0;
    for (int mi = 0; mi < ms1.length; mi++) {
        if (ms1[mi] != ms2[mi]) {
            transpositions++;
        }
    }
    int prefix = 0;
    for (int mi = 0; mi < min.length(); mi++) {
        if (s1.charAt(mi) == s2.charAt(mi)) {
            prefix++;
        } else {
            break;
        }
    }
    return new int[]{matches, transpositions / 2, prefix, max.length()};
}
4

0 回答 0