text_html_output_1.png application_vnd.jupyter.stderr_output_1.png
from sklearn.metrics.pairwise import cosine_similarity

import numpy as np

import pandas as pd

import tensorflow_hub as hub

path = '../input/CORD-19-research-challenge/'
all_sources = pd.read_csv(path + 'metadata.csv')

all_sources.isna().sum()

def prepare_similarity(vectors):
    similarity = cosine_similarity(vectors)
    return similarity
def get_top_similar(sentence, sentence_list, similarity_matrix, topN):
    index = sentence_list.index(sentence)
    similarity_row = np.array(similarity_matrix[index, :])
    indices = similarity_row.argsort()[-topN:][::-1]
    return [(i, sentence_list[i]) for i in indices]

module_url = '../input/universalsentenceencoderlarge4'
embed = hub.load(module_url)

titles = all_sources['title'].fillna('Unknown')
embed_vectors = embed(titles[:100].values)['outputs'].numpy()
sentence_list = titles.values.tolist()
sentence = titles.iloc[5]
similarity_matrix = prepare_similarity(embed_vectors)
similar = get_top_similar(sentence, sentence_list, similarity_matrix, 6)

for sentence in similar:
    print(sentence)
    print('\n')