Java 如何通过在相邻字符之间添加空格将单词拆分为两个单词
我试图将单词:拼写错误拆分为两个单词,在相邻的字符之间添加一个“”(空格),并希望得到单词:拼写错误。任何指导都会有所帮助,他们一直在尝试不同的代码,但并没有看到结果 用于其他建议的代码仅供参考*请注意,注释掉的代码是我一直在尝试得到正确结果的地方Java 如何通过在相邻字符之间添加空格将单词拆分为两个单词,java,spell-checking,trie,Java,Spell Checking,Trie,我试图将单词:拼写错误拆分为两个单词,在相邻的字符之间添加一个“”(空格),并希望得到单词:拼写错误。任何指导都会有所帮助,他们一直在尝试不同的代码,但并没有看到结果 用于其他建议的代码仅供参考*请注意,注释掉的代码是我一直在尝试得到正确结果的地方 /** * Returns possible suggestions for misspelled word * * @param tree The Trie that will be checked
/**
* Returns possible suggestions for misspelled word
*
* @param tree The Trie that will be checked
* @param word The word in trie that is checked
*/
public static void suggest(TrieNode tree, String word) {
Set<String> result = new HashSet<>();
System.out.println("Suggestions: ");
// Remove a character
for (int i = 0; i < word.length(); ++i)
result.add(word.substring(0, i) + word.substring(i + 1));
// Swap two consecutive characters
for (int i = 0; i < word.length() - 1; ++i)
result.add(word.substring(0, i) + word.substring(i + 1, i + 2) + word.substring(i, i + 1)
+ word.substring(i + 2));
// Replace a character with other
for (int i = 0; i < word.length(); ++i)
for (char c = 'a'; c <= 'z'; ++c)
result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i + 1));
// Add a new character
for (int i = 0; i <= word.length(); ++i)
for (char c = 'a'; c <= 'z'; ++c)
result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
// Split word into pair of words by adding a " " between adjacent pairs
// Need help here
for (int i = 0; i < word.length(); ++i)
for (char c = ' '; c <= ' '; ++c)
if (search(tree, word.substring(0, i)) && search(tree, word.substring(i)) == true)
result.add(word.substring(0, i) + String.valueOf(c) + word.substring(i));
ArrayList<String> res = new ArrayList<>(result);
int j = 0;
for (int i = 0; i < result.size(); i++)
if (search(tree, res.get(i))) {
if (j == 0)
System.out.print("[");
System.out.print(res.get(i) + ",");
System.out.print("");
j++;
}
System.out.print("]" + "\n");
}
/**
*返回拼写错误单词的可能建议
*
*@param tree将要检查的Trie
*@param word在trie中选中的单词
*/
公共静态无效建议(三元组树、字符串字){
Set result=new HashSet();
System.out.println(“建议:”);
//删除字符
for(int i=0;i 对于(char c='a';c,我编写了一段最小的、可运行的代码,如果在字典中找到这两个单词片段,它将拆分单词
这是我的测试结果
miss spelling
apple
下面是代码。重要的方法是splitWord方法
package com.ggl.testing;
import java.util.ArrayList;
import java.util.List;
public class DoubleWord implements Runnable {
public static void main(String[] args) {
new DoubleWord().run();
}
@Override
public void run() {
Dictionary dictionary = new Dictionary();
System.out.println(splitWord("missspelling", dictionary));
System.out.println(splitWord("apple", dictionary));
}
public String splitWord(String word, Dictionary dictionary) {
for (int index = 1; index < word.length(); index++) {
String prefix = word.substring(0, index);
if (dictionary.isWordInDictionary(prefix)) {
String suffix = word.substring(index);
if (dictionary.isWordInDictionary(suffix)) {
return prefix + " " + suffix;
}
}
}
return word;
}
public class Dictionary {
private List<String> words;
public Dictionary() {
this.words = setWords();
}
public boolean isWordInDictionary(String word) {
return words.contains(word);
}
private List<String> setWords() {
List<String> words = new ArrayList<>();
words.add("apple");
words.add("miss");
words.add("spelling");
words.add("zebra");
return words;
}
}
}
package com.ggl.testing;
导入java.util.ArrayList;
导入java.util.List;
公共类双字实现可运行{
公共静态void main(字符串[]args){
新的DoubleWord().run();
}
@凌驾
公开募捐{
字典=新字典();
System.out.println(splitWord(“拼写错误”,字典));
System.out.println(splitWord(“苹果”,字典));
}
公共字符串拆分词(字符串词、字典){
for(int index=1;index
我编写了一段最小的、可运行的代码,如果在字典中找到这两个单词,它会将单词拆分
这是我的测试结果
miss spelling
apple
下面是代码。重要的方法是splitWord方法
package com.ggl.testing;
import java.util.ArrayList;
import java.util.List;
public class DoubleWord implements Runnable {
public static void main(String[] args) {
new DoubleWord().run();
}
@Override
public void run() {
Dictionary dictionary = new Dictionary();
System.out.println(splitWord("missspelling", dictionary));
System.out.println(splitWord("apple", dictionary));
}
public String splitWord(String word, Dictionary dictionary) {
for (int index = 1; index < word.length(); index++) {
String prefix = word.substring(0, index);
if (dictionary.isWordInDictionary(prefix)) {
String suffix = word.substring(index);
if (dictionary.isWordInDictionary(suffix)) {
return prefix + " " + suffix;
}
}
}
return word;
}
public class Dictionary {
private List<String> words;
public Dictionary() {
this.words = setWords();
}
public boolean isWordInDictionary(String word) {
return words.contains(word);
}
private List<String> setWords() {
List<String> words = new ArrayList<>();
words.add("apple");
words.add("miss");
words.add("spelling");
words.add("zebra");
return words;
}
}
}
package com.ggl.testing;
导入java.util.ArrayList;
导入java.util.List;
公共类双字实现可运行{
公共静态void main(字符串[]args){
新的DoubleWord().run();
}
@凌驾
公开募捐{
字典=新字典();
System.out.println(splitWord(“拼写错误”,字典));
System.out.println(splitWord(“苹果”,字典));
}
公共字符串拆分词(字符串词、字典){
for(int index=1;index
首先要做几件事
这句话太疯狂了:
for (char c = ' '; c <= ' '; ++c)
通过交换字符,然后替换字符,试图找到有效的单词,这是在重新发明轮子:阅读相关内容,实现该算法,然后根据输入的Levenshtein距离对词典排序,以找到“最佳匹配”,应该通过最大Levenshtein距离进行过滤-也许3是一个好的起点(测试代码,看看结果是否合理)
你的triode
应该有一个search()
方法,而不是你的search()
方法接受一个trie和一个单词,但这更多的是一个设计问题,不是你最大的问题
现在,关于你的实际问题,尝试分割输入是复杂的,但“答案”是:
在字母之间循环输入中的所有位置,并将每个“一半”与您的输入进行相同的处理,只是您不应进行嵌套拆分,将每个一半的建议组合在一起,然后返回所有唯一建议组合的集合
但是,这样做会产生“非常多”的建议,因此无法扩展,因此您可能不应该这样做。首先要做几件事
这句话太疯狂了:
for (char c = ' '; c <= ' '; ++c)
你试图找到v,这是在重塑方向盘