#NEWLINE# def ComputeTopicCooccurrences( self ) : #NEWLINE# #INDENT# self . logger . debug ( '    Computing topic cooccurrences...' ) #NEWLINE# matrix = [ ( [ 0.0 ] * self . topicCount ) for _ in range ( self . topicCount ) ] #NEWLINE# for ( docID , topicMixture ) in self . docsAndTopics . iteritems ( ) : #NEWLINE# #INDENT# for ( i , iValue ) in topicMixture . iteritems ( ) : #NEWLINE# #INDENT# for ( j , jValue ) in topicMixture . iteritems ( ) : #NEWLINE# #INDENT# matrix [ i ] [ j ] += ( iValue * jValue ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# normalization = ( ( 1.0 / self . docCount ) if ( self . docCount > 1.0 ) else 1.0 ) #NEWLINE# for i in range ( self . topicCount ) : #NEWLINE# #INDENT# for j in range ( self . topicCount ) : #NEWLINE# #INDENT# matrix [ i ] [ j ] *= normalization #NEWLINE# #UNINDENT# #UNINDENT# data = [ ] #NEWLINE# for ( i , row ) in enumerate ( matrix ) : #NEWLINE# #INDENT# for ( j , value ) in enumerate ( row ) : #NEWLINE# #INDENT# data . append ( { #NEWLINE# #INDENT# 'first_topic_index' : i , #NEWLINE# 'second_topic_index' : j , #NEWLINE# 'value' : value , #NEWLINE# 'rank' : 0 , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# #UNINDENT# data . sort ( key = ( lambda x : ( - x [ 'value' ] ) ) ) #NEWLINE# for ( rank , d ) in enumerate ( data ) : #NEWLINE# #INDENT# d [ 'rank' ] = ( rank + 1 ) #NEWLINE# #UNINDENT# data = data [ : matrix . maxCoTopicCount ] #NEWLINE# self . topicCooccurrences = data