示例#1
0
  /**
   * 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);
    }
  }
示例#2
0
  /**
   * 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();
  }
示例#3
0
  /** 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());
  }
示例#4
0
 /** 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());
 }
示例#5
0
 /**
  * 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;
 }