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

ljacqu / wordeval / 14561507106

20 Apr 2025 04:52PM UTC coverage: 55.233% (-0.08%) from 55.313%
14561507106

push

github

ljacqu
Convert all main methods to self-contained Spring beans

291 of 596 branches covered (48.83%)

0 of 9 new or added lines in 4 files covered. (0.0%)

760 of 1376 relevant lines covered (55.23%)

3.15 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.wordgraph.GraphBuilder;
6
import ch.jalu.wordeval.wordgraph.WordGraphService;
7
import org.jgrapht.graph.DefaultWeightedEdge;
8
import org.jgrapht.graph.SimpleGraph;
9
import org.springframework.beans.factory.annotation.Autowired;
10

11
import java.nio.file.Files;
12
import java.nio.file.Paths;
13
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.Optional;
16
import java.util.Scanner;
17
import java.util.Set;
18

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

27
  @Autowired
28
  private AppData appData;
29

30
  @Autowired
31
  private WordGraphService wordGraphService;
32
  
33
  /**
34
   * Main function.
35
   *
36
   * @param args .
37
   */
38
  public static void main(String[] args) {
NEW
39
    runApplication(WordGraphMain.class);
×
40
  }
×
41

42
  @Override
43
  public void run(String... args) {
44
    try (Scanner scanner = new Scanner(System.in)) {
×
45
      String dictionaryCode = initializeDictionaryCode(scanner);
×
46

47
      Optional<String> exportFilename = initializeExportFilename(scanner, dictionaryCode);
×
48
      SimpleGraph<String, DefaultWeightedEdge> graph;
49
      if (exportFilename.isPresent()) {
×
50
        graph = wordGraphService.importConnections(exportFilename.get());
×
51
      } else {
52
        GraphBuilder builder = new GraphBuilder(appData.getDictionary(dictionaryCode));
×
53
        processExportChoice(scanner, dictionaryCode, builder);
×
54
        graph = builder.getGraph();
×
55
      }
56

57
      connectionsFinderLoop(scanner, graph);
×
58
    }
59
  }
×
60

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

99
      System.out.print("Enter word 2: ");
×
100
      right = scanner.nextLine().trim();
×
101

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

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

© 2026 Coveralls, Inc