Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
58b6eaec
Commit
58b6eaec
authored
Aug 04, 2021
by
刘基明
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
热度算法优化
parent
77bdd06b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
79 additions
and
73 deletions
+79
-73
ThemeAnalysDO.java
.../java/com/tanpu/community/api/beans/qo/ThemeAnalysDO.java
+2
-20
TopicRankQo.java
...in/java/com/tanpu/community/api/beans/qo/TopicRankQo.java
+1
-15
CollectionService.java
...n/java/com/tanpu/community/service/CollectionService.java
+1
-1
RankService.java
...rc/main/java/com/tanpu/community/service/RankService.java
+71
-33
RecommendService.java
...in/java/com/tanpu/community/service/RecommendService.java
+3
-3
ThemeService.java
...c/main/java/com/tanpu/community/service/ThemeService.java
+1
-1
No files found.
community-api/src/main/java/com/tanpu/community/api/beans/qo/ThemeAnalysDO.java
View file @
58b6eaec
...
@@ -49,25 +49,7 @@ public class ThemeAnalysDO {
...
@@ -49,25 +49,7 @@ public class ThemeAnalysDO {
@ApiModelProperty
(
value
=
"用户质量"
)
@ApiModelProperty
(
value
=
"用户质量"
)
private
Double
userWeight
=
0.0
;
private
Double
userWeight
=
0.0
;
public
Double
getRank
()
{
private
Double
score
=
0.0
;
double
viewRatio
=
0.1
;
double
forwardRatio
=
3
;
double
commentRatio
=
2
;
double
likeRation
=
1
;
double
collectRatio
=
3
;
double
userWeightRatio
=
0.8
;
double
initialWeight
=
1.0
;
double
timeRation
=
0.2
;
// 质量=帖子质量+用户质量
double
w
=
viewCount
*
viewRatio
+
forwardCount
*
forwardRatio
+
commentCount
*
commentRatio
+
likeCount
*
likeRation
+
collectCount
*
collectRatio
+
Math
.
pow
(
userWeight
,
userWeightRatio
);
double
i
=
initialWeight
;
// 初始权重
double
t
=
Double
.
valueOf
(
minuteTillNow
)
/
60
;
double
g
=
timeRation
;
// 时间系数
return
(
w
+
i
)
/
Math
.
pow
(
t
+
1
,
g
);
}
}
}
community-api/src/main/java/com/tanpu/community/api/beans/qo/TopicRankQo.java
View file @
58b6eaec
...
@@ -40,19 +40,5 @@ public class TopicRankQo {
...
@@ -40,19 +40,5 @@ public class TopicRankQo {
private
Integer
hoursTillNow
;
private
Integer
hoursTillNow
;
private
Double
score
;
/**
* 热度计算算法
*
* @return
*/
public
Double
getRank
()
{
double
g
=
0.3
;
//时间系数
//顶置话题
if
(
isTop
>
0
)
{
return
Double
.
MAX_VALUE
;
}
Double
socre
=
((
disscussCount
*
3
+
viewCount
)
/
Math
.
pow
(
hoursTillNow
+
1
,
g
))
+
themeWeight
;
return
socre
;
}
}
}
community-service/src/main/java/com/tanpu/community/service/CollectionService.java
View file @
58b6eaec
...
@@ -116,7 +116,7 @@ public class CollectionService {
...
@@ -116,7 +116,7 @@ public class CollectionService {
if
(
StringUtils
.
isNotEmpty
(
lastId
))
{
if
(
StringUtils
.
isNotEmpty
(
lastId
))
{
CollectionEntity
target
=
queryIncludeDelete
(
lastId
,
userId
,
type
);
CollectionEntity
target
=
queryIncludeDelete
(
lastId
,
userId
,
type
);
if
(
target
==
null
)
return
Collections
.
emptyList
();
if
(
target
==
null
)
return
Collections
.
emptyList
();
queryWrapper
.
lt
(
CollectionEntity:
:
getCollectionT
yp
e
,
target
.
getCollectionTime
());
queryWrapper
.
lt
(
CollectionEntity:
:
getCollectionT
im
e
,
target
.
getCollectionTime
());
}
}
if
(
pageSize
!=
null
)
{
if
(
pageSize
!=
null
)
{
queryWrapper
.
last
(
"limit "
+
pageSize
);
queryWrapper
.
last
(
"limit "
+
pageSize
);
...
...
community-service/src/main/java/com/tanpu/community/service/RankService.java
View file @
58b6eaec
...
@@ -31,23 +31,34 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USE
...
@@ -31,23 +31,34 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.CACHE_FEIGN_USE
@Service
@Service
public
class
RankService
{
public
class
RankService
{
@Value
(
"${rank.theme.viewRatio:0.1}"
)
@Value
(
"${rank.theme.viewRate:0.1}"
)
public
Double
viewRatio
;
public
Double
viewRate
;
@Value
(
"${rank.theme.forwardRatio:3}"
)
@Value
(
"${rank.theme.forwardRate:3}"
)
public
Double
forwardRatio
;
public
Double
forwardRate
;
@Value
(
"${rank.theme.commentRatio:2}"
)
@Value
(
"${rank.theme.commentRate:2}"
)
public
Double
commentRatio
;
public
Double
commentRate
;
@Value
(
"${rank.theme.likeRation:3}"
)
@Value
(
"${rank.theme.likeRaten:3}"
)
public
Double
likeRation
;
public
Double
likeRaten
;
@Value
(
"${rank.theme.collectRatio:3}"
)
@Value
(
"${rank.theme.collectRate:3}"
)
public
Double
collectRatio
;
public
Double
collectRate
;
//用户质量权重
@Value
(
"${rank.theme.userWeightRatio:0.9}"
)
@Value
(
"${rank.theme.userWeightRate:0.9}"
)
public
Double
userWeightRatio
;
public
Double
userWeightRate
;
//初始质量
@Value
(
"${rank.theme.initialWeight:1.0}"
)
@Value
(
"${rank.theme.initialWeight:1.0}"
)
public
Double
initialWeight
;
public
Double
initialWeight
;
@Value
(
"${rank.theme.timeRation:0.2}"
)
//时间系数
public
Double
timeRation
;
@Value
(
"${rank.theme.timeRaten:0.2}"
)
public
Double
timeRate
;
@Value
(
"${rank.topic.viewRate:1}"
)
public
Double
viewRateTopic
;
@Value
(
"${rank.topic.discussRate:3}"
)
public
Double
discussRateTopic
;
//时间系数
@Value
(
"${rank.theme.timeRaten:1}"
)
public
Double
timeRateTopic
;
@Autowired
@Autowired
private
ThemeService
themeService
;
private
ThemeService
themeService
;
...
@@ -114,9 +125,11 @@ public class RankService {
...
@@ -114,9 +125,11 @@ public class RankService {
theme
.
setUserWeight
(
authorInfo
.
getLevelGrade
()
*
1.0
);
theme
.
setUserWeight
(
authorInfo
.
getLevelGrade
()
*
1.0
);
}
}
//打分
this
.
calculateThemeScore
(
theme
);
}
}
//
打分
//
排序
Map
<
ThemeAnalysDO
,
Double
>
map
=
themeAnalysDOS
.
stream
().
collect
(
Collectors
.
toMap
(
o
->
o
,
ThemeAnalysDO:
:
get
Rank
));
Map
<
ThemeAnalysDO
,
Double
>
map
=
themeAnalysDOS
.
stream
().
collect
(
Collectors
.
toMap
(
o
->
o
,
ThemeAnalysDO:
:
get
Score
));
//排序
//排序
hotestThemes
=
map
.
entrySet
().
stream
()
hotestThemes
=
map
.
entrySet
().
stream
()
.
sorted
(
Map
.
Entry
.
comparingByValue
(
Comparator
.
reverseOrder
()))
.
sorted
(
Map
.
Entry
.
comparingByValue
(
Comparator
.
reverseOrder
()))
...
@@ -146,27 +159,27 @@ public class RankService {
...
@@ -146,27 +159,27 @@ public class RankService {
topic
.
setViewCount
(
topicViewMap
.
getOrDefault
(
topic
.
getTopicId
(),
0
));
topic
.
setViewCount
(
topicViewMap
.
getOrDefault
(
topic
.
getTopicId
(),
0
));
topic
.
setDisscussCount
(
0
);
topic
.
setDisscussCount
(
0
);
topic
.
setThemeWeight
(
0.0
);
topic
.
setThemeWeight
(
0.0
);
topic
.
setFormatViewCount
(
BizUtils
.
formatCountNumber
(
topic
.
getViewCount
()));
}
else
{
topic
.
setFormatDisscussCount
(
BizUtils
.
formatCountNumber
(
topic
.
getDisscussCount
()));
// 浏览量
continue
;
Integer
topicPV
=
topicViewMap
.
getOrDefault
(
topic
.
getTopicId
(),
0
);
Integer
themePV
=
visitLogService
.
queryThemeVisit
(
themeIds
);
topic
.
setViewCount
(
topicPV
+
themePV
+
topic
.
getViewCntAdjust
());
//讨论数=发布主题贴数+回复总数
Integer
commentCount
=
commentService
.
getTotalCountByThemeIds
(
themeIds
);
topic
.
setDisscussCount
(
themeIds
.
size
()
+
commentCount
);
//帖子权重,求和
double
themeSum
=
getHotestThemes
().
stream
().
filter
(
o
->
topic
.
getTopicId
().
equals
(
o
.
getTopicId
()))
.
mapToDouble
(
ThemeAnalysDO:
:
getScore
)
.
sum
();
topic
.
setThemeWeight
(
themeSum
);
}
}
// 浏览量
calculateTopicScore
(
topic
);
Integer
topicPV
=
topicViewMap
.
getOrDefault
(
topic
.
getTopicId
(),
0
);
Integer
themePV
=
visitLogService
.
queryThemeVisit
(
themeIds
);
topic
.
setViewCount
(
topicPV
+
themePV
+
topic
.
getViewCntAdjust
());
//讨论数=发布主题贴数+回复总数
Integer
commentCount
=
commentService
.
getTotalCountByThemeIds
(
themeIds
);
topic
.
setDisscussCount
(
themeIds
.
size
()
+
commentCount
);
//帖子权重,求和
double
themeSum
=
getHotestThemes
().
stream
().
filter
(
o
->
topic
.
getTopicId
().
equals
(
o
.
getTopicId
()))
.
mapToDouble
(
ThemeAnalysDO:
:
getRank
)
.
sum
();
topic
.
setThemeWeight
(
themeSum
);
//格式化浏览量、讨论量
//格式化浏览量、讨论量
topic
.
setFormatViewCount
(
BizUtils
.
formatCountNumber
(
topic
.
getViewCount
()));
topic
.
setFormatViewCount
(
BizUtils
.
formatCountNumber
(
topic
.
getViewCount
()));
topic
.
setFormatDisscussCount
(
BizUtils
.
formatCountNumber
(
topic
.
getDisscussCount
()));
topic
.
setFormatDisscussCount
(
BizUtils
.
formatCountNumber
(
topic
.
getDisscussCount
()));
}
}
Map
<
TopicRankQo
,
Double
>
map
=
topicRankQos
.
stream
().
collect
(
Collectors
.
toMap
(
o
->
o
,
TopicRankQo:
:
get
Rank
));
Map
<
TopicRankQo
,
Double
>
map
=
topicRankQos
.
stream
().
collect
(
Collectors
.
toMap
(
o
->
o
,
TopicRankQo:
:
get
Score
));
List
<
TopicRankQo
>
rankList
=
map
.
entrySet
().
stream
()
List
<
TopicRankQo
>
rankList
=
map
.
entrySet
().
stream
()
.
sorted
(
Map
.
Entry
.
comparingByValue
(
Comparator
.
reverseOrder
()))
.
sorted
(
Map
.
Entry
.
comparingByValue
(
Comparator
.
reverseOrder
()))
.
map
(
Map
.
Entry
::
getKey
)
.
map
(
Map
.
Entry
::
getKey
)
...
@@ -234,4 +247,29 @@ public class RankService {
...
@@ -234,4 +247,29 @@ public class RankService {
.
collect
(
Collectors
.
toList
());
.
collect
(
Collectors
.
toList
());
}
}
private
void
calculateThemeScore
(
ThemeAnalysDO
theme
)
{
// 质量=帖子质量+用户质量
double
w
=
theme
.
getViewCount
()
*
viewRate
+
theme
.
getForwardCount
()
*
forwardRate
+
theme
.
getCommentCount
()
*
commentRate
+
theme
.
getLikeCount
()
*
likeRaten
+
theme
.
getCollectCount
()
*
collectRate
+
Math
.
pow
(
theme
.
getUserWeight
(),
userWeightRate
);
double
t
=
Double
.
valueOf
(
theme
.
getMinuteTillNow
())
/
60
;
double
score
=
(
w
+
initialWeight
)
/
Math
.
pow
(
t
+
1
,
timeRate
);
theme
.
setScore
(
score
);
}
public
void
calculateTopicScore
(
TopicRankQo
topic
)
{
//顶置话题
if
(
topic
.
getIsTop
()
>
0
)
{
topic
.
setScore
(
Double
.
MAX_VALUE
);
}
Double
socre
=
((
topic
.
getDisscussCount
()
*
discussRateTopic
+
topic
.
getViewCount
()
*
viewRateTopic
)
/
Math
.
pow
(
topic
.
getHoursTillNow
()
+
1
,
timeRateTopic
))
+
topic
.
getThemeWeight
();
topic
.
setScore
(
socre
);
}
}
}
community-service/src/main/java/com/tanpu/community/service/RecommendService.java
View file @
58b6eaec
...
@@ -179,9 +179,9 @@ public class RecommendService {
...
@@ -179,9 +179,9 @@ public class RecommendService {
if
(
hotStart
>=
hotIds
.
size
()
&&
newestStart
>=
newestIds
.
size
()
&&
recmdStart
>=
recmdIds
.
size
())
{
if
(
hotStart
>=
hotIds
.
size
()
&&
newestStart
>=
newestIds
.
size
()
&&
recmdStart
>=
recmdIds
.
size
())
{
break
;
break
;
}
}
retList
.
addAll
(
BizUtils
.
subList
(
newestIds
,
newestStart
,
newestStart
+
3
));
retList
.
addAll
(
BizUtils
.
subList
(
newestIds
,
newestStart
,
newestStart
+
newRatio
));
retList
.
addAll
(
BizUtils
.
subList
(
hotIds
,
hotStart
,
hotStart
+
6
));
retList
.
addAll
(
BizUtils
.
subList
(
hotIds
,
hotStart
,
hotStart
+
hotRatio
));
retList
.
addAll
(
BizUtils
.
subList
(
recmdIds
,
recmdStart
,
recmdStart
+
1
));
retList
.
addAll
(
BizUtils
.
subList
(
recmdIds
,
recmdStart
,
recmdStart
+
pythonRatio
));
round
++;
round
++;
}
}
...
...
community-service/src/main/java/com/tanpu/community/service/ThemeService.java
View file @
58b6eaec
...
@@ -222,7 +222,7 @@ public class ThemeService {
...
@@ -222,7 +222,7 @@ public class ThemeService {
* 查询更新节点后的用户新建主题数
* 查询更新节点后的用户新建主题数
*
*
* @param userIds 用户ids
* @param userIds 用户ids
* @param last
ViewTime
更新时间节点
* @param last
Id
更新时间节点
* @return
* @return
*/
*/
public
Integer
queryCountFromLastId
(
List
<
String
>
userIds
,
Long
lastId
)
{
public
Integer
queryCountFromLastId
(
List
<
String
>
userIds
,
Long
lastId
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment