Search in sources :

Example 36 with Transliterator

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

the class TransliteratorTest method checkRegistry.

private void checkRegistry(String id) {
    Transliterator fie = Transliterator.getInstance(id);
    final UnicodeSet fae = new UnicodeSet("[a-z5]");
    fie.setFilter(fae);
    Transliterator foe = Transliterator.getInstance(id);
    UnicodeFilter fee = foe.getFilter();
    if (fae.equals(fee)) {
        errln("Changed what is in registry for " + id);
    }
}
Also used : UnicodeFilter(android.icu.text.UnicodeFilter) UnicodeSet(android.icu.text.UnicodeSet) Transliterator(android.icu.text.Transliterator)

Example 37 with Transliterator

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

the class TransliteratorTest method TestRegisterAlias.

@Test
public void TestRegisterAlias() {
    String longID = "Lower;[aeiou]Upper";
    String shortID = "Any-CapVowels";
    String reallyShortID = "CapVowels";
    Transliterator.registerAlias(shortID, longID);
    Transliterator t1 = Transliterator.getInstance(longID);
    Transliterator t2 = Transliterator.getInstance(reallyShortID);
    if (!t1.getID().equals(longID))
        errln("Transliterator instantiated with long ID doesn't have long ID");
    if (!t2.getID().equals(reallyShortID))
        errln("Transliterator instantiated with short ID doesn't have short ID");
    if (!t1.toRules(true).equals(t2.toRules(true)))
        errln("Alias transliterators aren't the same");
    Transliterator.unregister(shortID);
    try {
        t1 = Transliterator.getInstance(shortID);
        errln("Instantiation with short ID succeeded after short ID was unregistered");
    } catch (IllegalArgumentException e) {
    }
    // try the same thing again, but this time with something other than
    // an instance of CompoundTransliterator
    String realID = "Latin-Greek";
    String fakeID = "Latin-dlgkjdflkjdl";
    Transliterator.registerAlias(fakeID, realID);
    t1 = Transliterator.getInstance(realID);
    t2 = Transliterator.getInstance(fakeID);
    if (!t1.toRules(true).equals(t2.toRules(true)))
        errln("Alias transliterators aren't the same");
    Transliterator.unregister(fakeID);
}
Also used : CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 38 with Transliterator

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

the class TransliteratorTest method TestUserFunction.

/**
 * Test that user-registered transliterators can be used under function
 * syntax.
 */
@Test
public void TestUserFunction() {
    Transliterator t;
    // There's no need to register inverses if we don't use them
    TestUserFunctionFactory.add("Any-gif", Transliterator.createFromRules("gif", "'\\'u(..)(..) > '<img src=\"http://www.unicode.org/gifs/24/' $1 '/U' $1$2 '.gif\">';", Transliterator.FORWARD));
    // TestUserFunctionFactory.add("gif-Any", Transliterator.getInstance("Any-Null"));
    TestUserFunctionFactory.add("Any-RemoveCurly", Transliterator.createFromRules("RemoveCurly", "[\\{\\}] > ; \\\\N > ;", Transliterator.FORWARD));
    // TestUserFunctionFactory.add("RemoveCurly-Any", Transliterator.getInstance("Any-Null"));
    logln("Trying &hex");
    t = Transliterator.createFromRules("hex2", "(.) > &hex($1);", Transliterator.FORWARD);
    logln("Registering");
    TestUserFunctionFactory.add("Any-hex2", t);
    t = Transliterator.getInstance("Any-hex2");
    expect(t, "abc", "\\u0061\\u0062\\u0063");
    logln("Trying &gif");
    t = Transliterator.createFromRules("gif2", "(.) > &Gif(&Hex2($1));", Transliterator.FORWARD);
    logln("Registering");
    TestUserFunctionFactory.add("Any-gif2", t);
    t = Transliterator.getInstance("Any-gif2");
    expect(t, "ab", "<img src=\"http://www.unicode.org/gifs/24/00/U0061.gif\">" + "<img src=\"http://www.unicode.org/gifs/24/00/U0062.gif\">");
    // Test that filters are allowed after &
    t = Transliterator.createFromRules("test", "(.) > &Hex($1) ' ' &Any-RemoveCurly(&Name($1)) ' ';", Transliterator.FORWARD);
    expect(t, "abc", "\\u0061 LATIN SMALL LETTER A \\u0062 LATIN SMALL LETTER B \\u0063 LATIN SMALL LETTER C ");
    // Unregister our test stuff
    TestUserFunctionFactory.unregister();
}
Also used : Transliterator(android.icu.text.Transliterator) Test(org.junit.Test)

