package sms.codec;

import sms.model.*;
import java.io.*;

public class TrieLoader {
	static String trieExtension = ".trie";
	static String trainingExtension = ".txt";
	int cutoff = 120;
	String basepath;
	public TrieLoader() {
		this("../data/training-iso");
	}
	public TrieLoader(String basepath) {
		this.basepath = basepath;
	}
	
	protected void createTrieFile() throws IOException {
		FileInputStream training = new FileInputStream(basepath + trainingExtension);
		Trie<Character, Dictionary> temp = new sms.builder.TrieBuilder().build(training, cutoff);
		
		FileOutputStream fos = new FileOutputStream(basepath + trieExtension);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		
		oos.writeObject(temp);
	}
	
	public Trie<Character, Dictionary> loadTrie() throws IOException {
		FileInputStream trieFile;
		while(true) {
			try {
				//try to load the serialized trie
				trieFile = new FileInputStream(basepath + trieExtension);
				
				ObjectInputStream ois = new ObjectInputStream(trieFile);
				
				return (Trie<Character, Dictionary>) (ois.readObject());
			} catch(Exception e) {
				System.err.println("trie needs to be rebuild...");
				createTrieFile();
				continue;
			}
		}
		
	}
}

@@ -50,16 +50,70 @@
 va.io.*;
+%0Aimport java.util.zip.*;%0Aimport java.util.Enumeration;
 %0A%0Apublic
@@ -387,35 +387,49 @@
 %09%0A%09p
-rotected void createTrieFil
+ublic Trie%3CCharacter, Dictionary%3E loadTri
 e() 
@@ -443,36 +443,32 @@
 IOException %7B%0A%09%09
-File
 InputStream trai
@@ -469,185 +469,100 @@
 m tr
-aining = new FileInputStream(basepath + trainingExtension);%0A%09%09Trie%3CCharacter, Dictionary%3E temp = new sms.builder.TrieBuilder().build(training, cutoff);
+ieFile;%0A%09%09try %7B //loading trie from file%0A%09%09%09try %7B //to load the serialized trie
 %0A%09%09
-%0A
 %09%09
+trie
 File
-OutputStream fos
  = n
@@ -568,19 +568,18 @@
 new File
-Out
+In
 putStrea
@@ -613,253 +613,215 @@
 ;%0A%09%09
-ObjectOutputStream oos = new ObjectOutputStream(fos);
+%09%7D catch(Exception exception) %7B
 %0A%09%09
-%0A
 %09%09
-oos.writeObject(temp);%0A%09%7D%0A%09%0A%09public Trie%3CCharacter, Dictionary%3E loadTrie() throws IOException %7B%0A%09%09FileInputStream trieFile;%0A%09%09while(true) %7B%0A%09%09%09try %7B%0A%09%09%09%09//try to load the serialized trie
+//see if we can load the zipped version instead%0A%09%09%09%09ZipFile zipfile = new ZipFile(basepath + trieExtension + %22.zip%22);%0A%09%09%09%09ZipEntry entry = zipfile.entries().nextElement();
 %0A%09%09%09
@@ -828,32 +828,35 @@
 %09trieFile = 
-new File
+zipfile.get
 InputStream(
@@ -859,43 +859,27 @@
 eam(
-basepath + trieExtension
+entry
 );%0A%09%09%09
-%09
+%7D
 %0A%09%09%09
+%0A%09%09
 %09Obj
@@ -939,14 +939,12 @@
 %0A%09%09%09
-%09
 %0A%09%09%09
-%09
 retu
@@ -994,25 +994,24 @@
 bject());%0A%09%09
-%09
 %7D catch(Exce
@@ -1021,17 +1021,16 @@
 on e) %7B%0A
-%09
 %09%09%09Syste
@@ -1075,59 +1075,392 @@
 d...
-%22);%0A%09%09%09%09createTrieFile();%0A%09%09%09%09continue
+ this may take a while%22);%0A%09%09%09FileInputStream training = new FileInputStream(basepath + trainingExtension);%0A%09%09%09Trie%3CCharacter, Dictionary%3E temp = new sms.builder.TrieBuilder().build(training, cutoff);%0A%09%09%09%0A%09%09%09FileOutputStream fos = new FileOutputStream(basepath + trieExtension);%0A%09%09%09ObjectOutputStream oos = new ObjectOutputStream(fos)
 ;%0A%09%09%09
-%7D
 %0A%09%09
-%7D%0A%09%09
+%09oos.writeObject(temp);%0A%09%09%09return temp;%0A%09%09%7D
 %0A%09%7D%0A

enable loading zipped trie files