匹配改造

This commit is contained in:
沈健
2025-04-14 17:50:00 +08:00
parent 1c61cc3b76
commit b361dbae19
@@ -1,25 +1,23 @@
package com.fangjia.service.api;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import com.fangjia.core.util.GeoUtils;
import com.fangjia.core.util.HttpRequest;
import com.fangjia.core.util.HttpRequestException;
import com.fangjia.core.util.JsonUtils;
import com.fangjia.core.util.*;
import com.fangjia.dal.mongo.open.model.WaitCrawlKeywords;
import com.fangjia.service.address.AddressParserService;
import com.fangjia.service.address.AddressParserUtil;
import com.fangjia.service.address.mode.*;
import com.fangjia.service.bo.CheckMatchDistrictCorrectParam;
import com.fangjia.service.bo.MatchAddressScore;
import com.fangjia.service.bo.MatchDistrictResult;
import com.fangjia.service.datastore.EvaluationService;
import com.fangjia.service.mongo.bkoff.HouseLoudongService;
import com.fangjia.service.open.WaitCrawlKeywordsService;
import com.fangjia.service.sql.FjtEvaluationService;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import java.util.*;
/**
* Created by wenyan on 2016/1/15.
@@ -28,17 +26,97 @@ public class AddressMatchProxyServiceImpl extends AbstractHttpApiService impleme
public static final int MIN_TIMEOUT = 1000 * 2;
private final static String TOKEN = "F54F52381C49BB9EB4A33EB1B65604AE4B71A28AEE53518A94A2F360408B9056D57553931D15CE6DDE765562DAD286BA38E05A4CDAFC51C3D527A4959BF8E75A3B95DB7108FCEA340DDE61925616DB55118E1851E67D83EAD800460D100D6B667A4ED8EE67C8F7FB";
private AddressParserService addressParserService;
public AddressMatchProxyServiceImpl(String apiHost) {
private HouseLoudongService houseLoudongService;
private FjtEvaluationService fjtEvaluationService;
private EvaluationService evaluationService;
private WaitCrawlKeywordsService waitCrawlKeywordsService;
public AddressMatchProxyServiceImpl(String apiHost) {
super(apiHost);
}
private String realMatch(Map<String, Object> paramsMap) {
String address = paramsMap.get("address").toString();
address = address.replaceAll("@@", "#");// 框架无法处理参数带有#的Buge 用@@代替
BuildingType buildingType = null;
if (paramsMap.containsKey("category")) {
if ("property".equals(paramsMap.get("category"))) {
paramsMap.put("category", "district");
}
try {
buildingType = BuildingType.valueOf(((String)paramsMap.get("category")).toUpperCase());
} catch (Exception e) {
}
}
PreciseLevel preciseLevel = PreciseLevel.FUZZY;
if ("strict".equals(paramsMap.get("preciseLevel"))) {
preciseLevel = PreciseLevel.STRICT;
}
MatchParams params = new MatchParams();
if (paramsMap.containsKey("city")) {
params.setCity(paramsMap.get("city").toString());
}
if (paramsMap.containsKey("suggestDistrict")) {
params.setSuggestDistrict(paramsMap.get("suggestDistrict").toString());
}
if (paramsMap.containsKey("suggestName")) {
params.setSuggestName(paramsMap.get("suggestName").toString());
}
params.setAddress(address);
params.setBuildingType(buildingType);
params.setPreciseLevel(preciseLevel);
if (paramsMap.containsKey("province")) {
params.setProvince(paramsMap.get("province").toString());
}
if (paramsMap.containsKey("useMap")) {
params.setUseMap((Boolean) paramsMap.get("useMap"));
}
if (paramsMap.containsKey("debugName")) {
params.setDebugName(paramsMap.get("debugName").toString());
}
if (paramsMap.containsKey("propertyType")) {
params.setPropertyType(paramsMap.get("propertyType").toString());
}
long start = System.currentTimeMillis();
Building building = addressParserService.parser(params);
long end = System.currentTimeMillis();
if (end - start > 200) {
LOGGER.warn("匹配parser耗时:\t" + (end - start) + "ms");
}
if (paramsMap.containsKey("verify") && "true".equalsIgnoreCase(paramsMap.get("verify").toString())) {
verify(building);
}
JSONObject mav = new JSONObject();
mav.put("viewType", "rest");
mav.put("viewParam", new JSONObject().put("contentType", "application/json;charset=UTF-8"));
JSONObject baseModel = new JSONObject();
mav.put("model", baseModel);
Map<String, Object> result = convertBuildingToMap(building);
if (paramsMap.containsKey("token") && !TOKEN.equals(paramsMap.get("token"))) {
submitFetchTask(building);
result.remove("precise");
result.remove("searchAddress");
result.remove("map");
}
baseModel.put("code", 200);
baseModel.put("msg", "ok");
baseModel.put("result", result);
return mav.toString();
}
@Override
public String parser(Map<String, Object> params) {
params.put("token", TOKEN);
long start = System.currentTimeMillis();
try {
String result = this.invoke("address/realMatch", params, "UTF-8");
String result = realMatch(params);
return result;
} catch (JSONException e) {
LOGGER.error("", e);
@@ -429,5 +507,121 @@ public class AddressMatchProxyServiceImpl extends AbstractHttpApiService impleme
return null;
}
Map<String, Object> convertBuildingToMap(Building building) {
Map<String, Object> result = new LinkedHashMap<String, Object>();
if (building != null) {
result.put("id", building.getId());
result.put("credit", building.getCredit());
result.put("precise", building.getPrecise());
result.put("province", building.getProvince());
result.put("city", building.getCity());
result.put("district", building.getDistrict());
result.put("name", building.getName());
result.put("suggestName", building.getSuggestName());
result.put("map", building.getMap());
result.put("propertyType", building.getPropertyType());
result.put("township", building.getTownship());
result.put("suggestAddress", building.getSuggestAddress());
if ("district".equals(building.getCategory())) {
result.put("category", "property");
} else {
result.put("category", building.getCategory());
}
result.put("address", building.getAddress());
result.put("block", building.getBlock());
result.put("lng", building.getLng());
result.put("lat", building.getLat());
result.put("alias", building.getAlias());
result.put("searchAddress", building.getSearchAddress());
}
return result;
}
private void submitFetchTask(Building building) {
if (building.getPrecise() == -1) {
List<String> keywords = new ArrayList<String>();
Address address = building.getSearchAddress();
if (StringUtils.isNotBlank(address.getKeywords()) && address.getKeywords().length() > 2) {
keywords.add(address.getKeywords());
} else if (address.getOrgAddress().length() > 2 && address.getOrgAddress().length() < 10) {// 一般小区名不会超过10个字
keywords.add(address.getOrgAddress());
}
if (address.isStandardAddress()) {
if (address.getStreetNumbers() != null) {
for (String b : address.getStreetNumbers()) {
keywords.add(address.getStreet() + b);
}
} else {
keywords.add(address.getStreet());
}
}
keywords = DataUtil.uniqueList(keywords);
for (String k : keywords) {
WaitCrawlKeywords waitCrawlKeywords = new WaitCrawlKeywords();
waitCrawlKeywords.setCity(address.getCity());
waitCrawlKeywords.setDateCreated(new Date());
waitCrawlKeywords.setRead(0);
waitCrawlKeywords.setKeywords(k);
waitCrawlKeywordsService.save(waitCrawlKeywords);
}
}
}
private void verify(Building building) {
String city = building.getCity();
String region = building.getDistrict();
String name = null;
if (building.getPrecise() > -1) {
name = building.getName();
} else {
name = building.getSearchAddress().getOrgKeywords();
}
if (StringUtils.isNotBlank(region) && StringUtils.isNotBlank(name)) {
String buildingNumber = AddressParserUtil
.standardizeBuildingNumber(building.getSearchAddress().getOrgBuildingNumber());
if (buildingNumber != null) {
if (houseLoudongService.findHouseLoudong(city, region, name, buildingNumber, null, null) != null) {
building.getSearchAddress().setBuildingNumberCertified(true);
} else if (fjtEvaluationService.findFjtEvaluationByConditions(city, region, name, buildingNumber,
null) != null) {
building.getSearchAddress().setBuildingNumberCertified(true);
} else if (evaluationService.findEvaluationByNum(city, region, name, buildingNumber, null) != null) {
building.getSearchAddress().setBuildingNumberCertified(true);
}
}
String roomNumber = AddressParserUtil.standardizeRoomNumber(building.getSearchAddress().getOrgRoomNumber());
if (roomNumber != null) {
if (houseLoudongService.findHouseLoudong(city, region, name, null, null, roomNumber) != null) {
building.getSearchAddress().setRoomNumberCertified(true);
} else if (fjtEvaluationService.findFjtEvaluationByConditions(city, region, name, null,
roomNumber) != null) {
building.getSearchAddress().setRoomNumberCertified(true);
} else if (evaluationService.findEvaluationByNum(city, region, name, null, roomNumber) != null) {
building.getSearchAddress().setRoomNumberCertified(true);
}
}
}
}
public void setHouseLoudongService(HouseLoudongService houseLoudongService) {
this.houseLoudongService = houseLoudongService;
}
public void setFjtEvaluationService(FjtEvaluationService fjtEvaluationService) {
this.fjtEvaluationService = fjtEvaluationService;
}
public void setEvaluationService(EvaluationService evaluationService) {
this.evaluationService = evaluationService;
}
public void setAddressParserService(AddressParserService addressParserService) {
this.addressParserService = addressParserService;
}
public void setWaitCrawlKeywordsService(WaitCrawlKeywordsService waitCrawlKeywordsService) {
this.waitCrawlKeywordsService = waitCrawlKeywordsService;
}
}