@Test void discoverMatchedQueries() throws JsonSyntaxException, IOException { Explanation explanation = new Gson().fromJson(ResourceUtils.getString("explain_api_example.json"), Explanation.class); Set<String> discoverMatchedQueries = explainServiceHelper.findMatchedWords(explanation); assertEquals(2, discoverMatchedQueries.size()); assertTrue(discoverMatchedQueries.contains("high blood")); assertTrue(discoverMatchedQueries.contains("medic")); }
Set<String> findMatchedWords(Explanation explanation) { Set<String> words = new HashSet<>(); String description = explanation.getDescription(); if (description.startsWith(Options.SUM_OF.toString()) || description.startsWith(Options.PRODUCT_OF.toString())) { if (newArrayList(explanation.getDetails()).stream().allMatch(this::reachLastLevel)) { words.add(extractMatchedWords(explanation.getDetails())); } else { for (Explanation subExplanation : explanation.getDetails()) { words.addAll(findMatchedWords(subExplanation)); } } } else if (description.startsWith(Options.MAX_OF.toString())) { Explanation maxExplanation = newArrayList(explanation.getDetails()).stream() .max( (explanation1, explanation2) -> Float.compare(explanation1.getValue(), explanation2.getValue())) .orElseThrow( () -> new IllegalStateException( "explanation.getDetails() shouldn't return an empty array")); words.addAll(findMatchedWords(maxExplanation)); } else if (description.startsWith(Options.WEIGHT.toString())) { words.add(getMatchedWord(description)); } return words; }
ExplainServiceHelper { Set<String> findMatchedWords(Explanation explanation) { Set<String> words = new HashSet<>(); String description = explanation.getDescription(); if (description.startsWith(Options.SUM_OF.toString()) || description.startsWith(Options.PRODUCT_OF.toString())) { if (newArrayList(explanation.getDetails()).stream().allMatch(this::reachLastLevel)) { words.add(extractMatchedWords(explanation.getDetails())); } else { for (Explanation subExplanation : explanation.getDetails()) { words.addAll(findMatchedWords(subExplanation)); } } } else if (description.startsWith(Options.MAX_OF.toString())) { Explanation maxExplanation = newArrayList(explanation.getDetails()).stream() .max( (explanation1, explanation2) -> Float.compare(explanation1.getValue(), explanation2.getValue())) .orElseThrow( () -> new IllegalStateException( "explanation.getDetails() shouldn't return an empty array")); words.addAll(findMatchedWords(maxExplanation)); } else if (description.startsWith(Options.WEIGHT.toString())) { words.add(getMatchedWord(description)); } return words; } }
ExplainServiceHelper { Set<String> findMatchedWords(Explanation explanation) { Set<String> words = new HashSet<>(); String description = explanation.getDescription(); if (description.startsWith(Options.SUM_OF.toString()) || description.startsWith(Options.PRODUCT_OF.toString())) { if (newArrayList(explanation.getDetails()).stream().allMatch(this::reachLastLevel)) { words.add(extractMatchedWords(explanation.getDetails())); } else { for (Explanation subExplanation : explanation.getDetails()) { words.addAll(findMatchedWords(subExplanation)); } } } else if (description.startsWith(Options.MAX_OF.toString())) { Explanation maxExplanation = newArrayList(explanation.getDetails()).stream() .max( (explanation1, explanation2) -> Float.compare(explanation1.getValue(), explanation2.getValue())) .orElseThrow( () -> new IllegalStateException( "explanation.getDetails() shouldn't return an empty array")); words.addAll(findMatchedWords(maxExplanation)); } else if (description.startsWith(Options.WEIGHT.toString())) { words.add(getMatchedWord(description)); } return words; }  }
ExplainServiceHelper { Set<String> findMatchedWords(Explanation explanation) { Set<String> words = new HashSet<>(); String description = explanation.getDescription(); if (description.startsWith(Options.SUM_OF.toString()) || description.startsWith(Options.PRODUCT_OF.toString())) { if (newArrayList(explanation.getDetails()).stream().allMatch(this::reachLastLevel)) { words.add(extractMatchedWords(explanation.getDetails())); } else { for (Explanation subExplanation : explanation.getDetails()) { words.addAll(findMatchedWords(subExplanation)); } } } else if (description.startsWith(Options.MAX_OF.toString())) { Explanation maxExplanation = newArrayList(explanation.getDetails()).stream() .max( (explanation1, explanation2) -> Float.compare(explanation1.getValue(), explanation2.getValue())) .orElseThrow( () -> new IllegalStateException( "explanation.getDetails() shouldn't return an empty array")); words.addAll(findMatchedWords(maxExplanation)); } else if (description.startsWith(Options.WEIGHT.toString())) { words.add(getMatchedWord(description)); } return words; }   }
ExplainServiceHelper { Set<String> findMatchedWords(Explanation explanation) { Set<String> words = new HashSet<>(); String description = explanation.getDescription(); if (description.startsWith(Options.SUM_OF.toString()) || description.startsWith(Options.PRODUCT_OF.toString())) { if (newArrayList(explanation.getDetails()).stream().allMatch(this::reachLastLevel)) { words.add(extractMatchedWords(explanation.getDetails())); } else { for (Explanation subExplanation : explanation.getDetails()) { words.addAll(findMatchedWords(subExplanation)); } } } else if (description.startsWith(Options.MAX_OF.toString())) { Explanation maxExplanation = newArrayList(explanation.getDetails()).stream() .max( (explanation1, explanation2) -> Float.compare(explanation1.getValue(), explanation2.getValue())) .orElseThrow( () -> new IllegalStateException( "explanation.getDetails() shouldn't return an empty array")); words.addAll(findMatchedWords(maxExplanation)); } else if (description.startsWith(Options.WEIGHT.toString())) { words.add(getMatchedWord(description)); } return words; }    }