video_recommender.py 845 Bytes
from gensim.models import Word2Vec, KeyedVectors
import gensim.downloader as api
import numpy as np


def recommend_videos(tags, tag_model_path, video_model_path, top_k):
    tag_vectors = Word2Vec.load(tag_model_path).wv
    # tag_vectors = KeyedVectors.load_word2vec_format(tag_model_path, binary=True)
    # tag_vectors = api.load(tag_model_path)
    video_vectors = Word2Vec().wv.load(video_model_path)
    error_tags = []

    video_vector = np.zeros(100)
    for (tag, weight) in tags:
        if tag in tag_vectors.vocab:
            video_vector = video_vector + (tag_vectors[tag] * float(weight))
        else:
            # Pass if tag is unknown
            if tag not in error_tags:
                error_tags.append(tag)

    similar_ids = [x[0] for x in video_vectors.similar_by_vector(video_vector, top_k)]
    return similar_ids