mirror of
https://github.com/MarginaliaSearch/MarginaliaSearch.git
synced 2025-02-24 05:18:58 +00:00
(qs, WIP) Tidy it up a bit
This commit is contained in:
parent
0bd1e15cce
commit
98a1adbf81
@ -225,7 +225,8 @@ public class QWordGraph implements Iterable<QWord> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Comparator<QWord> topologicalComparator() {
|
public Comparator<QWord> topologicalComparator() {
|
||||||
return Comparator.comparing(sortOrder::get);
|
Comparator<QWord> comp = Comparator.comparing(sortOrder::get);
|
||||||
|
return comp.thenComparing(QWord::ord);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,13 @@ class QWordPathsRenderer {
|
|||||||
return new QWordPathsRenderer(graph).render(graph.reachability());
|
return new QWordPathsRenderer(graph).render(graph.reachability());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static String render(Collection<QWordPath> paths,
|
||||||
|
QWordGraph.ReachabilityData reachability)
|
||||||
|
{
|
||||||
|
return new QWordPathsRenderer(paths).render(reachability);
|
||||||
|
}
|
||||||
|
|
||||||
/** Render the paths into a human-readable infix-style expression.
|
/** Render the paths into a human-readable infix-style expression.
|
||||||
* <p></p>
|
* <p></p>
|
||||||
* This method is recursive, but the recursion depth is limited by the
|
* This method is recursive, but the recursion depth is limited by the
|
||||||
@ -34,8 +41,7 @@ class QWordPathsRenderer {
|
|||||||
|
|
||||||
// Find the commonality of words in the paths
|
// Find the commonality of words in the paths
|
||||||
|
|
||||||
Map<QWord, Integer> commonality = paths.stream().flatMap(QWordPath::stream)
|
Map<QWord, Integer> commonality = nodeCommonality();
|
||||||
.collect(Collectors.groupingBy(w -> w, Collectors.summingInt(w -> 1)));
|
|
||||||
|
|
||||||
// Break the words into two categories: those that are common to all paths, and those that are not
|
// Break the words into two categories: those that are common to all paths, and those that are not
|
||||||
|
|
||||||
@ -72,10 +78,7 @@ class QWordPathsRenderer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Recurse into the non-overlapping portions
|
// Recurse into the non-overlapping portions
|
||||||
if (!nonOverlappingPortions.isEmpty()) {
|
resultJoiner.add(render(nonOverlappingPortions, reachability));
|
||||||
var wp = new QWordPathsRenderer(nonOverlappingPortions);
|
|
||||||
resultJoiner.add(wp.render(reachability));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (commonality.size() > 1) { // The case where no words are common to all paths
|
} else if (commonality.size() > 1) { // The case where no words are common to all paths
|
||||||
|
|
||||||
@ -117,8 +120,10 @@ class QWordPathsRenderer {
|
|||||||
.sorted(Map.Entry.comparingByKey(reachability.topologicalComparator())) // Sort by topological order to ensure consistent output
|
.sorted(Map.Entry.comparingByKey(reachability.topologicalComparator())) // Sort by topological order to ensure consistent output
|
||||||
.map(e -> {
|
.map(e -> {
|
||||||
String commonWord = e.getKey().word();
|
String commonWord = e.getKey().word();
|
||||||
|
|
||||||
// Recurse into the branches:
|
// Recurse into the branches:
|
||||||
String branchPart = new QWordPathsRenderer(e.getValue()).render(reachability);
|
String branchPart = render(e.getValue(), reachability);
|
||||||
|
|
||||||
return STR."\{commonWord} \{branchPart}";
|
return STR."\{commonWord} \{branchPart}";
|
||||||
})
|
})
|
||||||
.collect(Collectors.joining(" | ", " ( ", " ) "));
|
.collect(Collectors.joining(" | ", " ( ", " ) "));
|
||||||
@ -130,4 +135,10 @@ class QWordPathsRenderer {
|
|||||||
return resultJoiner.toString().replaceAll("\\s+", " ").trim();
|
return resultJoiner.toString().replaceAll("\\s+", " ").trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Compute how many paths each word is part of */
|
||||||
|
private Map<QWord, Integer> nodeCommonality() {
|
||||||
|
return paths.stream().flatMap(QWordPath::stream)
|
||||||
|
.collect(Collectors.groupingBy(w -> w, Collectors.summingInt(w -> 1)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user