/** * add an array list of categories with a specific file name * * @param file name of AIML file * @param moreCategories list of categories */ void addMoreCategories(String file, ArrayList<Category> moreCategories) { if (file.contains(MagicStrings.deleted_aiml_file)) { /* for (Category c : moreCategories) { //System.out.println("Delete "+c.getPattern()); deletedGraph.addCategory(c); }*/ } else if (file.contains(MagicStrings.learnf_aiml_file)) { if (MagicBooleans.trace_mode) System.out.println("Reading Learnf file"); for (Category c : moreCategories) { brain.addCategory(c); learnfGraph.addCategory(c); // patternGraph.addCategory(c); } // this.categories.addAll(moreCategories); } else { for (Category c : moreCategories) { // System.out.println("Brain size="+brain.root.size()); // brain.printgraph(); brain.addCategory(c); // patternGraph.addCategory(c); // brain.printgraph(); } // this.categories.addAll(moreCategories); } }
/** * traverse graph and test all categories found in leaf nodes for shadows * * @param node */ void shadowChecker(Nodemapper node) { if (NodemapperOperator.isLeaf(node)) { String input = node.category.getPattern(); input = brain.replaceBotProperties(input); input = input.replace("*", "XXX").replace("_", "XXX").replace("^", "").replace("#", ""); String that = node.category .getThat() .replace("*", "XXX") .replace("_", "XXX") .replace("^", "") .replace("#", ""); String topic = node.category .getTopic() .replace("*", "XXX") .replace("_", "XXX") .replace("^", "") .replace("#", ""); input = instantiateSets(input); System.out.println("shadowChecker: input=" + input); Nodemapper match = brain.match(input, that, topic); if (match != node) { System.out.println("" + Graphmaster.inputThatTopic(input, that, topic)); System.out.println("MATCHED: " + match.category.inputThatTopic()); System.out.println("SHOULD MATCH:" + node.category.inputThatTopic()); } } else { for (String key : NodemapperOperator.keySet(node)) { shadowChecker(NodemapperOperator.get(node, key)); } } }
/** * Constructor * * @param name name of bot * @param path root path of Program AB * @param action Program AB action */ public Bot(String name, String path, String action) { int cnt = 0; int elementCnt = 0; this.name = name; setAllPaths(path, name); this.brain = new Graphmaster(this); this.learnfGraph = new Graphmaster(this, "learnf"); this.learnGraph = new Graphmaster(this, "learn"); // this.unfinishedGraph = new Graphmaster(this); // this.categories = new ArrayList<Category>(); preProcessor = new PreProcessor(this); addProperties(); cnt = addAIMLSets(); if (MagicBooleans.trace_mode) System.out.println("Loaded " + cnt + " set elements."); cnt = addAIMLMaps(); if (MagicBooleans.trace_mode) System.out.println("Loaded " + cnt + " map elements"); this.pronounSet = getPronouns(); AIMLSet number = new AIMLSet(MagicStrings.natural_number_set_name, this); setMap.put(MagicStrings.natural_number_set_name, number); AIMLMap successor = new AIMLMap(MagicStrings.map_successor, this); mapMap.put(MagicStrings.map_successor, successor); AIMLMap predecessor = new AIMLMap(MagicStrings.map_predecessor, this); mapMap.put(MagicStrings.map_predecessor, predecessor); AIMLMap singular = new AIMLMap(MagicStrings.map_singular, this); mapMap.put(MagicStrings.map_singular, singular); AIMLMap plural = new AIMLMap(MagicStrings.map_plural, this); mapMap.put(MagicStrings.map_plural, plural); // System.out.println("setMap = "+setMap); Date aimlDate = new Date(new File(aiml_path).lastModified()); Date aimlIFDate = new Date(new File(aimlif_path).lastModified()); if (MagicBooleans.trace_mode) System.out.println("AIML modified " + aimlDate + " AIMLIF modified " + aimlIFDate); // readUnfinishedIFCategories(); MagicStrings.pannous_api_key = Utilities.getPannousAPIKey(this); MagicStrings.pannous_login = Utilities.getPannousLogin(this); if (action.equals("aiml2csv")) addCategoriesFromAIML(); else if (action.equals("csv2aiml")) addCategoriesFromAIMLIF(); else if (action.equals("chat-app")) { if (MagicBooleans.trace_mode) System.out.println("Loading only AIMLIF files"); cnt = addCategoriesFromAIMLIF(); } else if (aimlDate.after(aimlIFDate)) { if (MagicBooleans.trace_mode) System.out.println("AIML modified after AIMLIF"); cnt = addCategoriesFromAIML(); writeAIMLIFFiles(); } else { addCategoriesFromAIMLIF(); if (brain.getCategories().size() == 0) { System.out.println("No AIMLIF Files found. Looking for AIML"); cnt = addCategoriesFromAIML(); } } Category b = new Category( 0, "PROGRAM VERSION", "*", "*", MagicStrings.program_name_version, "update.aiml"); brain.addCategory(b); brain.nodeStats(); learnfGraph.nodeStats(); }
/** * write certain specified categories as AIMLIF files * * @param graph the Graphmaster containing the categories to write * @param file the destination AIMLIF file */ public void writeCertainIFCategories(Graphmaster graph, String file) { if (MagicBooleans.trace_mode) System.out.println( "writeCertainIFCaegories " + file + " size= " + graph.getCategories().size()); writeIFCategories(graph.getCategories(), file + MagicStrings.aimlif_file_suffix); File dir = new File(aimlif_path); dir.setLastModified(new Date().getTime()); }
public void deleteLearnCategories() { ArrayList<Category> learnCategories = learnGraph.getCategories(); for (Category c : learnCategories) { Nodemapper n = brain.findNode(c); System.out.println("Found node " + n + " for " + c.inputThatTopic()); if (n != null) n.category = null; } learnGraph = new Graphmaster(this); }
/** Write all AIML files. Adds categories for BUILD and DEVELOPMENT ENVIRONMENT */ public void writeAIMLFiles() { if (MagicBooleans.trace_mode) System.out.println("writeAIMLFiles"); HashMap<String, BufferedWriter> fileMap = new HashMap<String, BufferedWriter>(); Category b = new Category(0, "BRAIN BUILD", "*", "*", new Date().toString(), "update.aiml"); brain.addCategory(b); // b = new Category(0, "PROGRAM VERSION", "*", "*", MagicStrings.program_name_version, // "update.aiml"); // brain.addCategory(b); ArrayList<Category> brainCategories = brain.getCategories(); Collections.sort(brainCategories, Category.CATEGORY_NUMBER_COMPARATOR); for (Category c : brainCategories) { if (!c.getFilename().equals(MagicStrings.null_aiml_file)) try { // System.out.println("Writing "+c.getCategoryNumber()+" "+c.inputThatTopic()); BufferedWriter bw; String fileName = c.getFilename(); if (fileMap.containsKey(fileName)) bw = fileMap.get(fileName); else { String copyright = Utilities.getCopyright(this, fileName); bw = new BufferedWriter(new FileWriter(aiml_path + "/" + fileName)); fileMap.put(fileName, bw); bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" + "<aiml>\n"); bw.write(copyright); // bw.newLine(); } bw.write(Category.categoryToAIML(c) + "\n"); // bw.newLine(); } catch (Exception ex) { ex.printStackTrace(); } } Set set = fileMap.keySet(); for (Object aSet : set) { BufferedWriter bw = fileMap.get(aSet); // Close the bw try { if (bw != null) { bw.write("</aiml>\n"); bw.flush(); bw.close(); } } catch (IOException ex) { ex.printStackTrace(); } } File dir = new File(aiml_path); dir.setLastModified(new Date().getTime()); }
/** * read categories from specified AIMLIF file into specified graph * * @param graph Graphmaster to store categories * @param fileName file name of AIMLIF file */ public int readCertainIFCategories(Graphmaster graph, String fileName) { int cnt = 0; try { Alice.getContext() .getAssets() .open(aimlif_path + "/" + fileName + MagicStrings.aimlif_file_suffix); ArrayList<Category> certainCategories = readIFCategories(aimlif_path + "/" + fileName + MagicStrings.aimlif_file_suffix); for (Category d : certainCategories) graph.addCategory(d); cnt = certainCategories.size(); System.out.println( "readCertainIFCategories " + cnt + " categories from " + fileName + MagicStrings.aimlif_file_suffix); } catch (IOException e) { System.out.println("Problem loading " + fileName); e.printStackTrace(); } return cnt; }
/** Write all AIMLIF files from bot brain */ public void writeAIMLIFFiles() { if (MagicBooleans.trace_mode) System.out.println("writeAIMLIFFiles"); HashMap<String, BufferedWriter> fileMap = new HashMap<String, BufferedWriter>(); Category b = new Category(0, "BRAIN BUILD", "*", "*", new Date().toString(), "update.aiml"); brain.addCategory(b); ArrayList<Category> brainCategories = brain.getCategories(); Collections.sort(brainCategories, Category.CATEGORY_NUMBER_COMPARATOR); for (Category c : brainCategories) { try { BufferedWriter bw; File aimlifFile; String fileName = c.getFilename(); if (fileMap.containsKey(fileName)) bw = fileMap.get(fileName); else { aimlifFile = new File(aimlif_path + "/" + fileName + MagicStrings.aimlif_file_suffix); aimlifFile.getParentFile().mkdirs(); bw = new BufferedWriter(new FileWriter(aimlifFile)); fileMap.put(fileName, bw); } bw.write(Category.categoryToIF(c)); bw.newLine(); } catch (Exception ex) { ex.printStackTrace(); } } Set set = fileMap.keySet(); for (Object aSet : set) { BufferedWriter bw = fileMap.get(aSet); // Close the bw try { if (bw != null) { bw.flush(); bw.close(); } } catch (IOException ex) { ex.printStackTrace(); } } File dir = new File(aimlif_path); dir.setLastModified(new Date().getTime()); }
/** * read categories from specified AIMLIF file into specified graph * * @param graph Graphmaster to store categories * @param fileName file name of AIMLIF file */ public int readCertainIFCategories(Graphmaster graph, String fileName) { int cnt = 0; File file = new File(aimlif_path + "/" + fileName + MagicStrings.aimlif_file_suffix); if (file.exists()) { try { ArrayList<Category> certainCategories = readIFCategories(aimlif_path + "/" + fileName + MagicStrings.aimlif_file_suffix); for (Category d : certainCategories) graph.addCategory(d); cnt = certainCategories.size(); System.out.println( "readCertainIFCategories " + cnt + " categories from " + fileName + MagicStrings.aimlif_file_suffix); } catch (Exception iex) { System.out.println("Problem loading " + fileName); iex.printStackTrace(); } } else System.out.println( "No " + aimlif_path + "/" + fileName + MagicStrings.aimlif_file_suffix + " file found"); return cnt; }
/** * Gets the match result from the Graphmaster. * * @param input * @param that * @param topic * @param userid * @param botid * @param parser * @param timeout whether to control the match attempt with a timeout thread */ private static String getMatchResult( String input, String that, String topic, String userid, String botid, TemplateParser parser) { // Always show the input path (in any case, if match trace is on). if (SHOW_MATCH_TRACE) { Trace.userinfo( PredicateMaster.get(Globals.getClientNamePredicate(), userid, botid) + '>' + SPACE + input + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + that + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + topic + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + botid); } // Create a case-insensitive pattern-fitted version of the input. String inputIgnoreCase = InputNormalizer.patternFitIgnoreCase(input); Match match = null; try { match = Graphmaster.match(InputNormalizer.patternFitIgnoreCase(input), that, topic, botid); } catch (NoMatchException e) { Log.userinfo(e.getMessage(), Log.CHAT); return EMPTY_STRING; } if (match == null) { Log.userinfo("No match found for input \"" + input + "\".", Log.CHAT); return EMPTY_STRING; } if (SHOW_MATCH_TRACE) { Trace.userinfo(LABEL_MATCH + match.getPath()); Trace.userinfo(LABEL_FILENAME + QUOTE_MARK + match.getFileName() + QUOTE_MARK); } ArrayList stars = match.getInputStars(); if (stars.size() > 0) { parser.setInputStars(stars); } stars = match.getThatStars(); if (stars.size() > 0) { parser.setThatStars(stars); } stars = match.getTopicStars(); if (stars.size() > 0) { parser.setTopicStars(stars); } String template = match.getTemplate(); String reply = null; try { reply = parser.processResponse(template); } catch (ProcessorException e) { // Log the error message. Log.userinfo(e.getMessage(), Log.ERROR); // Set response to empty string. return EMPTY_STRING; } // Record activation, if targeting is in use. // Needs review in light of multi-bot update if (USE_TARGETING) { Nodemapper matchNodemapper = match.getNodemapper(); if (matchNodemapper == null) { Trace.devinfo("Match nodemapper is null!"); } else { Set activations = (Set) matchNodemapper.get(Graphmaster.ACTIVATIONS); if (activations == null) { activations = new HashSet(); } String path = match.getPath() + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + inputIgnoreCase + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + that + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + topic + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + botid + SPACE + Graphmaster.PATH_SEPARATOR + SPACE + reply; if (!activations.contains(path)) { activations.add(path); match.getNodemapper().put(Graphmaster.ACTIVATIONS, activations); Graphmaster.activatedNode(match.getNodemapper()); } } } return reply; }
/** * Gets the list of replies to some input sentences. Assumes that the sentences have already had * all necessary pre-processing and substitutions performed. * * @param sentenceList the input sentences * @param userid the userid requesting the replies * @param botid * @return the list of replies to the input sentences */ private static ArrayList getReplies(ArrayList sentenceList, String userid, String botid) { // All replies will be assembled in this ArrayList. ArrayList replies = new ArrayList(sentenceList.size()); // Get the requested bot. Bot bot = Bots.getBot(botid); // Ready the that and topic predicates for constructing the match path. ArrayList thatSentences = bot.sentenceSplit(PredicateMaster.get(THAT, 1, userid, botid)); String that = InputNormalizer.patternFitIgnoreCase((String) thatSentences.get(thatSentences.size() - 1)); if (that.equals(EMPTY_STRING) || that.equals(PREDICATE_EMPTY_DEFAULT)) { that = ASTERISK; } String topic = InputNormalizer.patternFitIgnoreCase(PredicateMaster.get(TOPIC, userid, botid)); if (topic.equals(EMPTY_STRING) || topic.equals(PREDICATE_EMPTY_DEFAULT)) { topic = ASTERISK; } Iterator sentences = sentenceList.iterator(); // We might use this to track matching statistics. long time = 0; // If match trace info is on, mark the time just before matching starts. if (SHOW_MATCH_TRACE) { time = System.currentTimeMillis(); } // Get a reply for each sentence. while (sentences.hasNext()) { replies.add(getReply((String) sentences.next(), that, topic, userid, botid)); } // Increment the (static) response count. responseCount++; ; // If match trace info is on, produce statistics about the response time. if (SHOW_MATCH_TRACE) { // Mark the time that processing is finished. time = System.currentTimeMillis() - time; // Calculate the average response time. totalTime += time; avgResponseTime = (float) totalTime / (float) responseCount; Trace.userinfo( RESPONSE_SPACE + responseCount + SPACE_IN_SPACE + time + MS_AVERAGE + avgResponseTime + MS); } // Invoke targeting if appropriate. if (responseCount % TARGET_SKIP == 0) { if (USE_TARGETING) { Graphmaster.checkpoint(); } } // If no replies, return an empty string. if (replies.size() == 0) { replies.add(EMPTY_STRING); } return replies; }