for issue 26

pull/21/merge
Sun Junyi 12 years ago
parent 981d58e106
commit d58402c8f6

@ -7,6 +7,7 @@ import time
import tempfile import tempfile
import marshal import marshal
from math import log from math import log
import random
FREQ = {} FREQ = {}
total =0.0 total =0.0
@ -50,13 +51,14 @@ if load_from_cache_fail:
FREQ = dict([(k,log(float(v)/total)) for k,v in FREQ.iteritems()]) #normalize FREQ = dict([(k,log(float(v)/total)) for k,v in FREQ.iteritems()]) #normalize
min_freq = min(FREQ.itervalues()) min_freq = min(FREQ.itervalues())
print >> sys.stderr, "dumping model to file cache" print >> sys.stderr, "dumping model to file cache"
marshal.dump((trie,FREQ,total,min_freq),open(cache_file+".tmp",'wb')) tmp_suffix = "."+str(random.random())
marshal.dump((trie,FREQ,total,min_freq),open(cache_file+tmp_suffix,'wb'))
if os.name=='nt': if os.name=='nt':
import shutil import shutil
replace_file = shutil.move replace_file = shutil.move
else: else:
replace_file = os.rename replace_file = os.rename
replace_file(cache_file+".tmp",cache_file) replace_file(cache_file+tmp_suffix,cache_file)
print >> sys.stderr, "loading model cost ", time.time() - t1, "seconds." print >> sys.stderr, "loading model cost ", time.time() - t1, "seconds."
print >> sys.stderr, "Trie has been built succesfully." print >> sys.stderr, "Trie has been built succesfully."

Loading…
Cancel
Save