Example 39 with Transliterator

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

the class TransliteratorTest method TestIncrementalProgress.

// Check to see that incremental gets at least part way through a reasonable string.
// TODO(junit): should be working - also should be converted to parameterized test
@Ignore
@Test
public void TestIncrementalProgress() {
    String latinTest = "The Quick Brown Fox.";
    String devaTest = Transliterator.getInstance("Latin-Devanagari").transliterate(latinTest);
    String kataTest = Transliterator.getInstance("Latin-Katakana").transliterate(latinTest);
    String[][] tests = { { "Any", latinTest }, { "Latin", latinTest }, { "Halfwidth", latinTest }, { "Devanagari", devaTest }, { "Katakana", kataTest } };
    Enumeration sources = Transliterator.getAvailableSources();
    while (sources.hasMoreElements()) {
        String source = (String) sources.nextElement();
        String test = findMatch(source, tests);
        if (test == null) {
            logln("Skipping " + source + "-X");
            continue;
        }
        Enumeration targets = Transliterator.getAvailableTargets(source);
        while (targets.hasMoreElements()) {
            String target = (String) targets.nextElement();
            Enumeration variants = Transliterator.getAvailableVariants(source, target);
            while (variants.hasMoreElements()) {
                String variant = (String) variants.nextElement();
                String id = source + "-" + target + "/" + variant;
                logln("id: " + id);
                Transliterator t = Transliterator.getInstance(id);
                CheckIncrementalAux(t, test);
                String rev = t.transliterate(test);
                Transliterator inv = t.getInverse();
                CheckIncrementalAux(inv, rev);
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) CaseInsensitiveString(android.icu.util.CaseInsensitiveString) ReplaceableString(android.icu.text.ReplaceableString) Transliterator(android.icu.text.Transliterator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 40 with Transliterator

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

the class TransliteratorTest method TestGurmukhiDevanagari.

/**
 * Test Gurmukhi-Devanagari Tippi and Bindi
 */
@Test
public void TestGurmukhiDevanagari() {
    // the rule says:
    // (\u0902) (when preceded by vowel)      --->  (\u0A02)
    // (\u0902) (when preceded by consonant)  --->  (\u0A70)
    UnicodeSet vowel = new UnicodeSet("[\u0905-\u090A \u090F\u0910\u0913\u0914 \u093e-\u0942\u0947\u0948\u094B\u094C\u094D]");
    UnicodeSet non_vowel = new UnicodeSet("[\u0915-\u0928\u092A-\u0930]");
    UnicodeSetIterator vIter = new UnicodeSetIterator(vowel);
    UnicodeSetIterator nvIter = new UnicodeSetIterator(non_vowel);
    Transliterator trans = Transliterator.getInstance("Devanagari-Gurmukhi");
    StringBuffer src = new StringBuffer(" \u0902");
    StringBuffer expect = new StringBuffer(" \u0A02");
    while (vIter.next()) {
        src.setCharAt(0, (char) vIter.codepoint);
        expect.setCharAt(0, (char) (vIter.codepoint + 0x0100));
        expect(trans, src.toString(), expect.toString());
    }
    expect.setCharAt(1, '\u0A70');
    while (nvIter.next()) {
        // src.setCharAt(0,(char) nvIter.codepoint);
        src.setCharAt(0, (char) nvIter.codepoint);
        expect.setCharAt(0, (char) (nvIter.codepoint + 0x0100));
        expect(trans, src.toString(), expect.toString());
    }
}
Also used : UnicodeSetIterator(android.icu.text.UnicodeSetIterator) UnicodeSet(android.icu.text.UnicodeSet) 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