通过文本识别省市区完整算法PHP版本

<?php
    function init(){
        $d = include_once ('area.php');
        $data = $d['list'];
        foreach ($data as $k=>&$v){
            if($v['level'] == 3 && $v['shortname'] =='平陆'){
                $v['shortname'] ='平陆县';
            }
            if($v['level'] == 3 && $v['shortname'] =='资源'){
                $v['shortname'] ='资源县';
            }
            if($v['level'] == 3 && $v['shortname'] =='河北'){
                $v['shortname'] ='河北区';
            }
            if($v['level'] == 3 && $v['shortname'] =='运河'){
                $v['shortname'] ='运河区';
            }
        }
        return $data;
    }
    //过滤二维数组
    function filterUniqueByKeys(array $array, array $keys) {
        $uniqueItems = [];
        $uniqueIdentifiers = [];

        foreach ($array as $item) {
            // 构建唯一标识符(基于提供的键)
            $identifier = '';
            foreach ($keys as $key) {
                if (isset($item[$key])) {
                    $identifier .= $item[$key] . '-';
                }
            }
            // 移除末尾的'-'(如果有的话)
            $identifier = rtrim($identifier, '-');

            // 检查标识符是否已存在
            if (!in_array($identifier, $uniqueIdentifiers)) {
                // 如果不存在,则添加到结果数组和唯一标识符数组中
                $uniqueItems[] = $item;
                $uniqueIdentifiers[] = $identifier;
            }
        }

        return $uniqueItems;
    }
