AddressCommon.java 5.44 KB
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<Term> termList = AddressSegment.getAddressSegment().seg(address);
		//CustomDictionary.add("七台河市", "ns");
		//按序取词性为ns
		List<Term> allNs = new ArrayList<Term>();
		for(int i=0; i<termList.size(); ++i){
			Term item = termList.get(i);
			if(item.nature.startsWith(Nature.ns.toString())){
				allNs.add(item);
			}
		}
		//UtilLog.infoForMessage(termList.toString(), this.getClass());
		
		BeanAddress result = new BeanAddress();
		if(allNs.size() >= 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<RegionPcdsEntity> 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<RegionPcdsEntity> pList, Term city){
		int matchCount = 0;
		for(int i=0; i<pList.size(); ++i){
			List<RegionPcdsEntity> 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<RegionPcdsEntity> pList, Term district, String address) throws Exception{
		int matchCount = 0;
		RegionPcdsEntity matchPEntity = null;
		RegionPcdsEntity matchCEntity = null;
		RegionPcdsEntity matchDEntity = null;
		for(int i=0; i<pList.size(); ++i){
			List<RegionPcdsEntity> dList = this.regionPcdsRepo.findListBySql("level="+RegionPcdsRepo.LEVEL_DISTRICT+" and name like '%"+district.word+"%'");
			if(dList != null && dList.size() == 1){
				for(int j=0; j<dList.size(); ++j){
					List<RegionPcdsEntity> 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<RegionPcdsEntity> 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<cList.size(); ++i){
				List<RegionPcdsEntity> 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 List<String>findOrderPdcResult(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 ;
		
		
	}
}