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

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

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Handles precomputed answers to some common bad words.
 * Contains method to get an answer to a phrase containig one known bad word.
 */
public class BadWords {

  /**
   * Path to known insults database.
   */
  private static String badWordsPath = "/conversation/bad_words.txt";

  /**
   * Lists different insults known by the Bot.
   */
  private static List<String> knownBadWords;

  /**
   * Array of different answers that the bot gives in output.
   */
  private static String[] readyAnswers = {
    "I do not like your way of talking.",
    "You should speak more politely.",
    "Why are you talking like this? Are you a little kid?",
    "Come back when you grow up."
  };

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

  /**
   * Loads the known bad words in memory.
   */
  private static void loadSentences() {
    knownBadWords = new ArrayList<>();
    Path path = null;

    try {
      URL resource = Conversation.class.getResource(badWordsPath);
      path = Paths.get(resource.toURI());
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }

    try (Stream<String> stream = Files.lines(path)) {
      stream.forEach(line -> knownBadWords.add(line));
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Returns an answer for the given phrase.
   * @param phrase the original phrase to be answered
   * @return an Optional instance.
   *         The returned instance is empty, if no bad word or insult has been found in the original
   *         Phrase, contains a value (an instance of BrainResponse) otherwise.
   */
  public static Optional<BrainResponse> getAnswer(Phrase phrase) {
    if (knownBadWords == null) {
      loadSentences();
    }
    int randpos = (int)(Math.random() * readyAnswers.length);
    boolean bad = phrase.getTokens().stream()
        .map(word -> word.getText())
        .anyMatch(word -> knownBadWords.contains(word));
    if (bad) {
      return Optional.of(new BrainResponse(readyAnswers[randpos], ""));
    }
    return Optional.empty();
  }
}

@@ -446,16 +446,27 @@
 ontains 
+an utility 
 method t
@@ -486,17 +486,17 @@
 er to a 
-p
+P
 hrase co
@@ -523,16 +523,83 @@
 d word.%0A
+ * @see  com.github.bot.curiosone.core.nlp.Phrase The Phrase Class%0A
  */%0Apubl
@@ -1772,17 +1772,17 @@
 e given 
-p
+P
 hrase.%0A 
@@ -1792,22 +1792,36 @@
  @param 
+ 
 phrase
+%0A   *        
  the ori
@@ -1826,17 +1826,17 @@
 riginal 
-p
+P
 hrase to
@@ -1861,16 +1861,17 @@
 @return 
+ 
 an Optio
@@ -1884,24 +1884,25 @@
 stance.%0A   *
+ 
          The
@@ -1976,25 +1976,16 @@
 d in the
- original
 %0A   *   
@@ -1990,16 +1990,26 @@
         
+ original 
 Phrase, 
@@ -2063,24 +2063,248 @@
  otherwise.%0A
+   * @see  com.github.bot.curiosone.core.nlp.Phrase The Phrase Class%0A   * @see  %3Ca href=%22https://goo.gl/sWfXyh%22%3EThe Optional Class%3C/a%3E%0A   * @see com.github.bot.curiosone.core.extraction.BrainResponse The BrainResponse Class%0A
    */%0A  publ

improve BadWords javadoc