#NEWLINE# def intertextuality( texts = [ ] , n = 5 , weight = ( lambda ngram : 1.0 ) , **kwargs ) : #NEWLINE# #INDENT# ' Returns a dictionary of (i, j) => float.\n        For indices i and j in the given list of texts,\n        the corresponding float is the percentage of text i that is also in text j.\n        Overlap is measured by matching n-grams (by default, 5 successive words).\n        An optional weight function can be used to supply the weight of each n-gram.\n    ' #NEWLINE# map = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# sum = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( i , txt ) in enumerate ( texts ) : #NEWLINE# #INDENT# for ( j , ngram ) in enumerate ( ngrams ( txt , n , ** kwargs ) ) : #NEWLINE# #INDENT# if ( ngram not in map ) : #NEWLINE# #INDENT# map [ ngram ] = set ( ) #NEWLINE# #UNINDENT# map [ ngram ] . add ( i ) #NEWLINE# sum [ i ] = ( sum . get ( i , 0 ) + weight ( ngram ) ) #NEWLINE# #UNINDENT# #UNINDENT# w = defaultdict ( Weight ) #NEWLINE# for ngram in map : #NEWLINE# #INDENT# for i in texts [ ngram ] : #NEWLINE# #INDENT# for j in map [ ngram ] : #NEWLINE# #INDENT# if ( i != j ) : #NEWLINE# #INDENT# if ( ( i , j ) not in w ) : #NEWLINE# #INDENT# w [ ( i , j ) ] = Weight ( 0.0 ) #NEWLINE# #UNINDENT# w [ ( i , j ) ] += weight ( ngram ) #NEWLINE# w [ ( i , j ) ] . assessments . add ( ngram ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# for ( i , j ) in w : #NEWLINE# #INDENT# w [ ( i , j ) ] /= float ( sum [ i ] ) #NEWLINE# w [ ( i , j ) ] = min ( w [ ( i , j ) ] , Weight ( 1.0 ) ) #NEWLINE# #UNINDENT# return w