
// SingleAnagrams.java
// https://rosettacode.org/wiki/Anagrams#Java
// Modified by Andrew Davison, ad@coe.psu.ac.th, Dec. 2022

/* Generate anagrams of the supplied word. The 'single' in
   the program name is to distinguish this approach from that
   used in Anagrams.java which allows the input string to be
   broken up into smaller words.

   For input ideas, I referred to:
https://www.reddit.com/r/answers/comments/13o511/what_is_the_word_with_the_most_anagrams/

  Usage:
    > compile SingleAnagrams.java
    > run SingleAnagrams <word>
  e.g.                                   Using big US dictionary
    > run SingleAnagrams alerts     // 4		6
                                (including original word)
    > run SingleAnagrams parse      // 7		10
    > run SingleAnagrams least      // 6		8
    > run SingleAnagrams rates      // 8        8
    > run SingleAnagrams saint      // 4		4
    > run SingleAnagrams ears       // 4		4

*/


import java.util.*;


public class SingleAnagrams
{
  public static void main(String[] args)
  {
    if (args.length != 1) {
      System.out.println("Usage: java SingleAnagrams <word>");
      return;
    }
    String word = args[0];

    HunspellChecker hsc = new HunspellChecker(true);
               // true switches Hunspell to the en_US-large dictionary
        
    System.out.println("Single anagrams:");
    ArrayList<String> permutes = WordUtils.permutes(word);
    int count = 0;
    for (String wd : permutes) {
      if (hsc.isCorrect(wd)) {
        System.out.println("  " + wd);
        count++;
      }
    }
    System.out.println("No. of words: " + count);
    hsc.close();
  } /* main */

} // end of SingleAnagrams class

