From dc595edc50cb4de35be53f4c87229380956e2a68 Mon Sep 17 00:00:00 2001 From: ashilkn Date: Mon, 6 Nov 2023 10:30:44 +0530 Subject: [PATCH] added more smartness on deciding which sub descriptions of all descriptions to show --- lib/services/search_service.dart | 40 +++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/services/search_service.dart b/lib/services/search_service.dart index 5a5e0077d..ad7b4ab0a 100644 --- a/lib/services/search_service.dart +++ b/lib/services/search_service.dart @@ -1,3 +1,5 @@ +import "dart:math"; + import "package:flutter/cupertino.dart"; import 'package:logging/logging.dart'; import 'package:photos/core/event_bus.dart'; @@ -370,7 +372,7 @@ class SearchService { if (limit == null || distinctFullDescriptionCount < limit) { distinctFullDescriptionCount++; - final words = file.caption!.split(" "); + final words = file.caption!.trim().split(" "); orderedSubDescriptions.add({0: [], 1: []}); for (int i = 1; i <= words.length; i++) { @@ -422,13 +424,45 @@ class SearchService { } } - descriptionAndMatchingFiles.forEach((key, value) { + ///[relevantDescAndFiles] will be a filterd version of [descriptionAndMatchingFiles] + ///In [descriptionAndMatchingFiles], there will be descriptions with the same + ///set of matching files. These descriptions will be substrings of a full + ///description. [relevantDescAndFiles] will keep only the entry which has the + ///longest description among enties with matching set of files. + final relevantDescAndFiles = >{}; + while (descriptionAndMatchingFiles.isNotEmpty) { + final baseEntry = descriptionAndMatchingFiles.entries.first; + final descsWithSameFiles = >{}; + final baseUploadedFileIDs = + baseEntry.value.map((e) => e.uploadedFileID).toSet(); + + descriptionAndMatchingFiles.forEach((desc, files) { + final uploadedFileIDs = files.map((e) => e.uploadedFileID).toSet(); + + final hasSameFiles = uploadedFileIDs.containsAll(baseUploadedFileIDs) && + baseUploadedFileIDs.containsAll(uploadedFileIDs); + if (hasSameFiles) { + descsWithSameFiles.addAll({desc: files}); + } + }); + descriptionAndMatchingFiles + .removeWhere((desc, files) => descsWithSameFiles.containsKey(desc)); + final longestDescription = descsWithSameFiles.keys.reduce( + (desc1, desc2) => desc1.length > desc2.length ? desc1 : desc2, + ); + relevantDescAndFiles.addAll( + {longestDescription: descsWithSameFiles[longestDescription]!}, + ); + } + + relevantDescAndFiles.forEach((key, value) { searchResults.add( GenericSearchResult(ResultType.fileCaption, key, value.toList()), ); }); if (limit != null && distinctFullDescriptionCount >= limit) { - return (searchResults..shuffle()).sublist(0, limit); + return (searchResults..shuffle()) + .sublist(0, min(limit, searchResults.length)); } else { return searchResults; }