示例#1
0
def make_obama(ngram_order=3, select=None):
    sys.stderr.write('assembling obama...\n')
    model_fname = make_model_fname(ngram_order)
    model_loc = os.path.join(LANG_MODEL_DIR, model_fname)
    # if not os.path.exists(model_loc):
    #     print model_loc
    #     raise Exception('no {order}-gram model found for Obama)'.format(
    #         order=ngram_order))
    build_model(ngram_order)
    obama = Obama(model_loc)
    sys.stderr.write('loaded.\n')
    return obama
示例#2
0
def make_presidents(stats, ngram_order=3, select=None):
    sys.stderr.write('assembling presidents...\n')
    presidents = {}
    if isinstance(stats, basestring):
        _stat_file = open(stats)
    else:
        _stat_file = stats
    if select:
        stat_dicts = [sd for sd in json.load(_stat_file) if sd['id'] in select]
    else:
        stat_dicts = json.load(_stat_file)
    for stat_dict in stat_dicts:
        prez_id = stat_dict['id']
        sys.stderr.write(stat_dict['name']+'...')
        model_fname = make_model_fname(prez_id, ngram_order)
        model_loc = os.path.join(LANG_MODEL_DIR, model_fname)
        if os.path.exists(model_loc):
            presidents[prez_id] = President(os.path.join(LANG_MODEL_DIR, model_fname), stat_dict)
        else:
            print model_loc
            raise Exception('no {order}-gram model found for {prez_id} ({prez_name})'.format(
                order=ngram_order, prez_id = prez_id, prez_name = stat_dict['name']))
        sys.stderr.write('loaded.\n')
    return presidents
def pickle_model(ngram_model, prez_number):
    prez_number = unicode(prez_number).zfill(2)
    order = ngram_model._n
    model_fname = make_model_fname(prez_number, order)
    with open(os.path.join(LANG_MODEL_DIR, model_fname), 'wb') as modelfile:
        cPickle.dump(ngram_model, modelfile)
示例#4
0
def pickle_model(ngram_model):
    order = ngram_model._n
    model_fname = make_model_fname(order)
    # import ipdb; ipdb.set_trace()
    with open(os.path.join(LANG_MODEL_DIR, model_fname), 'wb') as modelfile:
        pickle.dump(ngram_model, modelfile)