'Returns an op that runs an eval on a word similarity dataset.
The eval dataset is assumed to be tab-separated, one scored word pair per
line.  The resulting value is Spearman\'s rho of the human judgements with
the cosine similarity of the word embeddings.
Args:
filename: the filename containing the word similarity data.
Returns:
An operator that will compute Spearman\'s rho of the current row
embeddings.'

def wordsim_eval_op(self, filename):

    with open(filename, 'r') as fh:
        tuples = (line.strip().split(' DCTB ') for line in fh.read().splitlines())
        (word1s, word2s, sims) = zip(*tuples)
        actuals = map(float, sims)
    v1s_t = tf.nn.embedding_lookup(self.row_embedding, [self.row_word_to_ix.get(w, 0) for w in word1s])
    v2s_t = tf.nn.embedding_lookup(self.row_embedding, [self.row_word_to_ix.get(w, 0) for w in word2s])
    preds_t = tf.reduce_sum((tf.nn.l2_normalize(v1s_t, dim=1) * tf.nn.l2_normalize(v2s_t, dim=1)), axis=1)
    def _op(preds):
        (rho, _) = scipy.stats.spearmanr(preds, actuals)
        return rho
    return tf.py_func(_op, [preds_t], tf.float64)
