def bm25_weight(X, K1=<NUM_LIT:100>, B=<NUM_LIT>):
<EOL>X = coo_matrix(X)<EOL>N = float(X.shape[<NUM_LIT:0>])<EOL>idf = log(N) - log1p(bincount(X.col))<EOL>row_sums = numpy.ravel(X.sum(axis=<NUM_LIT:1>))<EOL>average_length = row_sums.mean()<EOL>length_norm = (<NUM_LIT:1.0> - B) + B * row_sums / average_length<EOL>X.data = X.data * (K1 + <NUM_LIT:1.0>) / (K1 * length_norm[X.row] + X.data) * idf[X.col]<EOL>return X<EOL>
Weighs each row of a sparse matrix X  by BM25 weighting