SkyAphid

Memboric Class

May 20th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. public class Memboric implements Serializable{
  2.    
  3.     /**
  4.      *
  5.      * The Memboric Core stores the various databases the AI needs to function.
  6.      *
  7.      * Essentially acting as a subconcious.
  8.      *
  9.      * @author Brayden McKinney
  10.      * @date 5/14/2013
  11.      */
  12.  
  13.     private static final long serialVersionUID = 4477889114653605232L;
  14.    
  15.     ArrayList<String> knownWords = new ArrayList<String>();
  16.     HashMap<String, Word> thesaurus = new HashMap<String, Word>();
  17.     ArrayList<ConversationLog> conversations = new ArrayList<ConversationLog>();
  18.    
  19.     File location = null;
  20.    
  21.     public void addWord(Word word){
  22.         if (!knownWords.contains(word.getName())){
  23.             knownWords.add(word.getName());
  24.             thesaurus.put(word.getName(), word);
  25.         }
  26.     }
  27.    
  28.     public Word getWord(String name){
  29.         return getWord(name, Brain.get().tagWord(name));
  30.     }
  31.    
  32.     public Word getWord(String name, String tag){
  33.         Word word = thesaurus.get(name.toLowerCase());
  34.         WikiParser parser = Brain.get().getParser();
  35.        
  36.         if (word == null){
  37.             word = new Word(name);
  38.             addWord(word);
  39.            
  40.             word.addTag(tag);
  41.            
  42.             if (word.tagsConsistOf("N") || word.tagsConsistOf("J") || word.tagsConsistOf("V")){
  43.                 ArrayList<String> synonyms = parser.getSynonyms(word.getName());
  44.                
  45.                 if (synonyms != null){
  46.                     for (int a = 0; a < synonyms.size(); a++){
  47.                         Word get = getWord(synonyms.get(a));
  48.                        
  49.                         get.addSynonym(word);
  50.                         word.addSynonym(get);
  51.                     }
  52.                 }
  53.             }
  54.            
  55.         }
  56.        
  57.         word.addTag(tag);
  58.        
  59.         return word;
  60.     }
  61.    
  62.     public void addConversation(ConversationLog log){
  63.         conversations.add(log);
  64.     }
  65.    
  66.     public ArrayList<String> getKnownWords(){
  67.         return knownWords;
  68.     }
  69.    
  70.     public boolean save(){
  71.         if (location == null){
  72.             location = new File("memboric.core");
  73.         }
  74.        
  75.         return save(location);
  76.     }
  77.  
  78.     public static Memboric load(File location){
  79.         Memboric memboric;
  80.        
  81.         try{
  82.             FileInputStream file = new FileInputStream(location);
  83.             ObjectInputStream oo = new ObjectInputStream(file);
  84.             memboric = (Memboric) oo.readObject();
  85.             oo.close();
  86.             System.out.println("Memboric Core loaded and mounted successfully.");
  87.             return memboric;
  88.         }catch(FileNotFoundException fn){
  89.             System.err.println("Failed to load Memboric Core.");
  90.         }catch(InvalidClassException ic){
  91.             System.err.println("Memboric Core is incompatible.");
  92.         }catch(Exception e){
  93.             e.printStackTrace();
  94.         }
  95.        
  96.         System.err.println("Creating new Memboric Core.");
  97.         return new Memboric();
  98.     }
  99.    
  100.     public boolean save(File location){
  101.         this.location = location;
  102.        
  103.         try{
  104.             FileOutputStream file = new FileOutputStream(location);
  105.             ObjectOutputStream oo = new ObjectOutputStream(file);
  106.             oo.writeObject(this);
  107.             oo.close();
  108.             System.out.println("Memboric Core saved successfully.");
  109.             return true;
  110.         }catch(FileNotFoundException ex){
  111.             try{
  112.                 if (location.createNewFile()) save(location);
  113.             }catch(IOException exx){
  114.                 exx.printStackTrace();
  115.             }
  116.         }catch(Exception e){
  117.             e.printStackTrace();
  118.         }
  119.        
  120.         return false;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment