@@ -3,6 +3,7 @@ package models.input
3
3
import models .input .ListItemType .ListItemType
4
4
import models .spellings .{CanonicalSpelling , CanonicalSpellingWithAlternatives }
5
5
import play .api .libs .json .{Format , Json , OFormat }
6
+ import services .RulesUsage
6
7
7
8
object ListItemType extends Enumeration {
8
9
type ListItemType = Value
@@ -18,12 +19,30 @@ case class ListItem(id: String,
18
19
synonyms : Seq [String ] = Seq .empty,
19
20
tags : Seq [InputTag ] = Seq .empty,
20
21
comment : String = " " ,
21
- additionalTermsForSearch : Seq [String ] = Seq .empty)
22
+ additionalTermsForSearch : Seq [String ] = Seq .empty,
23
+ usageFrequency : Option [Int ] = None )
22
24
23
25
object ListItem {
24
- def create (searchInputs : Seq [SearchInputWithRules ], spellings : Seq [CanonicalSpellingWithAlternatives ]): Seq [ListItem ] = {
26
+
27
+ def create (searchInputs : Seq [SearchInputWithRules ],
28
+ spellings : Seq [CanonicalSpellingWithAlternatives ],
29
+ optRuleUsageStatistics : Option [Seq [RulesUsage ]]): Seq [ListItem ] = {
25
30
val listItems = listItemsForRules(searchInputs) ++ listItemsForSpellings(spellings)
26
- listItems.sortBy(_.term.trim.toLowerCase.replace(" \" " , " " ))
31
+ // augment with usage statistics, only if available, pass through otherwise
32
+ val listItemsWithUsageStatistics = optRuleUsageStatistics match {
33
+ case Some (rulesUsage) if rulesUsage.isEmpty => listItems
34
+ case Some (ruleUsage) => augmentRulesWithUsage(listItems, ruleUsage)
35
+ case None => listItems
36
+ }
37
+ listItemsWithUsageStatistics.sortBy(_.term.trim.toLowerCase.replace(" \" " , " " ))
38
+ }
39
+
40
+ private def augmentRulesWithUsage (listItems : Seq [ListItem ], ruleUsage : Seq [RulesUsage ]): Seq [ListItem ] = {
41
+ // there can be multiple rule usage items for the the same rule, one per keyword combination that triggered the usage
42
+ val combinedRuleUsageFrequency = ruleUsage.groupBy(_.inputId.id).view.mapValues(_.map(_.frequency).sum)
43
+ listItems.map { listItem =>
44
+ listItem.copy(usageFrequency = combinedRuleUsageFrequency.get(listItem.id))
45
+ }
27
46
}
28
47
29
48
private def listItemsForRules (searchInputs : Seq [SearchInputWithRules ]): Seq [ListItem ] = {
0 commit comments