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

标签 java spell-checking trie

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

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

    /**
     * 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/

相关文章:

c++ - Radix/Patricia Trie 的 STYLish 下界函数

c - Trie执行效率

database - 为什么数据库中不使用 trie 索引来进行字符串索引?

java - 如何在低(汇编)级别捕获和处理异常?

java - 拼写检查 - 带有 Java 流的嘈杂 channel 模型

java - 如何从 java servlet 控制类(运行/停止)

vim - 拼写文件之间的单词字符不同 (E763)

python - 比较字符串以匹配品牌名称并删除拼写错误

java - 如何在 messages.properties 文件中使用参数?

java - 无法在我的代码中访问 DatePicker.ValidationCallback