Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Generate a poem from an input string.
- *
- * @param input string from which to create the poem
- * @return poem (as described above)
- */
- public String poem(String input) {
- List<String> output = new ArrayList<>();
- try (Scanner s = new Scanner(input).useDelimiter("\\s+")) {
- String old = "";
- while (s.hasNext()) {
- String cur = s.next();
- String prev = old.toLowerCase();
- String next = cur.toLowerCase();
- if (!old.isEmpty()) {
- output.add(old);
- Optional<String> bestBridge = graph.targets(prev).entrySet().stream()
- .filter(e -> graph.targets(e.getKey()).containsKey(next))
- .map(e -> e.getKey())
- .collect(Collectors.toMap(e -> e,
- e -> graph.sources(e).get(prev) + graph.targets(e).get(next)))
- .entrySet().stream()
- .sorted((a,b) -> b.getValue().compareTo(a.getValue()))
- .findFirst().map(e -> e.getKey());
- bestBridge.ifPresent(bridge -> output.add(bridge));
- }
- old = cur;
- }
- output.add(old);
- }
- checkRep();
- return output.stream().reduce((a,b) -> a + " " + b).orElse("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement