mirror of
https://github.com/fxsjy/jieba.git
synced 2025-07-10 00:01:33 +08:00
make cache dumping more robust
This commit is contained in:
parent
38b6bcd54e
commit
1a3be67691
@ -81,15 +81,20 @@ def initialize(*args):
|
|||||||
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 " + cache_file
|
print >> sys.stderr, "dumping model to file cache " + cache_file
|
||||||
tmp_suffix = "."+str(random.random())
|
try:
|
||||||
with open(cache_file+tmp_suffix,'wb') as temp_cache_file:
|
tmp_suffix = "."+str(random.random())
|
||||||
marshal.dump((trie,FREQ,total,min_freq),temp_cache_file)
|
with open(cache_file+tmp_suffix,'wb') as temp_cache_file:
|
||||||
if os.name=='nt':
|
marshal.dump((trie,FREQ,total,min_freq),temp_cache_file)
|
||||||
import shutil
|
if os.name=='nt':
|
||||||
replace_file = shutil.move
|
import shutil
|
||||||
else:
|
replace_file = shutil.move
|
||||||
replace_file = os.rename
|
else:
|
||||||
replace_file(cache_file+tmp_suffix,cache_file)
|
replace_file = os.rename
|
||||||
|
replace_file(cache_file+tmp_suffix,cache_file)
|
||||||
|
except:
|
||||||
|
print >> sys.stderr, "dump cache file failed."
|
||||||
|
import traceback
|
||||||
|
print >> sys.stderr, traceback.format_exc()
|
||||||
|
|
||||||
initialized = True
|
initialized = True
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user