<s> import numpy as np <EOL> import grequests <EOL> def _softmax ( x ) : <EOL> e_x = np . exp ( x - np . max ( x ) ) <EOL> out = e_x / e_x . sum ( ) <EOL> return out <EOL> def _softmax_2d ( x ) : <EOL> y = x - x . max ( axis = <NUM_LIT:1> , keepdims = True ) <EOL> np . exp ( y , out = y ) <EOL> y /= y . sum ( axis = <NUM_LIT:1> , keepdims = True ) <EOL> return y <EOL> def prob_words ( context , vocab , temperature = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> dot = np . dot ( vocab , context ) <EOL> prob = _softmax ( dot / temperature ) <EOL> return prob <EOL> def prepare_topics ( weights , factors , word_vectors , vocab , temperature = <NUM_LIT:1.0> , <EOL> doc_lengths = None , term_frequency = None , normalize = False ) : <EOL> """<STR_LIT>""" <EOL> topic_to_word = [ ] <EOL> msg = "<STR_LIT>" <EOL> assert len ( vocab ) == word_vectors . shape [ <NUM_LIT:0> ] , msg <EOL> if normalize : <EOL> word_vectors /= np . linalg . norm ( word_vectors , axis = <NUM_LIT:1> ) [ : , None ] <EOL> for factor_vector in factors : <EOL> factor_to_word = prob_words ( factor_vector , word_vectors , <EOL> temperature = temperature ) <EOL> topic_to_word . append ( np . ravel ( factor_to_word ) ) <EOL> topic_to_word = np . array ( topic_to_word ) <EOL> msg = "<STR_LIT>" <EOL> assert np . allclose ( np . sum ( topic_to_word , axis = <NUM_LIT:1> ) , <NUM_LIT:1> ) , msg <EOL> doc_to_topic = _softmax_2d ( weights ) <EOL> msg = "<STR_LIT>" <EOL> assert np . allclose ( np . sum ( doc_to_topic , axis = <NUM_LIT:1> ) , <NUM_LIT:1> ) , msg <EOL> data = { '<STR_LIT>' : topic_to_word , <EOL> '<STR_LIT>' : doc_to_topic , <EOL> '<STR_LIT>' : doc_lengths , <EOL> '<STR_LIT>' : vocab , <EOL> '<STR_LIT>' : term_frequency } <EOL> return data <EOL> def print_top_words_per_topic ( data , top_n = <NUM_LIT:10> , do_print = True ) : <EOL> """<STR_LIT>""" <EOL> msgs = [ ] <EOL> for j , topic_to_word in enumerate ( data [ '<STR_LIT>' ] ) : <EOL> top = np . argsort ( topic_to_word ) [ : : - <NUM_LIT:1> ] [ : top_n ] <EOL> prefix = "<STR_LIT>" % j <EOL> msg = '<STR_LIT:U+0020>' . join ( [ data [ '<STR_LIT>' ] [ i ] . strip ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> for i in top ] ) <EOL> if do_print : <EOL> print prefix + msg <EOL> msgs . append ( msg ) <EOL> return msgs <EOL> def topic_coherence ( lists , services = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) : <EOL> """<STR_LIT>""" <EOL> url = u'<STR_LIT>' <EOL> reqs = [ url . format ( s , '<STR_LIT>' . join ( top ) ) for s in services for top in lists ] <EOL> topic_coherence = grequests . map ( ( grequests . get ( r ) for r in reqs ) ) <EOL> return list ( float ( t . text ) for t in topic_coherence ) </s>
