Thread: [F-Script-talk] Uniformly distributing results using message patterns
Brought to you by:
pmougin
From: Sarat K. <sar...@gm...> - 2006-04-04 03:01:08
|
Hi Guys, A couple of weeks ago I was trying to solve this problem of distributing results from different categories & sites to a fixed number of rows. This is similar to the way Spotlight uniformly distributes the search results in the menu like display. So you see results from each category (Photos, Documents, Folders, Music, Mail, etc). The case I was trying to solve is a little complicated because each result has an additional attribute "Site" apart from the "Category". Here is what I like to do. Say I have 10 rows to display the seach results and I get 40 results that match the search criteria. I want to make sure I display results uniformly from each Category and then uniformly across Sites in each category. F-Script seemed like a perform fit for these kinds of problems. Here is the code I came up with so far [:results | categoryResults :=3D results at: @ ((results objectForKey:'Category') =3D @((results objectForKey:'Category') distinct)). categorySiteResults :=3D [:a| Sites:=3D (a objectForKey:'Site') distinct. a at: @((a objectForKey:'Site') =3D @Sites)] value: @categoryResults. addBlock :=3D [:container :source | container addObject:(source objectAtIndex:0). source removeObjectAtIndex:0]. alternatePickBlock :=3D [:a :b | container :=3D NSMutableArray array. [ ((a count) > 0) | ((b count) > 0) ] whileTrue: [(a count) > 0 ifTrue:[addBlock value:container value:a]. (b count) > 0 ifTrue:[addBlock value:container value:b] ]. container]. distributedCategoryResults :=3D categorySiteResults @ \ alternatePickBlock. distributedResults :=3D distributedCategoryResults \ alternatePickBlock. distributedResults ] The input argument to this block is the array of search results. Each result is a dictionary (the two important keys are Category and Site). Is there a better way to do this? Thanks. Regards Sarat |
From: Philippe M. <pm...@ac...> - 2006-04-08 15:52:27
|
Hi Sarat, At first glance, it seems that the way you construct a distributed =20 array from n undistributed ones using the pickBlock reduction will =20 not give you a perfect distribution (it gives a good one if n =3D 2, =20 but for n > 2 the elements are no longer uniformly distributed). Here is a version which I think gives a better distribution. [:results| categoryResults :=3D results at: @ ((results objectForKey:'Category') =3D = =20 @((results objectForKey:'Category') distinct)). categorySiteResults :=3D [:a| Sites:=3D (a objectForKey:'Site') = distinct. =20 a at: @((a objectForKey:'Site') =3D @Sites)] value: @categoryResults. distribute :=3D [:undistributed| pick :=3D [:a| a count > 0 ifTrue:[container add:(a at:0). a =20 removeAt:0]]. container :=3D {}. [undistributed @count =3D 0 \ #&] whileFalse:[pick =20 value:@undistributed]. container ]. distributedCategoryResults :=3D distribute value:@categorySiteResults. distributedResults :=3D distribute value:distributedCategoryResults. distributedResults ] Now, you can select the 10 first rows by doing: selected :=3D distributedResults at: 10 iota. You can get them sorted by category (and by site for each category) =20 by doing: selected :=3D selected at:(selected objectForKey:'Site') sort. selected :=3D selected at:(selected objectForKey:'Category') sort. -- -- Philippe Le 4 avr. 06 =E0 05:01, Sarat Kongara a =E9crit : > Hi Guys, > A couple of weeks ago I was trying to solve this problem of > distributing results from different categories & sites to a fixed > number of rows. This is similar to the way Spotlight uniformly > distributes the search results in the menu like display. So you see > results from each category (Photos, Documents, Folders, Music, Mail, > etc). The case I was trying to solve is a little complicated because > each result has an additional attribute "Site" apart from the > "Category". > > Here is what I like to do. Say I have 10 rows to display the seach > results and I get 40 results that match the search criteria. I want to > make sure I display results uniformly from each Category and then > uniformly across Sites in each category. > > F-Script seemed like a perform fit for these kinds of problems. > > Here is the code I came up with so far > > [:results | > categoryResults :=3D results at: @ ((results objectForKey:'Category') = =3D > @((results objectForKey:'Category') distinct)). > categorySiteResults :=3D [:a| Sites:=3D (a objectForKey:'Site') = distinct. > a at: @((a objectForKey:'Site') =3D @Sites)] value: @categoryResults. > addBlock :=3D [:container :source | container addObject:(source > objectAtIndex:0). source removeObjectAtIndex:0]. > alternatePickBlock :=3D [:a :b | container :=3D NSMutableArray array. = [ > ((a count) > 0) | ((b count) > 0) ] whileTrue: [(a count) > 0 > ifTrue:[addBlock value:container value:a]. (b count) > 0 > ifTrue:[addBlock value:container value:b] ]. container]. > distributedCategoryResults :=3D categorySiteResults @ \ =20 > alternatePickBlock. > distributedResults :=3D distributedCategoryResults \ = alternatePickBlock. > distributedResults > ] > > The input argument to this block is the array of search results. Each > result is a dictionary (the two important keys are Category and Site). > > Is there a better way to do this? Thanks. > > Regards > Sarat > > > ------------------------------------------------------- > This SF.Net email is sponsored by xPML, a groundbreaking scripting =20 > language > that extends applications into web and mobile media. Attend the =20 > live webcast > and join the prime developer group breaking into this new coding =20 > territory! > http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=110944&bid$1720&dat=121642= > _______________________________________________ > F-Script-talk mailing list > F-S...@li... > https://lists.sourceforge.net/lists/listinfo/f-script-talk > |