Skip to content

Commit 2f2c425

Browse files
authored
CLDR-15331 Add RBNF to coverage (#4503)
1 parent 673269c commit 2f2c425

File tree

4 files changed

+175
-6
lines changed

4 files changed

+175
-6
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.unicode.cldr.tool;
2+
3+
import com.google.common.base.Joiner;
4+
import com.google.common.base.Splitter;
5+
import com.google.common.collect.Multimap;
6+
import com.google.common.collect.TreeMultimap;
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.List;
10+
import java.util.Map.Entry;
11+
import java.util.Set;
12+
import java.util.TreeSet;
13+
import org.unicode.cldr.util.RbnfData;
14+
15+
public class ListRbnf {
16+
public static void main(String[] args) {
17+
listRbnf();
18+
}
19+
20+
public static void listRbnf() {
21+
Joiner joinTab = Joiner.on('\t');
22+
Joiner joinCommaSpace = Joiner.on(", ");
23+
Splitter splitDash = Splitter.on('-');
24+
System.out.println("\nLocale to type to subtype");
25+
Set<String> keys = RbnfData.INSTANCE.getRbnfTypeToLocales().keySet();
26+
System.out.println("locale\t" + joinTab.join(keys));
27+
28+
Multimap<String, String> typeToSubtype = TreeMultimap.create();
29+
30+
for (Entry<String, Multimap<String, String>> entry :
31+
RbnfData.INSTANCE.getLocaleToTypesToSubtypes().entrySet()) {
32+
String locale = entry.getKey();
33+
List<String> row = new ArrayList<>();
34+
row.add(locale);
35+
for (String key : keys) {
36+
Collection<String> values = entry.getValue().get(key);
37+
row.add(values == null ? "" : joinCommaSpace.join(values));
38+
typeToSubtype.putAll(key, values);
39+
}
40+
System.out.println(joinTab.join(row));
41+
}
42+
System.out.println("\nType to subtype");
43+
Set<String> allPieces = new TreeSet<>();
44+
Set<String> allSubtypes = new TreeSet<>();
45+
46+
for (Entry<String, Collection<String>> entry : typeToSubtype.asMap().entrySet()) {
47+
Collection<String> values = entry.getValue();
48+
allSubtypes.addAll(values);
49+
Set<String> pieces = new TreeSet<>();
50+
values.stream().forEach(x -> pieces.addAll(splitDash.splitToList(x)));
51+
System.out.println(
52+
joinTab.join(
53+
entry.getKey(),
54+
joinCommaSpace.join(values),
55+
joinCommaSpace.join(pieces)));
56+
allPieces.addAll(pieces);
57+
}
58+
System.out.println("\nAll subtypes");
59+
System.out.println(joinCommaSpace.join(allSubtypes));
60+
61+
System.out.println("\nAll pieces");
62+
System.out.println(joinCommaSpace.join(allPieces));
63+
64+
System.out.println("\nSubtype to locale");
65+
for (Entry<String, Collection<String>> entry :
66+
RbnfData.INSTANCE.getRbnfTypeToLocales().asMap().entrySet()) {
67+
System.out.println(entry.getKey() + "\t" + joinCommaSpace.join(entry.getValue()));
68+
}
69+
}
70+
}

tools/cldr-code/src/main/java/org/unicode/cldr/util/CoreCoverageInfo.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ public enum CoreItems {
5252
plurals(Level.MODERATE, Sublevel.start),
5353
collation(Level.MODERATE),
5454

55-
grammar(Level.MODERN, Sublevel.start),
55+
spellout_cardinal(Level.MODERN),
56+
spellout_ordinal(Level.MODERN),
57+
digits_ordinals(Level.MODERN),
58+
5659
ordinals(Level.MODERN),
5760
romanization(Level.MODERN),
61+
62+
grammar(Level.MODERN, Sublevel.start),
5863
;
5964

6065
public static final Set<CoreItems> ALL = ImmutableSet.copyOf(CoreItems.values());
@@ -102,7 +107,8 @@ public static Set<CoreItems> getCoreCoverageInfo(
102107
CLDRFile resolvedFile, Multimap<CoreItems, String> detailedErrors) {
103108
detailedErrors.clear();
104109
if (!resolvedFile.isResolved()) {
105-
throw new IllegalArgumentException();
110+
throw new IllegalArgumentException(
111+
"Don't call on unresolved locales: " + resolvedFile.getLocaleID());
106112
}
107113
CLDRFile file = resolvedFile.getUnresolved();
108114
String locale = file.getLocaleID();
@@ -141,6 +147,10 @@ public static Set<CoreItems> getCoreCoverageInfo(
141147
+ "\"]/pluralRule[@count=\"other\"]");
142148
}
143149

150+
rbnfHelper(baseLanguage, "spellout-cardinal", detailedErrors, CoreItems.spellout_cardinal);
151+
rbnfHelper(baseLanguage, "spellout-ordinal", detailedErrors, CoreItems.spellout_ordinal);
152+
rbnfHelper(baseLanguage, "digits-ordinal", detailedErrors, CoreItems.digits_ordinals);
153+
144154
// (01) Default content script and region (normally: normally country with largest
145155
// population using that language, and normal script for that).
146156
// [supplemental/supplementalMetadata.xml]
@@ -289,6 +299,26 @@ public static Set<CoreItems> getCoreCoverageInfo(
289299
return ImmutableSet.copyOf(Sets.difference(CoreItems.ALL, detailedErrors.keySet()));
290300
}
291301

302+
private static void rbnfHelper(
303+
String stringLocale,
304+
String rbnfType,
305+
Multimap<CoreItems, String> detailedErrors,
306+
CoreItems coreItems) {
307+
CLDRLocale cldrLocale = CLDRLocale.getInstance(stringLocale);
308+
while (cldrLocale != null // if either null or root, we fail
309+
&& !cldrLocale.equals(CLDRLocale.ROOT)) {
310+
Multimap<String, String> typeInfo =
311+
RbnfData.INSTANCE.getLocaleToTypesToSubtypes().get(cldrLocale.toString());
312+
if (typeInfo != null // if we succeed, just return
313+
&& typeInfo.containsKey(rbnfType)) {
314+
return;
315+
}
316+
// otherwise try the parent
317+
cldrLocale = cldrLocale.getParent();
318+
}
319+
detailedErrors.put(coreItems, RbnfData.INSTANCE.getPath(rbnfType));
320+
}
321+
292322
private static final String[][] ROMANIZATION_PATHS = {
293323
{"", "-Latin"},
294324
{"", "-Latin-BGN"},
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.unicode.cldr.util;
2+
3+
import com.google.common.collect.Multimap;
4+
import com.google.common.collect.TreeMultimap;
5+
import java.util.Map;
6+
import java.util.TreeMap;
7+
8+
public enum RbnfData {
9+
INSTANCE;
10+
11+
private final Map<String, Multimap<String, String>> localeToTypesToSubtypes;
12+
private final Multimap<String, String> rbnfTypeToLocales;
13+
14+
{
15+
Map<String, Multimap<String, String>> _localeToRbnfType = new TreeMap<>();
16+
Multimap<String, String> _rbnfTypeToLocales = TreeMultimap.create();
17+
Factory factory = CLDRConfig.getInstance().getRBNFFactory();
18+
for (String locale : factory.getAvailable()) {
19+
CLDRFile cldrFile = factory.make(locale, false);
20+
Multimap<String, String> typeToSubtype = _localeToRbnfType.get(locale);
21+
if (typeToSubtype == null) {
22+
_localeToRbnfType.put(locale, typeToSubtype = TreeMultimap.create());
23+
}
24+
for (String dpath : cldrFile) {
25+
String path = cldrFile.getFullXPath(dpath);
26+
XPathParts parts = XPathParts.getFrozenInstance(path);
27+
if (!"rbnf".equals(parts.getElement(1))
28+
|| !"ruleset".equals(parts.getElement(3))
29+
|| "private".equals(parts.getAttributeValue(3, "access"))) {
30+
continue;
31+
}
32+
String fullType = parts.getAttributeValue(3, "type");
33+
String rbnfType;
34+
String rbnfSubtype;
35+
if (fullType.startsWith("spellout") || fullType.startsWith("digits")) {
36+
int index2 = fullType.indexOf('-', fullType.indexOf('-') + 1);
37+
if (index2 == -1) {
38+
rbnfType = fullType;
39+
rbnfSubtype = "DEFAULT";
40+
} else {
41+
rbnfType = fullType.substring(0, index2);
42+
rbnfSubtype = fullType.substring(index2 + 1);
43+
}
44+
} else {
45+
rbnfType = "UNKNOWN";
46+
rbnfSubtype = fullType;
47+
}
48+
typeToSubtype.put(rbnfType, rbnfSubtype);
49+
_rbnfTypeToLocales.put(rbnfType, locale);
50+
}
51+
}
52+
this.localeToTypesToSubtypes = CldrUtility.protectCollection(_localeToRbnfType);
53+
this.rbnfTypeToLocales = CldrUtility.protectCollection(_rbnfTypeToLocales);
54+
}
55+
56+
public Multimap<String, String> getRbnfTypeToLocales() {
57+
return rbnfTypeToLocales;
58+
}
59+
60+
public Map<String, Multimap<String, String>> getLocaleToTypesToSubtypes() {
61+
return localeToTypesToSubtypes;
62+
}
63+
64+
public String getPath(String rbnfType) {
65+
return "//ldml/rbnf/rulesetGrouping[@type=\"SpelloutRules\"]/ruleset[@type=\""
66+
+ rbnfType
67+
+ "\"]";
68+
}
69+
}

tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestCoverage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public void TestBasic() {
4141
Multimap<CoreItems, String> errors = LinkedHashMultimap.create();
4242
Set<CoreItems> coreCoverage = CoreCoverageInfo.getCoreCoverageInfo(engCldrFile, errors);
4343
if (!assertEquals("English should be complete", all, coreCoverage)) {
44-
showDiff("Missing", all, coreCoverage);
44+
showDiff("English Missing", all, coreCoverage);
4545
}
46-
CLDRFile skimpyLocale = testInfo.getCldrFactory().make("asa", false);
46+
CLDRFile skimpyLocale = testInfo.getCldrFactory().make("asa", true);
4747
errors.clear();
4848
coreCoverage = CoreCoverageInfo.getCoreCoverageInfo(skimpyLocale, errors);
4949
if (!assertEquals("Skimpy locale should not be complete", none, coreCoverage)) {
50-
showDiff("Missing", all, coreCoverage);
51-
showDiff("Extra", coreCoverage, none);
50+
showDiff("Skimpy Missing", all, coreCoverage);
51+
showDiff("Skimpy Extra", coreCoverage, none);
5252
}
5353
}
5454

0 commit comments

Comments
 (0)