From c7b6b8a63bb8c142f0951b4d5c8650929e4fee6e Mon Sep 17 00:00:00 2001 From: x ronger Date: Thu, 29 Oct 2020 21:41:03 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=201.=20=E6=96=87=E7=AB=A0=E6=91=98?= =?UTF-8?q?=E8=A6=81=E6=9C=8D=E5=8A=A1=E9=9B=86=E6=88=90=202.=20=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E6=A0=87=E7=AD=BE=E6=9C=8D=E5=8A=A1=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 ++++++-- .../com/rymcu/vertical/dto/baidu/TagNlpDTO.java | 17 +++++++++++++++++ .../com/rymcu/vertical/service/TagService.java | 3 ++- .../service/impl/ArticleServiceImpl.java | 13 ++++++------- .../vertical/service/impl/TagServiceImpl.java | 16 +++++++++++++++- 5 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/rymcu/vertical/dto/baidu/TagNlpDTO.java diff --git a/pom.xml b/pom.xml index bd2ce30..fd053c5 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.2.1.RELEASE + 2.2.6.RELEASE com.rymcu @@ -168,7 +168,6 @@ org.springframework.boot spring-boot-starter-websocket - 2.2.1.RELEASE org.jodd @@ -180,6 +179,11 @@ weixin-java-open 3.7.0 + + com.baidu.aip + java-sdk + 4.11.3 + diff --git a/src/main/java/com/rymcu/vertical/dto/baidu/TagNlpDTO.java b/src/main/java/com/rymcu/vertical/dto/baidu/TagNlpDTO.java new file mode 100644 index 0000000..2dcd19d --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/baidu/TagNlpDTO.java @@ -0,0 +1,17 @@ +package com.rymcu.vertical.dto.baidu; + +import lombok.Data; + +import java.math.BigDecimal; + +/** + * @author ronger + */ +@Data +public class TagNlpDTO { + + private BigDecimal score; + + private String tag; + +} diff --git a/src/main/java/com/rymcu/vertical/service/TagService.java b/src/main/java/com/rymcu/vertical/service/TagService.java index 6664bbb..9dc4300 100644 --- a/src/main/java/com/rymcu/vertical/service/TagService.java +++ b/src/main/java/com/rymcu/vertical/service/TagService.java @@ -18,11 +18,12 @@ public interface TagService extends Service { /** * 保存文章标签 * @param article + * @param articleContentHtml * @throws UnsupportedEncodingException * @throws BaseApiException * @return * */ - Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException; + Integer saveTagArticle(Article article, String articleContentHtml) throws UnsupportedEncodingException, BaseApiException; /** * 清除未使用标签 diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 5589d9f..756bb39 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -176,7 +176,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } } - tagService.saveTagArticle(newArticle); + tagService.saveTagArticle(newArticle, articleContentHtml); if (defaultStatus.equals(newArticle.getArticleStatus())) { newArticle.setArticlePermalink(domain + "/article/" + newArticle.getIdArticle()); @@ -187,12 +187,11 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } if (StringUtils.isNotBlank(articleContentHtml)) { - Integer length = articleContentHtml.length(); - if (length > MAX_PREVIEW) { - length = MAX_PREVIEW; + String previewContent = BaiDuAipUtils.getNewsSummary(newArticle.getArticleTitle(), articleContentHtml, MAX_PREVIEW); + if (previewContent.length() > MAX_PREVIEW) { + previewContent = previewContent.substring(0, MAX_PREVIEW); } - String articlePreviewContent = articleContentHtml.substring(0, length); - newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); + newArticle.setArticlePreviewContent(previewContent); } articleMapper.updateByPrimaryKeySelective(newArticle); @@ -326,7 +325,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti if (Objects.nonNull(article)) { article.setArticleTags(tags); articleMapper.updateArticleTags(idArticle, tags); - tagService.saveTagArticle(article); + tagService.saveTagArticle(article, ""); map.put("success", true); } else { map.put("success", false); diff --git a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index 6c1b3e3..2b9b65f 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -3,12 +3,14 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.dto.ArticleTagDTO; import com.rymcu.vertical.dto.LabelModel; +import com.rymcu.vertical.dto.baidu.TagNlpDTO; import com.rymcu.vertical.entity.Article; import com.rymcu.vertical.entity.Tag; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.mapper.TagMapper; import com.rymcu.vertical.service.TagService; +import com.rymcu.vertical.util.BaiDuAipUtils; import com.rymcu.vertical.util.CacheUtils; import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.web.api.exception.BaseApiException; @@ -38,7 +40,7 @@ public class TagServiceImpl extends AbstractService implements TagService { @Override @Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class}) - public Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException { + public Integer saveTagArticle(Article article, String articleContentHtml) throws UnsupportedEncodingException, BaseApiException { User user = UserUtils.getCurrentUserByToken(); String articleTags = article.getArticleTags(); if (StringUtils.isNotBlank(articleTags)) { @@ -92,6 +94,18 @@ public class TagServiceImpl extends AbstractService implements TagService { } } return 1; + } else { + if (StringUtils.isNotBlank(articleContentHtml)) { + List list = BaiDuAipUtils.getKeywords(article.getArticleTitle(), articleContentHtml); + if (list.size() > 0) { + StringBuffer tags = new StringBuffer(); + for (TagNlpDTO tagNlpDTO : list) { + tags.append(tagNlpDTO.getTag()).append(","); + } + article.setArticleTags(tags.toString()); + saveTagArticle(article, articleContentHtml); + } + } } return 0; }