From 594ff99cf446659674a524b6b63b0a4787045951 Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Fri, 18 Oct 2024 13:55:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8D=AF=E6=96=B9=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/define.php | 30 ++++++-------- control/admin.php | 9 +++++ model/mFormula.php | 63 ++++++++++++++++++++++++++++- view/templates/admin/formula_add.html | 72 +++++++++++++++++++++++++++++++--- view/templates/admin/formula_list.html | 30 +++++++++----- 5 files changed, 169 insertions(+), 35 deletions(-) diff --git a/config/define.php b/config/define.php index 3966b90..0414d1e 100644 --- a/config/define.php +++ b/config/define.php @@ -57,24 +57,17 @@ ); $GLOBALS['num_list'] = array( - 'num' => array( - '半'=>0.5, - '一'=>1, - '二'=>2, - '三'=>3, - '四'=>4, - '五'=>5, - '六'=>6, - '七'=>7, - '八'=>8, - '九'=>9, - ), - 'unit' => array( - '十'=>10, - '百'=>100, - '千'=>1000, - '万'=>10000, - ) + '半'=>0.5, + '一'=>1, + '二'=>2, + '三'=>3, + '四'=>4, + '五'=>5, + '六'=>6, + '七'=>7, + '八'=>8, + '九'=>9, + '十'=>10, ); define('WEIGHT_LIANG', 1); @@ -84,6 +77,7 @@ WEIGHT_SHENG => '升', ); + define('WEIGHT_UNIT_FROM_HAN','汉'); $GLOBALS['weight_list'] = array( "汉" => array( '少许'=>'', diff --git a/control/admin.php b/control/admin.php index 4487a51..39c5315 100644 --- a/control/admin.php +++ b/control/admin.php @@ -161,6 +161,15 @@ class admin extends publicBase { $this->ajax_json(true, '操作成功', array('id' => $id)); } + public function ajax_shibie_original() { + $original = trim($this->post('original')); + + $mformula = new mFormula(); + $data = $mformula->convOriginal($original); + + $this->ajax_json(true, '识别成功', $data); + } + public function ajax_delete_formula() { $id = $this->post('id') + 0; $is_delete = $this->post('is_delete') + 0; diff --git a/model/mFormula.php b/model/mFormula.php index 4d5306a..b7d7e6e 100644 --- a/model/mFormula.php +++ b/model/mFormula.php @@ -323,6 +323,67 @@ class mFormula extends mBase { return $formula; } + public function convOriginal($original) { + $herbString_arr = explode(' ', $original); + + $num_map = $GLOBALS['num_list']; + $num_list = implode('', array_keys($num_map)); + foreach ($herbString_arr as $herbString) { + $pattern = '/^(..)(.*?)([' . $num_list . ']+)([^(]*)?((.*?))?/u'; + preg_match($pattern, $herbString, $matches); + + if ($matches) { + $herb_arr[] = array( + 'herb_name' => trim($matches[1] . $matches[2]), + 'herb_num' => trim($matches[3] . $matches[4]), + 'herb_desc' => isset($matches[5]) ? str_replace(array('(', ')'), '', trim($matches[5])) : "", + ); + } else { + $herb_arr[] = array( + 'herb_name' => $herbString, + 'herb_num' => "", + 'herb_desc' => "", + ); + } + + } + + return $herb_arr; + } + + public function convToNumber($str) { + $num_map = $GLOBALS['num_list']; + $num_list = implode('', array_keys($num_map)); + preg_match_all('/[' . $num_list . ']+/u', $str, $matches); + + foreach ($matches[0] as $chinese) { + $result = 0; + $current = 0; // 当前的数字 + + for ($i = 0; $i < mb_strlen($chinese); $i++) { + $char = mb_substr($chinese, $i, 1); + + if (isset($num_map[$char])) { + $num = $num_map[$char]; + if ($num == 10) { + // 如果是“十” + if ($current == 0) { + $current = 1; // 处理“十”前没有数字的情况 + } + $result += $current * 10; // 将当前数字乘以十 + $current = 0; // 重置当前数字 + } else { + $current += $num; // 累加当前数字 + } + } + } + $result = $result + $current; + + $str = str_replace($chinese, $result, $str); + } + return $str; + } + public function convertToNum($herb_name, $str, $onlyconertNum = true, $from = '汉') { $num_list = $GLOBALS['num_list']; $num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit'])); @@ -556,7 +617,7 @@ class mFormula extends mBase { ); //如果转义成功 则保存转义后的数据 - $num = $this->convertToNum(trim($herb['name']), trim($herb['num'])); + $num = $this->convToNumber(trim($herb['num'])); if ($num != 0) $org_herb[$key]['num'] = $num; } diff --git a/view/templates/admin/formula_add.html b/view/templates/admin/formula_add.html index a7111f6..42aec48 100644 --- a/view/templates/admin/formula_add.html +++ b/view/templates/admin/formula_add.html @@ -42,7 +42,7 @@