Search in sources :

Example 91 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class RoundTripTest method TestHangul2.

/**
 * This is a shorter version of the test for doubles, that allows us to skip lots of cases, but
 * does check the ones that should cause problems (if any do).
 */
@Test
public void TestHangul2() {
    Transliterator lh = Transliterator.getInstance("Latin-Hangul");
    Transliterator hl = lh.getInverse();
    final UnicodeSet representativeHangul = getRepresentativeHangul();
    for (UnicodeSetIterator it = new UnicodeSetIterator(representativeHangul); it.next(); ) {
        assertRoundTripTransform("Transform", it.getString(), lh, hl);
    }
}
Also used : UnicodeSetIterator(android.icu.text.UnicodeSetIterator) UnicodeSet(android.icu.text.UnicodeSet) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 92 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class RoundTripTest method TestSingle.

@Test
public void TestSingle() {
    Transliterator t = Transliterator.getInstance("Latin-Greek");
    t.transliterate("\u0061\u0101\u0069");
}
Also used : Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 93 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestCaseMap.

/**
 * Test the case mapping transliterators.
 */
@Test
public void TestCaseMap() {
    Transliterator toUpper = Transliterator.getInstance("Any-Upper[^xyzXYZ]");
    Transliterator toLower = Transliterator.getInstance("Any-Lower[^xyzXYZ]");
    Transliterator toTitle = Transliterator.getInstance("Any-Title[^xyzXYZ]");
    expect(toUpper, "The quick brown fox jumped over the lazy dogs.", "THE QUICK BROWN FOx JUMPED OVER THE LAzy DOGS.");
    expect(toLower, "The quIck brown fOX jUMPED OVER THE LAzY dogs.", "the quick brown foX jumped over the lazY dogs.");
    expect(toTitle, "the quick brown foX caN'T jump over the laZy dogs.", "The Quick Brown FoX Can't Jump Over The LaZy Dogs.");
}
Also used : Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 94 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class TransliteratorTest method TestCompoundRBT.

/**
 * Test compound RBT rules.
 */
