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.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.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Precomputed answers to some common conversational phrases.
 */
public class Conversation {
  private static String conversationsPath = "/conversation/conversation.txt";
  /** Path of the database containing known answers. */


  /** Map from recognized tokens to possible phrases given in output. */
  private static LinkedHashMap<String, String[]> knownQuestions;

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

  /**
   * Loads the known answers in memory.
   */
  private static void loadSentences() {
    knownQuestions = new LinkedHashMap<>();
    Path path = null;
    try {
      URL resource = Conversation.class.getResource(conversationsPath);
      path = Paths.get(resource.toURI());
    } catch (URISyntaxException e) {
      e.printStackTrace();
    }

    try (Stream<String> stream = Files.lines(path)) {
      stream.forEach(line -> {
        int splitIndex = line.indexOf(":");
        String key = line.substring(0, splitIndex);
        String[] values = line.substring(splitIndex + 1, line.length()).split("\t");
        knownQuestions.put(key, values);
      });
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * Checks if the given input is present in our known answers.
   *
   * @param  phrase phrase given by user's input
   * @return answer if the input is known
   */
  public static Optional<BrainResponse> getAnswer(Phrase phrase) {
    if (knownQuestions == null) {
      loadSentences();
    }
    List<String> lemmas = phrase.getTokens()
        .stream()
        .map(Token::getLemma)
        .collect(Collectors.toList());

    for (Map.Entry<String, String[]> entry : knownQuestions.entrySet()) {
      boolean isKnown = true;

      for (String s : entry.getKey().split("\t")) {
        if (!lemmas.contains(s)) {
          isKnown = false;
          break;
        }
      }
      if (isKnown) {
        int randpos = (int)(Math.random() * entry.getValue().length);
        return Optional.of(new BrainResponse(entry.getValue()[randpos], ""));
      }
    }
    return Optional.empty();
  }
}

@@ -520,17 +520,29 @@
 %0A/**%0A * 
-P
+Handles the p
 recomput
@@ -597,37 +597,172 @@
 .%0A *
-/%0Apublic class Conversation %7B
+ Provides an utility method to get an answer from a given sentence.%0A */%0Apublic class Conversation %7B%0A%0A  /**%0A   * Path to the database containing known answers.%0A   */
 %0A  p
@@ -840,80 +840,28 @@
 t%22;%0A
+%0A
   /**
- Path of the database containing known answers. */%0A%0A%0A  /** Map from
+%0A   * Maps the
  rec
@@ -878,16 +878,22 @@
 kens to 
+their 
 possible
@@ -904,25 +904,12 @@
 ases
- given in output.
+.%0A  
  */%0A
@@ -925,26 +925,16 @@
  static 
-LinkedHash
 Map%3CStri
@@ -931,24 +931,26 @@
 c Map%3CString
+%5B%5D
 , String%5B%5D%3E 
@@ -1530,16 +1530,18 @@
   String
+%5B%5D
  key = l
@@ -1568,16 +1568,28 @@
 itIndex)
+.split(%22%5Ct%22)
 ;%0A      
@@ -1801,17 +1801,15 @@
   * 
-Checks if
+Answers
  the
@@ -1819,142 +1819,255 @@
 ven 
-input is present in our known answers.%0A   *%0A   * @param  phrase phrase given by user's input%0A   * @return answer if the input is known
+Phrase.%0A   * @param phrase the input phrase to be checked%0A   * @return An Optional instance. The instance is empty, if the input is an unknown Phrase.%0A   *         Otherwise, the istance contains the answer, encapsulated in a BrainResponse object.
 %0A   
@@ -2357,16 +2357,18 @@
 y%3CString
+%5B%5D
 , String
@@ -2475,28 +2475,16 @@
 getKey()
-.split(%22%5Ct%22)
 ) %7B%0A    

improve Conversation javadoc