#NEWLINE# def build_coocurrence_matrix( sentences ) : #NEWLINE# #INDENT# dictionary = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# rows = [ ] #NEWLINE# cols = [ ] #NEWLINE# data = array . array ( 'f' ) #NEWLINE# window = 10 #NEWLINE# for sentence in sentences : #NEWLINE# #INDENT# for ( i , first_word ) in enumerate ( sentence ) : #NEWLINE# #INDENT# first_word_idx = dictionary . setdefault ( first_word , len ( dictionary ) ) #NEWLINE# for ( j , second_word ) in enumerate ( sentence [ i : ( ( i + window ) + 1 ) ] ) : #NEWLINE# #INDENT# second_word_idx = dictionary . setdefault ( second_word , len ( dictionary ) ) #NEWLINE# distance = j #NEWLINE# if ( first_word_idx == second_word_idx ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( first_word_idx < second_word_idx ) : #NEWLINE# #INDENT# rows . append ( first_word_idx ) #NEWLINE# cols . append ( second_word_idx ) #NEWLINE# data . append ( ( np . float32 ( 1.0 ) / distance ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# rows . append ( second_word_idx ) #NEWLINE# cols . append ( first_word_idx ) #NEWLINE# data . append ( ( np . float32 ( 1.0 ) / distance ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return sp . coo_matrix ( ( data , ( rows , cols ) ) , shape = ( len ( dictionary ) , len ( dictionary ) ) , dtype = np . float32 ) . tocsr ( ) . tocoo ( )