private void findRecommendations( @NotNull Document doc, @NotNull BilingualQueryResultBuilder resultBuilder) { // Determine all candidate nodes: Elements alternativeNodes = doc.select("div.cc > p > *"); Language currentLanguage = null; for (Element node : alternativeNodes) { // If the next node is a flagicon, try to determine the language for the next entries from the // class name if (node.tagName().equals("span") && node.hasClass("flagicon")) { Set<String> classNames = node.classNames(); classNames.remove("flagicon"); for (String className : classNames) { Language candidate = Language.getExistingLanguageById(className); if (candidate != null) { currentLanguage = candidate; break; } } } else if (node.tagName().equals("a")) { String recommendationText = node.text(); DictionaryObjectBuilder objectBuilder = ImmutableDictionaryObject.builder(); objectBuilder.setLanguage(currentLanguage).setGeneralForm(recommendationText); resultBuilder.addSimilarRecommendation(objectBuilder.build()); } } }
private void extractGender(@NotNull Element element, DictionaryObjectBuilder objectBuilder) { Element genderNode = element.getElementsByClass("gender").first(); if (genderNode != null) { String gender = genderNode.text(); if (GENDER_MAP.containsKey(gender)) objectBuilder.setGrammaticalGender(GENDER_MAP.get(gender)); } }
@NotNull private DictionaryObject processSingleNode( @NotNull Element element, @NotNull Language language, String queryString) { DictionaryObjectBuilder objectBuilder = ImmutableDictionaryObject.builder(); objectBuilder.setLanguage(language); // Extract entry text: String context = StringUtils.substringBefore(element.text(), element.getElementsByTag("a").first().text()); String generalForm = context + element.getElementsByTag("a").first().text(); objectBuilder.setGeneralForm(StringUtils.strip(generalForm)); // Extract description: extractDescription(element, queryString, objectBuilder); // Extract gender: extractGender(element, objectBuilder); return objectBuilder.build(); }
private void extractDescription( @NotNull Element element, String queryString, DictionaryObjectBuilder objectBuilder) { Element descriptionNode = element.getElementsByClass("info").first(); if (descriptionNode != null) { String description = descriptionNode.text(); description = StringUtils.removeStart(description, "("); description = StringUtils.removeEnd(description, ")"); if (!StringUtils.equalsIgnoreCase( description, queryString)) // Set description only if it is different from request string objectBuilder.setDescription(StringUtils.strip(description)); } }