Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Index: src/main/java/org/alicebot/ab/AIMLProcessor.java
===================================================================
--- src/main/java/org/alicebot/ab/AIMLProcessor.java (revision 100)
+++ src/main/java/org/alicebot/ab/AIMLProcessor.java (working copy)
@@ -18,6 +18,8 @@
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
@@ -31,6 +33,10 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+import static org.alicebot.ab.MagicStrings.*;
+
+import java.io.*;
+
/**
* The core AIML parser and interpreter.
* Implements the AIML 2.0 specification as described in
@@ -882,7 +888,7 @@
return result;
}
- private static String learn(Node node, ParseState ps) { // learn, learnf AIML 2.0
+ private static String learn(Node node, ParseState ps) throws IOException { // learn, learnf AIML 2.0
NodeList childList = node.getChildNodes();
String pattern = "";
String that="*";
@@ -917,12 +923,34 @@
c = new Category(0, pattern, that, "*", template, MagicStrings.null_aiml_file);
else {// learnf
c = new Category(0, pattern, that, "*", template, MagicStrings.learnf_aiml_file);
- //ps.chatSession.bot.learnfCategories.add(c);
ps.chatSession.bot.learnfGraph.addCategory(c);
- //ps.chatSession.bot.categories.add(c);
+
+ File learnf_f = new File("your_path_to_learnf.aiml");
+ if(!learnf_f.exists()) {
+ BufferedWriter output = null;
+ try {
+ learnf_f.createNewFile();
+ output = new BufferedWriter(new FileWriter(learnf_f));
+ output.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<aiml version=\"2.0\">\n" + "\n" + "</aiml>");
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (output != null) {
+ output.close();
+ }
+ }
+ } else { /* if your file already exists */
+ RandomAccessFile access = new RandomAccessFile(learnf_f, "rw");
+ access.seek(learnf_f.length() - 8); /* Skip the </aiml>
+ String s = String.format(content, pattern, template);
+ byte [] b = s.getBytes(StandardCharsets.UTF_8);
+ access.write(b);
+ access.writeBytes(aiml_end_format);
+ access.close();
+ }
}
ps.chatSession.bot.brain.addCategory(c);
- //ps.chatSession.bot.brain.printgraph();
}
}
return "";
@luulinh90s

This comment has been minimized.

Copy link
Owner Author

commented Jun 20, 2018

Fix bug learnf tag AIML 2.0 doesn't work in Program AB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.