Guest User

Untitled

a guest
Jun 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. Index: src/main/java/org/alicebot/ab/AIMLProcessor.java
  2. ===================================================================
  3. --- src/main/java/org/alicebot/ab/AIMLProcessor.java (revision 100)
  4. +++ src/main/java/org/alicebot/ab/AIMLProcessor.java (working copy)
  5. @@ -18,6 +18,8 @@
  6. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  7. Boston, MA 02110-1301, USA.
  8. */
  9. +import java.nio.ByteBuffer;
  10. +import java.nio.charset.StandardCharsets;
  11. import java.util.ArrayList;
  12. import java.util.HashSet;
  13. import java.util.Set;
  14. @@ -31,6 +33,10 @@
  15. import org.w3c.dom.Node;
  16. import org.w3c.dom.NodeList;
  17.  
  18. +import static org.alicebot.ab.MagicStrings.*;
  19. +
  20. +import java.io.*;
  21. +
  22. /**
  23. * The core AIML parser and interpreter.
  24. * Implements the AIML 2.0 specification as described in
  25. @@ -882,7 +888,7 @@
  26. return result;
  27. }
  28.  
  29. - private static String learn(Node node, ParseState ps) { // learn, learnf AIML 2.0
  30. + private static String learn(Node node, ParseState ps) throws IOException { // learn, learnf AIML 2.0
  31. NodeList childList = node.getChildNodes();
  32. String pattern = "";
  33. String that="*";
  34. @@ -917,12 +923,34 @@
  35. c = new Category(0, pattern, that, "*", template, MagicStrings.null_aiml_file);
  36. else {// learnf
  37. c = new Category(0, pattern, that, "*", template, MagicStrings.learnf_aiml_file);
  38. - //ps.chatSession.bot.learnfCategories.add(c);
  39.  
  40. ps.chatSession.bot.learnfGraph.addCategory(c);
  41. - //ps.chatSession.bot.categories.add(c);
  42. +
  43. + File learnf_f = new File("your_path_to_learnf.aiml");
  44. + if(!learnf_f.exists()) {
  45. + BufferedWriter output = null;
  46. + try {
  47. + learnf_f.createNewFile();
  48. + output = new BufferedWriter(new FileWriter(learnf_f));
  49. + output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<aiml version=\"2.0\">\n" + "\n" + "</aiml>");
  50. + } catch (IOException e) {
  51. + e.printStackTrace();
  52. + } finally {
  53. + if (output != null) {
  54. + output.close();
  55. + }
  56. + }
  57. + } else { /* if your file already exists */
  58. + RandomAccessFile access = new RandomAccessFile(learnf_f, "rw");
  59. + access.seek(learnf_f.length() - 8); /* Skip the </aiml>
  60. + String s = String.format(content, pattern, template);
  61. + byte [] b = s.getBytes(StandardCharsets.UTF_8);
  62. + access.write(b);
  63. + access.writeBytes(aiml_end_format);
  64. + access.close();
  65. + }
  66. }
  67. ps.chatSession.bot.brain.addCategory(c);
  68. - //ps.chatSession.bot.brain.printgraph();
  69. }
  70. }
  71. return "";
Add Comment
Please, Sign In to add comment