• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ljacqu / wordeval / 14540892325

18 Apr 2025 07:50PM UTC coverage: 51.4% (-12.1%) from 63.456%
14540892325

push

github

ljacqu
Merge remote-tracking branch 'origin/master' into dependencies

239 of 546 branches covered (43.77%)

93 of 383 new or added lines in 27 files covered. (24.28%)

5 existing lines in 4 files now uncovered.

679 of 1321 relevant lines covered (51.4%)

3.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.15
/src/main/java/ch/jalu/wordeval/evaluators/impl/Emordnilap.java
1
package ch.jalu.wordeval.evaluators.impl;
2

3
import ch.jalu.wordeval.dictionary.Word;
4
import ch.jalu.wordeval.evaluators.AllWordsEvaluator;
5
import ch.jalu.wordeval.evaluators.result.WordWithKey;
6
import com.google.common.collect.ArrayListMultimap;
7
import com.google.common.collect.ListMultimap;
8
import lombok.Getter;
9
import org.apache.commons.lang3.StringUtils;
10

11
import java.util.ArrayList;
12
import java.util.Collection;
13
import java.util.Comparator;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Map;
17
import java.util.Set;
18
import java.util.TreeMap;
19
import java.util.stream.Collectors;
20

21
/**
22
 * Finds emordnilaps, words that produce another word when reversed,
23
 * such as German "Lager" and "Regal".
24
 */
25
public class Emordnilap implements AllWordsEvaluator {
2✔
26

27
  @Getter
6✔
28
  private final List<WordWithKey> results = new ArrayList<>();
29

30
  @Override
31
  public void evaluate(Collection<Word> words) {
32
    TreeMap<String, Word> wordsByLowercase = words.stream()
6✔
33
      .collect(Collectors.toMap(Word::getLowercase, word -> word, (a, b) -> b, TreeMap::new));
6✔
34

35
    for (Map.Entry<String, Word> entry : wordsByLowercase.entrySet()) {
11✔
36
      String lowercase = entry.getKey();
4✔
37
      String reversed = StringUtils.reverse(lowercase);
3✔
38
      Word reversedWord = wordsByLowercase.get(reversed);
5✔
39
      if (lowercase.compareTo(reversed) < 0 && reversedWord != null) {
6✔
40
        // TODO: Add smarter checks to avoid performing work, maybe can even stop once compareTo is < 0 ?
41
        results.add(new WordWithKey(reversedWord, entry.getKey()));
11✔
42
      }
43
    }
1✔
44
  }
1✔
45

46
  @Override
47
  public ListMultimap<Object, Object> getTopResults(int topScores, int maxLimit) {
NEW
48
    List<WordWithKey> sortedResult = results.stream()
×
NEW
49
        .sorted(Comparator.<WordWithKey>comparingInt(wordWithKey -> wordWithKey.getKey().length()).reversed())
×
NEW
50
        .toList();
×
51

NEW
52
    Set<Integer> uniqueValues = new HashSet<>();
×
NEW
53
    ListMultimap<Object, Object> filteredResults = ArrayListMultimap.create();
×
NEW
54
    for (WordWithKey wordWithKey : sortedResult) {
×
NEW
55
      int score = wordWithKey.getKey().length();
×
NEW
56
      if (uniqueValues.add(score) && uniqueValues.size() > topScores) {
×
NEW
57
        break;
×
58
      }
NEW
59
      filteredResults.put(score, wordWithKey.getWord().getRaw() + " (" + wordWithKey.getKey() + ")");
×
NEW
60
      if (filteredResults.size() >= maxLimit) {
×
NEW
61
        break;
×
62
      }
NEW
63
    }
×
64

NEW
65
    return filteredResults;
×
66
  }
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc