更新推荐算法

pdf
pan 4 years ago
parent bb9508c389
commit dce9531a8a
  1. 47
      doc/db_table.md
  2. 7
      pom.xml
  3. 116
      src/main/java/com/bupt/note/Controller/FileController.java
  4. 74
      src/main/java/com/bupt/note/Controller/NoteController.java
  5. 157
      src/main/java/com/bupt/note/Controller/PaperController.java
  6. 33
      src/main/java/com/bupt/note/Model/File.java
  7. 36
      src/main/java/com/bupt/note/Model/Paper.java
  8. 11
      src/main/java/com/bupt/note/Model/PaperNote.java
  9. 28
      src/main/java/com/bupt/note/Model/Similarity.java
  10. 39
      src/main/java/com/bupt/note/Model/SimilarityId.java
  11. 32
      src/main/java/com/bupt/note/Model/Word.java
  12. 8
      src/main/java/com/bupt/note/Repository/FileRepository.java
  13. 7
      src/main/java/com/bupt/note/Repository/PaperRepository.java
  14. 9
      src/main/java/com/bupt/note/Repository/RatingRepository.java
  15. 10
      src/main/java/com/bupt/note/Repository/SimilarityRepository.java
  16. 1
      src/main/java/com/bupt/note/dto/UploadPaper.java
  17. 17
      src/main/java/com/bupt/note/service/FileService.java
  18. 5
      src/main/resources/application.yaml
  19. 28
      src/test/java/com/bupt/note/MyTest.java
  20. 21
      src/test/java/com/bupt/note/NoteApplicationTests.java

@ -6,22 +6,6 @@
| user_name | varchar | not null | 用户名 | | user_name | varchar | not null | 用户名 |
| password | 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 # 论文 sys_paper
| 列名 | 数据类型 | 约束条件 | 含义 | | 列名 | 数据类型 | 约束条件 | 含义 |
@ -33,9 +17,11 @@
| profession | varchar | not null | 学科专业 | | profession | varchar | not null | 学科专业 |
| school | varchar | not null | 学校 | | school | varchar | not null | 学校 |
| year | int | not null | 学位年度 | | year | int | not null | 学位年度 |
| summary | varchar | not null | 摘要 | | summary | longtext | not null | 摘要 |
| tag | varchar | 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 | 创建时间戳 | | create_time | Long | not null | 创建时间戳 |
# 论文评价 sys_paper_rating # 论文评价 sys_paper_rating
@ -48,6 +34,15 @@
| paper_id | bigint | not null | 论文id | | paper_id | bigint | not null | 论文id |
| user_name | varchar | not null | 评价用户 | | 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 # 笔记 sys_note
| 列名 | 数据类型 | 约束条件 | 含义 | | 列名 | 数据类型 | 约束条件 | 含义 |
@ -56,6 +51,7 @@
| note_title | varchar | not null | 笔记标题 | | note_title | varchar | not null | 笔记标题 |
| note_content | varchar | not null | 笔记内容 | | note_content | varchar | not null | 笔记内容 |
| paper_id | bigint | not null | 论文id | | paper_id | bigint | not null | 论文id |
| paper_note_id | bigint | not null | html笔记id |
| user_name | varchar | not null | 笔记用户 | | user_name | varchar | not null | 笔记用户 |
# 收藏 sys_collect # 收藏 sys_collect
@ -65,4 +61,17 @@
| id | bigint | primary key; identity | 收藏id | | id | bigint | primary key; identity | 收藏id |
| paper_id | bigint | not null | 收藏论文id | | paper_id | bigint | not null | 收藏论文id |
| user_name | varchar | not null | 收藏用户 | | user_name | varchar | not null | 收藏用户 |
| create_time | Long | not null | 收藏时间戳 | | 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 | 相似度 |

@ -67,13 +67,6 @@
<version>2.6</version> <version>2.6</version>
</dependency> </dependency>
<!-- PDF解析-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.20</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -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();
}
}
}

