1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| package cn.edu.bupt.aiswitchboard.service;
import cn.edu.bupt.aiswitchboard.config.ApplicationConfig; import cn.edu.bupt.aiswitchboard.dao.WorkerDao; import cn.edu.bupt.aiswitchboard.model.NameFinderFindRequest; import cn.edu.bupt.aiswitchboard.model.NameFinderInsertRequest; import cn.edu.bupt.aiswitchboard.model.Worker; import cn.edu.bupt.aiswitchboard.utils.ScoreUtils; import cn.edu.bupt.aiswitchboard.utils.PinyinUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
import java.util.*;
@Service public class NameFinderServiceImpl implements NameFinderService{
private final ApplicationConfig applicationConfig; public NameFinderServiceImpl(ApplicationConfig applicationConfig) { this.applicationConfig = applicationConfig; }
@Autowired private WorkerDao workerDao;
@Override public Object find(NameFinderFindRequest requestParameters) { Map<String, Object> data = new HashMap<>(); data.put("corpId", requestParameters.getCorpId()); data.put("taskId", requestParameters.getTaskId());
String content = requestParameters.getContent();
String mandarinContent = PinyinUtils.getPinyin(content, "withTone"); String mandarinStripContent = PinyinUtils.getPinyin(content, "withoutTone"); String mandarinSetContent = PinyinUtils.toMandarinSet(mandarinStripContent);
List<Worker> workerList = workerDao.selectList(null); List<Map<String, Object>> matchRes = new ArrayList<>();
for (Worker worker : workerList) { String dbPer = worker.getPer(); String dbDep = worker.getDep(); String dbMandarinPer = worker.getMandarinPer(); String dbMandarinStripPer = worker.getMandarinStripPer(); String dbMandarinDep = worker.getMandarinDep(); String dbMandarinStripDep = worker.getMandarinStripDep(); String dbMandarinSet = worker.getMandarinSet();
double scoreChar = ScoreUtils.basicEditDistance(content, dbPer+dbDep); double scorePinyinTone = ScoreUtils.strHashEditDistance(mandarinContent, dbMandarinPer+dbMandarinDep); double scorePinyinWithoutTone = ScoreUtils.strHashEditDistance(mandarinStripContent, dbMandarinStripPer+dbMandarinStripDep); double scoreSet = ScoreUtils.setScore(mandarinSetContent, dbMandarinSet);
double totalScore = applicationConfig.getWeightChar() * scoreChar + applicationConfig.getWeightPinyinTone() * scorePinyinTone + applicationConfig.getWeightPinyinWithoutTone() * scorePinyinWithoutTone + applicationConfig.getWeightSet() * scoreSet;
Map<String, Object> mp = new HashMap<>(); mp.put("per", dbPer); mp.put("dep", dbDep); mp.put("score", totalScore); matchRes.add(mp); }
Collections.sort(matchRes, new Comparator<Map<String, Object>>() { @Override public int compare(Map<String, Object> o1, Map<String, Object> o2) { double score1 = (double) o1.get("score"); double score2 = (double) o2.get("score"); return Double.compare(score2, score1); } }); data.put("match", matchRes);
return data; }
public Object insert(NameFinderInsertRequest requestParameters) { Map<String, Object> data = new HashMap<>();
String corpId = requestParameters.getCorpId(); String dep = requestParameters.getDep(); String per = requestParameters.getPer();
String mandarinPer = PinyinUtils.getPinyin(per, "withTone"); String mandarinStripPer = PinyinUtils.getPinyin(per, "withoutTone"); String mandarinDep = PinyinUtils.getPinyin(dep, "withTone"); String mandarinStripDep = PinyinUtils.getPinyin(dep, "withoutTone");
String mandarinSet = PinyinUtils.toMandarinSet(mandarinStripPer) + PinyinUtils.toMandarinSet(mandarinStripDep); System.out.println("mandarinSet: " + mandarinSet);
Worker worker = new Worker(corpId, dep, per, "12345678", "12345678", mandarinPer, mandarinStripPer, mandarinDep, mandarinStripDep, mandarinSet, "", ""); workerDao.insert(worker);
return data; } }
|