<reponame>Youguang-Zhou/67373-RecommSystem
import numpy as np
from joblib import load

NUM_RECOMM_VIDEOS = 12


# 读取本地测试结果的第一条数据（手动粘贴一下吧！）
input_title = "《黑暗之魂3 》艾利德修女boss战 ——来自高端玩家的教学视频"
input_cateName = "视频-游戏视频"
input_videoId = "c07bd7d0e4784013a234a62f653be295"
input_results = '''
[['《GTA5里等红绿灯的女司机》' '视频-游戏视频' '43d08001f09d474fb68e51d49b0a270b']
 ['《月入百万公交坐过站，隔桌明星一起吃过饭》' '视频-直播剪辑' '198004c6eb1d48f390617e6c405da385']
 ['《一封特别的信》' '视频-直播剪辑' '0b2f8dea47fa418780cd9e68d8f8f01c']
 ['《受邀参加英女王生日会！》' '视频-直播剪辑' 'd975f997a5f44961ae92195d00f4659e']
 ['《洗面奶的故事》' '视频-直播剪辑' '8669c160fc1046aebfd3cd29353fee2e']
 ['陈一发儿：油管直播终极计划——《格局》' '视频-直播剪辑' 'ea477760e11641049e7ca0f139aefd75']
 ['之前一曲不插电的《我们》， 送给各位毕业生' '视频-直播剪辑' '154aabf4d9dd49f6a3df2187ba5c6e6b']
 ['《一个鸡蛋的暴走》——上海徒步公益活动' '视频-日常' '8572790d11a3481a95e3b3634e4b7504']
 ['《白車过隙听过没？》' '视频-游戏视频' '4539d1ccb0244d1c808137077d7998d0']
 ['潜水艇小游戏' '视频-日常' '7e912121d1ac4e9b954c89a6697154ed']
 ['你比特币赚了多少？' '视频-直播剪辑' 'cf0d8273b34342269ea6d898a7d982dc']
 ['分享一些男性健康方面的小知识，不用谢。' '视频-直播剪辑' '5a342d4f3ccd4955a06d64501e4f8bbd']]
'''


def load_model(file_name):
    return load('models/{}'.format(file_name))


def load_dataset():
    f = open("dataset/dataset.txt", "r")
    lines = f.read().splitlines()
    dataset = []
    for line in lines:
        arr = line.split(',')
        dataset.append(arr)
    dataset = np.array(dataset)
    f.close()
    return dataset


def BoW(inputs, codebook):
    vec = [0] * len(codebook)
    indices = []
    if (type(inputs) == list):
        inputs = ''.join(inputs)
    for char in list(inputs):
        index = np.argwhere(codebook == char)
        if (index.shape == (1, 1)):
            vec[index[0][0]] = 1
            indices.append(index[0][0])
    return np.array(vec), np.array(indices)


dataset = load_dataset()
codebook = load_model('model_codebook')
som = load_model('model_som')
knn = load_model('model_knn')

vec = np.array(BoW([input_title, input_cateName], codebook)[0])
knn_results = knn.kneighbors([som.winner(vec)])[1][0]

recomms = [data[[0, 1, 5]] for data in dataset[knn_results]]
filters = np.array(
    list(filter(lambda data: data[2] != input_videoId, recomms)))

print(str(filters))
print(input_results)

import numpy as np
from joblib import load

NUM_RECOMM_VIDEOS = 12


