gpt4 book ai didi

java - 如何通过在相邻字符之间添加空格将一个单词分成两个单词

转载 作者:行者123 更新时间:2023-12-02 03:56:42 24 4
gpt4 key购买 nike

我正在尝试获取单词:拼写错误,并通过在相邻字符之间添加“”(空格)将单词拆分为两个单词,并希望获取单词:拼写错误 结果。任何指导都会有所帮助,一直在尝试不同的代码,但还没有看到结果。

其他建议的代码仅供引用。 *请注意,注释掉的代码是我一直在尝试获得正确结果的代码。

    /**
* 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");
}

最佳答案

我编写了一段最小的、可运行的代码,如果在字典中找到两个单词片段,它就会拆分单词。

这是我的测试结果

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

}

关于java - 如何通过在相邻字符之间添加空格将一个单词分成两个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com