package com.github.bot.curiosone.core.extraction;

import com.github.bot.curiosone.core.nlp.Phrase;
import com.github.bot.curiosone.core.nlp.Token;

import java.util.List;
import java.util.Optional;

/**
 * Precomuted answers to sentences not understood by chatbot.
 */
public class RandomAnswer {

  /**
   * Private constructor.
   */
  private RandomAnswer() { }

  /** This array contains costants added to the answer when the input is made by a single word. */
  private static String[] costants;

  /** This array contains answers to a not-english sentence. */
  private static String[] englishAnswers;

  /** This array contains answers made for too much general/not understood sentence. */
  private static String[] generalAnswers;

  /**
   * Gives a general answer to a general user input.
   * @param phrase given in input
   * @return general answer
   */
  public static BrainResponse getAnswer(Phrase phrase) {
    if (costants == null) {
      costants = new String[] {
          "?",
          "? I do not understand",
          ".. that is cool"
      };
      englishAnswers = new String[] {
          "I think you should speak english",
          "PLEASE, speak english!",
          "Are you a robot too?"
      };
      generalAnswers = new String[] {
          "What a nice day to talk to a chatbot!",
          "Can you reformulate your sentence please?",
          "Tell me something interesting please."
      };
    }

    List<Token> tokenList = phrase.getTokens();

    if (tokenList.size() == 1) {
      return new BrainResponse(phrase.getText() + costants[randomIndex(costants)], "");
    } else {
      long count = tokenList.stream().map(Token::isKnown).filter(x -> !x).count();
      if (count > tokenList.size() / 2) {
        return new BrainResponse(englishAnswers[randomIndex(englishAnswers)], "");
      }
    }
    return new BrainResponse(generalAnswers[randomIndex(generalAnswers)], "");
  }

  /**
   * Returns a random index of the array given in input.
   * @param array whose index is needed
   * @return index
   */
  private static int randomIndex(String[] array) {
    return (int)(Math.random() * array.length);
  }
}

package com.github.bot.curiosone.core.extraction;

import com.github.bot.curiosone.core.nlp.Phrase;
import com.github.bot.curiosone.core.nlp.Token;

import java.util.List;
import java.util.Optional;

/**
 * Handles precomputed answers to sentences not understood by the chatbot.
 */
public class RandomAnswer {

  /**
   * Contains constants added to the answer when the input is made by a single word.
   */
  private static final String[] CONSTANTS = {
      "?",
      "? I do not understand",
      ".. that is cool"
  };

  /**
   * Contains answers to a not-english sentence.
   */
  private static final String[] ENGLISH_ANSWERS = {
      "I think you should speak english",
      "PLEASE, speak english!",
      "Are you a robot too?"
  };

  /**
   * Contains answers made for too much general/not understood sentence.
   */
  private static final String[] GENERAL_ANSWERS = {
      "What a nice day to talk to a chatbot!",
      "Can you reformulate your sentence please?",
      "Tell me something interesting please."
  };

  /**
   * Private constructor.
   */
  private RandomAnswer() { }

  /**
   * Gives a general answer to a general user input.
   * @param phrase given in input
   * @return a BrainResponse instance, containing a general answer
   */
  public static BrainResponse getAnswer(Phrase phrase) {
    List<Token> tokenList = phrase.getTokens();

    if (tokenList.size() == 1) {
      return new BrainResponse(phrase.getText() + CONSTANTS[randomIndex(CONSTANTS)], "");
    } else {
      long count = tokenList.stream().map(Token::isKnown).filter(x -> !x).count();
      if (count > tokenList.size() / 2) {
        return new BrainResponse(ENGLISH_ANSWERS[randomIndex(ENGLISH_ANSWERS)], "");
      }
    }
    return new BrainResponse(GENERAL_ANSWERS[randomIndex(GENERAL_ANSWERS)], "");
  }

  /**
   * Returns a random index of the array given in input.
   * @param array whose index is needed
   * @return the randomly generated index
   */
  private static int randomIndex(String[] array) {
    return (int)(Math.random() * array.length);
  }
}

improve RandomAnswer javadoc and codestyle