# 读取本地测试结果的第一条数据（手动粘贴一下吧！）
input_title = "《黑暗之魂3 》艾利德修女boss战 ——来自高端玩家的教学视频"
input_cateName = "视频-游戏视频"
input_videoId = "c07bd7d0e4784013a234a62f653be295"
input_results = '''
[['《GTA5里等红绿灯的女司机》' '视频-游戏视频' '43d08001f09d474fb68e51d49b0a270b']
 ['《月入百万公交坐过站，隔桌明星一起吃过饭》' '视频-直播剪辑' '198004c6eb1d48f390617e6c405da385']
 ['《一封特别的信》' '视频-直播剪辑' '0b2f8dea47fa418780cd9e68d8f8f01c']
 ['《受邀参加英女王生日会！》' '视频-直播剪辑' 'd975f997a5f44961ae92195d00f4659e']
 ['《洗面奶的故事》' '视频-直播剪辑' '8669c160fc1046aebfd3cd29353fee2e']
 ['陈一发儿：油管直播终极计划——《格局》' '视频-直播剪辑' 'ea477760e11641049e7ca0f139aefd75']
 ['之前一曲不插电的《我们》， 送给各位毕业生' '视频-直播剪辑' '154aabf4d9dd49f6a3df2187ba5c6e6b']
 ['《一个鸡蛋的暴走》——上海徒步公益活动' '视频-日常' '8572790d11a3481a95e3b3634e4b7504']
 ['《白車过隙听过没？》' '视频-游戏视频' '4539d1ccb0244d1c808137077d7998d0']
 ['潜水艇小游戏' '视频-日常' '7e912121d1ac4e9b954c89a6697154ed']
 ['你比特币赚了多少？' '视频-直播剪辑' 'cf0d8273b34342269ea6d898a7d982dc']
 ['分享一些男性健康方面的小知识，不用谢。' '视频-直播剪辑' '5a342d4f3ccd4955a06d64501e4f8bbd']]
'''


def load_model(file_name):
    return load('models/{}'.format(file_name))


def load_dataset():
    f = open("dataset/dataset.txt", "r")
    lines = f.read().splitlines()
    dataset = []
    for line in lines:
        arr = line.split(',')
        dataset.append(arr)
    dataset = np.array(dataset)
    f.close()
    return dataset


def BoW(inputs, codebook):
    vec = [0] * len(codebook)
    indices = []
    if (type(inputs) == list):
        inputs = ''.join(inputs)
    for char in list(inputs):
        index = np.argwhere(codebook == char)
        if (index.shape == (1, 1)):
            vec[index[0][0]] = 1
            indices.append(index[0][0])
    return np.array(vec), np.array(indices)


dataset = load_dataset()
codebook = load_model('model_codebook')
som = load_model('model_som')
knn = load_model('model_knn')

vec = np.array(BoW([input_title, input_cateName], codebook)[0])
knn_results = knn.kneighbors([som.winner(vec)])[1][0]

recomms = [data[[0, 1, 5]] for data in dataset[knn_results]]
filters = np.array(
    list(filter(lambda data: data[2] != input_videoId, recomms)))

print(str(filters))
print(input_results)
# 读取本地测试结果的第一条数据（手动粘贴一下吧！） [['《GTA5里等红绿灯的女司机》' '视频-游戏视频' '43d08001f09d474fb68e51d49b0a270b']  ['《月入百万公交坐过站，隔桌明星一起吃过饭》' '视频-直播剪辑' '198004c6eb1d48f390617e6c405da385']  ['《一封特别的信》' '视频-直播剪辑' '0b2f8dea47fa418780cd9e68d8f8f01c']  ['《受邀参加英女王生日会！》' '视频-直播剪辑' 'd975f997a5f44961ae92195d00f4659e']  ['《洗面奶的故事》' '视频-直播剪辑' '8669c160fc1046aebfd3cd29353fee2e']  ['陈一发儿：油管直播终极计划——《格局》' '视频-直播剪辑' 'ea477760e11641049e7ca0f139aefd75']  ['之前一曲不插电的《我们》， 送给各位毕业生' '视频-直播剪辑' '154aabf4d9dd49f6a3df2187ba5c6e6b']  ['《一个鸡蛋的暴走》——上海徒步公益活动' '视频-日常' '8572790d11a3481a95e3b3634e4b7504']  ['《白車过隙听过没？》' '视频-游戏视频' '4539d1ccb0244d1c808137077d7998d0']  ['潜水艇小游戏' '视频-日常' '7e912121d1ac4e9b954c89a6697154ed']  ['你比特币赚了多少？' '视频-直播剪辑' 'cf0d8273b34342269ea6d898a7d982dc']  ['分享一些男性健康方面的小知识，不用谢。' '视频-直播剪辑' '5a342d4f3ccd4955a06d64501e4f8bbd']]