java后台地址(省,市,区)、姓名、手机号算法智能识别

最近项目中需要根据前台需要识别的信息去做后台处理,根据地址识别出来姓名,手机号,以及地址信息返回给前端。我借用了一套算法: 这套算法是androi开发时候用的,我门java用的时候需要修改里面部分内容。
用的时候项目中也必须要有封装好的省、市、区(县信息 作为key,value为对应省市区编码 可以使用map封装。最好去网上搜 因为几千个地区。。。。

private void DiscernAddressInfo(String addressInfo) {
    //解析需要的字段
    String provinceInfo ="", cityInfo = "", countyInfo = "",areaInfo="", phoneInfo ="", nameInfo = "";
    //记录字段的下标
    int provinceIndex=-1,cityIndex=-1,areaIndex=-1,phoneIndex=-1,nameIndex=-1;
     /*识别电话号码*/
    Pattern pattern = Pattern.compile("([1][3-9][\\d]{9})|(0\\d{2,4}-\\d{7,8})");
    Matcher matcher = pattern.matcher(addressInfo);
    StringBuffer bf = new StringBuffer(64);
        while (matcher.find()) {
            bf.append(matcher.group()).append(",");
        }
    if (!StringUtil.isEmpty(phoneInfo)) {
        phoneIndex=addressInfo.indexOf(phoneInfo);
        addressInfo = addressInfo.replace(phoneInfo, "");//去掉已经识别的电话,防止加入详细地址
    }

    /*识别地址信息*/

    List<String> proList = itemNameList(file.getSampleProvinceList()); //省份列表
    for (String province : proList) {
        String tem;
        if (province.contains("自治区")) {
            tem = province.replace("自治区","");
        } else if (province.contains("省")) {
            tem = province.replace("省","");
        } else {
            tem = province;
        }
        if (addressInfo.contains(tem)) {
            provinceInfo = province;
            provinceIndex = addressInfo.indexOf(tem);
            //更新选择地址信息
            this.province=province;

            break;
        }
    }
    if (!StringUtil.isEmpty(provinceInfo)) { //当输入框中有省份的时候
    //根据省名称获取该省份编码
        String provinceCode = file.getProvinceCode(provinceInfo);

        //更新选择地址信息
        this.provinceCode=provinceCode;  //全局变量 自己再定义  
        //根据省编码获取市列表
        List<String> cityList = itemNameList(file.getSampleCityList(provinceCode));
        for (String city : cityList) {
            String temCity = null;
            if (city.contains("自治州")) {
                temCity = city.replace("自治州","");
            } else {
                temCity = city.replace("市","");
            }
            if (addressInfo.contains(temCity)) {
                cityInfo = city;
                //更新选择地址信息
                this.city=city;
                break;
            }
        }
    }

    if (!StringUtil.isEmpty(cityInfo)) {//如果输入框包含市
    //根据市名称、省份编码获取该市编码
        String cityCode = file.getCityCode(cityInfo, file.getProvinceCode(provinceInfo));
        //更新选择地址信息
        this.cityCode=cityCode;

        List<String> areaList = itemNameList(file.getSampleCountyList(cityCode));
        for (String county : areaList) {
            String countyTem = null;
            if (county.contains("自治")) {
                countyTem = county.replace("自治","");
            }
            countyTem = county.substring(0, county.length() - 1);
            if (addressInfo.contains(countyTem)) {
                countyInfo = county;
                /*截取详细地址发字符*/
                areaIndex = addressInfo.indexOf(countyTem)+countyTem.length();
                if(areaIndex<=addressInfo.length())
                {
                    areaInfo=addressInfo.substring(areaIndex,addressInfo.length());
                }
                //更新选择地址信息
                this.county=county;
                this.countyCode=file.getCountyCode(cityCode,county);
                break;
            }
        }
    }else{
        if(addressInfo.indexOf("市")>0){
            String cityTem=addressInfo.substring(addressInfo.indexOf("市")-2,addressInfo.indexOf("市")+1);
            if(!TextUtils.isEmpty(file.getCountyCode(cityTem))){
                this.countyCode=file.getCountyCode(cityTem);
                if(!TextUtils.isEmpty(file.getCountyName(countyCode))){
                    this.county=file.getCountyName(countyCode);
                    this.cityCode=countyCode.substring(0,countyCode.length()-4);
                    this.city=file.getCityName(cityCode);
                    cityInfo=this.city;
                    countyInfo=this.county;
                }
            }
        }

    }
    String areaTem = provinceInfo + cityInfo + countyInfo; //省市区
 
    if (!StringUtil.isEmpty(countyInfo)&&addressInfo.contains(countyInfo)) {
        areaIndex = addressInfo.indexOf(countyInfo)+countyInfo.length();
        if(areaIndex<=addressInfo.length())
        {
            areaInfo=addressInfo.substring(areaIndex,addressInfo.length());
        }
    }

    int cutNote = addressInfo.indexOf("备注");
 

    /*识别姓名 姓名必须要在输入框首位才能识别。。*/  
    if (!StringUtil.isEmpty(provinceInfo)&&addressInfo.contains(provinceInfo)) {
        provinceIndex = addressInfo.indexOf(provinceInfo);
    }
    if (provinceIndex > 0 && phoneIndex > 0) {
        nameIndex = provinceIndex < phoneIndex ? provinceIndex : phoneIndex;
    } else {
        nameIndex = provinceIndex > phoneIndex ? provinceIndex : phoneIndex;
    }
    if (nameIndex > 0) {
        String nameTem = addressInfo.substring(0, nameIndex);
        if (nameTem.contains("姓名")) {
            String[] nameTem1 = nameTem.split("姓名");
            nameInfo = nameTem1[nameTem1.length - 1];
        }
        if (nameTem.contains("name")) {
            String[] nameTem1 = nameTem.split("name");
            nameInfo = nameTem1[nameTem1.length - 1];
        }
        if (StringUtil.isEmpty(nameInfo)) {
        
            nameInfo = nameTem;
        }
    }
}

觉得还不错。每个地方可以打个日志看一下。

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
智能识别收货地址Java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.TimeZone; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Encoder; class Demo { public static String calcAuthorization(String source, String secretId, String secretKey, String datetime) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { String signStr = "x-date: " + datetime + "\n" + "x-source: " + source; Mac mac = Mac.getInstance("HmacSHA1"); Key sKey = new SecretKeySpec(secretKey.getBytes("UTF-8"), mac.getAlgorithm()); mac.init(sKey); byte[] hash = mac.doFinal(signStr.getBytes("UTF-8")); String sig = new BASE64Encoder().encode(hash); String auth = "hmac id=\"" + secretId + "\", algorithm=\"hmac-sha1\", headers=\"x-date x-source\", signature=\"" + sig + "\""; return auth; } public static String urlencode(Map<?, ?> map) throws UnsupportedEncodingException { StringBuilder sb = new StringBuilder(); for (Map.Entry<?, ?> entry : map.entrySet()) { if (sb.length() > 0) { sb.append("&"); } sb.append(String.format("%s=%s", URLEncoder.encode(entry.getKey().toString(), "UTF-8"), URLEncoder.encode(entry.getValue().toString(), "UTF-8") )); } return sb.toString(); } public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException { //云场分配的密钥Id String secretId = "xxxx"; //云场分配的密钥Key String secretKey = "xxxx"; String source = "market"; Calendar cd = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Lo
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值