|
| 1 | +--- |
| 2 | +icon: ranking-list-fill |
| 3 | +shortTitle: Non-personalized |
| 4 | +--- |
| 5 | +# Non-personalized Recommenders |
| 6 | + |
| 7 | +Gorse recommender system offers two non-personalized recommenders: the latest recommender and the popular recommender. However, they are not flexible enough. To improve the flexibility of non-personalized recommendations, Gorse provides a new implementation of non-personalized recommenders. |
| 8 | + |
| 9 | +## Configuration |
| 10 | + |
| 11 | +Non-personalized recommenders will traverse each item and its feedback, using expressions provided by the user to filtering and scoring. The configuration of a non-personalized recommender consists of three fields: |
| 12 | +- `name` is the name of the recommender. |
| 13 | +- `score` is the scoring function, represented in [Expr](https://expr-lang.org/) language, requiring output typed floating point or integer. Items are sorted in descending order. |
| 14 | +- `filter` is the filtering function, represented in [Expr](https://expr-lang.org/) language, requiring output typed boolean. |
| 15 | + |
| 16 | +::: tip |
| 17 | +If you want to sort items in ascending order, you need to add a negative sign yourself. |
| 18 | +::: |
| 19 | + |
| 20 | +In expressions, the `item` variable represents the current item while `feedback` variable represents positive feedback to the current item. |
| 21 | + |
| 22 | +The type of `item` is the `Item` structure, and the syntax for accessing fields is consistent with Go language. For example, `item.Timestamp` will return the timestamp of the item. |
| 23 | + |
| 24 | +```go |
| 25 | +type Item struct { |
| 26 | + ItemId string |
| 27 | + IsHidden bool |
| 28 | + Categories []string |
| 29 | + Timestamp time.Time |
| 30 | + Labels any |
| 31 | + Comment string |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The type of `feedback` is `[]Feedback`, a slice of `Feedback`. The syntax for accessing the array is consistent with the Go language. For example, `feedback[0].FeedbackKey.FeedbackType` will return the feedback type of the first positive feedback. |
| 36 | + |
| 37 | +```go |
| 38 | +type Feedback struct { |
| 39 | + FeedbackType string |
| 40 | + UserId string |
| 41 | + ItemId string |
| 42 | + Timestamp time.Time |
| 43 | + Comment string |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +[Expr](https://expr-lang.org/) language provides a lot of built-in functions that are sufficient to write more complex calculation logic. In addition to built-in functions that handle single variables such as mathematical operators, comparison operators, logical operators, etc., it also provides many array-related built-in functions. To learn more about syntax please visit [Expr's official documentation](https://expr-lang.org/docs/language-definition). |
| 48 | + |
| 49 | +## API |
| 50 | + |
| 51 | +You can access non-personalized recommendations through the following API: |
| 52 | + |
| 53 | +```bash |
| 54 | +curl http://localhost:8087/api/non-personalized/<name> |
| 55 | +``` |
| 56 | + |
| 57 | +## Examples |
| 58 | + |
| 59 | +### Latest Items |
| 60 | + |
| 61 | +You can sort the latest items directly by using timestamps. |
| 62 | + |
| 63 | +```toml |
| 64 | +[[recommend.non-personalized]] |
| 65 | +name = "latest" |
| 66 | +score = "item.Timestamp.Unix()" |
| 67 | +``` |
| 68 | + |
| 69 | +Since `item.Timestamp` is a `time.Time` struct and cannot be used directly for sorting, it needs to call the `Unix()` method to convert it into `int64` type. |
| 70 | + |
| 71 | +### Most Liked Items Updated within the Last Week |
| 72 | + |
| 73 | +Use filter to exclude items from more than a week ago, and calculate the number of likes as the sorting score. |
| 74 | + |
| 75 | +```toml |
| 76 | +[[recommend.non-personalized]] |
| 77 | +name = "most_liked_weekly" |
| 78 | +score = "count(feedback, .FeedbackType == 'like')" |
| 79 | +filter = "(now() - item.Timestamp).Hours() < 24 * 7" |
| 80 | +``` |
| 81 | + |
| 82 | +The `count` function will calculate the length of the feedback slice, the second parameter indicates that it only counts when the feedback type is 'like'. The `now()` function gets the current time, subtracts item time and requires that the number of hours be within a week (24*7 hours). |
| 83 | + |
| 84 | +### Hacker News |
| 85 | + |
| 86 | +It is said that Hacker News uses the following formula to calculate scores for content ranking: |
| 87 | + |
| 88 | +$$ |
| 89 | +\frac{p-1}{(T+2)^G} |
| 90 | +$$ |
| 91 | + |
| 92 | +where $p$ is the number of upvotes for the content, $T$ is the time from posting to score calculation, and $G$ is the gravity value used to control the rate at which content popularity decays over time. If you set $G$ to 0.5, you can add the following configuration item: |
| 93 | + |
| 94 | +```toml |
| 95 | +[[recommend.non-personalized]] |
| 96 | +name = "trending" |
| 97 | +score = "(count(feedback, .FeedbackType == 'upvote')-1)/((now() - item.Timestamp).Seconds()+2)^0.5" |
| 98 | +``` |
0 commit comments