@@ -55,6 +55,8 @@ public SearchClient(SearchConfig config)
5555 /// Contains search results
5656 /// </summary>
5757 public List < SearchResult > Results { get ; }
58+
59+
5860
5961 /// <summary>
6062 /// Contains filtered search results
@@ -164,26 +166,34 @@ public async Task RunSearchAsync()
164166 var args = new SearchCompletedEventArgs
165167 {
166168 Results = Results ,
167- Detailed = new Lazy < ImageResult > ( ( ) => GetDetailedResults ( ) . FirstOrDefault ( ) ) ,
169+ Detailed = new Lazy < ImageResult > ( ( ) => GetDetailedImageResults ( ) . FirstOrDefault ( ) ) ,
168170 Direct = new Lazy < ImageResult [ ] > ( ( ) =>
169171 {
170172 Debug . WriteLine ( $ "{ nameof ( SearchClient ) } : Finding direct results", C_DEBUG ) ;
171- ImageResult [ ] direct = GetDirectResults ( ) ;
173+ ImageResult [ ] direct = GetDirectImageResults ( ) ;
172174
173175 return direct ;
174176 } ) ,
175- FirstDirect = new Lazy < ImageResult > ( GetDirectResult )
177+ FirstDirect = new Lazy < ImageResult > ( GetDirectImageResult )
176178 } ;
177179
178180 SearchCompleted ? . Invoke ( null , args ) ;
179181 }
180182
183+
184+ /*
185+ * TODO
186+ *
187+ * Queue a thread to run in the background upon each result completion
188+ * in which the thread scans for direct images, instead of doing the scanning post hoc
189+ */
190+
181191 #endregion
182192
183193 #region Secondary operations
184194
185195 /// <summary>
186- /// Refines search results by searching with the most-detailed result (<see cref="GetDirectResult " />).
196+ /// Refines search results by searching with the most-detailed result (<see cref="GetDirectImageResult " />).
187197 /// </summary>
188198 public async Task RefineSearchAsync ( )
189199 {
@@ -193,7 +203,7 @@ public async Task RefineSearchAsync()
193203
194204 Debug . WriteLine ( $ "{ nameof ( SearchClient ) } : Finding best result", C_DEBUG ) ;
195205
196- var directResult = GetDirectResult ( ) ;
206+ var directResult = GetDirectImageResult ( ) ;
197207
198208 if ( directResult == null ) {
199209 throw new SmartImageException ( "Could not find best result" ) ;
@@ -228,47 +238,32 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
228238 }
229239
230240 [ CanBeNull ]
231- public ImageResult GetDirectResult ( ) => GetDirectResults ( 1 ) ? . FirstOrDefault ( ) ;
241+ public ImageResult GetDirectImageResult ( ) => GetDirectImageResults ( 1 ) ? . FirstOrDefault ( ) ;
232242
233- public ImageResult [ ] GetDirectResults ( int count = 5 )
243+ public ImageResult [ ] GetDirectImageResults ( int count = 5 )
234244 {
245+ var imageResults = RefineFilter ( DirectFilterPredicate ) . ToList ( ) ;
235246
236- // var best = FindBestResults().ToList();
237- /*var best = Results.Where(r => r.IsNonPrimitive)
238- .Where(r => r.Engine.SearchType.HasFlag(EngineSearchType.Image))
239- .AsParallel()
240- .OrderByDescending(r => r.PrimaryResult.Similarity)
241- .ThenByDescending(r => r.PrimaryResult.PixelResolution)
242- .ThenByDescending(r => r.PrimaryResult.DetailScore)
243- .SelectMany(r =>
244- {
245- var x = r.OtherResults;
246- x.Insert(0, r.PrimaryResult);
247- return x;
248- })
249- .ToList();*/
250-
251- var results = RefineFilter ( r => DetailPredicate ( r )
252- && r . Engine . SearchType . HasFlag ( EngineSearchType . Image ) ) . ToList ( ) ;
253-
254- Debug . WriteLine ( $ "{ nameof ( SearchClient ) } : Found { results . Count } best results", C_DEBUG ) ;
247+ Debug . WriteLine ( $ "{ nameof ( SearchClient ) } : Found { imageResults . Count } best results", C_DEBUG ) ;
255248
256249 const int i = 10 ;
257250
258- var query = results . Where ( x => x . CheckDirect ( DirectImageCriterion . Regex ) )
251+ var query = imageResults . Where ( x => x . CheckDirect ( DirectImageCriterion . Regex ) )
259252 . Take ( i )
260253 . AsParallel ( ) ;
261254
262255 List < ImageResult > images ;
263256
264- if ( count == 1 ) {
257+ if ( count == 1 )
258+ {
265259 images = new List < ImageResult >
266260 {
267261 query . FirstOrDefault ( x => x . CheckDirect ( DirectImageCriterion . Binary ) )
268262 } ;
269263
270264 }
271- else {
265+ else
266+ {
272267 images = query . Where ( x => x . CheckDirect ( DirectImageCriterion . Binary ) )
273268 . Take ( count )
274269 // .OrderByDescending(r => r.Similarity)
@@ -284,7 +279,7 @@ public ImageResult[] GetDirectResults(int count = 5)
284279 /// Selects the most detailed results.
285280 /// </summary>
286281 /// <returns>The <see cref="ImageResult" />s of the best <see cref="Results" /></returns>
287- public ImageResult [ ] GetDetailedResults ( ) => RefineFilter ( DetailPredicate ) . ToArray ( ) ;
282+ public ImageResult [ ] GetDetailedImageResults ( ) => RefineFilter ( DetailPredicate ) . ToArray ( ) ;
288283
289284 public IEnumerable < ImageResult > RefineFilter ( Predicate < SearchResult > predicate )
290285 {
@@ -334,6 +329,9 @@ public static BaseSearchEngine[] GetAllSearchEngines()
334329 private static readonly Predicate < SearchResult > DetailPredicate = r => r . IsNonPrimitive ;
335330
336331 private static readonly SmartImageException SearchException = new ( "Search must be completed" ) ;
332+
333+ private static readonly Predicate < SearchResult > DirectFilterPredicate = r => DetailPredicate ( r )
334+ && r . Engine . SearchType . HasFlag ( EngineSearchType . Image ) ;
337335 }
338336
339337 public sealed class SearchCompletedEventArgs : EventArgs
0 commit comments