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

ljacqu / wordeval / 14560448446

20 Apr 2025 02:30PM UTC coverage: 55.102% (+1.4%) from 53.745%
14560448446

push

github

ljacqu
Springify processing of evaluators

291 of 596 branches covered (48.83%)

51 of 63 new or added lines in 8 files covered. (80.95%)

57 existing lines in 6 files now uncovered.

756 of 1372 relevant lines covered (55.1%)

3.13 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.wordgraph.GraphBuilder;
5
import ch.jalu.wordeval.wordgraph.WordGraphService;
6
import org.jgrapht.graph.DefaultWeightedEdge;
7
import org.jgrapht.graph.SimpleGraph;
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.boot.CommandLineRunner;
10
import org.springframework.boot.SpringApplication;
11
import org.springframework.boot.autoconfigure.SpringBootApplication;
12
import org.springframework.context.annotation.Profile;
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
 */
25
@Profile("wordgraph")
26
@SpringBootApplication
27
public class WordGraphMain implements CommandLineRunner {
28
  
29
  /** Directory for graph exports. */
30
  private static final String GRAPH_EXPORT_DIRECTORY = "export/graph/";
31

32
  @Autowired
33
  private AppData appData;
34

35
  @Autowired
36
  private WordGraphService wordGraphService;
37
  
38
  private WordGraphMain() {
39
  }
40
  
41
  /**
42
   * Main function.
43
   * @param args .
44
   */
45
  public static void main(String[] args) {
UNCOV
46
    SpringApplication app = new SpringApplication(WordGraphMain.class);
×
47
    app.setAdditionalProfiles("wordgraph");
×
48
    app.run(args);
×
49
  }
×
50

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

UNCOV
56
      Optional<String> exportFilename = initializeExportFilename(scanner, dictionaryCode);
×
57
      SimpleGraph<String, DefaultWeightedEdge> graph;
58
      if (exportFilename.isPresent()) {
×
59
        graph = wordGraphService.importConnections(exportFilename.get());
×
60
      } else {
61
        GraphBuilder builder = new GraphBuilder(appData.getDictionary(dictionaryCode));
×
UNCOV
62
        processExportChoice(scanner, dictionaryCode, builder);
×
63
        graph = builder.getGraph();
×
64
      }
65

UNCOV
66
      connectionsFinderLoop(scanner, graph);
×
67
    }
68
  }
×
69

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

UNCOV
108
      System.out.print("Enter word 2: ");
×
UNCOV
109
      right = scanner.nextLine().trim();
×
110

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

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