@ -11,7 +11,6 @@ import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.dto.DeleteNote; import com.bupt.note.dto.DeleteNote;
import com.bupt.note.dto.NoteForm; import com.bupt.note.dto.NoteForm;
import com.bupt.note.dto.Page; import com.bupt.note.dto.Page;
import com.bupt.note.service.FileService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -19,11 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.persistence.criteria.Predicate; 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -41,9 +35,6 @@ public class NoteController {
@Autowired @Autowired
private PaperRepository paperRepository; private PaperRepository paperRepository;
@Autowired
private FileService fileService;
private final Logger logger = LoggerFactory.getLogger(NoteController.class); private final Logger logger = LoggerFactory.getLogger(NoteController.class);
/** /**
@ -62,39 +53,17 @@ public class NoteController {
note.setUserName(username); note.setUserName(username);
noteRepository.save(note); noteRepository.save(note);
if (noteRepository.existsById(note.getNoteId())) { if (noteRepository.existsById(note.getNoteId())) {
Writer wr = null; PaperNote paperNote;
try { if (paperNoteRepository.existsByPaperIdAndUserName(noteForm.getPaperId(), username)) {
PaperNote paperNote; paperNote = paperNoteRepository.findByPaperIdAndUserName(note.getPaperId(), username);
File f; } else {
if (paperNoteRepository.existsByPaperIdAndUserName(noteForm.getPaperId(), username)) { paperNote = new PaperNote();
paperNote = paperNoteRepository.findByPaperIdAndUserName(note.getPaperId(), username); paperNote.setPaperId(noteForm.getPaperId());
f = new File(paperNote.getFilePath()); paperNote.setUserName(username);
} 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));
}
}
} }
logger.error(String.format("更新论文id=%d失败", noteForm.getPaperId())); paperNote.setTxtContent(noteForm.getContent());
return ResponseDataUtil.buildError(); paperNoteRepository.save(paperNote);
return ResponseDataUtil.buildSuccess(note);
} else { } else {
logger.error("保存笔记失败"); logger.error("保存笔记失败");
return ResponseDataUtil.buildError(); return ResponseDataUtil.buildError();
@ -155,26 +124,9 @@ public class NoteController {
paperNoteRepository.existsByPaperIdAndUserName(deleteNote.getPaperId(),username)){ paperNoteRepository.existsByPaperIdAndUserName(deleteNote.getPaperId(),username)){
noteRepository.deleteById(deleteNote.getNoteId()); noteRepository.deleteById(deleteNote.getNoteId());
PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(deleteNote.getPaperId(), username); PaperNote paperNote = paperNoteRepository.findByPaperIdAndUserName(deleteNote.getPaperId(), username);
File f = new File(paperNote.getFilePath()); paperNote.setTxtContent(deleteNote.getContent());
Writer wr = null; paperNoteRepository.save(paperNote);
try { return ResponseDataUtil.buildSuccess();
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("删除笔记失败");
}else{ }else{
logger.error("删除笔记表单校验失败"); logger.error("删除笔记表单校验失败");
} }

@ -1,28 +1,29 @@
package com.bupt.note.Controller; package com.bupt.note.Controller;
import com.bupt.note.Model.Paper; import com.bupt.note.Model.*;
import com.bupt.note.Model.Rating; import com.bupt.note.Repository.*;
import com.bupt.note.Repository.CollectRepository;
import com.bupt.note.Repository.PaperRepository;
import com.bupt.note.Repository.RatingRepository;
import com.bupt.note.ResponseData.ResponseData; import com.bupt.note.ResponseData.ResponseData;
import com.bupt.note.ResponseData.ResponseDataUtil; import com.bupt.note.ResponseData.ResponseDataUtil;
import com.bupt.note.dto.QueryPaper; import com.bupt.note.dto.QueryPaper;
import com.bupt.note.dto.RatingForm; import com.bupt.note.dto.RatingForm;
import com.bupt.note.dto.UploadPaper; import com.bupt.note.dto.UploadPaper;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.query.criteria.internal.OrderImpl;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 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.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; 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") @RequestMapping(value = "/v1/api/paper")
public class PaperController { public class PaperController {
private Logger logger= LoggerFactory.getLogger(PaperController.class); private Logger logger = LoggerFactory.getLogger(PaperController.class);
@Autowired @Autowired
private PaperRepository paperRepository; private PaperRepository paperRepository;
@ -42,59 +43,68 @@ public class PaperController {
@Autowired @Autowired
private CollectRepository collectRepository; private CollectRepository collectRepository;
@Autowired
private PaperNoteRepository paperNoteRepository;
@Autowired
private SimilarityRepository similarityRepository;
@Value("${score}")
private Integer score;
@PostMapping("upload") @PostMapping("upload")
public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) { public ResponseData upload(@RequestBody UploadPaper uploadPaper, @CookieValue("user") String username) {
if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(),uploadPaper.getType(), if (uploadPaper.getFileId() != null && uploadPaper.getYear() != null && StringUtils.isNoneEmpty(uploadPaper.getTitle(), uploadPaper.getType(),
uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(),uploadPaper.getTag())){ uploadPaper.getAuthor(), uploadPaper.getProfession(), uploadPaper.getSchool(), uploadPaper.getSummary(), uploadPaper.getTag())) {
Paper paper = uploadPaper.toPaper(); Paper paper = uploadPaper.toPaper();
paperRepository.save(paper); paperRepository.save(paper);
if(paperRepository.existsById(paper.getId())){ if (paperRepository.existsById(paper.getId())) {
return ResponseDataUtil.buildSuccess(); return ResponseDataUtil.buildSuccess();
}else{ } else {
logger.error("论文上传失败"); logger.error("论文上传失败");
return ResponseDataUtil.buildError(); return ResponseDataUtil.buildError();
} }
}else{ } else {
logger.error("论文表单校验失败"); logger.error("论文表单校验失败");
return ResponseDataUtil.buildError(); return ResponseDataUtil.buildError();
} }
} }
@GetMapping("list") @GetMapping("list")
public ResponseData list(QueryPaper queryPaper,@CookieValue("user") String username){ public ResponseData list(QueryPaper queryPaper, @CookieValue("user") String username) {
try { try {
// Page<Paper> list=new Page<>();
// list.setTotal(paperRepository.count());
Page<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> { Page<Paper> papers = paperRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>(); List<Predicate> predicates = new ArrayList<>();
if(StringUtils.isNotEmpty(queryPaper.getTitle())){ if (StringUtils.isNotEmpty(queryPaper.getTitle())) {
predicates.add(criteriaBuilder.like(root.get("title"),"%"+queryPaper.getTitle()+"%")); predicates.add(criteriaBuilder.like(root.get("title"), "%" + queryPaper.getTitle() + "%"));
} }
if(StringUtils.isNotEmpty(queryPaper.getAuthor())){ if (StringUtils.isNotEmpty(queryPaper.getAuthor())) {
predicates.add(criteriaBuilder.like(root.get("author"),"%"+queryPaper.getAuthor()+"%")); predicates.add(criteriaBuilder.like(root.get("author"), "%" + queryPaper.getAuthor() + "%"));
} }
if(StringUtils.isNotEmpty(queryPaper.getTag())){ if (StringUtils.isNotEmpty(queryPaper.getTag())) {
predicates.add(criteriaBuilder.like(root.get("tag"),"%"+queryPaper.getTag()+"%")); predicates.add(criteriaBuilder.like(root.get("tag"), "%" + queryPaper.getTag() + "%"));
} }
if(StringUtils.isNotEmpty(queryPaper.getProfession())){ if (StringUtils.isNotEmpty(queryPaper.getProfession())) {
predicates.add(criteriaBuilder.like(root.get("profession"),"%"+queryPaper.getTag()+"%")); predicates.add(criteriaBuilder.like(root.get("profession"), "%" + queryPaper.getTag() + "%"));
} }
if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()==null){ if (queryPaper.getStartYear() != null && queryPaper.getEndYear() == null) {
predicates.add(criteriaBuilder.ge(root.get("year"),queryPaper.getStartYear())); predicates.add(criteriaBuilder.ge(root.get("year"), queryPaper.getStartYear()));
}else if(queryPaper.getStartYear()==null&&queryPaper.getEndYear()!=null){ } else if (queryPaper.getStartYear() == null && queryPaper.getEndYear() != null) {
predicates.add(criteriaBuilder.le(root.get("year"),queryPaper.getEndYear())); predicates.add(criteriaBuilder.le(root.get("year"), queryPaper.getEndYear()));
}else if(queryPaper.getStartYear()!=null&&queryPaper.getEndYear()!=null){ } else if (queryPaper.getStartYear() != null && queryPaper.getEndYear() != null) {
predicates.add(criteriaBuilder.between(root.get("year"),queryPaper.getStartYear(),queryPaper.getEndYear())); predicates.add(criteriaBuilder.between(root.get("year"), queryPaper.getStartYear(), queryPaper.getEndYear()));
} }
if(queryPaper.getOwn()){ if (queryPaper.getOwn()) {
predicates.add(criteriaBuilder.equal(root.get("userName"),username)); predicates.add(criteriaBuilder.equal(root.get("userName"), username));
} }
Order order=new OrderImpl(root.get("createTime"),false); return criteriaQuery.where(predicates.toArray(new Predicate[0])).getRestriction();
return criteriaQuery.where(predicates.toArray(new Predicate[0])).orderBy(order).getRestriction(); }, PageRequest.of(queryPaper.getPage() - 1, 10, Sort.Direction.DESC, "createTime"));
}, PageRequest.of(queryPaper.getPage()-1,10));
papers.forEach(paper -> { 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); return ResponseDataUtil.buildSuccess(papers);
} catch (Exception e) { } catch (Exception e) {
@ -107,40 +117,87 @@ public class PaperController {
/** /**
* 论文推荐 * 论文推荐
*
* @return * @return
*/ */
@GetMapping("recommend") @GetMapping("recommend")
public ResponseData recommend(){ public ResponseData recommend(@CookieValue("user") String userName) {
List<Paper> papers=paperRepository.recommend(); // 查询已收藏论文
return ResponseDataUtil.buildSuccess(papers); List<Long> collectIds = collectRepository.findByUserName(userName).stream().map(Collect::getPaperId).collect(Collectors.toList());
//查询喜欢论文
List<Long> likeIds = ratingRepository.findLike(score);
List<Long> 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<Similarity> similarities = similarityRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<Long> paperIds=similarities.stream().map(similarity -> {
if (ids.contains(similarity.getId().getSourcePaper())) {
return similarity.getId().getTargetPaper();
} else {
return similarity.getId().getSourcePaper();
}
}).collect(Collectors.toList());
List<Paper> 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") @PostMapping("rating")
public ResponseData rating(@RequestBody RatingForm ratingForm,@CookieValue("user") String userName) { public ResponseData rating(@RequestBody RatingForm ratingForm, @CookieValue("user") String userName) {
if(ratingForm.getPaperId()!=null&&ratingForm.getScore1()!=null&&ratingForm.getScore2()!=null&&ratingForm.getScore3()!=null){ if (ratingForm.getPaperId() != null && ratingForm.getScore1() != null && ratingForm.getScore2() != null && ratingForm.getScore3() != null) {
Rating rating=ratingForm.toRating(); Rating rating = ratingForm.toRating();
rating.setUserName(userName); rating.setUserName(userName);
ratingRepository.save(rating); ratingRepository.save(rating);
if(ratingRepository.existsById(rating.getId())){ if (ratingRepository.existsById(rating.getId())) {
return ResponseDataUtil.buildSuccess(rating); return ResponseDataUtil.buildSuccess(rating);
}else{ } else {
logger.warn("论文保存失败"); logger.warn("论文保存失败");
return ResponseDataUtil.buildSuccess(); return ResponseDataUtil.buildSuccess();
} }
}else{ } else {
logger.error("论文表单校验失败"); logger.error("论文表单校验失败");
return ResponseDataUtil.buildError(); return ResponseDataUtil.buildError();
} }
} }
@GetMapping("findRating") @GetMapping("findRating")
public ResponseData findRating(Long paperId,@CookieValue("user") String userName){ public ResponseData findRating(Long paperId, @CookieValue("user") String userName) {
if(paperId!=null&&StringUtils.isNotEmpty(userName)){ if (paperId != null && StringUtils.isNotEmpty(userName)) {
Rating rating=ratingRepository.findByPaperIdAndUserName(paperId,userName); Rating rating = ratingRepository.findByPaperIdAndUserName(paperId, userName);
return ResponseDataUtil.buildSuccess(rating); return ResponseDataUtil.buildSuccess(rating);
}else{ } else {
logger.error("论文评价表单校验失败"); logger.error("论文评价表单校验失败");
return ResponseDataUtil.buildError(); 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();
}
}
} }

@ -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;
}
}

@ -18,7 +18,9 @@ public class Paper {
private Integer year; private Integer year;
private String summary; private String summary;
private String tag; private String tag;
private Long fileId; private byte[] pdfContent;
private byte[] htmlContent;
private String txtContent;
private Long createTime; private Long createTime;
private boolean hasCollect; private boolean hasCollect;
private Long collectTime; private Long collectTime;
@ -87,7 +89,8 @@ public class Paper {
this.year = year; this.year = year;
} }
@Column(nullable = false,length = 512) @Lob
@Column(nullable = false)
public String getSummary() { public String getSummary() {
return summary; return summary;
} }
@ -105,13 +108,34 @@ public class Paper {
this.tag = tag; 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) @Column(nullable = false)
public Long getFileId() { public String getTxtContent() {
return fileId; return txtContent;
} }
public void setFileId(Long fileId) { public void setTxtContent(String txtContent) {
this.fileId = fileId; this.txtContent = txtContent;
} }
@Column(nullable = false) @Column(nullable = false)

@ -7,7 +7,7 @@ import javax.persistence.*;
public class PaperNote { public class PaperNote {
private Long id; private Long id;
private Long paperId; private Long paperId;
private String filePath; private String txtContent;
private String userName; private String userName;
@Id @Id
@ -20,13 +20,14 @@ public class PaperNote {
this.id = id; this.id = id;
} }
@Lob
@Column(nullable = false) @Column(nullable = false)
public String getFilePath() { public String getTxtContent() {
return filePath; return txtContent;
} }
public void setFilePath(String filePath) { public void setTxtContent(String txtContent) {
this.filePath = filePath; this.txtContent = txtContent;
} }
@Column(nullable = false) @Column(nullable = false)

@ -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;
}
}

