/** * Save this HMM model to a file. * * @param filePath file path to save the model */ public void saveModel(String filePath) { File parentFile = new File(filePath).getParentFile(); if (parentFile != null && !parentFile.exists()) { parentFile.mkdirs(); } try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath)); oos.writeObject(new HMMModel(pi, A, B)); oos.close(); System.out.println("Model saved."); } catch (FileNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { e.printStackTrace(); } }
/** * Load an HMM model from a file. * * @param filePath file Path to load an HMM model from */ public void loadModel(String filePath) { System.out.println("Loading model..."); try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath)); HMMModel HMMModel = (HMMModel) ois.readObject(); N = HMMModel.N; M = HMMModel.M; pi = HMMModel.pi; A = HMMModel.A; B = HMMModel.B; ois.close(); System.out.println("Model loaded."); } catch (FileNotFoundException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }