#NEWLINE# def get_BoW( seed ) : #NEWLINE# #INDENT# nObsTotal = 0 #NEWLINE# doc_range = np . zeros ( ( D , 2 ) ) #NEWLINE# nUniqueEntry = 0 #NEWLINE# true_td = np . zeros ( ( K , D ) ) #NEWLINE# PRNG = np . random . RandomState ( seed ) #NEWLINE# WCD = list ( ) #NEWLINE# unif = ( np . ones ( K ) / K ) #NEWLINE# for d in xrange ( D ) : #NEWLINE# #INDENT# td_weights = ( PRNG . multinomial ( 3 , unif ) + 0.001 ) #NEWLINE# true_td [ : , d ] = ( td_weights / td_weights . sum ( ) ) #NEWLINE# Npercomp = PRNG . multinomial ( Nperdoc , true_td [ : , d ] ) #NEWLINE# temp_word_count = defaultdict ( int ) #NEWLINE# for k in xrange ( K ) : #NEWLINE# #INDENT# wordCounts = PRNG . multinomial ( Npercomp [ k ] , true_tw [ k , : ] ) #NEWLINE# for ( wordID , count ) in enumerate ( wordCounts ) : #NEWLINE# #INDENT# if ( count == 0 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# temp_word_count [ wordID ] += count #NEWLINE# nObsTotal += count #NEWLINE# #UNINDENT# #UNINDENT# nDistinctEntry = len ( temp_word_count ) #NEWLINE# WCD . append ( temp_word_count ) #NEWLINE# doc_range [ ( d , 0 ) ] = nUniqueEntry #NEWLINE# doc_range [ ( d , 1 ) ] = ( nUniqueEntry + nDistinctEntry ) #NEWLINE# nUniqueEntry += nDistinctEntry #NEWLINE# #UNINDENT# word_id = np . zeros ( nUniqueEntry ) #NEWLINE# word_count = np . zeros ( nUniqueEntry ) #NEWLINE# ii = 0 #NEWLINE# for d in xrange ( D ) : #NEWLINE# #INDENT# for ( key , value ) in WCD [ d ] . iteritems ( ) : #NEWLINE# #INDENT# word_id [ ii ] = int ( key ) #NEWLINE# word_count [ ii ] = value #NEWLINE# ii += 1 #NEWLINE# #UNINDENT# #UNINDENT# myDict = defaultdict ( ) #NEWLINE# myDict [ 'true_tw' ] = true_tw #NEWLINE# myDict [ 'true_td' ] = true_td #NEWLINE# myDict [ 'true_K' ] = K #NEWLINE# myDict [ 'doc_range' ] = doc_range #NEWLINE# myDict [ 'word_id' ] = word_id #NEWLINE# myDict [ 'word_count' ] = word_count #NEWLINE# myDict [ 'vocab_size' ] = V #NEWLINE# return myDict