package com.taover.ai.common; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import com.hankcs.hanlp.corpus.tag.Nature; import com.hankcs.hanlp.dictionary.CustomDictionary; import com.hankcs.hanlp.seg.common.Term; import com.taover.ai.bean.normal.BeanAddress; import com.taover.ai.init.AddressSegment; import com.taover.ai.repository.RegionPcdsEntity; import com.taover.ai.repository.RegionPcdsRepo; import com.taover.util.UtilLog; @Service public class AddressCommon { @Resource private RegionPcdsRepo regionPcdsRepo; @Resource private AddressSegment addressSegment; @Resource private JdbcTemplate jdbcTemplate; public BeanAddress pcdAddressOneAnalysis(String address){ List termList = AddressSegment.getAddressSegment().seg(address); //CustomDictionary.add("七台河市", "ns"); //按序取词性为ns List allNs = new ArrayList(); for(int i=0; i= 3){ result.setProvince(allNs.get(0).word); result.setCity(allNs.get(1).word); Term districtTerm = allNs.get(2); result.setDistrict(districtTerm.word); result.setAddress(address.substring(allNs.get(2).offset+districtTerm.word.length())); }else if(allNs.size() == 2){ try{ String[] addInfo = this.completeRegion(allNs.get(0), allNs.get(1), address); result.setProvince(addInfo[0]); result.setCity(addInfo[1]); result.setDistrict(addInfo[2]); result.setAddress(addInfo[3]); }catch(Exception e){ result.setAddress(address); } }else{ result.setAddress(address); } return result; } private String[] completeRegion(Term regionFirst, Term regionSecond, String address) throws Exception{ List pList = this.regionPcdsRepo.findListBySql("level="+RegionPcdsRepo.LEVEL_PROVINCE+" and name like '%"+regionFirst.word+"%'"); if(pList != null && pList.size() > 0){ if(this.isProvinceAndCity(pList, regionSecond)){ //省市 return new String[]{regionFirst.word, regionSecond.word, "", address.substring(regionSecond.offset+regionSecond.length())}; }else{ //省区 return this.completeCity(pList, regionSecond, address); } }else{ //市区 return this.completeProvince(regionFirst, regionSecond, address); } } private boolean isProvinceAndCity(List pList, Term city){ int matchCount = 0; for(int i=0; i cList = this.regionPcdsRepo.findListBySql("level="+RegionPcdsRepo.LEVEL_CITY+" and name like '%"+city.word+"%' and upid="+pList.get(i).getId()); if(cList.size() == 1){ ++matchCount; } } if(matchCount == 1){ return true; }else{ return false; } } private String[] completeCity(List pList, Term district, String address) throws Exception{ int matchCount = 0; RegionPcdsEntity matchPEntity = null; RegionPcdsEntity matchCEntity = null; RegionPcdsEntity matchDEntity = null; for(int i=0; i dList = this.regionPcdsRepo.findListBySql("level="+RegionPcdsRepo.LEVEL_DISTRICT+" and name like '%"+district.word+"%'"); if(dList != null && dList.size() == 1){ for(int j=0; j cList = this.regionPcdsRepo.findListBySql("level="+RegionPcdsRepo.LEVEL_CITY+" and upid="+pList.get(i).getId()+" and id="+dList.get(j).getUpid()); if(cList != null && cList.size() == 1){ ++matchCount; matchDEntity = dList.get(j); matchCEntity = cList.get(0); matchPEntity = pList.get(i); } } } } if(matchCount == 1){ return new String[]{matchPEntity.getName(), matchCEntity.getName(), matchDEntity.getName(), address.substring(district.offset+district.length())}; } throw new Exception("识别失败"); } private String[] completeProvince(Term city, Term district, String address) throws Exception{ List cList = this.regionPcdsRepo.findListBySql("level="+RegionPcdsRepo.LEVEL_CITY+" and name like '%"+city.word+"%'"); if(cList != null && cList.size() > 0){ int matchCount = 0; RegionPcdsEntity matchDEntity = null; RegionPcdsEntity matchCEntity = null; for(int i=0; i dList = this.regionPcdsRepo.findListBySql("level="+RegionPcdsRepo.LEVEL_DISTRICT+" and name like '%"+district.word+"%' and upid="+cList.get(i).getId()); if(dList != null && dList.size() == 1){ ++matchCount; matchDEntity = dList.get(0); matchCEntity = cList.get(i); } } if(matchCount == 1){ RegionPcdsEntity pEntity = this.regionPcdsRepo.findEntityByID(matchCEntity.getUpid()); return new String[]{pEntity.getName(), matchCEntity.getName(), matchDEntity.getName(), address.substring(district.offset+district.length())}; } } throw new Exception("识别失败"); } //查询订单里地址处理结果 private ListfindOrderPdcResult(Integer fromOrderId,Integer toOrderId) { //List list = this.jdbcTemplate.queryForObject("select count(*) from wxorder_excel_data where id!="+entity.getId()+" and file_id = '"+entity.getFileId()+"' and tenant_id ="+tenantId+" limit 1", Integer.class); return null ; } }