PageRenderTime 3551ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/src/example/java/example/InteractiveExample.java

https://bitbucket.org/sgla/aiml-core
Java | 80 lines | 67 code | 13 blank | 0 comment | 0 complexity | 358f56bb5443777298308062daef2d08 MD5 | raw file
  1. package example;
  2. import java.io.File;
  3. import java.net.URL;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import javax.inject.Inject;
  8. import com.google.inject.Guice;
  9. import com.google.inject.Injector;
  10. import ai.saxatus.aiml.api.AIMLHandler;
  11. import ai.saxatus.aiml.api.AIMLHandlerBuilder;
  12. import ai.saxatus.aiml.api.AIMLResponse;
  13. import ai.saxatus.aiml.api.io.AIMLCreationException;
  14. import ai.saxatus.aiml.api.io.AIMLDirectoryReader;
  15. import ai.saxatus.aiml.module.AIMLModule;
  16. public class InteractiveExample
  17. {
  18. @Inject
  19. AIMLHandlerBuilder aimlHandlerBuilder;
  20. private Map<String, String> botMem = new HashMap<>();
  21. private AIMLHandler getAIMLHandler() throws AIMLCreationException
  22. {
  23. URL r = getClass().getResource("/interactiveCorpus");
  24. File file = new File(r.getFile());
  25. botMem.put("name", "TestBot");
  26. return aimlHandlerBuilder.nonStaticMemory(new HashMap<>())
  27. .withBotMemory(botMem)
  28. .withAimlProvider(new AIMLDirectoryReader(file))
  29. .build();
  30. }
  31. public void getAnswer()
  32. {
  33. AIMLHandler handler = null;
  34. try
  35. {
  36. handler = getAIMLHandler();
  37. }
  38. catch(AIMLCreationException e)
  39. {
  40. e.printStackTrace();
  41. System.exit(1);
  42. }
  43. try (Scanner sc = new Scanner(System.in))
  44. {
  45. String input;
  46. System.out.println("Interactive Bot initialized!");
  47. System.out.println("Hello my name is " + botMem.get("name"));
  48. do
  49. {
  50. System.out.print("Input: \t");
  51. input = sc.nextLine();
  52. handler.resetDepth();
  53. AIMLResponse response = handler.getAnswer(input);
  54. System.out.println("Result:\t" + response);
  55. }
  56. while(!input.equals("q"));
  57. }
  58. System.out.println("Interactive Bot terminated!");
  59. }
  60. public static void main(String[] args)
  61. {
  62. Injector injector = Guice.createInjector(new AIMLModule());
  63. InteractiveExample example = new InteractiveExample();
  64. injector.injectMembers(example);
  65. example.getAnswer();
  66. }
  67. }