From dce9531a8a390c3107fff05fe3a259f04e1fb8bf Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Mon, 10 Aug 2020 16:09:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=A8=E8=8D=90=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/db_table.md | 47 +++--- pom.xml | 7 - .../bupt/note/Controller/FileController.java | 116 ------------- .../bupt/note/Controller/NoteController.java | 74 ++------- .../bupt/note/Controller/PaperController.java | 157 ++++++++++++------ src/main/java/com/bupt/note/Model/File.java | 33 ---- src/main/java/com/bupt/note/Model/Paper.java | 36 +++- .../java/com/bupt/note/Model/PaperNote.java | 11 +- .../java/com/bupt/note/Model/Similarity.java | 28 ++++ .../com/bupt/note/Model/SimilarityId.java | 39 +++++ src/main/java/com/bupt/note/Model/Word.java | 32 ++++ .../bupt/note/Repository/FileRepository.java | 8 - .../bupt/note/Repository/PaperRepository.java | 7 +- .../note/Repository/RatingRepository.java | 9 +- .../note/Repository/SimilarityRepository.java | 10 ++ .../java/com/bupt/note/dto/UploadPaper.java | 1 - .../com/bupt/note/service/FileService.java | 17 -- src/main/resources/application.yaml | 5 +- src/test/java/com/bupt/note/MyTest.java | 28 ---- .../com/bupt/note/NoteApplicationTests.java | 21 ++- 20 files changed, 322 insertions(+), 364 deletions(-) delete mode 100644 src/main/java/com/bupt/note/Controller/FileController.java delete mode 100644 src/main/java/com/bupt/note/Model/File.java create mode 100644 src/main/java/com/bupt/note/Model/Similarity.java create mode 100644 src/main/java/com/bupt/note/Model/SimilarityId.java create mode 100644 src/main/java/com/bupt/note/Model/Word.java delete mode 100644 src/main/java/com/bupt/note/Repository/FileRepository.java create mode 100644 src/main/java/com/bupt/note/Repository/SimilarityRepository.java diff --git a/doc/db_table.md b/doc/db_table.md index e4b0ee0..ac8aedf 100644 --- a/doc/db_table.md +++ b/doc/db_table.md @@ -6,22 +6,6 @@ | user_name | varchar | not null | 用户名 | | password | varchar | not null | 密码 | -# 论文文件表 sys_file - -| 列名 | 数据类型 | 约束条件 | 含义 | -| ---- | -------- | -------- | ---- | -| id | bigint | primary key; identity | 文件id | -| file_path | varchar | not null | 文件路径 | - -# 笔记文件表 sys_paper_note - -| 列名 | 数据类型 | 约束条件 | 含义 | -| ---- | -------- | -------- | ---- | -| id | bigint | primary key; identity | 文件id | -| paper_id | bigint | not null | 论文id | -| file_path | varchar | not null | 文件路径 | -| user_name | varchar | not null | 修改用户 | - # 论文 sys_paper | 列名 | 数据类型 | 约束条件 | 含义 | @@ -33,9 +17,11 @@ | profession | varchar | not null | 学科专业 | | school | varchar | not null | 学校 | | year | int | not null | 学位年度 | -| summary | varchar | not null | 摘要 | +| summary | longtext | not null | 摘要 | | tag | varchar | not null | 论文标签 | -| file_id | bigint | not null | 论文文件id | +| html_content | longblob | not null | 论文html内容 | +| pdf_content | longblob | not null | 论文pdf内容 | +| txt_content | longblob | not null | 论文pdf内容 | | create_time | Long | not null | 创建时间戳 | # 论文评价 sys_paper_rating @@ -48,6 +34,15 @@ | paper_id | bigint | not null | 论文id | | user_name | varchar | not null | 评价用户 | +# html笔记内容表 sys_paper_note + +| 列名 | 数据类型 | 约束条件 | 含义 | +| ---- | -------- | -------- | ---- | +| id | bigint | primary key; identity | 文件id | +| paper_id | bigint | not null | 论文id | +| user_name | varchar | not null | 修改用户 | +| html_content | longblob | not null | html代码 | + # 笔记 sys_note | 列名 | 数据类型 | 约束条件 | 含义 | @@ -56,6 +51,7 @@ | note_title | varchar | not null | 笔记标题 | | note_content | varchar | not null | 笔记内容 | | paper_id | bigint | not null | 论文id | +| paper_note_id | bigint | not null | html笔记id | | user_name | varchar | not null | 笔记用户 | # 收藏 sys_collect @@ -65,4 +61,17 @@ | id | bigint | primary key; identity | 收藏id | | paper_id | bigint | not null | 收藏论文id | | user_name | varchar | not null | 收藏用户 | -| create_time | Long | not null | 收藏时间戳 | \ No newline at end of file +| create_time | Long | not null | 收藏时间戳 | + +# 词库 sys_word +| 列名 | 数据类型 | 约束条件 | 含义 | +| ---- | -------- | -------- | ---- | +| word | varchar | primary key;binary; not null | 词语 | +| flag | varchar | not null | 词性 | + +# 相似度 sys_similarity +| 列名 | 数据类型 | 约束条件 | 含义 | +| ---- | -------- | -------- | ---- | +| source_paper | bigint | primary key; not null | 论文id | +| target_paper | bigint | primary key; not null | 论文id | +| similarity | double | not null | 相似度 | \ No newline at end of file diff --git a/pom.xml b/pom.xml index 0de5c72..da4a87c 100644 --- a/pom.xml +++ b/pom.xml @@ -67,13 +67,6 @@ 2.6 - - - org.apache.pdfbox - pdfbox - 2.0.20 - - diff --git a/src/main/java/com/bupt/note/Controller/FileController.java b/src/main/java/com/bupt/note/Controller/FileController.java deleted file mode 100644 index 9b7d934..0000000 --- a/src/main/java/com/bupt/note/Controller/FileController.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.bupt.note.Controller; - -import com.bupt.note.Model.PaperNote; -import com.bupt.note.Repository.FileRepository; -import com.bupt.note.Repository.PaperNoteRepository; -import com.bupt.note.ResponseData.ResponseData; -import com.bupt.note.ResponseData.ResponseDataUtil; -import com.bupt.note.dto.FileForm; -import com.bupt.note.dto.QueryContent; -import com.bupt.note.service.FileService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; - -import java.io.File; -import java.io.IOException; -import java.util.UUID; - -/** - * 论文文本管理接口 - */ -@RestController -@RequestMapping(value = "/v1/api/file") -public class FileController { - - - private static Logger logger = LoggerFactory.getLogger(FileController.class); - - @Autowired - private FileRepository fileRepository; - - @Autowired - private PaperNoteRepository paperNoteRepository; - - @Autowired - private FileService fileService; - - //上传论文文本 - @PostMapping("upload") - public ResponseData upload(FileForm fileForm) { - MultipartFile multipartFile = fileForm.getFile(); - if (multipartFile != null && (MediaType.TEXT_PLAIN_VALUE.equals(multipartFile.getContentType()) || MediaType.APPLICATION_PDF_VALUE.equals(multipartFile.getContentType()))) { - - try { - String name = multipartFile.getOriginalFilename(); - File file; - if (name != null && name.contains(".")) { - file = fileService.newFile(name.replace(multipartFile.getOriginalFilename().split("\\.")[0], UUID.randomUUID().toString())); - } else { - file = fileService.newFile(); - } - logger.debug("论文保存到" + file.getAbsolutePath()); - multipartFile.transferTo(file); - com.bupt.note.Model.File f = new com.bupt.note.Model.File(); - f.setFilePath(file.getAbsolutePath()); - fileRepository.save(f); - return ResponseDataUtil.buildSuccess(f.getId()); - } catch (IOException e) { - logger.error(String.valueOf(e)); - e.printStackTrace(); - return ResponseDataUtil.buildError(); - } - } else { - logger.error("上传论文表单校验失败"); - return ResponseDataUtil.buildError(); - } - } - - @GetMapping("find") - public ResponseData get(QueryContent queryContent, @CookieValue("user") String username){ - try { - if (paperNoteRepository.existsByPaperIdAndUserName(queryContent.getPaperId(), username)) { - PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(queryContent.getPaperId(), username); - return ResponseDataUtil.buildSuccess(fileService.getContent(paperNote.getFilePath())); - } else if (fileRepository.existsById(queryContent.getFileId())) { - com.bupt.note.Model.File file = fileRepository.getOne(queryContent.getFileId()); - return ResponseDataUtil.buildSuccess(fileService.getContent(file.getFilePath())); - } else { - logger.error(String.format("论文文件id=%d不存在", queryContent.getFileId())); - return ResponseDataUtil.buildError(); - } - } catch (IOException e) { - e.printStackTrace(); - logger.error(String.format("解析文件失败,原因是%s", e)); - return ResponseDataUtil.buildError(); - } - } - - - /** - * 删除论文文本文件 - * - * @param id 文件id - * @return - */ - @DeleteMapping("remove/{id}") - public ResponseData remove(@PathVariable Long id) { - if (fileRepository.existsById(id)) { - com.bupt.note.Model.File txtFile = fileRepository.getOne(id); - File f = new File(txtFile.getFilePath()); - if (f.exists() && f.delete()) { - fileRepository.deleteById(id); - return ResponseDataUtil.buildSuccess(); - } else { - logger.error(String.format("删除论文id=%d失败", id)); - return ResponseDataUtil.buildError(); - } - } else { - logger.error(String.format("论文id=%d不存在", id)); - return ResponseDataUtil.buildError(); - } - } -} diff --git a/src/main/java/com/bupt/note/Controller/NoteController.java b/src/main/java/com/bupt/note/Controller/NoteController.java index f2c5be1..d144848 100644 --- a/src/main/java/com/bupt/note/Controller/NoteController.java +++ b/src/main/java/com/bupt/note/Controller/NoteController.java @@ -11,7 +11,6 @@ import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.dto.DeleteNote; import com.bupt.note.dto.NoteForm; import com.bupt.note.dto.Page; -import com.bupt.note.service.FileService; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,11 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.persistence.criteria.Predicate; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.Writer; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -41,9 +35,6 @@ public class NoteController { @Autowired private PaperRepository paperRepository; - @Autowired - private FileService fileService; - private final Logger logger = LoggerFactory.getLogger(NoteController.class); /** @@ -62,39 +53,17 @@ public class NoteController { note.setUserName(username); noteRepository.save(note); if (noteRepository.existsById(note.getNoteId())) { - Writer wr = null; - try { - PaperNote paperNote; - File f; - if (paperNoteRepository.existsByPaperIdAndUserName(noteForm.getPaperId(), username)) { - paperNote = paperNoteRepository.findByPaperIdAndUserName(note.getPaperId(), username); - f = new File(paperNote.getFilePath()); - } else { - f = fileService.newFile(); - paperNote = new PaperNote(); - paperNote.setPaperId(noteForm.getPaperId()); - paperNote.setFilePath(f.getAbsolutePath()); - paperNoteRepository.save(paperNote); - } - wr = new FileWriter(f, StandardCharsets.UTF_8); - wr.write(noteForm.getContent()); - return ResponseDataUtil.buildSuccess(note); - } catch (IOException e) { - e.printStackTrace(); - logger.error(String.valueOf(e)); - } finally { - if (wr != null) { - try { - wr.close(); - } catch (IOException e) { - e.printStackTrace(); - logger.error(String.valueOf(e)); - } - } + PaperNote paperNote; + if (paperNoteRepository.existsByPaperIdAndUserName(noteForm.getPaperId(), username)) { + paperNote = paperNoteRepository.findByPaperIdAndUserName(note.getPaperId(), username); + } else { + paperNote = new PaperNote(); + paperNote.setPaperId(noteForm.getPaperId()); + paperNote.setUserName(username); } - logger.error(String.format("更新论文id=%d失败", noteForm.getPaperId())); - return ResponseDataUtil.buildError(); - + paperNote.setTxtContent(noteForm.getContent()); + paperNoteRepository.save(paperNote); + return ResponseDataUtil.buildSuccess(note); } else { logger.error("保存笔记失败"); return ResponseDataUtil.buildError(); @@ -155,26 +124,9 @@ public class NoteController { paperNoteRepository.existsByPaperIdAndUserName(deleteNote.getPaperId(),username)){ noteRepository.deleteById(deleteNote.getNoteId()); PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(deleteNote.getPaperId(), username); - File f = new File(paperNote.getFilePath()); - Writer wr = null; - try { - wr = new FileWriter(f); - wr.write(deleteNote.getContent()); - return ResponseDataUtil.buildSuccess(); - } catch (IOException e) { - e.printStackTrace(); - logger.error(String.valueOf(e)); - } finally { - if (wr != null) { - try { - wr.close(); - } catch (IOException e) { - e.printStackTrace(); - logger.error(String.valueOf(e)); - } - } - } - logger.error("删除笔记失败"); + paperNote.setTxtContent(deleteNote.getContent()); + paperNoteRepository.save(paperNote); + return ResponseDataUtil.buildSuccess(); }else{ logger.error("删除笔记表单校验失败"); } diff --git a/src/main/java/com/bupt/note/Controller/PaperController.java b/src/main/java/com/bupt/note/Controller/PaperController.java index 21adefd..90e69a3 100644 --- a/src/main/java/com/bupt/note/Controller/PaperController.java +++ b/src/main/java/com/bupt/note/Controller/PaperController.java @@ -1,28 +1,29 @@ package com.bupt.note.Controller; -import com.bupt.note.Model.Paper; -import com.bupt.note.Model.Rating; -import com.bupt.note.Repository.CollectRepository; -import com.bupt.note.Repository.PaperRepository; -import com.bupt.note.Repository.RatingRepository; +import com.bupt.note.Model.*; +import com.bupt.note.Repository.*; import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.dto.QueryPaper; import com.bupt.note.dto.RatingForm; import com.bupt.note.dto.UploadPaper; import org.apache.commons.lang3.StringUtils; -import org.hibernate.query.criteria.internal.OrderImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; -import javax.persistence.criteria.Order; import javax.persistence.criteria.Predicate; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * 论文管理接口 @@ -31,7 +32,7 @@ import java.util.List; @RequestMapping(value = "/v1/api/paper") public class PaperController { - private Logger logger= LoggerFactory.getLogger(PaperController.class); + private Logger logger = LoggerFactory.getLogger(PaperController.class); @Autowired private PaperRepository paperRepository; @@ -42,59 +43,68 @@ public class PaperController { @Autowired private CollectRepository collectRepository; + @Autowired + private PaperNoteRepository paperNoteRepository; + + @Autowired + private SimilarityRepository similarityRepository; + + @Value("${score}") + private Integer score; + @PostMapping("upload") - public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { - if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(),uploadPaper.getType(), - uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){ + public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { + if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(), uploadPaper.getType(), + uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(), uploadPaper.getTag())) { Paper paper = uploadPaper.toPaper(); paperRepository.save(paper); - if(paperRepository.existsById(paper.getId())){ + if (paperRepository.existsById(paper.getId())) { return ResponseDataUtil.buildSuccess(); - }else{ + } else { logger.error("论文上传失败"); return ResponseDataUtil.buildError(); } - }else{ + } else { logger.error("论文表单校验失败"); return ResponseDataUtil.buildError(); } } @GetMapping("list") - public ResponseData list(QueryPaper queryPaper,@CookieValue("user") String username){ + public ResponseData list(QueryPaper queryPaper, @CookieValue("user") String username) { try { -// Page list=new Page<>(); -// list.setTotal(paperRepository.count()); Page papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { List predicates = new ArrayList<>(); - if(StringUtils.isNotEmpty(queryPaper.getTitle())){ - predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%")); + if (StringUtils.isNotEmpty(queryPaper.getTitle())) { + predicates.add(criteriaBuilder.like(root.get("title"), "%" + queryPaper.getTitle() + "%")); } - if(StringUtils.isNotEmpty(queryPaper.getAuthor())){ - predicates.add(criteriaBuilder.like(root.get("author"),"%"+queryPaper.getAuthor()+"%")); + if (StringUtils.isNotEmpty(queryPaper.getAuthor())) { + predicates.add(criteriaBuilder.like(root.get("author"), "%" + queryPaper.getAuthor() + "%")); } - if(StringUtils.isNotEmpty(queryPaper.getTag())){ - predicates.add(criteriaBuilder.like(root.get("tag"),"%"+queryPaper.getTag()+"%")); + if (StringUtils.isNotEmpty(queryPaper.getTag())) { + predicates.add(criteriaBuilder.like(root.get("tag"), "%" + queryPaper.getTag() + "%")); } - if(StringUtils.isNotEmpty(queryPaper.getProfession())){ - predicates.add(criteriaBuilder.like(root.get("profession"),"%"+queryPaper.getTag()+"%")); + if (StringUtils.isNotEmpty(queryPaper.getProfession())) { + predicates.add(criteriaBuilder.like(root.get("profession"), "%" + queryPaper.getTag() + "%")); } - if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()==null){ - predicates.add(criteriaBuilder.ge(root.get("year"),queryPaper.getStartYear())); - }else if(queryPaper.getStartYear()==null&&queryPaper.getEndYear()!=null){ - predicates.add(criteriaBuilder.le(root.get("year"),queryPaper.getEndYear())); - }else if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()!=null){ - predicates.add(criteriaBuilder.between(root.get("year"),queryPaper.getStartYear(),queryPaper.getEndYear())); + if (queryPaper.getStartYear() != null && queryPaper.getEndYear() == null) { + predicates.add(criteriaBuilder.ge(root.get("year"), queryPaper.getStartYear())); + } else if (queryPaper.getStartYear() == null && queryPaper.getEndYear() != null) { + predicates.add(criteriaBuilder.le(root.get("year"), queryPaper.getEndYear())); + } else if (queryPaper.getStartYear() != null && queryPaper.getEndYear() != null) { + predicates.add(criteriaBuilder.between(root.get("year"), queryPaper.getStartYear(), queryPaper.getEndYear())); } - if(queryPaper.getOwn()){ - predicates.add(criteriaBuilder.equal(root.get("userName"),username)); + if (queryPaper.getOwn()) { + predicates.add(criteriaBuilder.equal(root.get("userName"), username)); } - Order order=new OrderImpl(root.get("createTime"),false); - return criteriaQuery.where(predicates.toArray(new Predicate[0])).orderBy(order).getRestriction(); - }, PageRequest.of(queryPaper.getPage()-1,10)); + return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); + }, PageRequest.of(queryPaper.getPage() - 1, 10, Sort.Direction.DESC, "createTime")); papers.forEach(paper -> { - paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(),username)); + paper.setHtmlContent(null); + paper.setPdfContent(null); + paper.setTxtContent(null); + paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(), username)); }); return ResponseDataUtil.buildSuccess(papers); } catch (Exception e) { @@ -107,40 +117,87 @@ public class PaperController { /** * 论文推荐 + * * @return */ @GetMapping("recommend") - public ResponseData recommend(){ - List papers=paperRepository.recommend(); - return ResponseDataUtil.buildSuccess(papers); + public ResponseData recommend(@CookieValue("user") String userName) { +// 查询已收藏论文 + List collectIds = collectRepository.findByUserName(userName).stream().map(Collect::getPaperId).collect(Collectors.toList()); + //查询喜欢论文 + List likeIds = ratingRepository.findLike(score); + List ids = Stream.of(collectIds, likeIds).flatMap(Collection::stream).distinct().collect(Collectors.toList()); + logger.info(String.format("找到论文id数%d", ids.size())); + if (ids.size() > 0) { + + Page similarities = similarityRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { + List predicates = new ArrayList<>(); + predicates.add(criteriaBuilder.or(criteriaBuilder.and(root.get("id").get("sourcePaper").in(ids), + criteriaBuilder.not(root.get("id").get("targetPaper").in(ids))), + criteriaBuilder.and(root.get("id").get("targetPaper").in(ids), + criteriaBuilder.not(root.get("id").get("sourcePaper").in(ids))))); + return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction(); + }, PageRequest.of(0, 10, Sort.Direction.DESC, "similarity")); + + logger.info(String.format("找到论文数%d", similarities.getTotalElements())); + List paperIds=similarities.stream().map(similarity -> { + if (ids.contains(similarity.getId().getSourcePaper())) { + return similarity.getId().getTargetPaper(); + } else { + return similarity.getId().getSourcePaper(); + } + }).collect(Collectors.toList()); + + + List papers = paperRepository.recommendWithSim(paperIds); + papers.forEach(paper -> { + paper.setTxtContent(null); + paper.setPdfContent(null); + paper.setHtmlContent(null); + }); + return ResponseDataUtil.buildSuccess(papers); + } else { + return ResponseDataUtil.buildSuccess("暂无推荐数据"); + } } @PostMapping("rating") - public ResponseData rating(@RequestBody RatingForm ratingForm,@CookieValue("user") String userName) { - if(ratingForm.getPaperId()!=null&&ratingForm.getScore1()!=null&&ratingForm.getScore2()!=null&&ratingForm.getScore3()!=null){ - Rating rating=ratingForm.toRating(); + public ResponseData rating(@RequestBody RatingForm ratingForm, @CookieValue("user") String userName) { + if (ratingForm.getPaperId() != null && ratingForm.getScore1() != null && ratingForm.getScore2() != null && ratingForm.getScore3() != null) { + Rating rating = ratingForm.toRating(); rating.setUserName(userName); ratingRepository.save(rating); - if(ratingRepository.existsById(rating.getId())){ + if (ratingRepository.existsById(rating.getId())) { return ResponseDataUtil.buildSuccess(rating); - }else{ + } else { logger.warn("论文保存失败"); return ResponseDataUtil.buildSuccess(); } - }else{ + } else { logger.error("论文表单校验失败"); return ResponseDataUtil.buildError(); } } @GetMapping("findRating") - public ResponseData findRating(Long paperId,@CookieValue("user") String userName){ - if(paperId!=null&&StringUtils.isNotEmpty(userName)){ - Rating rating=ratingRepository.findByPaperIdAndUserName(paperId,userName); + public ResponseData findRating(Long paperId, @CookieValue("user") String userName) { + if (paperId != null && StringUtils.isNotEmpty(userName)) { + Rating rating = ratingRepository.findByPaperIdAndUserName(paperId, userName); return ResponseDataUtil.buildSuccess(rating); - }else{ + } else { logger.error("论文评价表单校验失败"); return ResponseDataUtil.buildError(); } } + + @GetMapping(value = "content/{id}", produces = MediaType.TEXT_PLAIN_VALUE) + public String findContent(@PathVariable("id") Long paperId, @CookieValue("user") String userName) { + if (paperNoteRepository.existsByPaperIdAndUserName(paperId, userName)) { + PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(paperId, userName); + return paperNote.getTxtContent(); + } else { + Paper paper = paperRepository.getOne(paperId); + return paper.getTxtContent(); + } + } } diff --git a/src/main/java/com/bupt/note/Model/File.java b/src/main/java/com/bupt/note/Model/File.java deleted file mode 100644 index 7ac5625..0000000 --- a/src/main/java/com/bupt/note/Model/File.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.bupt.note.Model; - -import javax.persistence.*; - -/** - * 论文文本实体 - */ -@Entity -@Table(name = "sys_file") -public class File { - - private Long Id; - private String filePath; - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - public Long getId() { - return Id; - } - - public void setId(Long id) { - Id = id; - } - - @Column(nullable = false) - public String getFilePath() { - return filePath; - } - - public void setFilePath(String filePath) { - this.filePath = filePath; - } -} diff --git a/src/main/java/com/bupt/note/Model/Paper.java b/src/main/java/com/bupt/note/Model/Paper.java index e85c03c..66ee21c 100644 --- a/src/main/java/com/bupt/note/Model/Paper.java +++ b/src/main/java/com/bupt/note/Model/Paper.java @@ -18,7 +18,9 @@ public class Paper { private Integer year; private String summary; private String tag; - private Long fileId; + private byte[] pdfContent; + private byte[] htmlContent; + private String txtContent; private Long createTime; private boolean hasCollect; private Long collectTime; @@ -87,7 +89,8 @@ public class Paper { this.year = year; } - @Column(nullable = false,length = 512) + @Lob + @Column(nullable = false) public String getSummary() { return summary; } @@ -105,13 +108,34 @@ public class Paper { this.tag = tag; } + @Lob + @Column(nullable = false) + public byte[] getPdfContent() { + return pdfContent; + } + + public void setPdfContent(byte[] content) { + this.pdfContent = content; + } + + @Lob + @Column(nullable = false) + public byte[] getHtmlContent() { + return htmlContent; + } + + public void setHtmlContent(byte[] htmlContent) { + this.htmlContent = htmlContent; + } + + @Lob @Column(nullable = false) - public Long getFileId() { - return fileId; + public String getTxtContent() { + return txtContent; } - public void setFileId(Long fileId) { - this.fileId = fileId; + public void setTxtContent(String txtContent) { + this.txtContent = txtContent; } @Column(nullable = false) diff --git a/src/main/java/com/bupt/note/Model/PaperNote.java b/src/main/java/com/bupt/note/Model/PaperNote.java index bfabb64..384b8a5 100644 --- a/src/main/java/com/bupt/note/Model/PaperNote.java +++ b/src/main/java/com/bupt/note/Model/PaperNote.java @@ -7,7 +7,7 @@ import javax.persistence.*; public class PaperNote { private Long id; private Long paperId; - private String filePath; + private String txtContent; private String userName; @Id @@ -20,13 +20,14 @@ public class PaperNote { this.id = id; } + @Lob @Column(nullable = false) - public String getFilePath() { - return filePath; + public String getTxtContent() { + return txtContent; } - public void setFilePath(String filePath) { - this.filePath = filePath; + public void setTxtContent(String txtContent) { + this.txtContent = txtContent; } @Column(nullable = false) diff --git a/src/main/java/com/bupt/note/Model/Similarity.java b/src/main/java/com/bupt/note/Model/Similarity.java new file mode 100644 index 0000000..769d88c --- /dev/null +++ b/src/main/java/com/bupt/note/Model/Similarity.java @@ -0,0 +1,28 @@ +package com.bupt.note.Model; + +import javax.persistence.*; + +@Entity +@Table(name = "sys_similarity") +public class Similarity { + private SimilarityId id; + private Double similarity; + + @EmbeddedId + public SimilarityId getId() { + return id; + } + + public void setId(SimilarityId id) { + this.id = id; + } + + @Column(nullable = false) + public Double getSimilarity() { + return similarity; + } + + public void setSimilarity(Double similarity) { + this.similarity = similarity; + } +} diff --git a/src/main/java/com/bupt/note/Model/SimilarityId.java b/src/main/java/com/bupt/note/Model/SimilarityId.java new file mode 100644 index 0000000..649ae44 --- /dev/null +++ b/src/main/java/com/bupt/note/Model/SimilarityId.java @@ -0,0 +1,39 @@ +package com.bupt.note.Model; + +import java.io.Serializable; +import java.util.Objects; + +public class SimilarityId implements Serializable { + private Long sourcePaper; + private Long targetPaper; + + public Long getSourcePaper() { + return sourcePaper; + } + + public void setSourcePaper(Long sourcePaper) { + this.sourcePaper = sourcePaper; + } + + public Long getTargetPaper() { + return targetPaper; + } + + public void setTargetPaper(Long targetPaper) { + this.targetPaper = targetPaper; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SimilarityId that = (SimilarityId) o; + return Objects.equals(sourcePaper, that.sourcePaper) && + Objects.equals(targetPaper, that.targetPaper); + } + + @Override + public int hashCode() { + return Objects.hash(sourcePaper, targetPaper); + } +} diff --git a/src/main/java/com/bupt/note/Model/Word.java b/src/main/java/com/bupt/note/Model/Word.java new file mode 100644 index 0000000..3d884d0 --- /dev/null +++ b/src/main/java/com/bupt/note/Model/Word.java @@ -0,0 +1,32 @@ +package com.bupt.note.Model; + +import javax.persistence.*; + +/** + * 词库 + */ +@Entity +@Table(name = "sys_word") +public class Word { + private String word; + private String flag; + + @Id + @Column(columnDefinition = "varchar(255) binary") + public String getWord() { + return word; + } + + public void setWord(String word) { + this.word = word; + } + + @Column(nullable = false) + public String getFlag() { + return flag; + } + + public void setFlag(String flag) { + this.flag = flag; + } +} diff --git a/src/main/java/com/bupt/note/Repository/FileRepository.java b/src/main/java/com/bupt/note/Repository/FileRepository.java deleted file mode 100644 index 23ce4ad..0000000 --- a/src/main/java/com/bupt/note/Repository/FileRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.bupt.note.Repository; - -import com.bupt.note.Model.File; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface FileRepository extends JpaRepository { - -} diff --git a/src/main/java/com/bupt/note/Repository/PaperRepository.java b/src/main/java/com/bupt/note/Repository/PaperRepository.java index ffab879..09cc1c1 100644 --- a/src/main/java/com/bupt/note/Repository/PaperRepository.java +++ b/src/main/java/com/bupt/note/Repository/PaperRepository.java @@ -4,10 +4,12 @@ import com.bupt.note.Model.Paper; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import java.util.List; public interface PaperRepository extends JpaRepository, JpaSpecificationExecutor { + @Query(value = "select *\n" + "from sys_paper\n" + "where id in (select a.id\n" + @@ -18,5 +20,8 @@ public interface PaperRepository extends JpaRepository, JpaSpecific " group by p.id\n" + " order by score desc\n" + " limit 10) a)",nativeQuery = true) - List recommend(); + List recommendWithScore(); + + @Query(value = "select * from sys_paper where id in (:ids) order by FIELD(`id`,:ids)",nativeQuery = true) + List recommendWithSim(@Param("ids") List ids); } diff --git a/src/main/java/com/bupt/note/Repository/RatingRepository.java b/src/main/java/com/bupt/note/Repository/RatingRepository.java index 58d76bd..728a233 100644 --- a/src/main/java/com/bupt/note/Repository/RatingRepository.java +++ b/src/main/java/com/bupt/note/Repository/RatingRepository.java @@ -2,7 +2,14 @@ package com.bupt.note.Repository; import com.bupt.note.Model.Rating; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Query; -public interface RatingRepository extends JpaRepository { +import java.util.List; + +public interface RatingRepository extends JpaRepository, JpaSpecificationExecutor { Rating findByPaperIdAndUserName(Long paperId, String userName); + + @Query(value = "select id from sys_paper_rating where score1+score2+score3>?1",nativeQuery = true) + List findLike(int score); } diff --git a/src/main/java/com/bupt/note/Repository/SimilarityRepository.java b/src/main/java/com/bupt/note/Repository/SimilarityRepository.java new file mode 100644 index 0000000..8eb8529 --- /dev/null +++ b/src/main/java/com/bupt/note/Repository/SimilarityRepository.java @@ -0,0 +1,10 @@ +package com.bupt.note.Repository; + +import com.bupt.note.Model.Similarity; +import com.bupt.note.Model.SimilarityId; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +public interface SimilarityRepository extends JpaRepository, JpaSpecificationExecutor { + +} diff --git a/src/main/java/com/bupt/note/dto/UploadPaper.java b/src/main/java/com/bupt/note/dto/UploadPaper.java index b478ea9..7547d0b 100644 --- a/src/main/java/com/bupt/note/dto/UploadPaper.java +++ b/src/main/java/com/bupt/note/dto/UploadPaper.java @@ -95,7 +95,6 @@ public class UploadPaper { paper.setYear(this.year); paper.setSummary(this.summary); paper.setTag(this.tag); - paper.setFileId(this.fileId); paper.setCreateTime(System.currentTimeMillis()); return paper; } diff --git a/src/main/java/com/bupt/note/service/FileService.java b/src/main/java/com/bupt/note/service/FileService.java index 416795b..9335e69 100644 --- a/src/main/java/com/bupt/note/service/FileService.java +++ b/src/main/java/com/bupt/note/service/FileService.java @@ -1,8 +1,5 @@ package com.bupt.note.service; -import org.apache.commons.io.FileUtils; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.text.PDFTextStripper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -11,7 +8,6 @@ import org.springframework.util.ResourceUtils; import java.io.File; import java.io.FileNotFoundException; -import java.io.IOException; import java.net.URLDecoder; import java.nio.charset.StandardCharsets; import java.util.UUID; @@ -35,17 +31,4 @@ public class FileService { public File newFile() throws FileNotFoundException { return newFile(UUID.randomUUID() + ".txt"); } - - public String getContent(String filePath) throws IOException { - if(filePath.endsWith(".txt")||filePath.endsWith(".text")){ - return FileUtils.readFileToString(new File(filePath), StandardCharsets.UTF_8); - }else if(filePath.endsWith(".pdf")){ - PDDocument document = PDDocument.load(new File(filePath)); - PDFTextStripper s = new PDFTextStripper(); - s.setParagraphStart(" "); - return s.getText(document); - }else{ - throw new RuntimeException("解析文件内容失败"); - } - } } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 2cdbafd..bd614de 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -13,5 +13,6 @@ spring: static-path-pattern: /file/** resources: static-locations: classpath:/resources/static/file -file: - upload-dir: note_file/ \ No newline at end of file +#喜好评价分 +score: 7 + diff --git a/src/test/java/com/bupt/note/MyTest.java b/src/test/java/com/bupt/note/MyTest.java index ec19356..bda0273 100644 --- a/src/test/java/com/bupt/note/MyTest.java +++ b/src/test/java/com/bupt/note/MyTest.java @@ -1,39 +1,11 @@ package com.bupt.note; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.text.PDFTextStripper; import org.junit.jupiter.api.Test; -import java.io.File; -import java.io.IOException; - public class MyTest { @Test public void test1() { System.out.println(String.format("%d=%d", 1, 1L)); } - - @Test - public void testPDF() throws IOException { - PDDocument document = PDDocument.load(new File("E:\\JetBrains\\PycharmProjects\\python_requirements_summary\\libcon\\Management+Game环境下戴乐国际表业有限公司总体战略规划.pdf")); - PDFTextStripper s = new PDFTextStripper(); -// for (PDPage page : document.getPages()) { -// -// -// -// ByteArrayOutputStream result = new ByteArrayOutputStream(); -// byte[] buffer = new byte[1024]; -// int length; -// while ((length = page.getContents().read(buffer)) != -1) { -// result.write(buffer, 0, length); -// } -//// String f = new String(result.toByteArray()); -//// System.out.println(f); -// } -// s.setArticleStart(" "); -// s.setParagraphEnd(" .\n"); - System.out.println(s.getText(document)); - - } } diff --git a/src/test/java/com/bupt/note/NoteApplicationTests.java b/src/test/java/com/bupt/note/NoteApplicationTests.java index 3a48dea..313d4de 100644 --- a/src/test/java/com/bupt/note/NoteApplicationTests.java +++ b/src/test/java/com/bupt/note/NoteApplicationTests.java @@ -2,6 +2,8 @@ package com.bupt.note; import com.bupt.note.Model.Paper; import com.bupt.note.Repository.PaperRepository; +import com.bupt.note.Repository.RatingRepository; +import com.bupt.note.Repository.SimilarityRepository; import com.bupt.note.service.FileService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -15,7 +17,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import java.io.IOException; +import java.util.ArrayList; import java.util.List; @SpringBootTest @@ -32,6 +34,12 @@ class NoteApplicationTests { @Autowired private PaperRepository paperRepository; + @Autowired + private RatingRepository ratingRepository; + + @Autowired + private SimilarityRepository similarityRepository; + @BeforeEach public void setUp() { mvc = MockMvcBuilders.webAppContextSetup(context).build(); @@ -46,15 +54,10 @@ class NoteApplicationTests { mvc.perform(builder).andDo(MockMvcResultHandlers.print()); } - - @Test - public void testRead() throws IOException { - String a= fileService.getContent("E:\\JetBrains\\IdeaProjects\\back-end code\\target\\classes\\resources\\static\\txt\\1aee7062-58a7-46e0-ba4d-84a9a14f4205.txt"); - } - @Test public void testPaper(){ - List papers=paperRepository.recommend(); - System.out.println(papers.size()); + List ids= paperRepository.recommendWithSim(new ArrayList<>(){{ + add(5L); + }}); } }