3 回答

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
我正在添加 JavaFX 答案。這個(gè)應(yīng)用程序使用Levenshtein Distance
. 您必須單擊Check Spelling
才能開始。您可以從列表中選擇一個(gè)單詞來(lái)替換當(dāng)前正在檢查的單詞。我注意到Levenshtein Distance
返回了很多單詞,因此您可能需要找到其他方法來(lái)進(jìn)一步減少列表。
主要的
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class App extends Application
{
public static void main(String[] args)
{
launch(args);
}
TextArea taWords = new TextArea("Tak Carrage thiss on hoemaker answe");
TextField tfCurrentWordBeingChecked = new TextField();
//TextField tfMisspelledWord = new TextField();
ListView<String> lvReplacementWords = new ListView();
TextField tfReplacementWord = new TextField();
Button btnCheckSpelling = new Button("Check Spelling");
Button btnReplaceWord = new Button("Replace Word");
List<String> wordList = new ArrayList();
List<String> returnList = new ArrayList();
HandleLevenshteinDistance handleLevenshteinDistance = new HandleLevenshteinDistance();
ObservableList<String> listViewData = FXCollections.observableArrayList();
@Override
public void start(Stage primaryStage)
{
setupListView();
handleBtnCheckSpelling();
handleBtnReplaceWord();
VBox root = new VBox(taWords, tfCurrentWordBeingChecked, lvReplacementWords, tfReplacementWord, btnCheckSpelling, btnReplaceWord);
root.setSpacing(5);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public void handleBtnCheckSpelling()
{
btnCheckSpelling.setOnAction(actionEvent -> {
if (btnCheckSpelling.getText().equals("Check Spelling")) {
wordList = new ArrayList(Arrays.asList(taWords.getText().split(" ")));
returnList = new ArrayList(Arrays.asList(taWords.getText().split(" ")));
loadWord();
btnCheckSpelling.setText("Check Next Word");
}
else if (btnCheckSpelling.getText().equals("Check Next Word")) {
loadWord();
}
});
}
public void handleBtnReplaceWord()
{
btnReplaceWord.setOnAction(actionEvent -> {
int indexOfWordToReplace = returnList.indexOf(tfCurrentWordBeingChecked.getText());
returnList.set(indexOfWordToReplace, tfReplacementWord.getText());
taWords.setText(String.join(" ", returnList));
btnCheckSpelling.fire();
});
}
public void setupListView()
{
lvReplacementWords.setItems(listViewData);
lvReplacementWords.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection) -> {
tfReplacementWord.setText(newSelection);
});
}
private void loadWord()
{
if (wordList.size() > 0) {
tfCurrentWordBeingChecked.setText(wordList.get(0));
wordList.remove(0);
showPotentialCorrectSpellings();
}
}
private void showPotentialCorrectSpellings()
{
List<String> potentialCorrentSpellings = handleLevenshteinDistance.getPotentialCorretSpellings(tfCurrentWordBeingChecked.getText().trim());
listViewData.setAll(potentialCorrentSpellings);
}
}
自定義Word類
/**
*
* @author blj0011
*/
public class CustomWord
{
private int distance;
private String word;
public CustomWord(int distance, String word)
{
this.distance = distance;
this.word = word;
}
public String getWord()
{
return word;
}
public void setWord(String word)
{
this.word = word;
}
public int getDistance()
{
return distance;
}
public void setDistance(int distance)
{
this.distance = distance;
}
@Override
public String toString()
{
return "CustomWord{" + "distance=" + distance + ", word=" + word + '}';
}
}
HandleLevenshteinDistance 類
/**
*
* @author blj0011
*/
public class HandleLevenshteinDistance
{
private List<String> dictionary = new ArrayList<>();
public HandleLevenshteinDistance()
{
try {
//Load DictionaryFrom file
//See if the dictionary file exists. If it don't download it from Github.
File file = new File("alpha.txt");
if (!file.exists()) {
FileUtils.copyURLToFile(
new URL("https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt"),
new File("alpha.txt"),
5000,
5000);
}
//Load file content to a List of Strings
dictionary = FileUtils.readLines(file, Charset.forName("UTF8"));
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public List<String> getPotentialCorretSpellings(String misspelledWord)
{
LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
List<CustomWord> customWords = new ArrayList();
dictionary.stream().forEach((wordInDictionary) -> {
int distance = levenshteinDistance.apply(misspelledWord, wordInDictionary);
if (distance <= 2) {
customWords.add(new CustomWord(distance, wordInDictionary));
}
});
Collections.sort(customWords, (CustomWord o1, CustomWord o2) -> o1.getDistance() - o2.getDistance());
List<String> returnList = new ArrayList();
customWords.forEach((item) -> {
System.out.println(item.getDistance() + " - " + item.getWord());
returnList.add(item.getWord());
});
return returnList;
}
}

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊
您只需要進(jìn)一步了解詞典即可
我們確定您從詞典中得到了很多建議的單詞?
我們測(cè)試了您的代碼,有時(shí)它發(fā)現(xiàn)了 3000 個(gè)或更多可能的匹配項(xiàng)哇,
所以這是一個(gè)很大的改進(jìn)。它仍然需要大量的測(cè)試,我們使用這條線進(jìn)行測(cè)試,獲得了 100% 良好的結(jié)果。
Tske Charriage 到 hommaker 以及 hommake 作為 hommaer
我們擔(dān)心的是,如果拼寫者真的把這個(gè)詞弄亂了,這項(xiàng)改進(jìn)可能會(huì)解決一定程度的拼寫錯(cuò)誤。
我們確信您知道,如果第一個(gè)字母是錯(cuò)誤的,這將不起作用,
就像仇外心理對(duì)仇外心理一樣
這是重大改進(jìn)
cs.stream().filter(s -> s.startsWith(strSF) || s.startsWith(nF, 0) && s.length() > 1 && s.length() <= W+3 // <== HERE && s.endsWith(nE) && s.startsWith(nF) && s.contains(nM)) .forEach(list :: add);
您可以將支票發(fā)送到我的地址 55 48 196 195

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊
我認(rèn)為你應(yīng)該使用類似于Levenshtein Distance
or的東西Jaro Winkler Distance
。如果你可以使用Apache's
?Commons
.?我建議使用Apache Commons Lang
.?它有一個(gè)實(shí)現(xiàn)Levenshtein Distance
。該示例演示了此實(shí)現(xiàn)。如果將距離設(shè)置為(distance <= 2)
,您可能會(huì)獲得更多結(jié)果。
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
/**
?*
?* @author blj0011
?*/
public class Main
{
? ? public static void main(String[] args)
? ? {
? ? ? ? try {
? ? ? ? ? ? System.out.println("Hello World!");
? ? ? ? ? ? File file = new File("alpha.txt");
? ? ? ? ? ? if (!file.exists()) {
? ? ? ? ? ? ? ? FileUtils.copyURLToFile(
? ? ? ? ? ? ? ? ? ? ? ? new URL("https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt"),
? ? ? ? ? ? ? ? ? ? ? ? new File("alpha.txt"),
? ? ? ? ? ? ? ? ? ? ? ? 5000,
? ? ? ? ? ? ? ? ? ? ? ? 5000);
? ? ? ? ? ? }
? ? ? ? ? ? List<String> lines = FileUtils.readLines(file, Charset.forName("UTF8"));
? ? ? ? ? ? //lines.forEach(System.out::println);
? ? ? ? ? ? lines.stream().forEach(line -> {
? ? ? ? ? ? ? ? int distance = StringUtils.getLevenshteinDistance(line, "zorilta");
? ? ? ? ? ? ? ? //System.out.println(line + ": " + distance);
? ? ? ? ? ? ? ? if (distance <= 1) {
? ? ? ? ? ? ? ? ? ? System.out.println("Did you mean: " + line);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? }
? ? ? ? catch (IOException ex) {
? ? ? ? ? ? Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
? ? ? ? }
? ? }
}
輸出距離<=1
Building JavaTestingGround 1.0
------------------------------------------------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaTestingGround ---
Hello World!
Did you mean: zorilla
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.329 s
Finished at: 2019-11-01T11:02:48-05:00
Final Memory: 7M/30M
距離 <= 2
Hello World!
Did you mean: corita
Did you mean: gorilla
Did you mean: zoril
Did you mean: zorilla
Did you mean: zorillas
Did you mean: zorille
Did you mean: zorillo
Did you mean: zorils
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.501 s
Finished at: 2019-11-01T14:03:33-05:00
Final Memory: 7M/34M
添加回答
舉報(bào)