
// SplitWords.java
// Andrew Davison, ad@coe.psu.ac.th, Dec. 2022

/* Partition a string into dictionary words

  Usage:
    > compile SplitWords.java
    > run SplitWords <string>
  e.g.
    > run SplitWords iloveicecreamandmango
    > run SplitWords ilovesamsungmobile
    
*/

import java.io.*;
import java.util.*;


public class SplitWords
{

  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: run SplitWords <string>");
      return;
    }
    String s = args[0];

    HunspellChecker hsc = new HunspellChecker();

    System.out.println("Splitting up \"" + s + "\"");
    List<List<String>> parts = WordUtils.partitions(s);
    for (List<String> part : parts) {
      if (areWords(part, hsc))
        System.out.println( Arrays.toString( part.toArray() ));
    }

    hsc.close();
  } /* main */


  private static boolean areWords(List<String> part,
                                     HunspellChecker hsc)
  {
    for(String s : part) {
      if (!hsc.isCorrect(s) || !isValid(s))
        return false;
    }
    return true;
  }


  private static boolean isValid(String word)
  /* Strip out most single letter words

     Also see isValid() in Anagrams.java which further
     restricts the valid words by checking 
     WordUtils.COMMON_TWOS and WordUtils.COMMON_THREES
  */
  {
    if (word.length() == 1) {
      char ch = word.charAt(0);
      if ((ch == 'a') || (ch == 'i') || (ch == 'o'))
        return true;
      else
        return false;
    }
    return true;
  }  // end of isValid()


}  // end of SplitWords class


