Skip to content

Commit 27f61e0

Browse files
committed
Add time units strings and utilities
1 parent fa3974b commit 27f61e0

96 files changed

Lines changed: 19600 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

timeago-parser/raw/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Time ago parser raw resources
2+
3+
This directory contains some JSON-formatted time ago strings gather directly from YouTube.
4+
5+
#### Java directory
6+
7+
Some useful classes that can generate an overview and check if we have all the time units for all the languages.
8+
9+
It also contains the resource bundle generator.
10+
11+
#### Times directory
12+
13+
All the units organized by their unit value and name (e.g. 1s = "1 second", 2y = "2 años", 4w = "4 semanas").
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import com.grack.nanojson.JsonObject;
2+
import com.grack.nanojson.JsonParser;
3+
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.util.Arrays;
7+
import java.util.Map;
8+
9+
public class CheckAll {
10+
11+
public static void main(String[] args) throws Exception {
12+
int SECONDS = 59, currentSeconds = 0;
13+
int MINUTES = 59, currentMinutes = 0;
14+
int HOURS = 23, currentHours = 0;
15+
int DAYS = 6, currentDays = 0;
16+
int WEEKS = 4, currentWeeks = 0;
17+
int MONTHS = 11, currentMonths = 0;
18+
int YEARS = 12, currentYears = 0;
19+
20+
for (String name : Arrays.asList("seconds", "minutes", "hours", "days", "weeks", "months", "years")) {
21+
JsonObject object = JsonParser.object().from(new FileInputStream(new File("timeago-parser/raw/times/" + name + ".json")));
22+
23+
for (Map.Entry<String, Object> entry : object.entrySet()) {
24+
JsonObject value = (JsonObject) entry.getValue();
25+
26+
final int size = value.keySet().size();
27+
if (size >= 80) {
28+
if (name.equals("seconds")) currentSeconds++;
29+
if (name.equals("minutes")) currentMinutes++;
30+
if (name.equals("hours")) currentHours++;
31+
if (name.equals("days")) currentDays++;
32+
if (name.equals("weeks")) currentWeeks++;
33+
if (name.equals("months")) currentMonths++;
34+
if (name.equals("years")) currentYears++;
35+
} else {
36+
System.err.println("Missing some units in: " + name + " → " + entry.getKey() + " (current size = " + size + ")");
37+
}
38+
39+
String number = entry.getKey().replaceAll("\\D", "");
40+
for (Map.Entry<String, Object> langsKeys : value.entrySet()) {
41+
String lang = langsKeys.getKey();
42+
String langValue = String.valueOf(langsKeys.getValue());
43+
44+
String langValueNumber = langValue.replaceAll("\\D", "");
45+
if (!langValueNumber.equals(number)) {
46+
final String msg = langValueNumber.isEmpty() ? "doesn't contain number" : "different number";
47+
System.out.printf("%-20s[!] %22s: %10s = %s \n", entry.getKey(), msg, lang, langValue);
48+
}
49+
}
50+
}
51+
}
52+
System.out.println("\n\nHow many:\n");
53+
54+
if (currentSeconds == SECONDS) System.out.println("seconds: " + currentSeconds);
55+
else System.out.println("[!] missing seconds: " + currentSeconds);
56+
57+
if (currentMinutes == MINUTES) System.out.println("minutes: " + currentMinutes);
58+
else System.out.println("[!] missing minutes: " + currentMinutes);
59+
60+
if (currentHours == HOURS) System.out.println("hours: " + currentHours);
61+
else System.out.println("[!] missing hours: " + currentHours);
62+
63+
if (currentDays == DAYS) System.out.println("days: " + currentDays);
64+
else System.out.println("[!] missing days: " + currentDays);
65+
66+
if (currentWeeks == WEEKS) System.out.println("weeks: " + currentWeeks);
67+
else System.out.println("[!] missing weeks: " + currentWeeks);
68+
69+
if (currentMonths == MONTHS) System.out.println("months: " + currentMonths);
70+
else System.out.println("[!] missing months: " + currentMonths);
71+
72+
if (currentYears == YEARS) System.out.println("years: " + currentYears);
73+
else System.out.println("[!] missing years: " + currentYears);
74+
}
75+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import com.grack.nanojson.JsonAppendableWriter;
2+
import com.grack.nanojson.JsonObject;
3+
import com.grack.nanojson.JsonParser;
4+
import com.grack.nanojson.JsonWriter;
5+
6+
import java.io.File;
7+
import java.io.FileInputStream;
8+
import java.io.FileOutputStream;
9+
import java.util.*;
10+
11+
public class GenerateOverview {
12+
13+
public static void main(String[] args) throws Exception {
14+
Map<String, Map<String, Collection<String>>> outMap = new TreeMap<>(new Comparator<String>() {
15+
@Override
16+
public int compare(String o1, String o2) {
17+
return o1.compareToIgnoreCase(o2);
18+
}
19+
});
20+
for (String unitName : Arrays.asList("seconds", "minutes", "hours", "days", "weeks", "months", "years")) {
21+
JsonObject object = JsonParser.object().from(new FileInputStream(new File("timeago-parser/raw/times/" + unitName + ".json")));
22+
23+
for (Map.Entry<String, Object> timeKeyValue : object.entrySet()) {
24+
JsonObject timeObject = (JsonObject) timeKeyValue.getValue();
25+
for (Map.Entry<String, Object> langsKeyValue : timeObject.entrySet()) {
26+
String langKey = langsKeyValue.getKey();
27+
String langValue = langsKeyValue.getValue().toString();
28+
29+
Map<String, Collection<String>> langUnitsMap;
30+
if (outMap.containsKey(langKey)) {
31+
langUnitsMap = outMap.get(langKey);
32+
} else {
33+
langUnitsMap = new TreeMap<>(Utils.compareByUnitName());
34+
outMap.put(langKey, langUnitsMap);
35+
}
36+
37+
Collection<String> langUnitListValues;
38+
if (langUnitsMap.containsKey(unitName)) {
39+
langUnitListValues = langUnitsMap.get(unitName);
40+
} else {
41+
langUnitListValues = new TreeSet<>(Utils.compareByNumber());
42+
langUnitsMap.put(unitName, langUnitListValues);
43+
}
44+
45+
46+
langUnitListValues.add(langValue);
47+
}
48+
}
49+
}
50+
51+
writeMapTo(outMap, JsonWriter.indent(" ").on(new FileOutputStream(new File("timeago-parser/raw/overview.json"))));
52+
}
53+
54+
public static void writeMapTo(Map<String, Map<String, Collection<String>>> outMap, JsonAppendableWriter out) {
55+
out.object();
56+
for (Map.Entry<String, Map<String, Collection<String>>> langMapEntry : outMap.entrySet()) {
57+
final String langName = langMapEntry.getKey();
58+
out.object(langName);
59+
for (Map.Entry<String, Collection<String>> langValuesEntry : langMapEntry.getValue().entrySet()) {
60+
final String unitName = langValuesEntry.getKey();
61+
out.array(unitName);
62+
for (String timeValue : langValuesEntry.getValue()) out.value(timeValue);
63+
out.end();
64+
}
65+
out.end();
66+
}
67+
out.end().done();
68+
}
69+
70+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import com.grack.nanojson.JsonArray;
2+
import com.grack.nanojson.JsonObject;
3+
import com.grack.nanojson.JsonParser;
4+
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.io.OutputStream;
9+
import java.util.Iterator;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.TreeMap;
13+
14+
import static org.schabi.newpipe.extractor.timeago.TimeAgoPatternsManager.RESOURCE_BUNDLE_ARRAY_SEPARATOR;
15+
16+
public class GenerateResourceBundles {
17+
18+
public static void main(String[] args) throws Exception {
19+
File outDir = new File("timeago-parser/outBundle");
20+
if (!outDir.isDirectory()) outDir.mkdir();
21+
22+
JsonObject object = JsonParser.object().from(new FileInputStream(new File("timeago-parser/raw/unique_patterns.json")));
23+
24+
for (Map.Entry<String, Object> langTimeEntry : new TreeMap<>(object).entrySet()) {
25+
final String langName = langTimeEntry.getKey();
26+
StringBuilder outString = new StringBuilder();
27+
28+
final TreeMap<String, Object> sortedMap = new TreeMap<>(Utils.compareByUnitName());
29+
sortedMap.putAll((JsonObject) langTimeEntry.getValue());
30+
31+
final Iterator<Map.Entry<String, Object>> unitEntriesIterator = sortedMap.entrySet().iterator();
32+
while (unitEntriesIterator.hasNext()) {
33+
final Map.Entry<String, Object> unitEntry = unitEntriesIterator.next();
34+
final String unitName = unitEntry.getKey();
35+
final List<Object> unitList = (JsonArray) unitEntry.getValue();
36+
37+
outString.append(unitName).append("=\\\n");
38+
39+
for (int i = 0; i < unitList.size(); i++) {
40+
final String s = unitList.get(i).toString();
41+
outString.append(" ").append(s);
42+
43+
if (i < unitList.size() - 1) {
44+
outString.append(RESOURCE_BUNDLE_ARRAY_SEPARATOR).append("\\").append("\n");
45+
}
46+
}
47+
48+
if (unitEntriesIterator.hasNext()) outString.append("\n\n");
49+
}
50+
51+
String fileName = "time_units_" + langName.replaceAll("-", "_") + ".properties";
52+
System.out.println("Writing " + fileName + "...");
53+
try (OutputStream out = new FileOutputStream(new File(outDir, fileName))) {
54+
out.write(outString.toString().getBytes("UTF-8"));
55+
}
56+
}
57+
58+
}
59+
}

timeago-parser/raw/java/Utils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class Utils {
4+
5+
static Comparator<String> compareByNumber() {
6+
return new Comparator<String>() {
7+
@Override
8+
public int compare(String o1, String o2) {
9+
return extractInt(o1) - extractInt(o2);
10+
}
11+
12+
private int extractInt(String s) {
13+
String num = s.replaceAll("\\D", "");
14+
return num.isEmpty() ? 0 : Integer.parseInt(num);
15+
}
16+
};
17+
}
18+
19+
static Comparator<Object> compareByUnitName() {
20+
return new Comparator<Object>() {
21+
private final List<String> ORDER = Arrays.asList("seconds", "minutes", "hours", "days", "weeks", "months", "years");
22+
23+
@Override
24+
public int compare(Object o1, Object o2) {
25+
return Integer.compare(ORDER.indexOf(o1.toString()), ORDER.indexOf(o2.toString()));
26+
}
27+
};
28+
}
29+
}

0 commit comments

Comments
 (0)