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

ljacqu / wordeval / 14561760799

20 Apr 2025 05:26PM UTC coverage: 53.367% (-0.04%) from 53.402%
14561760799

push

github

ljacqu
jgrapht: Bump to 1.3.0

270 of 578 branches covered (46.71%)

729 of 1366 relevant lines covered (53.37%)

3.1 hits per line

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

66.67
/src/main/java/ch/jalu/wordeval/wordgraph/GraphBuilder.java
1
package ch.jalu.wordeval.wordgraph;
2

3
import ch.jalu.wordeval.dictionary.Dictionary;
4
import ch.jalu.wordeval.dictionary.DictionaryProcessor;
5
import ch.jalu.wordeval.dictionary.Word;
6
import lombok.Getter;
7
import lombok.extern.log4j.Log4j2;
8
import org.jgrapht.graph.DefaultWeightedEdge;
9
import org.jgrapht.graph.SimpleGraph;
10
import org.jgrapht.graph.SimpleWeightedGraph;
11

12
import java.util.List;
13
import java.util.stream.Collectors;
14

15
/**
16
 * Finds which words have a Damerau-Levenshtein distance of 1 and saves these
17
 * connections, forming a graph over the dictionary words.
18
 */
19
@Log4j2
4✔
20
public class GraphBuilder {
21
  
22
  /** 
23
   * The interval in which to display the words that have been processed.
24
   * This must be a number corresponding to 2^k-1, where k >= 1 (e.g. 255, 1023).
25
   */
26
  private static final int STAT_INTERVAL = 255;
27
  
28
  @Getter
29
  private SimpleGraph<String, DefaultWeightedEdge> graph;
30

31
  /**
32
   * Builds a new ConnectionsBuilder object and computes the
33
   * connections for the given dictionary.
34
   *
35
   * @param dictionary the dictionary
36
   */
37
  public GraphBuilder(Dictionary dictionary) {
38
    this(getDictionaryWords(dictionary));
×
39
  }
×
40
  
41
  /**
42
   * Builds a new ConnectionsBuilder object and computes the
43
   * connections based on the given list of words.
44
   *
45
   * @param words the list of words to process
46
   */
47
  public GraphBuilder(List<String> words) {
2✔
48
    constructGraph(words);
3✔
49
  }
1✔
50
  
51
  private static List<String> getDictionaryWords(Dictionary dictionary) {
52
    return DictionaryProcessor.readAllWords(dictionary).stream()
×
53
      .map(Word::getRaw)
×
54
      .distinct()
×
55
      .sorted()
×
56
      .collect(Collectors.toList());
×
57
  }
58

59
  private void constructGraph(List<String> words) {
60
    graph = new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
6✔
61
    for (int i = 0; i < words.size(); ++i) {
8✔
62
      final String leftWord = words.get(i);
5✔
63
      graph.addVertex(leftWord);
5✔
64
      for (int j = i + 1; j < words.size(); ++j) {
10✔
65
        final String rightWord = words.get(j);
5✔
66
        if (DamerauLevenshtein.isEditDistance1(rightWord, leftWord)) {
4✔
67
          // vertices must always be added before edges. addVertex() checks against a Set,
68
          // so the check is efficient enough for us to just always call the functions
69
          graph.addVertex(rightWord);
5✔
70
          graph.addEdge(leftWord, rightWord);
6✔
71
        }
72
      }
73
      if ((i & STAT_INTERVAL) == STAT_INTERVAL) {
5!
74
        log.info("Processed {} words", i);
×
75
      }
76
    }
77
    log.info("Processed total {} words", words.size());
6✔
78
  }
1✔
79

80
}
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