用java实现岗位推荐基于内容的推荐算法代码
时间: 2023-10-10 21:14:00 浏览: 57
岗位推荐常用的基于内容的推荐算法是基于tf-idf权重的文本相似度计算。以下是一个简单的Java实现示例:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ContentBasedJobRecommendation {
// 岗位描述文本数据,格式为Map<岗位ID, 岗位描述>
private Map<Integer, String> jobDescriptions = new HashMap<>();
// 岗位特征向量,格式为Map<岗位ID, Map<词项, 词项权重>>
private Map<Integer, Map<String, Double>> jobFeatureVectors = new HashMap<>();
/**
* 添加一个岗位的描述文本数据
*
* @param id 岗位ID
* @param jobDesc 岗位描述
*/
public void addJobDescription(int id, String jobDesc) {
jobDescriptions.put(id, jobDesc);
updateJobFeatureVector(id, jobDesc);
}
/**
* 更新一个岗位的特征向量
*
* @param id 岗位ID
* @param jobDesc 岗位描述
*/
private void updateJobFeatureVector(int id, String jobDesc) {
Map<String, Double> featureVector = new HashMap<>();
String[] terms = jobDesc.split("\\s+");
for (String term : terms) {
Double tf = featureVector.get(term);
if (tf == null) {
tf = 0.0;
}
featureVector.put(term, tf + 1.0);
}
double norm = 0.0;
for (Double tf : featureVector.values()) {
norm += tf * tf;
}
norm = Math.sqrt(norm);
for (Map.Entry<String, Double> entry : featureVector.entrySet()) {
entry.setValue(entry.getValue() / norm);
}
jobFeatureVectors.put(id, featureVector);
}
/**
* 根据输入的简历文本,推荐与之最匹配的K个岗位
*
* @param resume 简历文本
* @param topK 推荐的岗位数量
* @return 推荐的岗位ID列表
*/
public List<Integer> recommendJobs(String resume, int topK) {
Map<String, Double> resumeFeatureVector = new HashMap<>();
String[] terms = resume.split("\\s+");
for (String term : terms) {
Double tf = resumeFeatureVector.get(term);
if (tf == null) {
tf = 0.0;
}
resumeFeatureVector.put(term, tf + 1.0);
}
double norm = 0.0;
for (Double tf : resumeFeatureVector.values()) {
norm += tf * tf;
}
norm = Math.sqrt(norm);
for (Map.Entry<String, Double> entry : resumeFeatureVector.entrySet()) {
entry.setValue(entry.getValue() / norm);
}
List<Map.Entry<Integer, Double>> jobScores = new ArrayList<>();
for (Map.Entry<Integer, Map<String, Double>> entry : jobFeatureVectors.entrySet()) {
Integer jobId = entry.getKey();
Map<String, Double> jobVector = entry.getValue();
double score = 0.0;
for (Map.Entry<String, Double> termEntry : resumeFeatureVector.entrySet()) {
String term = termEntry.getKey();
Double tfidf = jobVector.get(term);
if (tfidf != null) {
score += termEntry.getValue() * tfidf;
}
}
jobScores.add(Map.entry(jobId, score));
}
jobScores.sort((o1, o2) -> Double.compare(o2.getValue(), o1.getValue()));
List<Integer> recommendedJobs = new ArrayList<>();
for (int i = 0; i < topK && i < jobScores.size(); i++) {
recommendedJobs.add(jobScores.get(i).getKey());
}
return recommendedJobs;
}
}
```
示例代码中,`addJobDescription`方法用于添加岗位描述文本数据,`recommendJobs`方法用于根据输入的简历文本推荐与之最匹配的K个岗位。在`updateJobFeatureVector`方法中,对每个岗位的特征向量进行计算和归一化;在`recommendJobs`方法中,对输入的简历文本也进行特征向量计算和归一化,并计算与每个岗位的相似度得分,并根据得分排序返回前K个岗位。