algorithm.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package model
  2. import (
  3. "math"
  4. "time"
  5. )
  6. // Algorithm Algorithm
  7. type Algorithm interface {
  8. Score(stat *ReplyStat) *ReplyScore
  9. Slots() []int
  10. Name() string
  11. }
  12. /*
  13. Origin Algorithm.
  14. */
  15. // Origin origin algorithm
  16. type Origin struct {
  17. name string
  18. slots []int
  19. }
  20. // NewOrigin NewLikeDesc Aogorithm
  21. func NewOrigin(name string, slots []int) *Origin {
  22. return &Origin{
  23. name: name,
  24. slots: slots,
  25. }
  26. }
  27. // Name get name
  28. func (o *Origin) Name() string {
  29. return o.name
  30. }
  31. // Slots get slots
  32. func (o *Origin) Slots() []int {
  33. return o.slots
  34. }
  35. // Score calc score
  36. func (o *Origin) Score(stat *ReplyStat) (rs *ReplyScore) {
  37. rs = new(ReplyScore)
  38. rs.RpID = stat.RpID
  39. if stat.Like < 0 || stat.Reply < 0 || stat.Report < 0 || stat.Hate < 0 {
  40. return
  41. }
  42. score := int64(100 * ((stat.Like + 2) / (stat.Hate + 4 + stat.Report)))
  43. score = score<<32 | (int64(stat.Reply) & 0xFFFFFFFF)
  44. rs.Score = float64(score)
  45. return
  46. }
  47. /*
  48. LikeDesc Algorithm order by like desc.
  49. */
  50. // LikeDesc like desc
  51. type LikeDesc struct {
  52. name string
  53. slots []int
  54. }
  55. // NewLikeDesc NewLikeDesc Aogorithm
  56. func NewLikeDesc(name string, slots []int) *LikeDesc {
  57. return &LikeDesc{
  58. name: name,
  59. slots: slots,
  60. }
  61. }
  62. // Name get name
  63. func (l *LikeDesc) Name() string {
  64. return l.name
  65. }
  66. // Slots get slots
  67. func (l *LikeDesc) Slots() []int {
  68. return l.slots
  69. }
  70. // Score calc score
  71. func (l *LikeDesc) Score(stat *ReplyStat) (rs *ReplyScore) {
  72. rs = new(ReplyScore)
  73. rs.RpID = stat.RpID
  74. rs.Score = float64(stat.Like)
  75. return
  76. }
  77. /*
  78. WilsonLHRR wilson algorithm
  79. like,reply
  80. hate,report
  81. */
  82. // WilsonLHRR WilsonLHRR
  83. type WilsonLHRR struct {
  84. name string
  85. slots []int
  86. weight *WilsonLHRRWeight
  87. }
  88. // NewWilsonLHRR NewWilsonLHRR
  89. func NewWilsonLHRR(name string, slots []int, weight *WilsonLHRRWeight) *WilsonLHRR {
  90. return &WilsonLHRR{
  91. name: name,
  92. slots: slots,
  93. weight: weight,
  94. }
  95. }
  96. // Name get name
  97. func (w *WilsonLHRR) Name() string {
  98. return w.name
  99. }
  100. // Slots get slots
  101. func (w *WilsonLHRR) Slots() []int {
  102. return w.slots
  103. }
  104. // Score calc score
  105. func (w *WilsonLHRR) Score(stat *ReplyStat) (rs *ReplyScore) {
  106. rs = new(ReplyScore)
  107. rs.RpID = stat.RpID
  108. if stat.Like < 0 || stat.Reply < 0 || stat.Report < 0 || stat.Hate < 0 {
  109. return
  110. }
  111. ups := float64(stat.Like)*w.weight.Like + float64(stat.Reply)*w.weight.Reply
  112. downs := float64(stat.Hate)*w.weight.Hate + float64(stat.Report)*w.weight.Report
  113. n := ups + downs
  114. if n == 0 {
  115. return
  116. }
  117. z := float64(2)
  118. p := ups / n
  119. rs.Score = (p + math.Pow(z, 2)/(2*n) - (z/(2*n))*math.Sqrt(4*n*(1-p)*p+math.Pow(z, 2))) / (1 + math.Pow(z, 2)/n)
  120. return
  121. }
  122. /*
  123. WilsonLHRRFluid wilson algorightm dynamic score by time
  124. like,reply
  125. hate,report
  126. */
  127. // WilsonLHRRFluid WilsonLHRRFluid
  128. type WilsonLHRRFluid struct {
  129. name string
  130. slots []int
  131. weight *WilsonLHRRFluidWeight
  132. }
  133. // NewWilsonLHRRFluid NewWilsonLHRRFluid
  134. func NewWilsonLHRRFluid(name string, slots []int, weight *WilsonLHRRFluidWeight) *WilsonLHRRFluid {
  135. return &WilsonLHRRFluid{
  136. name: name,
  137. slots: slots,
  138. weight: weight,
  139. }
  140. }
  141. // Name get name
  142. func (w *WilsonLHRRFluid) Name() string {
  143. return w.name
  144. }
  145. // Slots get slots
  146. func (w *WilsonLHRRFluid) Slots() []int {
  147. return w.slots
  148. }
  149. func coolDownFunc(weight *WilsonLHRRFluidWeight, duration float64) (coefficient float64) {
  150. return 1.5 - (0.5 / (1 + math.Exp(-weight.Slope*(duration))))
  151. }
  152. // Score calc score
  153. func (w *WilsonLHRRFluid) Score(stat *ReplyStat) (rs *ReplyScore) {
  154. rs = new(ReplyScore)
  155. rs.RpID = stat.RpID
  156. if stat.Like < 0 || stat.Reply < 0 || stat.Report < 0 || stat.Hate < 0 {
  157. return
  158. }
  159. ups := float64(stat.Like)*w.weight.Like + float64(stat.Reply)*w.weight.Reply
  160. downs := float64(stat.Hate)*w.weight.Hate + float64(stat.Report)*w.weight.Report
  161. n := ups + downs
  162. if n == 0 {
  163. return
  164. }
  165. z := float64(2)
  166. p := ups / n
  167. coefficient := coolDownFunc(w.weight, float64(time.Now().Unix()-int64(stat.ReplyTime))/86400)
  168. rs.Score = ((p + math.Pow(z, 2)/(2*n) - (z/(2*n))*math.Sqrt(4*n*(1-p)*p+math.Pow(z, 2))) / (1 + math.Pow(z, 2)/n)) * coefficient
  169. return
  170. }