Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort non-n6 spatial entries by ID, not by label (RPB-47) #65

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions app/controllers/nwbib/Classification.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -117,7 +118,7 @@ public Pair<List<JsonNode>, Map<String, List<JsonNode>>> buildHierarchy() {
|| Play.isTest())) { /* SpatialToSkos uses Play test server */
addFromCsv(subClasses);
}
Collections.sort(topClasses, comparator);
Collections.sort(topClasses, comparator(Classification::idText));
return Pair.of(topClasses, subClasses);
}

Expand Down Expand Up @@ -154,7 +155,7 @@ private static void addFromCsv(
*/
public JsonNode buildRegister() {
final List<JsonNode> result = ids(classificationData()).stream()
.sorted(comparator).collect(Collectors.toList());
.sorted(comparator(Classification::labelText)).collect(Collectors.toList());
return Json.toJson(result);
}

Expand Down Expand Up @@ -188,14 +189,20 @@ private enum Label {
private static Node node;

/** Compare German strings */
public static Comparator<JsonNode> comparator =
(JsonNode o1, JsonNode o2) -> Collator.getInstance(Locale.GERMAN)
.compare(labelText(o1), labelText(o2));
public static Comparator<JsonNode> comparator(Function<JsonNode, String> fun) {
return (JsonNode o1, JsonNode o2) -> Collator.getInstance(Locale.GERMAN)
.compare(fun.apply(o1), fun.apply(o2));
}

private Classification() {
/* Use via static functions, no instantiation. */
}

private static String idText(JsonNode node) {
String[] segments = node.get("value").asText().split("#");
return segments[segments.length-1].replaceAll("^n6$", "n0");
}

/**
* @param turtleUrl The URL of the RDF in TURTLE format
* @return The input, converted to JSON-LD, or null
Expand Down Expand Up @@ -293,7 +300,7 @@ static List<JsonNode> ids(SearchResponse response) {
return result;
}

private static String labelText(JsonNode json) {
public static String labelText(JsonNode json) {
String label = json.get("label").asText();
if (label.contains("Stadtbezirk")) {
List<Pair<String, String>> roman = Arrays.asList(Pair.of("I", "a"),
Expand Down Expand Up @@ -346,7 +353,7 @@ public static Pair<List<JsonNode>, Map<String, List<JsonNode>>> buildHierarchyCs
subClasses.put(broaderId, new ArrayList<JsonNode>());
List<JsonNode> sub = subClasses.get(broaderId);
sub.add(Json.toJson(ImmutableMap.of("value", thisId, "label", vg + ", Verbandsgemeinde", "notation", thisNotation, "hits", hits)));
Collections.sort(sub, comparator);
Collections.sort(sub, comparator(Classification::labelText));
broader = thisNotation;
}
}
Expand All @@ -359,13 +366,13 @@ public static Pair<List<JsonNode>, Map<String, List<JsonNode>>> buildHierarchyCs
subClasses.put(broaderId, new ArrayList<JsonNode>());
List<JsonNode> sub = subClasses.get(broaderId);
sub.add(Json.toJson(ImmutableMap.of("value", id, "label", label, "notation", notation, "hits", hits)));
Collections.sort(sub, comparator);
Collections.sort(sub, comparator(Classification::labelText));
}
}
} catch (IOException e) {
e.printStackTrace();
}
Collections.sort(topClasses, comparator);
Collections.sort(topClasses, comparator(Classification::labelText));
return Pair.of(topClasses, removeDuplicates(subClasses));
}

Expand Down Expand Up @@ -416,7 +423,9 @@ private static void addAsSubClass(Map<String, List<JsonNode>> subClasses,
subClasses.put(broader, new ArrayList<JsonNode>());
List<JsonNode> list = subClasses.get(broader);
list.addAll(valueAndLabelWithNotation(hit, json));
Collections.sort(list, comparator);
// non-n6 entries have 2-digit IDs or a second `n` -> sort by ID (RPB-47)
boolean sortById = idText(list.get(0)).matches("^n\\d{2}$|^n\\d+n\\d+$");
Collections.sort(list, comparator(sortById ? Classification::idText : Classification::labelText));
}

private static String focus(JsonNode json) {
Expand Down
4 changes: 2 additions & 2 deletions test/tests/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void sortArabicNumerals() {
Json.newObject().put("label", "Stadtbezirk 8"),
Json.newObject().put("label", "Stadtbezirk 9"),
Json.newObject().put("label", "Stadtbezirk 10") };
Arrays.sort(in, Classification.comparator);
Arrays.sort(in, Classification.comparator(Classification::labelText));
Assert.assertArrayEquals(correct, in);
}

Expand Down Expand Up @@ -143,7 +143,7 @@ public void sortRomanNumerals() {
Json.newObject().put("label", "Stadtbezirk VIII"),
Json.newObject().put("label", "Stadtbezirk IX"),
Json.newObject().put("label", "Stadtbezirk X") };
Arrays.sort(in, Classification.comparator);
Arrays.sort(in, Classification.comparator(Classification::labelText));
Assert.assertArrayEquals(correct, in);
}

Expand Down
Loading