1- using JetBrains . Annotations ;
2- using Novus . Utilities ;
3- using SmartImage . Lib . Engines ;
4- using SmartImage . Lib . Searching ;
5- using SmartImage . Lib . Utilities ;
6- using System ;
7- using System . Collections . Concurrent ;
1+ using System ;
82using System . Collections . Generic ;
9- using System . ComponentModel ;
103using System . Diagnostics ;
11- using System . IO ;
124using System . Linq ;
13- using System . Runtime . CompilerServices ;
14- using System . Threading ;
155using System . Threading . Tasks ;
16- using Kantan . Diagnostics ;
17- using Kantan . Net ;
18- using Kantan . Utilities ;
6+ using JetBrains . Annotations ;
7+ using Novus . Utilities ;
8+ using SmartImage . Lib . Engines ;
199using SmartImage . Lib . Engines . Model ;
10+ using SmartImage . Lib . Searching ;
2011using SmartImage . Lib . Upload ;
12+ using SmartImage . Lib . Utilities ;
2113using static Kantan . Diagnostics . LogCategories ;
2214
2315// ReSharper disable CognitiveComplexity
@@ -166,25 +158,21 @@ public async Task RunSearchAsync()
166158 Trace . WriteLine ( $ "{ nameof ( SearchClient ) } : Search complete", C_SUCCESS ) ;
167159
168160
169- var args2 = new SearchCompletedEventArgs ( )
161+ var args = new SearchCompletedEventArgs
170162 {
171- Results = Results ,
172- Best = new Lazy < ImageResult > ( ( ) => GetDetailedResults ( ) . FirstOrDefault ( ) ) ,
163+ Results = Results ,
164+ Detailed = new Lazy < ImageResult > ( ( ) => GetDetailedResults ( ) . FirstOrDefault ( ) ) ,
173165 Direct = new Lazy < ImageResult [ ] > ( ( ) =>
174166 {
175- if ( Config . Notification && Config . NotificationImage ) {
176-
177- Debug . WriteLine ( $ "Finding direct results") ;
178- var direct = GetDirectResults ( ) ;
179-
180- return direct ;
181- }
167+ Debug . WriteLine ( "Finding direct results" ) ;
168+ var direct = GetDirectResults ( ) ;
182169
183- return null ;
184- } )
170+ return direct ;
171+ } ) ,
172+ FirstDirect = new Lazy < ImageResult > ( GetDirectResult )
185173 } ;
186174
187- SearchCompleted ? . Invoke ( null , args2 ) ;
175+ SearchCompleted ? . Invoke ( null , args ) ;
188176 }
189177
190178 #endregion
@@ -202,8 +190,7 @@ public async Task RefineSearchAsync()
202190
203191 Debug . WriteLine ( "Finding best result" ) ;
204192
205- var best = GetDirectResults ( )
206- . FirstOrDefault ( f => ImageHelper . IsDirect ( f . Direct . ToString ( ) , DirectImageCriterion . Binary ) ) ;
193+ var best = GetDirectResult ( ) ;
207194
208195 if ( best == null ) {
209196 throw new SmartImageException ( ERR_NO_BEST_RESULT ) ;
@@ -237,6 +224,37 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
237224 return res ;
238225 }
239226
227+ [ CanBeNull ]
228+ public ImageResult GetDirectResult ( )
229+ {
230+
231+ // var best = FindBestResults().ToList();
232+ /*var best = Results.Where(r => r.IsNonPrimitive)
233+ .Where(r => r.Engine.SearchType.HasFlag(EngineSearchType.Image))
234+ .AsParallel()
235+ .OrderByDescending(r => r.PrimaryResult.Similarity)
236+ .ThenByDescending(r => r.PrimaryResult.PixelResolution)
237+ .ThenByDescending(r => r.PrimaryResult.DetailScore)
238+ .SelectMany(r =>
239+ {
240+ var x = r.OtherResults;
241+ x.Insert(0, r.PrimaryResult);
242+ return x;
243+ })
244+ .ToList();*/
245+
246+ var best = RefineFilter ( r => r . IsNonPrimitive
247+ && r . Engine . ResultType . HasFlag ( EngineResultType . Image ) ) . ToList ( ) ;
248+
249+ var images = best . Where ( x => x . CheckDirect ( DirectImageCriterion . Regex ) )
250+ . Take ( 10 )
251+ . AsParallel ( )
252+ . FirstOrDefault ( x => x . CheckDirect ( DirectImageCriterion . Binary ) ) ;
253+
254+
255+ return images ;
256+ }
257+
240258 public ImageResult [ ] GetDirectResults ( int count = 5 )
241259 {
242260
@@ -260,9 +278,12 @@ public ImageResult[] GetDirectResults(int count = 5)
260278
261279 Debug . WriteLine ( $ "{ nameof ( SearchClient ) } : Found { best . Count } best results", C_DEBUG ) ;
262280
263- var images1 = best . Where ( x => x . CheckDirect ( DirectImageCriterion . Regex ) ) . Take ( count * 2 ) ;
264-
265- var images = images1 . Where ( x => x . CheckDirect ( DirectImageCriterion . Binary ) ) . Take ( count ) . ToList ( ) ;
281+ var images = best . Where ( x => x . CheckDirect ( DirectImageCriterion . Regex ) )
282+ . Take ( count * 2 )
283+ . AsParallel ( )
284+ . Where ( x => x . CheckDirect ( DirectImageCriterion . Binary ) )
285+ . Take ( count )
286+ . ToList ( ) ;
266287
267288 Debug . WriteLine ( $ "{ nameof ( SearchClient ) } : Found { images . Count } direct results", C_DEBUG ) ;
268289
@@ -334,7 +355,16 @@ public sealed class SearchCompletedEventArgs : EventArgs
334355 public Lazy < ImageResult [ ] > Direct { get ; internal set ; }
335356
336357 [ CanBeNull ]
337- public Lazy < ImageResult > Best { get ; internal set ; }
358+ public Lazy < ImageResult > FirstDirect { get ; internal set ; }
359+
360+ [ CanBeNull ]
361+ public List < Lazy < ImageResult > > xDirect { get ; internal set ; }
362+
363+
364+ [ CanBeNull ]
365+ public Lazy < ImageResult > Detailed { get ; internal set ; }
366+
367+ // todo: maybe lazy list? i.e., each item is a lazy load
338368 }
339369
340370 public sealed class ResultCompletedEventArgs : EventArgs
0 commit comments