@Test
public void TestCompoundRBT() {
    // Careful with spacing and ';' here:  Phrase this exactly
    // as toRules() is going to return it.  If toRules() changes
    // with regard to spacing or ';', then adjust this string.
    String rule = "::Hex-Any;\n" + "::Any-Lower;\n" + "a > '.A.';\n" + "b > '.B.';\n" + "::[^t]Any-Upper;";
    Transliterator t = Transliterator.createFromRules("Test", rule, Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: createFromRules failed");
        return;
    }
    expect(t, "\u0043at in the hat, bat on the mat", "C.A.t IN tHE H.A.t, .B..A.t ON tHE M.A.t");
    String r = t.toRules(true);
    if (r.equals(rule)) {
        logln("OK: toRules() => " + r);
    } else {
        errln("FAIL: toRules() => " + r + ", expected " + rule);
    }
    // Now test toRules
    t = Transliterator.getInstance("Greek-Latin; Latin-Cyrillic", Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: createInstance failed");
        return;
    }
    String exp = "::Greek-Latin;\n::Latin-Cyrillic;";
    r = t.toRules(true);
    if (!r.equals(exp)) {
        errln("FAIL: toRules() => " + r + ", expected " + exp);
    } else {
        logln("OK: toRules() => " + r);
    }
    // Round trip the result of toRules
    t = Transliterator.createFromRules("Test", r, Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: createFromRules #2 failed");
        return;
    } else {
        logln("OK: createFromRules(" + r + ") succeeded");
    }
    // Test toRules again
    r = t.toRules(true);
    if (!r.equals(exp)) {
        errln("FAIL: toRules() => " + r + ", expected " + exp);
    } else {
        logln("OK: toRules() => " + r);
    }
    // Test Foo(Bar) IDs.  Careful with spacing in id; make it conform
    // to what the regenerated ID will look like.
    String id = "Upper(Lower);(NFKC)";
    t = Transliterator.getInstance(id, Transliterator.FORWARD);
    if (t == null) {
        errln("FAIL: createInstance #2 failed");
        return;
    }
    if (t.getID().equals(id)) {
        logln("OK: created " + id);
    } else {
        errln("FAIL: createInstance(" + id + ").getID() => " + t.getID());
    }
    Transliterator u = t.getInverse();
    if (u == null) {
        errln("FAIL: createInverse failed");
        return;
    }
    exp = "NFKC();Lower(Upper)";
    if (u.getID().equals(exp)) {
        logln("OK: createInverse(" + id + ") => " + u.getID());
    } else {
        errln("FAIL: createInverse(" + id + ") => " + u.getID());
    }
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 95 with Transliterator

use of android.icu.text.Transliterator in project j2objc by google.

the class JamoTest method TestRealText.

@Test
public void TestRealText() {
    Transliterator latinJamo = Transliterator.getInstance("Latin-Jamo");
    Transliterator jamoLatin = latinJamo.getInverse();
    Transliterator jamoHangul = Transliterator.getInstance("NFC");
    Transliterator hangulJamo = Transliterator.getInstance("NFD");
    // Transliterator rt = new CompoundTransliterator(new Transliterator[] {
    // hangulJamo, jamoLatin, latinJamo, jamoHangul });
    Transliterator rt = Transliterator.getInstance("NFD;Jamo-Latin;Latin-Jamo;NFC");
    int pos = 0;
    StringBuffer buf = new StringBuffer();
    int total = 0;
    int errors = 0;
    while (pos < WHAT_IS_UNICODE.length()) {
        int space = WHAT_IS_UNICODE.indexOf(' ', pos + 1);
        if (space < 0) {
            space = WHAT_IS_UNICODE.length();
        }
        if (pos < space) {
            ++total;
            String hangul = WHAT_IS_UNICODE.substring(pos, space);
            String hangulX = rt.transliterate(hangul);
            if (!hangul.equals(hangulX)) {
                ++errors;
                String jamo = hangulJamo.transliterate(hangul);
                String latin = jamoLatin.transliterate(jamo);
                String jamo2 = latinJamo.transliterate(latin);
                String hangul2 = jamoHangul.transliterate(jamo2);
                if (hangul.equals(hangul2)) {
                    buf.setLength(0);
                    buf.append("FAIL (Compound transliterator problem): ");
                    buf.append(hangul + " => " + jamoToName(jamo) + " => " + latin + " => " + jamoToName(jamo2) + " => " + hangul2 + "; but " + hangul + " =cpd=> " + jamoToName(hangulX));
                    errln(Utility.escape(buf.toString()));
                } else if (jamo.equals(jamo2)) {
                    buf.setLength(0);
                    buf.append("FAIL (Jamo<>Hangul problem): ");
                    if (!hangul2.equals(hangulX)) {
                        buf.append("(Weird: " + hangulX + " != " + hangul2 + ")");
                    }
                    buf.append(hangul + " => " + jamoToName(jamo) + " => " + latin + " => " + jamoToName(jamo2) + " => " + hangul2);
                    errln(Utility.escape(buf.toString()));
                } else {
                    buf.setLength(0);
                    buf.append("FAIL: ");
                    if (!hangul2.equals(hangulX)) {
                        buf.append("(Weird: " + hangulX + " != " + hangul2 + ")");
                    }
                    // The Hangul-Jamo conversion is not usually the
                    // bug here, so we hide it from display.
                    // Uncomment lines to see the Hangul.
                    // hangul + " => " +
                    buf.append(jamoToName(jamo) + " => " + latin + " => " + jamoToName(jamo2));
                    errln(Utility.escape(buf.toString()));
                }
            }
        }
        pos = space + 1;
    }
    if (errors != 0) {
        errln("Test word failures: " + errors + " out of " + total);
    } else {
        logln("All " + total + " test words passed");
    }
}
Also used : Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Aggregations

Transliterator (android.icu.text.Transliterator)97 Test (org.junit.Test)88 ReplaceableString (android.icu.text.ReplaceableString)66 CaseInsensitiveString (android.icu.util.CaseInsensitiveString)57 UnicodeSet (android.icu.text.UnicodeSet)19 UnicodeSetIterator (android.icu.text.UnicodeSetIterator)5 ULocale (android.icu.util.ULocale)3 Enumeration (java.util.Enumeration)3 UnicodeFilter (android.icu.text.UnicodeFilter)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintWriter (java.io.PrintWriter)2 ArrayList (java.util.ArrayList)2 UnicodeMap (android.icu.dev.util.UnicodeMap)1 CanonicalIterator (android.icu.text.CanonicalIterator)1 Normalizer2 (android.icu.text.Normalizer2)1 Replaceable (android.icu.text.Replaceable)1 BufferedWriter (java.io.BufferedWriter)1 HashSet (java.util.HashSet)1