def log_ratio_measure(segmented_topics, per_topic_postings, num_docs, normalize=False):
 m_lr = []
 for s_i in segmented_topics:
  for (w_prime, w_star) in s_i:
   w_prime_docs = per_topic_postings[w_prime]
   w_star_docs = per_topic_postings[w_star]
   co_docs = w_prime_docs.intersection(w_star_docs)
   if normalize:
    numerator = log_ratio_measure([[(w_prime, w_star)]], per_topic_postings, num_docs)[0]
    co_doc_prob = (len(co_docs) / float(num_docs))
    m_lr_i = (numerator / (- np.log((co_doc_prob + EPSILON))))
   else:
    numerator = ((len(co_docs) / float(num_docs)) + EPSILON)
    denominator = ((len(w_prime_docs) / float(num_docs)) * (len(w_star_docs) / float(num_docs)))
    m_lr_i = np.log((numerator / denominator))
   m_lr.append(m_lr_i)
 return m_lr

def log_ratio_measure ( segmented_topics , per_topic_postings , num_docs , normalize = False ) : m_lr = [ ] for s_i in segmented_topics : for ( w_prime , w_star ) in s_i : w_prime_docs = per_topic_postings [ w_prime ] w_star_docs = per_topic_postings [ w_star ] co_docs = w_prime_docs . intersection ( w_star_docs ) if normalize : numerator = log_ratio_measure ( [ [ ( w_prime , w_star ) ] ] , per_topic_postings , num_docs ) [ 0 ] co_doc_prob = ( len ( co_docs ) / float ( num_docs ) ) m_lr_i = ( numerator / ( - np . log ( ( co_doc_prob + EPSILON ) ) ) ) else : numerator = ( ( len ( co_docs ) / float ( num_docs ) ) + EPSILON ) denominator = ( ( len ( w_prime_docs ) / float ( num_docs ) ) * ( len ( w_star_docs ) / float ( num_docs ) ) ) m_lr_i = np . log ( ( numerator / denominator ) ) m_lr . append ( m_lr_i ) return m_lr
if normalize=false: popularly known as pmi .