Now have a recommendation tool for the whole database

This commit is contained in:
emeric
2020-02-15 16:33:05 +01:00
parent cfb8bbe2ce
commit 6239064396
4 changed files with 23 additions and 25 deletions
@@ -0,0 +1,140 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <filesystem>
#include <iostream>
#include <stdexcept>
#include <stdlib.h>
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Db.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "utils/IConfig.hpp"
#include "utils/Service.hpp"
#include "utils/StreamLogger.hpp"
#include "recommendation/IEngine.hpp"
int main(int argc, char *argv[])
{
try
{
// log to stdout
ServiceProvider<Logger>::create<StreamLogger>(std::cout);
std::filesystem::path configFilePath {"/etc/lms.conf"};
if (argc >= 2)
configFilePath = std::string(argv[1], 0, 256);
ServiceProvider<IConfig>::assign(createConfig(configFilePath));
Database::Db db {ServiceProvider<IConfig>::get()->getPath("working-dir") / "lms.db"};
Database::Session session {db};
std::cout << "Creating recommendation engine..." << std::endl;
const auto engine {Recommendation::createEngine(session)};
std::cout << "DONE!" << std::endl;
const std::vector<Database::IdType> trackIds {[&]()
{
auto transaction {session.createSharedTransaction()};
return Database::Track::getAllIds(session);
}()};
std::cout << "*** Tracks (" << trackIds.size() << ") ***" << std::endl;
for (Database::IdType trackId : trackIds)
{
auto trackToString = [&](Database::IdType trackId)
{
std::string res;
auto transaction {session.createSharedTransaction()};
Database::Track::pointer track {Database::Track::getById(session, trackId)};
res += track->getName();
if (track->getRelease())
res += " [" + track->getRelease()->getName() + "]";
for (auto artist : track->getArtists())
res += " - " + artist->getName();
for (auto cluster : track->getClusters())
res += " {" + cluster->getType()->getName() + "-"+ cluster->getName() + "}";
return res;
};
std::cout << "Processing track '" << trackToString(trackId) << std::endl;
for (Database::IdType similarTrackId : engine->getSimilarTracks(session, {trackId}, 3))
std::cout << "\t- Similar track '" << trackToString(similarTrackId) << std::endl;
}
const std::vector<Database::IdType> releaseIds = std::invoke([&]()
{
auto transaction {session.createSharedTransaction()};
return Database::Release::getAllIds(session);
});
std::cout << "*** Releases ***" << std::endl;
for (Database::IdType releaseId : releaseIds)
{
auto releaseToString = [&](Database::IdType releaseId)
{
auto transaction {session.createSharedTransaction()};
Database::Release::pointer release {Database::Release::getById(session, releaseId)};
return release->getName();
};
std::cout << "Processing release '" << releaseToString(releaseId) << "'" << std::endl;
for (Database::IdType similarReleaseId : engine->getSimilarReleases(session, {releaseId}, 3))
std::cout << "\t- Similar release '" << releaseToString(similarReleaseId) << "'" << std::endl;
}
const std::vector<Database::IdType> artistIds = std::invoke([&]()
{
auto transaction {session.createSharedTransaction()};
return Database::Artist::getAllIds(session);
});
std::cout << "*** Artists ***" << std::endl;
for (Database::IdType artistId : artistIds)
{
auto artistToString = [&](Database::IdType artistId)
{
auto transaction {session.createSharedTransaction()};
Database::Artist::pointer artist {Database::Artist::getById(session, artistId)};
return artist->getName();
};
std::cout << "Processing artist '" << artistToString(artistId) << "'" << std::endl;
for (Database::IdType similarArtistId : engine->getSimilarArtists(session, {artistId}, 3))
std::cout << "\t- Similar artist '" << artistToString(similarArtistId) << "'" << std::endl;
}
}
catch( std::exception& e)
{
std::cerr << "Caught exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}