AddressCommon.java
5.44 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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 ;
}
}