write save_phrase_index
authorPeng Wu <alexepico@gmail.com>
Mon, 21 May 2012 06:56:50 +0000 (14:56 +0800)
committerPeng Wu <alexepico@gmail.com>
Mon, 21 May 2012 06:56:50 +0000 (14:56 +0800)
utils/storage/gen_binary_files.cpp
utils/storage/import_interpolation.cpp
utils/training/gen_ngram.cpp
utils/training/gen_unigram.cpp
utils/utils_helper.h

index 4519064..eeed911 100644 (file)
@@ -98,15 +98,8 @@ int main(int argc, char * argv[]){
 
     phrase_index.compat();
 
-    new_chunk = new MemoryChunk;
-    phrase_index.store(1, new_chunk);
-    new_chunk->save("gb_char.bin");
-    phrase_index.load(1, new_chunk);
+    if (!save_phrase_index(&phrase_index))
+        exit(ENOENT);
 
-    new_chunk = new MemoryChunk;
-    phrase_index.store(2, new_chunk);
-    new_chunk->save("gbk_char.bin");
-    phrase_index.load(2, new_chunk);
-    
     return 0;
 }
index 9d8f4bd..5325b95 100644 (file)
@@ -251,15 +251,8 @@ int main(int argc, char * argv[]){
 
     taglib_fini();
 
-    chunk = new MemoryChunk;
-    phrase_index.store(1, chunk);
-    chunk->save("gb_char.bin");
-    phrase_index.load(1, chunk);
-
-    chunk = new MemoryChunk;
-    phrase_index.store(2, chunk);
-    chunk->save("gbk_char.bin");
-    phrase_index.load(2, chunk);
+    if (!save_phrase_index(&phrase_index))
+        exit(ENOENT);
 
     return 0;
 }
index 5721ce7..20b160c 100644 (file)
@@ -130,15 +130,8 @@ int main(int argc, char * argv[]){
 
     free(linebuf);
     
-    MemoryChunk * new_chunk = new MemoryChunk;
-    phrase_index.store(1, new_chunk);
-    new_chunk->save("gb_char.bin");
-    phrase_index.load(1, new_chunk);
-
-    new_chunk = new MemoryChunk;
-    phrase_index.store(2, new_chunk);
-    new_chunk->save("gbk_char.bin");
-    phrase_index.load(2, new_chunk);
+    if (!save_phrase_index(&phrase_index))
+        exit(ENOENT);
 
     return 0;
 }
index 470a60a..f94c214 100644 (file)
@@ -52,15 +52,8 @@ int main(int argc, char * argv[]){
     }
 #endif
 
-    MemoryChunk * new_chunk = new MemoryChunk;
-    phrase_index.store(1, new_chunk);
-    new_chunk->save("gb_char.bin");
-    phrase_index.load(1, new_chunk);
-
-    new_chunk = new MemoryChunk;
-    phrase_index.store(2, new_chunk);
-    new_chunk->save("gbk_char.bin");
-    phrase_index.load(2, new_chunk);
+    if (!save_phrase_index(&phrase_index))
+        exit(ENOENT);
 
     return 0;
 }
index 59286db..2f143c9 100644 (file)
@@ -33,7 +33,7 @@ static bool load_phrase_index(FacadePhraseIndex * phrase_index) {
         chunk = new MemoryChunk;
         bool retval = chunk->load(bin_file);
         if (!retval) {
-            fprintf(stderr, "open %s failed!\n", bin_file);
+            fprintf(stderr, "load %s failed!\n", bin_file);
             return false;
         }
 
@@ -42,5 +42,24 @@ static bool load_phrase_index(FacadePhraseIndex * phrase_index) {
     return true;
 }
 
+static bool save_phrase_index(FacadePhraseIndex * phrase_index) {
+    MemoryChunk * new_chunk = NULL;
+    for (size_t i = 0; i < PHRASE_INDEX_LIBRARY_COUNT; ++i) {
+        const char * bin_file = pinyin_phrase_files[i];
+        if (NULL == bin_file)
+            continue;
+
+        new_chunk = new MemoryChunk;
+        phrase_index->store(i, new_chunk);
+        bool retval = new_chunk->save(bin_file);
+        if (!retval) {
+            fprintf(stderr, "save %s failed.", bin_file);
+            return false;
+        }
+
+        phrase_index->load(i, new_chunk);
+    }
+    return true;
+}
 
 #endif