function getarea($s=''){

         $beifens=$s;//需要一个备份才可以
         $temsheng  = [];//因为有可能匹配到多个,所以需要用到数组
         $temshi    = [];//因为有可能匹配到多个,所以需要用到数组
         $temquxian = [];//因为有可能匹配到多个,所以需要用到数组

         $shengtem_index_f  = 0;
         $shitem_index_f    = 0;
         $quxiantem_index_f = 0;
         $maparea   = init();

         $shengcount = 0;
         $shicount = 0;
         $quxiancount = 0;
         foreach ($maparea as $k=>$v){
             //省的循环与遍历
             if($v['level'] == 1){
                 if(stripos($s,$v['name']) !== false ){
                     if(stripos($s,$v['name']) <= $shengtem_index_f || $shengcount == 0){
//                         echo stripos($s,$v['name']).$v['name']."<br>";
                         $shengtem_index_f = stripos($s,$v['name']);
                     }
                     $temsheng[] = $v;
                     $shengcount++;
                 }
                 //找到了 省级别
                 if(stripos($s,$v['shortname']) !== false ){
                     if(stripos($s,$v['shortname']) <= $shengtem_index_f || $shengcount == 0){
//                         echo stripos($s,$v['shortname']).$v['shortname']."<br>";
                         $shengtem_index_f = stripos($s,$v['shortname']);
                     }
                     $temsheng[] = $v;
                     $shengcount++;
                 }

             }
             //市的循环与遍历
             if($v['level'] == 2){
                 if(stripos($s,$v['shortname']) !== false){
                     if(stripos($s,$v['shortname']) <= $shitem_index_f || $shicount==0){
                         $shitem_index_f = stripos($s,$v['shortname']);
                     }
                     $temshi[]=$v;
                     $shicount ++;
                 }
                 if(stripos($s,$v['name']) !== false){
                     if(stripos($s,$v['name']) <= $shitem_index_f || $shengcount == 0){
                         $shitem_index_f = stripos($s,$v['name']);
                     }
                     $temshi[]=$v;
                     $shicount ++;
                 }
             }
             //区县的循环与遍历
             if($v['level'] == 3){
                 if(stripos($s,$v['shortname']) !== false){
                     if(stripos($s,$v['shortname']) <= $quxiantem_index_f || $quxiancount ==0){
                         $quxiantem_index_f = stripos($s,$v['shortname']);
                     }
                     $temquxian[]=$v;
                     $quxiancount  ++;
                 }
                 if(stripos($s,$v['name']) !== false){
                     if(stripos($s,$v['name']) <= $quxiantem_index_f  || $quxiancount ==0){
                         $quxiantem_index_f = stripos($s,$v['name']);
                     }
                     $temquxian[]=$v;
                     $quxiancount  ++;
                 }
             }
         }
         //保持二维数组的唯一性
         $temsheng  = filterUniqueByKeys($temsheng,['id']);
         $temshi    = filterUniqueByKeys($temshi,['id']);
         $temquxian = filterUniqueByKeys($temquxian,['id']);

        if(($shengtem_index_f > $shitem_index_f && $shicount >0) || ($shengtem_index_f > $quxiantem_index_f && $quxiancount >0)){
//            echo "删除省";
            $temsheng = [];
        }

        if($shitem_index_f > $quxiantem_index_f && $quxiancount > 0){
//            echo "删除市";
            $temshi = [];

        }


        $shengtem_index  = 0;
        $shitem_index    = 0;
        $quxiantem_index = 0;


         $lastsheng = null;
         $lastshi   = null;
         $lastquxian    = null;

         //省同级别按照出现的顺序获得优先级
         foreach ($temsheng as $kk=>$vv){
             if(stripos($beifens,$vv['name']) !== false ){
                 if(stripos($beifens,$vv['name']) <=$shengtem_index || ($shengtem_index == 0 && $kk == 0)){
                     $shengtem_index = stripos($beifens,$vv['name']);
                     $lastsheng = $vv;
                 }
             }
            if(stripos($beifens,$vv['shortname']) !== false ){
                if(stripos($beifens,$vv['shortname']) <=$shengtem_index || ($shengtem_index == 0 && $kk == 0)){
                    $shengtem_index = stripos($beifens,$vv['shortname']);
                    $lastsheng = $vv;
                }
            }
         }


         //去掉不在这个省下面的市
         if($lastsheng){
             foreach ($temshi as $kk=>$vv){
                 if($vv['pid'] != $lastsheng['id']){
                     unset($temshi[$kk]);
                 }
             }
         }

         sort($temshi);
         //循环各个市
         foreach ($temshi as $kk=>$vv){
             if(stripos($beifens,$vv['name']) !== false ){
                 if(stripos($beifens,$vv['name']) <=$shitem_index || ($shitem_index == 0&& $kk ==0)){
                     $shitem_index = stripos($beifens,$vv['name']);
                     if($lastsheng){
                         if($lastsheng['id'] == $vv['pid']){
                             $lastshi = $vv;
                         }
                     }else{
                         $lastshi = $vv;
                     }
                 }
             }
             if(stripos($beifens,$vv['shortname']) !== false ){
                 if(stripos($beifens,$vv['shortname']) <=$shitem_index ||  ($shitem_index == 0&& $kk ==0) ){
                     $shitem_index = stripos($beifens,$vv['shortname']);
                     if($lastsheng){
                         if($lastsheng['id'] == $vv['pid']){
                             $lastshi = $vv;
                         }
                     }else{
                         $lastshi = $vv;
                     }
                 }
             }
         }
         //去掉不在这个市下面的区县
         if($lastshi){
             foreach ($temquxian as $kk=>$vv){
                if($vv['pid'] != $lastshi['id']){
                    unset($temquxian[$kk]);
                }
             }
         }
         //去掉不在这个省下面的区县
         if($lastsheng && !$lastshi){
             foreach ($temquxian as $kk=>$vv){
                 //找到市
                 $below = false;
                 foreach ($maparea as $k1=>$v1){
                     if($vv['pid'] == $v1['id'] && $v1['pid'] == $lastsheng['id']){
                         //这里计算要保留的区县
                        $below = true;
                        break;
                     }
                 }
                 if(!$below){

                     unset($temquxian[$kk]);
                 }
             }

         }

         sort($temquxian);

         foreach ($temquxian as $kk=>$vv){
             if(stripos($beifens,$vv['name']) !== false ){
                 if(stripos($beifens,$vv['name']) <=$quxiantem_index || ($quxiantem_index == 0 && $kk==0)){
                     $quxiantem_index = stripos($beifens,$vv['name']);
                     if($lastshi){
                        if($lastshi['id'] == $vv['pid']){
                            $lastquxian = $vv;
                        }
                     }else{
                         $lastquxian = $vv;
                     }
                 }
             }

             if(stripos($beifens,$vv['shortname']) !== false ){
                 if(stripos($beifens,$vv['shortname']) <=$quxiantem_index || ($quxiantem_index == 0 && $kk==0)){
                     $quxiantem_index = stripos($beifens,$vv['shortname']);
                     if($lastshi){
                         if($lastshi['id'] == $vv['pid']){
                             $lastquxian = $vv;
                         }
                     }else{
                         $lastquxian = $vv;
                     }
                 }
             }
         }

         //通过匹配的优先级进行计算
         if($lastquxian && ($quxiantem_index<$shitem_index || $quxiantem_index < $shengtem_index)){
            //区县最先找到。那么通过区县去找市省
             //循环获得市和省
             foreach ($maparea as $k=>$v){
                 if($lastquxian['pid'] == $v['id']){
                     $lastshi = $v;
                     break;
                 }
             }
             foreach ($maparea as $k=>$v){
                 if($lastshi['pid'] == $v['id']){
                     $lastsheng = $v;
                     break;
                 }
             }
         }
         // print_r($lastshi);
         if($lastshi && $shitem_index < $shengtem_index){
            //市就找省
             foreach ($maparea as $k=>$v){
                 if($lastshi['pid'] == $v['id']){
                     $lastsheng = $v;
                     break;
                 }
             }
         }
         
         //1、只存在省,不存在地区市县市的 就不用考虑
         if ($lastsheng && !$lastshi && !$lastquxian ){
             //pass //先不考虑
         }
         //2、存在市,则需要找到省
         if($lastshi){
             if($lastsheng){
                 //检查省是否正常,否则就去掉市,因为省的优先级更高
                 if($lastshi['pid'] != $lastsheng['id']){
                     $lastshi=null;//省都不对,那就不要这个市了
                     $lastquxian=null;//城市都不对,区县也就不要了
                 }
             }else{
                 //遍历获得省
                 foreach ($maparea as $k=>$v){
                     if($v['id']==$lastshi['pid']){
                         $lastsheng = $v;
                         break;
                     }
                 }
             }
         }
         //3、存在县,需要找到市和省(需要再加一步骤,就是如果存在省,那么就需要判断省是否存在)
         if($lastquxian){
             if($lastshi){
                 //判断对不对,不对的话那就都清空
                 if($lastquxian['pid'] != $lastshi['id']){
                     $lastquxian=null;//城市都不对,区县也就不要了
                     //省市的优先级比区域高,这里就不能处理
                 }
             }else{
                 //循环获得市和省
                 foreach ($maparea as $k=>$v){
                     if($lastquxian['pid'] == $v['id']){
                         $lastshi = $v;
                         break;
                     }
                 }
                 //如果不存在省,那就去便利获得,
                 if(!$lastsheng){
                     foreach ($maparea as $k=>$v){
                         if($v['id'] == $lastshi['pid'] && $v['level'] == 1){
                             $lastsheng = $v;
                             break;
                         }
                     }
                 }else{
                     //如果存在省,那就去对比成立不成立,不成立那么就清空省市
                     if($lastsheng['id']  != $lastshi['pid']){
                         $lastshi = null;
                         $lastquxian = null;
                     }
                 }
             }
         }

         $sheng = '';
         $shi   = '';
         $qu    ='';
         return ['sheng'=>$lastsheng,'shi'=>$lastshi,'qu'=>$lastquxian];
     }
        $string         = '富川瑶族自治县山塘维修基础设施建设项目';
        $sr             = getarea($string);
        print_r($sr);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Java Excel API 和正则表达式来实现这个功能。 首先,你需要导入 Java Excel API 库,然后读取 Excel 文件中的数据。 然后,你可以使用正则表达式来匹配手机号和省市区信息,例如: 手机号:使用正则表达式 `\d{11}` 来匹配11位数字,即手机号。 省市区:你可以使用第三方库或者公开数据源来获取省市区信息,然后使用正则表达式来匹配相应的信息。 最后,将匹配到的信息保存到一个数据结构中,例如一个 List 或者一个 Map。 以下是一个简单的示例代码,可以帮助你入门: ```java import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; public class ExcelParser { public static void main(String[] args) { try { // 读取 Excel 文件 Workbook workbook = Workbook.getWorkbook(new File("data.xls")); Sheet sheet = workbook.getSheet(0); // 定义正则表达式 Pattern phonePattern = Pattern.compile("\\d{11}"); Pattern areaPattern = Pattern.compile("上海市浦东新区"); // 定义数据结构 List<String> names = new ArrayList<>(); List<String> phones = new ArrayList<>(); List<String> areas = new ArrayList<>(); // 遍历所有单元格 for (int row = 0; row < sheet.getRows(); row++) { for (int col = 0; col < sheet.getColumns(); col++) { Cell cell = sheet.getCell(col, row); String text = cell.getContents(); // 匹配手机号 Matcher phoneMatcher = phonePattern.matcher(text); if (phoneMatcher.matches()) { phones.add(text); } // 匹配省市区 Matcher areaMatcher = areaPattern.matcher(text); if (areaMatcher.matches()) { areas.add(text); } // 匹配姓名 if (!phoneMatcher.matches() && !areaMatcher.matches()) { names.add(text); } } } // 输出结果 for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i) + " " + phones.get(i) + " " + areas.get(i)); } // 关闭 Excel 文件 workbook.close(); } catch (Exception e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值