fxsjy/jieba — pull request #40 by hermanschaaf (2013-04-25 12:52:27+00:00)
Title: Feature: Lazy initialization and custom dictionaries

This pull request adds some basic functionality to jieba so that you can do things like the following:

jieba.set_dictionary('data/dict.txt.big')

or

jieba.initialize('data/dict.txt.small')

and lazy-loads the trie by default. This means that the trie only gets built the first time it is needed (not necessarily when jieba is imported). This lends more flexibility to decide when you want to spend the 2-3 seconds necessary for initialization (likely there are cases in your code where jieba isn't necessary and you don't need to build the trie at all).

In the latest version of [mafan](https://github.com/hermanschaaf/mafan), I use it like this, for example:

if settings.TRADITIONAL_DICT:
print "Using traditional dictionary..."
_curpath=os.path.normpath( os.path.join( os.getcwd(), os.path.dirname(__file__) ) )
if os.path.exists(os.path.join(_curpath, 'data/dict.txt.big')):
jieba.set_dictionary('data/dict.txt.big')
else:
print "Warning: TRADITIONAL_DICT is enabled in settings, but the dictionary has not yet been downloaded. \n\nYou might want to try running download_data.py"