@SpringBootTest
public class StringRelevanceCalculationApplicationTests {
@Data
private static class Paper {
private String title;
int score = 0;
public void setScore(List<String> keyWords) {
for (String keyWord : keyWords) {
this.score += StrUtil.count(title.toLowerCase(), keyWord.toLowerCase());
}
}
public Paper(String title) {
this.title = title;
}
}
static List<Paper> list = new ArrayList<Paper>() {{
add(new Paper("Java 和 Python 对比,看完这篇你就知道有什么不同了"));
add(new Paper("三分钟看懂Python和Java的区别"));
add(new Paper("Python vs Java:区别有哪些,哪个更好,更适合您的项目?"));
add(new Paper("Java 使用 Redis"));
add(new Paper("Python操作Redis,你要的都在这了!"));
add(new Paper("写给新手的SpringBoot完整教程"));
add(new Paper("使用IDEA搭建一个简单的SpringBoot项目"));
}};
@Test
void contextLoads() {
TokenizerEngine engine = TokenizerUtil.createEngine();
String text = "python和java的区别";
List<String> keys = new ArrayList<>();
Result words = engine.parse(text);
for (Word word : words) {
String t = word.getText();
if (t.length() > 1)
keys.add(t);
}
list.parallelStream().forEach(paper -> paper.setScore(keys));
list = list.stream()
.filter(p -> p.getScore() > 0)
.sorted(Comparator.comparingInt(Paper::getScore).reversed())
.collect(Collectors.toList());
for (Paper paper : list) {
System.out.println(paper);
}
}
}
StringRelevanceCalculationApplicationTests.Paper(title=三分钟看懂Python和Java的区别, score=3)
StringRelevanceCalculationApplicationTests.Paper(title=Python vs Java:区别有哪些,哪个更好,更适合您的项目?, score=3)
StringRelevanceCalculationApplicationTests.Paper(title=Java 和 Python 对比,看完这篇你就知道有什么不同了, score=2)
StringRelevanceCalculationApplicationTests.Paper(title=Java 使用 Redis, score=1)
StringRelevanceCalculationApplicationTests.Paper(title=Python操作Redis,你要的都在这了!, score=1)