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

ljacqu / wordeval / 14562954234

20 Apr 2025 08:11PM UTC coverage: 55.116% (+0.5%) from 54.64%
14562954234

push

github

ljacqu
Merge evaluator handlers to EvaluatorService

269 of 568 branches covered (47.36%)

4 of 6 new or added lines in 2 files covered. (66.67%)

48 existing lines in 3 files now uncovered.

738 of 1339 relevant lines covered (55.12%)

3.27 hits per line

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

0.0
/src/main/java/ch/jalu/wordeval/WordGraphMain.java
1
package ch.jalu.wordeval;
2

3
import ch.jalu.wordeval.appdata.AppData;
4
import ch.jalu.wordeval.config.SpringContainedRunner;
5
import ch.jalu.wordeval.dictionary.Dictionary;
6
import ch.jalu.wordeval.dictionary.DictionaryService;
7
import ch.jalu.wordeval.dictionary.Word;
8
import ch.jalu.wordeval.wordgraph.GraphBuilder;
9
import ch.jalu.wordeval.wordgraph.WordGraphService;
10
import org.jgrapht.graph.DefaultWeightedEdge;
11
import org.jgrapht.graph.SimpleGraph;
12
import org.springframework.beans.factory.annotation.Autowired;
13

14
import java.nio.file.Files;
15
import java.nio.file.Paths;
16
import java.util.ArrayList;
17
import java.util.List;
18
import java.util.Optional;
19
import java.util.Scanner;
20
import java.util.Set;
21

22
/**
23
 * Entry point for the word graph feature of <i>wordeval</i>.
24
 */
UNCOV
25
public class WordGraphMain extends SpringContainedRunner {
×
26
  
27
  /** Directory for graph exports. */
28
  private static final String GRAPH_EXPORT_DIRECTORY = "export/graph/";
29

30
  @Autowired
31
  private AppData appData;
32

33
  @Autowired
34
  private DictionaryService dictionaryService;
35

36
  @Autowired
37
  private WordGraphService wordGraphService;
38
  
39
  /**
40
   * Main function.
41
   *
42
   * @param args .
43
   */
44
  public static void main(String[] args) {
45
    runApplication(WordGraphMain.class);
×
UNCOV
46
  }
×
47

48
  @Override
49
  public void run(String... args) {
50
    try (Scanner scanner = new Scanner(System.in)) {
×
UNCOV
51
      String dictionaryCode = initializeDictionaryCode(scanner);
×
52

53
      Optional<String> exportFilename = initializeExportFilename(scanner, dictionaryCode);
×
54
      SimpleGraph<String, DefaultWeightedEdge> graph;
UNCOV
55
      if (exportFilename.isPresent()) {
×
UNCOV
56
        graph = wordGraphService.importConnections(exportFilename.get());
×
57
      } else {
UNCOV
58
        Dictionary dictionary = appData.getDictionary(dictionaryCode);
×
59
        List<String> allWords = dictionaryService.readAllWords(dictionary).stream()
×
UNCOV
60
            .map(Word::getLowercase)
×
UNCOV
61
            .distinct()
×
62
            .toList();
×
63
        GraphBuilder builder = new GraphBuilder(allWords);
×
UNCOV
64
        processExportChoice(scanner, dictionaryCode, builder);
×
UNCOV
65
        graph = builder.getGraph();
×
66
      }
67

68
      connectionsFinderLoop(scanner, graph);
×
69
    }
70
  }
×
71

72
  private static String initializeDictionaryCode(Scanner scanner) {
73
    System.out.println("Dictionary code:");
×
UNCOV
74
    return scanner.nextLine().trim(); 
×
75
  }
76
  
77
  private static Optional<String> initializeExportFilename(Scanner scanner, String dictionaryCode) {
78
    String exportFilename = getExportFilename(dictionaryCode);
×
79
    boolean useExport = false;
×
80
    if (Files.isRegularFile(Paths.get(exportFilename))) {
×
81
      System.out.println("Graph for '" + dictionaryCode + "' is saved. Load from cache? [y/n]");
×
UNCOV
82
      useExport = getChoice(scanner);
×
83
    }
UNCOV
84
    return useExport ? Optional.of(exportFilename) : Optional.empty();
×
85
  }
86
  
87
  private void processExportChoice(Scanner scanner, String dictionaryCode, GraphBuilder builder) {
88
    System.out.println("Export connections to file? [y/n]");
×
UNCOV
89
    boolean doExport = getChoice(scanner);
×
90
    if (doExport) {
×
91
      wordGraphService.exportConnections(
×
92
          getExportFilename(dictionaryCode), builder.getGraph());
×
93
    }
94
  }
×
95
  
96
  private void connectionsFinderLoop(Scanner scanner, SimpleGraph<String, DefaultWeightedEdge> graph) {
UNCOV
97
    System.out.println("Connections finder\n");
×
98
    String left, right;
99
    List<String> disabledVertices = new ArrayList<>();
×
100
    while (true) {
UNCOV
101
      System.out.print("Enter word 1 (empty string to quit, ! to disable vertices): ");
×
102
      left = scanner.nextLine().trim();
×
103
      if (left.isEmpty()) {
×
104
        break;
×
105
      } else if ("!".equals(left)) {
×
UNCOV
106
        toggleVertices(scanner, graph, disabledVertices);
×
UNCOV
107
        continue;
×
108
      }
109

110
      System.out.print("Enter word 2: ");
×
111
      right = scanner.nextLine().trim();
×
112

113
      Set<String> path = wordGraphService.getShortestPath(graph, left, right);
×
114
      System.out.println(path);
×
115
    }
×
116
  }
×
117
  
118
  private void toggleVertices(Scanner scanner, SimpleGraph<String, DefaultWeightedEdge> graph,
119
                              List<String> disabledVertices) {
120
    while (true) {
121
      System.out.println("Disable a vertex? (empty string to quit, ! to see the list of disabled vertices):");
×
122
      String word = scanner.nextLine().trim();
×
123
      if (word.isEmpty()) {
×
124
        return;
×
UNCOV
125
      } else if ("!".equals(word)) {
×
UNCOV
126
        System.out.println(disabledVertices);
×
127
        continue;
×
128
      }
129
      
130
      boolean result;
UNCOV
131
      if (disabledVertices.contains(word)) {
×
UNCOV
132
        result = wordGraphService.enableVertexEdges(graph, word);
×
133
        if (result) {
×
134
          disabledVertices.remove(word);
×
UNCOV
135
          System.out.println("Enabled '" + word + "'");
×
136
        }
137
      } else {
UNCOV
138
        result = wordGraphService.disableVertexEdges(graph, word);
×
UNCOV
139
        if (result) {
×
140
          disabledVertices.add(word); 
×
UNCOV
141
          System.out.println("Disabled '" + word + "'");
×
142
        }
143
      }
UNCOV
144
      if (!result) {
×
145
        System.out.println("No such vertex in graph");
×
146
      }
147
    }
×
148
  }
149
  
150
  private static String getExportFilename(String code) {
151
    return GRAPH_EXPORT_DIRECTORY + code + ".json";
×
152
  }
153
  
154
  private static boolean getChoice(Scanner scanner) {
155
    while (true) {
UNCOV
156
      String line = scanner.nextLine().trim();
×
UNCOV
157
      if ("y".equals(line)) {
×
UNCOV
158
        return true;
×
UNCOV
159
      } else if ("n".equals(line)) {
×
UNCOV
160
        return false;
×
161
      }
UNCOV
162
      System.out.print("\nPlease enter 'y' or 'n': ");
×
UNCOV
163
    }
×
164
  }
165

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