Question
Need help with btSpellCheck. I get error message on if (!dictionary.contains(key)) { package application; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList;
Need help with btSpellCheck. I get error message on if (!dictionary.contains(key)) {
package application;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class SpellCheckFrequentWordsFX extends Application {
Dictionary dictionary = new Dictionary();
ArrayList document = new ArrayList<>();
Map wordFrequency = new TreeMap<>();
Map map = new TreeMap<>();
public void start(Stage primaryStage) throws IOException, SQLException {
TextArea textArea = new TextArea();
textArea.setWrapText(true);
Label nameLabel = new Label("File to Load: ");
ChoiceBox textfileToCheck = new ChoiceBox();
textfileToCheck.getItems().addAll("Sample.txt", "BillOfRights.txt", "greatgatsby.txt", "warandpeace.txt");
textfileToCheck.getSelectionModel().select(0);
String message = (LoadTheTextFile(textfileToCheck.getValue()));
textfileToCheck.setOnAction(event -> {
try {
textArea.setText(LoadTheTextFile(textfileToCheck.getValue()));
} catch (SQLException e) {
e.printStackTrace();
}
});
Button btFrequentWords = new Button("Frequent Words");
Button btSpellCheck = new Button("Spell Check");
Button btClearAnswers = new Button("Clear");
Button btnExit = new Button("Exit");
HBox hBoxTop = new HBox(nameLabel, textfileToCheck, btFrequentWords, btSpellCheck, btClearAnswers, btnExit);
hBoxTop.setSpacing(10);
hBoxTop.setAlignment(Pos.CENTER);
hBoxTop.setPadding(new Insets(5, 5, 5, 5));
BorderPane borderPane = new BorderPane();
borderPane.setTop(hBoxTop);
borderPane.setCenter(textArea);
dictionary = DbConnection.ReadDictionary();
// Dictionary Requires a Size Method
// textArea.setText("Dictionary Loaded with " + dictionary.size() + " words");
textArea.appendText(message + "");
btFrequentWords.setOnAction(new EventHandler() {
public void handle(ActionEvent e) {
String output = "";
wordFrequency.clear();
int maxCount = 0;
wordFrequency = document.stream().map(String :: toLowerCase).filter(word -> !word.trim().isEmpty()).collect(Collectors.toMap(word -> word, word -> 1, (a,b) -> a + b, TreeMap::new));
for (String word : document) {
Integer cnt = wordFrequency.get(word);
if (cnt != null) {
if ( cnt > maxCount) {
maxCount = cnt;
}
}
}
for (String k : wordFrequency.keySet()) {
if (maxCount == wordFrequency.get(k)) {
output += "Words that appear " + maxCount + " times: " + k + "";
}
}
textArea.setText(output);
}
});
btSpellCheck.setOnAction(new EventHandler() {
public void handle(ActionEvent e) {
String output = "";
map.clear();
boolean missSpelled = false;
int missSpelledCount = 0;
for (String key : document) {
if (key.length() > 0) {
key = key.toLowerCase();
if (!dictionary.contains(key)) {
missSpelled = true;
missSpelledCount++;
}
if (!map.containsKey(key)) {
if (missSpelled) {
map.put(key, 0);
} else {
map.put(key, 1);
}
} else {
int value = map.get(key);
value++;
map.put(key, value);
}
}
missSpelled = false;
}
List > entries = new ArrayList<>(map.entrySet());
Collections.sort(entries, (entry1, entry2) -> {
return entry2.getValue().compareTo(entry1.getValue());
});
int maxValue = 0;
for (Map.Entry entry : entries) {
if (entry.getValue() > maxValue) {
maxValue = entry.getValue();
}
}
output += "---------- Most Frequent Words ----------";
for (Map.Entry entry : entries) {
if (entry.getValue() == maxValue) {
output += entry.getKey() + " ";
}
}
output += "";
output += "---------- Frequency of Word ----------";
int lastValue = 0;
StringBuilder stringBuilder = new StringBuilder ();
for (Map.Entry entry : entries) {
if(!(entry.getValue() == lastValue)) {
output += stringBuilder;
stringBuilder.setLength(0);
stringBuilder.append("frequency " + entry.getValue() + "" + " ");
}
if (entry.getValue() == lastValue) {
stringBuilder = stringBuilder.append(", " + entry.getValue());
} else {
stringBuilder = stringBuilder.append(entry.getKey());
}
lastValue = entry.getValue();
}
output += "---------- Misspelled Words ---------- Count: " + missSpelledCount + "";
stringBuilder.setLength(0);
boolean beganPrintingMissSpelled = false;
for (Map.Entry entry : entries) {
if (beganPrintingMissSpelled == false) {
stringBuilder = stringBuilder.append(entry.getKey());
} else {
stringBuilder = stringBuilder.append(", " + entry.getKey());
}
beganPrintingMissSpelled = true;
}
output += stringBuilder;
textArea.setText(output);
}
});
btClearAnswers.setOnAction(e -> {
textArea.setText("");
});
btnExit.setOnAction(e -> {
System.exit(0);
});
// Create a scene and place it in the stage
Scene scene = new Scene(borderPane, 800, 450);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Spell Checking with Frequent Words");
primaryStage.setScene(scene);
primaryStage.show();
}
private String LoadTheTextFile(String fileName) throws SQLException {
document.clear();
File f = new File(fileName);
Scanner scan;
try {
scan = new Scanner(f);
while (scan.hasNext()) {
String l = scan.next();
if (l.endsWith(",") || l.endsWith(".") || l.endsWith("!") || l.endsWith("?") || l.endsWith(":")) {
l = l.substring(0, l.length() - 1);
}
l = (l.replaceAll("[^a-zA-Z]", " ")).trim();
String[] result = l.split(" ");
for (int i = 0; i < result.length; i++) {
if (result[i].length() > 0)
document.add(result[i]);
}
}
scan.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
StringBuilder sb = new StringBuilder();
for (String s : document)
sb = sb.append(s + " ");
return fileName + " loaded with " + document.size() + " words" + sb;
}
/**
* The main method is only needed for the IDE with limited JavaFX support. Not
* needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Step by Step Solution
3.43 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
The error message youre encountering on the line if dictionarycontainskey likely indicates that the ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started