Home | History | Annotate | Download | only in tool
      1 package org.unicode.cldr.tool;
      2 
      3 import java.util.Arrays;
      4 import java.util.EnumMap;
      5 import java.util.EnumSet;
      6 import java.util.Locale;
      7 import java.util.Map;
      8 import java.util.Map.Entry;
      9 import java.util.Set;
     10 import java.util.TreeMap;
     11 import java.util.TreeSet;
     12 
     13 import org.unicode.cldr.util.CLDRConfig;
     14 import org.unicode.cldr.util.LanguageTagParser;
     15 import org.unicode.cldr.util.Organization;
     16 import org.unicode.cldr.util.StandardCodes;
     17 import org.unicode.cldr.util.SupplementalDataInfo;
     18 import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo;
     19 import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo.Count;
     20 import org.unicode.cldr.util.SupplementalDataInfo.PluralType;
     21 
     22 import com.ibm.icu.impl.Relation;
     23 import com.ibm.icu.text.PluralRules;
     24 import com.ibm.icu.text.PluralRules.FixedDecimal;
     25 import com.ibm.icu.text.PluralRules.FixedDecimalRange;
     26 import com.ibm.icu.text.PluralRules.FixedDecimalSamples;
     27 
     28 public class GeneratePluralConfirmation {
     29     private static final com.ibm.icu.text.PluralRules.PluralType ICU_ORDINAL = com.ibm.icu.text.PluralRules.PluralType.ORDINAL;
     30 
     31     private static final CLDRConfig testInfo = ToolConfig.getToolInstance();
     32 
     33     private static final StandardCodes STANDARD_CODES = testInfo.getStandardCodes();
     34 
     35     private static final SupplementalDataInfo SUPPLEMENTAL = testInfo.getSupplementalDataInfo();
     36 
     37     private static final PluralRulesFactory prf = PluralRulesFactory.getInstance(SUPPLEMENTAL);
     38 
     39     public static void main(String[] args) {
     40         for (String uLocale : new TreeSet<>(prf.getLocales())) {
     41             for (PluralRules.PluralType type : PluralRules.PluralType.values()) {
     42                 for (Count count : Count.values()) {
     43                     String pattern = prf.getSamplePattern(uLocale, type, count);
     44                     if (pattern.contains("{no pattern available}")) {
     45                         continue;
     46                     }
     47                     System.out.println("locale="
     48                         + uLocale
     49                         + "; action=add ; new_path="
     50                         + "//ldml/numbers/minimalPairs/"
     51                         + (type == PluralRules.PluralType.CARDINAL ? "plural" : "ordinal")
     52                         + "MinimalPairs[@"
     53                         + (type == PluralRules.PluralType.CARDINAL ? "count" : "ordinal")
     54                         + "=\"" + count.toString().toLowerCase(Locale.ENGLISH) + "\"]"
     55                         + "; new_value="
     56                         + pattern);
     57                 }
     58                 System.out.println();
     59             }
     60         }
     61     }
     62 
     63     public static void mainOld2(String[] args) {
     64         Set<String> locales = STANDARD_CODES.getLocaleCoverageLocales(Organization.google);
     65         // SUPPLEMENTAL.getPluralLocales(PluralType.ordinal)
     66         LanguageTagParser ltp = new LanguageTagParser();
     67         for (String loc : locales) {
     68             ltp.set(loc);
     69             if (!ltp.getScript().isEmpty() || !ltp.getRegion().isEmpty()) {
     70                 continue;
     71             }
     72             EnumSet<Count> counts = EnumSet.noneOf(Count.class);
     73             for (Count count : Count.VALUES) {
     74                 String pat = prf.getSamplePattern(loc, ICU_ORDINAL, count);
     75                 if (pat != null && !pat.contains("{no pattern available}")) {
     76                     counts.add(count);
     77                 }
     78             }
     79             switch (counts.size()) {
     80             case 0:
     81                 System.out.format("%s\t%s\t%s\t%s\n", loc, "missing", "n/a", "n/a");
     82                 break;
     83             case 1: {
     84                 String pat = prf.getSamplePattern(loc, ICU_ORDINAL, Count.other);
     85                 System.out.format("%s\t%s\t%s\t%s\n", loc, "constant", Count.other, "n/a");
     86             }
     87                 break;
     88             default:
     89                 for (Count count : counts) {
     90                     String pat = prf.getSamplePattern(loc, ICU_ORDINAL, count);
     91                     System.out.format("%s\t%s\t%s\t%s\n", loc, "multiple", count, pat);
     92                 }
     93                 break;
     94             }
     95         }
     96     }
     97 
     98     public static void mainOld(String[] args) {
     99         Set<String> testLocales = new TreeSet(Arrays.asList(
    100             "az cy hy ka kk km ky lo mk mn my ne pa si sq uz eu my si sq vi zu"
    101                 .split(" ")));
    102         // STANDARD_CODES.getLocaleCoverageLocales("google");
    103         System.out.println(testLocales);
    104         LanguageTagParser ltp = new LanguageTagParser();
    105         for (String locale : testLocales) {
    106             // the only known case where plural rules depend on region or script is pt_PT
    107             if (locale.equals("root") || locale.equals("en_GB") || locale.equals("es_419") || locale.equals("*")) {
    108                 continue;
    109             }
    110             //            if (!locale.equals("en")) {
    111             //                continue;
    112             //            }
    113 
    114             for (PluralType type : PluralType.values()) {
    115                 // for now, just ordinals
    116                 if (type == PluralType.cardinal) {
    117                     continue;
    118                 }
    119                 PluralInfo pluralInfo = SUPPLEMENTAL.getPlurals(type, locale, false);
    120                 PluralRules rules;
    121                 if (pluralInfo == null) {
    122                     rules = PluralRules.DEFAULT;
    123                 } else {
    124                     rules = pluralInfo.getPluralRules();
    125                 }
    126                 Values values = new Values();
    127                 values.locale = locale;
    128                 values.type = type;
    129 
    130                 for (int i = 0; i < 30; ++i) {
    131                     FixedDecimal fd = new FixedDecimal(i);
    132                     String keyword = rules.select(fd);
    133                     values.showValue(keyword, fd);
    134                 }
    135                 for (String keyword : rules.getKeywords()) {
    136                     FixedDecimalSamples samples;
    137                     samples = rules.getDecimalSamples(keyword, PluralRules.SampleType.DECIMAL);
    138                     values.showSamples(keyword, samples);
    139                     samples = rules.getDecimalSamples(keyword, PluralRules.SampleType.INTEGER);
    140                     values.showSamples(keyword, samples);
    141                 }
    142                 System.out.println(values);
    143             }
    144         }
    145     }
    146 
    147     static class Values {
    148         String locale;
    149         PluralType type;
    150         Relation<Count, FixedDecimal> soFar = Relation.of(new EnumMap(Count.class), TreeSet.class);
    151         Map<FixedDecimal, String> sorted = new TreeMap();
    152 
    153         private void showValue(String keyword, FixedDecimal fd) {
    154             Set<FixedDecimal> soFarSet = soFar.getAll(keyword);
    155             if (soFarSet != null && soFarSet.contains(fd)) {
    156                 return;
    157             }
    158             soFar.put(Count.valueOf(keyword), fd);
    159             sorted.put(fd, keyword);
    160         }
    161 
    162         public void showSamples(String keyword, FixedDecimalSamples samples) {
    163             if (samples == null) {
    164                 return;
    165             }
    166             for (FixedDecimalRange range : samples.getSamples()) {
    167                 Set<FixedDecimal> soFarSet = soFar.getAll(keyword);
    168                 if (soFarSet != null && soFarSet.size() > 10) {
    169                     break;
    170                 }
    171                 showValue(keyword, range.start);
    172                 if (!range.end.equals(range.start)) {
    173                     soFarSet = soFar.getAll(keyword);
    174                     if (soFarSet != null && soFarSet.size() > 10) {
    175                         break;
    176                     }
    177                     showValue(keyword, range.end);
    178                 }
    179             }
    180         }
    181 
    182         @Override
    183         public String toString() {
    184             StringBuilder buffer = new StringBuilder();
    185             for (Entry<Count, Set<FixedDecimal>> entry : soFar.keyValuesSet()) {
    186                 Count count = entry.getKey();
    187                 for (FixedDecimal fd : entry.getValue()) {
    188                     String pattern = prf.getSamplePattern(locale, type.standardType, count);
    189                     buffer.append(locale + "\t" + type + "\t" + count + "\t" + fd + "\t" + pattern.replace("{0}", String.valueOf(fd)) + "\n");
    190                 }
    191                 buffer.append("\n");
    192             }
    193             return buffer.toString();
    194         }
    195     }
    196 }
    197