/* * Copyright (C) 2018 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 . */ #pragma once #include #include #include #include "database/Types.hpp" #include "som/DataNormalizer.hpp" #include "som/Network.hpp" #include "SimilarityFeaturesCache.hpp" #include "SimilarityFeaturesDefs.hpp" namespace Database { class Session; } namespace Similarity { using FeatureWeight = double; class FeaturesSearcher { public: using StopRequestedFunction = std::function; // return true if stop requested // Use cache FeaturesSearcher(Database::Session& session, FeaturesCache cache, StopRequestedFunction stopRequested); // Use training (may be very slow) struct TrainSettings { std::size_t iterationCount {10}; float sampleCountPerNeuron {4}; FeatureSettingsMap featureSettingsMap; }; FeaturesSearcher(Database::Session& session, const TrainSettings& trainSettings, StopRequestedFunction stopRequested = {}); bool isValid() const; bool isTrackClassified(Database::IdType trackId) const; bool isReleaseClassified(Database::IdType releaseId) const; bool isArtistClassified(Database::IdType artistId) const; std::vector getSimilarTracks(const std::set& tracksId, std::size_t maxCount) const; std::vector getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const; std::vector getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const; void dump(Database::Session& session, std::ostream& os) const; FeaturesCache toCache() const; private: using ObjectPositions = std::map>; void init(Database::Session& session, SOM::Network network, ObjectPositions tracksPosition, StopRequestedFunction stopRequested); std::vector getSimilarObjects(const std::set& ids, const SOM::Matrix>& objectsMap, const ObjectPositions& objectPosition, std::size_t maxCount) const; std::unique_ptr _network; double _networkRefVectorsDistanceMedian {}; SOM::Matrix> _artistsMap; ObjectPositions _artistPositions; SOM::Matrix> _releasesMap; ObjectPositions _releasePositions; SOM::Matrix> _tracksMap; ObjectPositions _trackPositions; }; } // ns Similarity