@ -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);
}
}

@ -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;
}
}

@ -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<File, Long> {
}

@ -4,10 +4,12 @@ import com.bupt.note.Model.Paper;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List; import java.util.List;
public interface PaperRepository extends JpaRepository<Paper, Long>, JpaSpecificationExecutor<Paper> { public interface PaperRepository extends JpaRepository<Paper, Long>, JpaSpecificationExecutor<Paper> {
@Query(value = "select *\n" + @Query(value = "select *\n" +
"from sys_paper\n" + "from sys_paper\n" +
"where id in (select a.id\n" + "where id in (select a.id\n" +
@ -18,5 +20,8 @@ public interface PaperRepository extends JpaRepository<Paper, Long>, JpaSpecific
" group by p.id\n" + " group by p.id\n" +
" order by score desc\n" + " order by score desc\n" +
" limit 10) a)",nativeQuery = true) " limit 10) a)",nativeQuery = true)
List<Paper> recommend(); List<Paper> recommendWithScore();
@Query(value = "select * from sys_paper where id in (:ids) order by FIELD(`id`,:ids)",nativeQuery = true)
List<Paper> recommendWithSim(@Param("ids") List<Long> ids);
} }

@ -2,7 +2,14 @@ package com.bupt.note.Repository;
import com.bupt.note.Model.Rating; import com.bupt.note.Model.Rating;
import org.springframework.data.jpa.repository.JpaRepository; 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<Rating, Long> { import java.util.List;
public interface RatingRepository extends JpaRepository<Rating, Long>, JpaSpecificationExecutor<Rating> {
Rating findByPaperIdAndUserName(Long paperId, String userName); Rating findByPaperIdAndUserName(Long paperId, String userName);
@Query(value = "select id from sys_paper_rating where score1+score2+score3>?1",nativeQuery = true)
List<Long> findLike(int score);
} }

