更新推荐算法

pdf
pan 4 years ago
parent bb9508c389
commit dce9531a8a
  1. 45
      doc/db_table.md
  2. 7
      pom.xml
  3. 116
      src/main/java/com/bupt/note/Controller/FileController.java
  4. 58
      src/main/java/com/bupt/note/Controller/NoteController.java
  5. 85
      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
@ -66,3 +62,16 @@
| 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;
try {
PaperNote paperNote; PaperNote paperNote;
File f;
if (paperNoteRepository.existsByPaperIdAndUserName(noteForm.getPaperId(), username)) { if (paperNoteRepository.existsByPaperIdAndUserName(noteForm.getPaperId(), username)) {
paperNote = paperNoteRepository.findByPaperIdAndUserName(note.getPaperId(), username); paperNote = paperNoteRepository.findByPaperIdAndUserName(note.getPaperId(), username);
f = new File(paperNote.getFilePath());
} else { } else {
f = fileService.newFile();
paperNote = new PaperNote(); paperNote = new PaperNote();
paperNote.setPaperId(noteForm.getPaperId()); paperNote.setPaperId(noteForm.getPaperId());
paperNote.setFilePath(f.getAbsolutePath()); paperNote.setUserName(username);
paperNoteRepository.save(paperNote);
} }
wr = new FileWriter(f, StandardCharsets.UTF_8); paperNote.setTxtContent(noteForm.getContent());
wr.write(noteForm.getContent()); paperNoteRepository.save(paperNote);
return ResponseDataUtil.buildSuccess(note); 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()));
return ResponseDataUtil.buildError();
} 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 {
wr = new FileWriter(f);
wr.write(deleteNote.getContent());
return ResponseDataUtil.buildSuccess(); 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;
/** /**
* 论文管理接口 * 论文管理接口
@ -42,6 +43,15 @@ 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(),
@ -64,8 +74,6 @@ public class PaperController {
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())) {
@ -90,10 +98,12 @@ public class PaperController {
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.setHtmlContent(null);
paper.setPdfContent(null);
paper.setTxtContent(null);
paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(), username)); paper.setHasCollect(collectRepository.existsByPaperIdAndUserName(paper.getId(), username));
}); });
return ResponseDataUtil.buildSuccess(papers); return ResponseDataUtil.buildSuccess(papers);
@ -107,12 +117,48 @@ public class PaperController {
/** /**
* 论文推荐 * 论文推荐
*
* @return * @return
*/ */
@GetMapping("recommend") @GetMapping("recommend")
public ResponseData recommend(){ public ResponseData recommend(@CookieValue("user") String userName) {
List<Paper> papers=paperRepository.recommend(); // 查询已收藏论文
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); return ResponseDataUtil.buildSuccess(papers);
} else {
return ResponseDataUtil.buildSuccess("暂无推荐数据");
}
} }
@PostMapping("rating") @PostMapping("rating")
@ -143,4 +189,15 @@ public class PaperController {
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