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;
        }
    }
}

覺得還不錯。每個地方可以打個日誌看一下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章