Search code examples
javaspell-checkingtrie

how to split a word into two words by adding a space between adjacent characters


I am trying to take the word: missspelling and split the word into two words by adding a " " (space) between adjacent chars and want to get the word: miss spelling as a result. Any guidance would help, been trying out different code, but have not seen results.

Code that works for other suggestions for reference only. *Note that commented out code is what I have been messing with to try and get the correct result.

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

Solution

  • I wrote a minimal, runnable piece of code that splits words if the two word pieces are found in the dictionary.

    Here are my test results

    miss spelling
    apple
    

    And here's the code. The important method is the splitWord method.

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