@ -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<Similarity, SimilarityId>, JpaSpecificationExecutor<Similarity> {
}

@ -95,7 +95,6 @@ public class UploadPaper {
paper.setYear(this.year); paper.setYear(this.year);
paper.setSummary(this.summary); paper.setSummary(this.summary);
paper.setTag(this.tag); paper.setTag(this.tag);
paper.setFileId(this.fileId);
paper.setCreateTime(System.currentTimeMillis()); paper.setCreateTime(System.currentTimeMillis());
return paper; return paper;
} }

@ -1,8 +1,5 @@
package com.bupt.note.service; 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.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -11,7 +8,6 @@ import org.springframework.util.ResourceUtils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.UUID; import java.util.UUID;
@ -35,17 +31,4 @@ public class FileService {
public File newFile() throws FileNotFoundException { public File newFile() throws FileNotFoundException {
return newFile(UUID.randomUUID() + ".txt"); 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("解析文件内容失败");
}
}
} }

@ -13,5 +13,6 @@ spring:
static-path-pattern: /file/** static-path-pattern: /file/**
resources: resources:
static-locations: classpath:/resources/static/file static-locations: classpath:/resources/static/file
file: #喜好评价分
upload-dir: note_file/ score: 7

@ -1,39 +1,11 @@
package com.bupt.note; package com.bupt.note;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.IOException;
public class MyTest { public class MyTest {
@Test @Test
public void test1() { public void test1() {
System.out.println(String.format("%d=%d", 1, 1L)); 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));
}
} }

@ -2,6 +2,8 @@ package com.bupt.note;
import com.bupt.note.Model.Paper; import com.bupt.note.Model.Paper;
import com.bupt.note.Repository.PaperRepository; 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 com.bupt.note.service.FileService;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import java.io.IOException; import java.util.ArrayList;
import java.util.List; import java.util.List;
@SpringBootTest @SpringBootTest
@ -32,6 +34,12 @@ class NoteApplicationTests {
@Autowired @Autowired
private PaperRepository paperRepository; private PaperRepository paperRepository;
@Autowired
private RatingRepository ratingRepository;
@Autowired
private SimilarityRepository similarityRepository;
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
mvc = MockMvcBuilders.webAppContextSetup(context).build(); mvc = MockMvcBuilders.webAppContextSetup(context).build();
@ -46,15 +54,10 @@ class NoteApplicationTests {
mvc.perform(builder).andDo(MockMvcResultHandlers.print()); 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 @Test
public void testPaper(){ public void testPaper(){
List<Paper> papers=paperRepository.recommend(); List<Paper> ids= paperRepository.recommendWithSim(new ArrayList<>(){{
System.out.println(papers.size()); add(5L);
}});
} }
} }

Loading…
Cancel
Save