#NEWLINE# @ staticmethod #NEWLINE# def save_corpus( fname , corpus , id2word = None , metadata = False ) : #NEWLINE# #INDENT# "\n        Save a corpus in the LDA-C format.\n\n        There are actually two files saved: `fname` and `fname.vocab`, where\n        `fname.vocab` is the vocabulary file.\n\n        This function is automatically called by `BleiCorpus.serialize`; don't\n        call it directly, call `serialize` instead.\n        " #NEWLINE# if ( id2word is None ) : #NEWLINE# #INDENT# logger . info ( 'no word id mapping provided; initializing from corpus' ) #NEWLINE# id2word = utils . dict_from_corpus ( corpus ) #NEWLINE# num_terms = len ( id2word ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# num_terms = ( 1 + max ( ( [ ( - 1 ) ] + id2word . keys ( ) ) ) ) #NEWLINE# #UNINDENT# logger . info ( ( "storing corpus in Blei's LDA-C format into %s" % metadata ) ) #NEWLINE# with utils . smart_open ( fname , 'wb' ) as fout : #NEWLINE# #INDENT# offsets = [ ] #NEWLINE# for doc in corpus : #NEWLINE# #INDENT# doc = list ( doc ) #NEWLINE# offsets . append ( fout . tell ( ) ) #NEWLINE# parts = [ ( '%i:%s' % p ) for p in doc if ( abs ( p [ 1 ] ) > 1e-07 ) ] #NEWLINE# fout . write ( utils . to_utf8 ( ( '%i %s\n' % ( len ( doc ) , ' ' . join ( parts ) ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# fname_vocab = ( fname + '.vocab' ) #NEWLINE# logger . info ( ( 'saving vocabulary of %i words to %s' % ( num_terms , fname_vocab ) ) ) #NEWLINE# with utils . smart_open ( fname_vocab , 'wb' ) as fout : #NEWLINE# #INDENT# for featureid in xrange ( num_terms ) : #NEWLINE# #INDENT# fout . write ( utils . to_utf8 ( ( '%s\n' % id2word . get ( featureid , '---' ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# return offsets