comparison src/core/anime_db.cc @ 254:d14f8e0e40c3

[UNFINISHED] *: update anime button
author Paper <paper@paper.us.eu.org>
date Wed, 07 Feb 2024 07:57:37 -0500
parents 4d461ef7d424
children 862d0d8619f6
comparison
equal deleted inserted replaced
253:b3549da699a6 254:d14f8e0e40c3
121 } 121 }
122 return id; 122 return id;
123 } 123 }
124 124
125 /* 125 /*
126 * This fairly basic algorithm is only in effect because 126 * TODO: separate this from the anime DB,
127 * there are some special cases, e.g. Another and Re:ZERO, where 127 * provide *some* sort of normalization
128 * we get the wrong match, so we have to create Advanced Techniques
129 * to solve this
130 *
131 * This algorithm:
132 * 1. searches each anime item for a match to the preferred title
133 * AND all synonyms and marks those matches with
134 * `synonym.length() - (synonym.find(needle) + needle.length());`
135 * which should never be less than zero and will be zero if, and only if
136 * the titles match exactly.
137 * 2. returns the id of the match that is the lowest, which will most
138 * definitely match anything that exactly matches the title of the
139 * filename
140 */ 128 */
141 int Database::GetAnimeFromTitle(const std::string& title) { 129 int Database::GetAnimeFromTitle(const std::string& title) {
142 if (title.empty()) 130 if (title.empty())
143 return 0; 131 return 0;
144 132
145 std::unordered_map<int, size_t> map;
146
147 static const auto process_title = [&map](const Anime& anime, const std::string& title, const std::string& needle) -> bool {
148 const std::string title_l = Strings::ToLower(title), needle_l = Strings::ToLower(needle);
149 size_t ret = title_l.find(needle_l);
150 if (ret == std::string::npos)
151 return false;
152
153 map[anime.GetId()] = title.length() - (ret + needle.length());
154 return true;
155 };
156
157 for (const auto& [id, anime] : items) { 133 for (const auto& [id, anime] : items) {
158 if (process_title(anime, anime.GetUserPreferredTitle(), title)) 134 if (anime.GetUserPreferredTitle() == title)
159 continue; 135 return id;
160 136
161 for (const auto& synonym : anime.GetTitleSynonyms()) 137 for (const auto& synonym : anime.GetTitleSynonyms())
162 if (process_title(anime, synonym, title)) 138 if (synonym == title)
163 continue; 139 return id;
164 } 140 }
165 141
166 return get_lowest_in_map(map); 142 return 0;
167 } 143 }
168 144
169 static bool GetListDataAsJSON(const Anime& anime, nlohmann::json& json) { 145 static bool GetListDataAsJSON(const Anime& anime, nlohmann::json& json) {
170 if (!anime.IsInUserList()) 146 if (!anime.IsInUserList())
171 return false; 147 return false;