xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/tool/CompareData.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.tool;
2 
3 import com.ibm.icu.dev.tool.shared.UOption;
4 import com.ibm.icu.text.Collator;
5 import com.ibm.icu.text.RuleBasedCollator;
6 import com.ibm.icu.util.ULocale;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.Set;
10 import java.util.TreeSet;
11 import org.unicode.cldr.util.CLDRFile;
12 import org.unicode.cldr.util.CLDRPaths;
13 import org.unicode.cldr.util.CldrUtility;
14 import org.unicode.cldr.util.Factory;
15 import org.unicode.cldr.util.LocaleIDParser;
16 import org.unicode.cldr.util.PathUtilities;
17 import org.unicode.cldr.util.PrettyPath;
18 
19 public class CompareData {
20 
21     private static final int HELP1 = 0, HELP2 = 1, SOURCEDIR = 2, DESTDIR = 3, MATCH = 4;
22 
23     private static final UOption[] options = {
24         UOption.HELP_H(),
25         UOption.HELP_QUESTION_MARK(),
26         UOption.SOURCEDIR().setDefault(CLDRPaths.BASE_DIRECTORY),
27         UOption.DESTDIR().setDefault(CLDRPaths.BASE_DIRECTORY + "../cldr-last/"),
28         UOption.create("match", 'm', UOption.REQUIRES_ARG).setDefault(".*"),
29     };
30 
31     String[] directoryList = {"main", "collation", "segmentations"};
32 
33     static RuleBasedCollator uca = (RuleBasedCollator) Collator.getInstance(ULocale.ROOT);
34 
35     {
36         uca.setNumericCollation(true);
37     }
38 
39     static PrettyPath prettyPathMaker = new PrettyPath();
40     static CLDRFile english;
41     static Set<String> locales;
42     static Factory cldrFactory;
43 
main(String[] args)44     public static void main(String[] args) throws Exception {
45         double deltaTime = System.currentTimeMillis();
46         try {
47             UOption.parseArgs(args, options);
48             String sourceDir = options[SOURCEDIR].value + "common/main/";
49             System.out.println(PathUtilities.getNormalizedPathString(sourceDir));
50             String compareDir = options[DESTDIR].value + "common/main/";
51             System.out.println(PathUtilities.getNormalizedPathString(compareDir));
52 
53             cldrFactory = Factory.make(sourceDir, options[MATCH].value);
54             Factory oldFactory = Factory.make(compareDir, options[MATCH].value);
55 
56             locales = new TreeSet<>(cldrFactory.getAvailable());
57             new CldrUtility.MatcherFilter(options[MATCH].value).retainAll(locales);
58             Set<String> pathsSeen = new HashSet<>();
59             int newItemsTotal = 0;
60             int replacementItemsTotal = 0;
61             int deletedItemsTotal = 0;
62             int sameItemsTotal = 0;
63 
64             for (Iterator<String> it = locales.iterator(); it.hasNext(); ) {
65                 int newItems = 0;
66                 int replacementItems = 0;
67                 int deletedItems = 0;
68                 int sameItems = 0;
69                 String locale = it.next();
70                 if (locale.startsWith("supplem") || locale.startsWith("character")) continue;
71                 CLDRFile file = cldrFactory.make(locale, false);
72                 try {
73                     CLDRFile oldFile = oldFactory.make(locale, false);
74                     pathsSeen.clear();
75                     for (Iterator<String> it2 = file.iterator(); it2.hasNext(); ) {
76                         String path = it2.next();
77                         String value = file.getStringValue(path);
78                         String oldValue = oldFile.getStringValue(path);
79                         if (oldValue == null) {
80                             newItems++;
81                         } else if (!value.equals(oldValue)) {
82                             replacementItems++;
83                         } else {
84                             sameItems++;
85                         }
86                         pathsSeen.add(path);
87                     }
88                     for (Iterator<String> it2 = oldFile.iterator(); it2.hasNext(); ) {
89                         String path = it2.next();
90                         if (!pathsSeen.contains(path)) {
91                             deletedItems++;
92                         }
93                     }
94                 } catch (Exception e) {
95                     newItems = size(file.iterator());
96                 }
97                 String langScript =
98                         new LocaleIDParser().set(file.getLocaleID()).getLanguageScript();
99                 System.out.println(
100                         langScript
101                                 + "\t"
102                                 + file.getLocaleID()
103                                 + "\t"
104                                 + sameItems
105                                 + "\t"
106                                 + newItems
107                                 + "\t"
108                                 + replacementItems
109                                 + "\t"
110                                 + deletedItems);
111                 newItemsTotal += newItems;
112                 replacementItemsTotal += replacementItems;
113                 deletedItemsTotal += deletedItems;
114                 sameItemsTotal += sameItems;
115             }
116             System.out.println(
117                     "TOTAL"
118                             + "\t"
119                             + "\t"
120                             + sameItemsTotal
121                             + "\t"
122                             + newItemsTotal
123                             + "\t"
124                             + replacementItemsTotal
125                             + "\t"
126                             + deletedItemsTotal);
127         } finally {
128             deltaTime = System.currentTimeMillis() - deltaTime;
129             System.out.println("Elapsed: " + deltaTime / 1000.0 + " seconds");
130             System.out.println("Done");
131         }
132     }
133 
size(Iterator iterator)134     private static int size(Iterator iterator) {
135         int count = 0;
136         for (; iterator.hasNext(); ) {
137             iterator.next();
138             ++count;
139         }
140         return count;
141     }
142 }
143