mirror of
https://github.com/fxsjy/jieba.git
synced 2025-07-10 00:01:33 +08:00
use logging instead of print
This commit is contained in:
parent
fd96527f71
commit
1cf3f0d00b
@ -13,6 +13,7 @@ from math import log
|
|||||||
import random
|
import random
|
||||||
import threading
|
import threading
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
import logging
|
||||||
|
|
||||||
DICTIONARY = "dict.txt"
|
DICTIONARY = "dict.txt"
|
||||||
DICT_LOCK = threading.RLock()
|
DICT_LOCK = threading.RLock()
|
||||||
@ -22,6 +23,7 @@ min_freq = 0.0
|
|||||||
total =0.0
|
total =0.0
|
||||||
user_word_tag_tab={}
|
user_word_tag_tab={}
|
||||||
initialized = False
|
initialized = False
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def gen_trie(f_name):
|
def gen_trie(f_name):
|
||||||
lfreq = {}
|
lfreq = {}
|
||||||
@ -43,8 +45,8 @@ def gen_trie(f_name):
|
|||||||
p = p[c]
|
p = p[c]
|
||||||
p['']='' #ending flag
|
p['']='' #ending flag
|
||||||
except ValueError, e:
|
except ValueError, e:
|
||||||
print >> sys.stderr, f_name, ' at line', lineno, line
|
logger.debug('%s at line %s %s' % (f_name, lineno, line))
|
||||||
raise e
|
raise ValueError, e
|
||||||
return trie, lfreq,ltotal
|
return trie, lfreq,ltotal
|
||||||
|
|
||||||
def initialize(*args):
|
def initialize(*args):
|
||||||
@ -62,7 +64,7 @@ def initialize(*args):
|
|||||||
_curpath=os.path.normpath( os.path.join( os.getcwd(), os.path.dirname(__file__) ) )
|
_curpath=os.path.normpath( os.path.join( os.getcwd(), os.path.dirname(__file__) ) )
|
||||||
|
|
||||||
abs_path = os.path.join(_curpath,dictionary)
|
abs_path = os.path.join(_curpath,dictionary)
|
||||||
print >> sys.stderr, "Building Trie..., from " + abs_path
|
logger.debug("Building Trie..., from %s" % abs_path)
|
||||||
t1 = time.time()
|
t1 = time.time()
|
||||||
if abs_path == os.path.join(_curpath,"dict.txt"): #defautl dictionary
|
if abs_path == os.path.join(_curpath,"dict.txt"): #defautl dictionary
|
||||||
cache_file = os.path.join(tempfile.gettempdir(),"jieba.cache")
|
cache_file = os.path.join(tempfile.gettempdir(),"jieba.cache")
|
||||||
@ -71,7 +73,7 @@ def initialize(*args):
|
|||||||
|
|
||||||
load_from_cache_fail = True
|
load_from_cache_fail = True
|
||||||
if os.path.exists(cache_file) and os.path.getmtime(cache_file)>os.path.getmtime(abs_path):
|
if os.path.exists(cache_file) and os.path.getmtime(cache_file)>os.path.getmtime(abs_path):
|
||||||
print >> sys.stderr, "loading model from cache " + cache_file
|
logger.debug("loading model from cache %s" % cache_file)
|
||||||
try:
|
try:
|
||||||
trie,FREQ,total,min_freq = marshal.load(open(cache_file,'rb'))
|
trie,FREQ,total,min_freq = marshal.load(open(cache_file,'rb'))
|
||||||
load_from_cache_fail = False
|
load_from_cache_fail = False
|
||||||
@ -82,7 +84,7 @@ def initialize(*args):
|
|||||||
trie,FREQ,total = gen_trie(abs_path)
|
trie,FREQ,total = gen_trie(abs_path)
|
||||||
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
|
logger.debug("dumping model to file cache %s" % cache_file)
|
||||||
try:
|
try:
|
||||||
tmp_suffix = "."+str(random.random())
|
tmp_suffix = "."+str(random.random())
|
||||||
with open(cache_file+tmp_suffix,'wb') as temp_cache_file:
|
with open(cache_file+tmp_suffix,'wb') as temp_cache_file:
|
||||||
@ -94,21 +96,20 @@ def initialize(*args):
|
|||||||
replace_file = os.rename
|
replace_file = os.rename
|
||||||
replace_file(cache_file+tmp_suffix,cache_file)
|
replace_file(cache_file+tmp_suffix,cache_file)
|
||||||
except:
|
except:
|
||||||
print >> sys.stderr, "dump cache file failed."
|
logger.error("dump cache file failed.")
|
||||||
import traceback
|
logger.exception("")
|
||||||
print >> sys.stderr, traceback.format_exc()
|
|
||||||
|
|
||||||
initialized = True
|
initialized = True
|
||||||
|
|
||||||
print >> sys.stderr, "loading model cost ", time.time() - t1, "seconds."
|
logger.debug("loading model cost %s seconds." % (time.time() - t1))
|
||||||
print >> sys.stderr, "Trie has been built succesfully."
|
logger.debug("Trie has been built succesfully.")
|
||||||
|
|
||||||
|
|
||||||
def require_initialized(fn):
|
def require_initialized(fn):
|
||||||
|
|
||||||
@wraps(fn)
|
@wraps(fn)
|
||||||
def wrapped(*args, **kwargs):
|
def wrapped(*args, **kwargs):
|
||||||
global initialized
|
global initialized
|
||||||
if initialized:
|
if initialized:
|
||||||
return fn(*args, **kwargs)
|
return fn(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user