This commit is contained in:
Your Name
2026-06-09 10:16:38 +08:00
parent f8ccd8b0af
commit 29ab45033d
2 changed files with 23 additions and 8 deletions
+9 -1
View File
@@ -204,7 +204,15 @@
"navigationStyle": "custom", "navigationStyle": "custom",
"navigationBarTitleText": "日常护理", "navigationBarTitleText": "日常护理",
"backgroundColor": "#f4fbf4", "backgroundColor": "#f4fbf4",
"enablePullDownRefresh": true "enablePullDownRefresh": true,
"mp-weixin": {
"usingPlugins": {
"WechatSI": {
"version": "0.3.5",
"provider": "wx069ba97219f66d99"
}
}
}
} }
}, },
{ {
+14 -7
View File
@@ -7,7 +7,7 @@
* - 饮食(早 / 午 / 晚餐 + 备注) * - 饮食(早 / 午 / 晚餐 + 备注)
* - 运动(类型 / 时长 / 强度) * - 运动(类型 / 时长 / 强度)
* *
* 兼容中文数字(六点五 / 一百二十)与阿拉伯数字(6.5 / 120)。 * 兼容中文数字(六点五 / 一百二十 / 一百二)与阿拉伯数字(6.5 / 120)。
* 纯前端解析,无网络依赖,识别失败时返回空对象。 * 纯前端解析,无网络依赖,识别失败时返回空对象。
*/ */
@@ -18,18 +18,23 @@ const CN_DIGIT = {
} }
const CN_UNIT = { : 10, : 10, : 100, : 100, : 1000, : 1000, : 10000, 亿: 100000000 } const CN_UNIT = { : 10, : 10, : 100, : 100, : 1000, : 1000, : 10000, 亿: 100000000 }
/** 中文整数串 → 数字,例如 "一百二十" → 120、"八十" → 80、"十" → 10 */ /** 中文整数串 → 数字,例如 "一百二十" → 120、"八十" → 80、"十" → 10、"一百二" → 120 */
function cnIntToNumber(s) { function cnIntToNumber(s) {
let total = 0 let total = 0
let section = 0 let section = 0
let number = 0 let number = 0
let hadUnit = false let hadUnit = false
let lastUnit = 0
let sawZeroAfterUnit = false
for (const ch of s) { for (const ch of s) {
if (CN_DIGIT[ch] !== undefined) { if (CN_DIGIT[ch] !== undefined) {
number = CN_DIGIT[ch] number = CN_DIGIT[ch]
if (number === 0) sawZeroAfterUnit = true
} else if (CN_UNIT[ch] !== undefined) { } else if (CN_UNIT[ch] !== undefined) {
hadUnit = true hadUnit = true
const unit = CN_UNIT[ch] const unit = CN_UNIT[ch]
lastUnit = unit
sawZeroAfterUnit = false
if (unit >= 10000) { if (unit >= 10000) {
section = (section + number) * unit section = (section + number) * unit
total += section total += section
@@ -41,7 +46,6 @@ function cnIntToNumber(s) {
number = 0 number = 0
} }
} }
const val = total + section + number
// 没有任何单位且为纯数字串(如 "一二零")时按位拼接更符合口语 // 没有任何单位且为纯数字串(如 "一二零")时按位拼接更符合口语
if (!hadUnit && s.length > 1) { if (!hadUnit && s.length > 1) {
let joined = '' let joined = ''
@@ -50,7 +54,11 @@ function cnIntToNumber(s) {
} }
if (joined) return Number(joined) if (joined) return Number(joined)
} }
return val // 口语省略尾部单位:"一百二" → 120、"一千五" → 1500(避开 "一百零五" 这类带零的)
if (number > 0 && lastUnit >= 100 && !sawZeroAfterUnit) {
number = number * (lastUnit / 10)
}
return total + section + number
} }
/** 中文数字串(含小数点)→ 数字,例如 "六点五" → 6.5 */ /** 中文数字串(含小数点)→ 数字,例如 "六点五" → 6.5 */
@@ -110,8 +118,7 @@ function textAfter(text, keys, stopKeys = []) {
// 去掉口语连接词 // 去掉口语连接词
rest = rest.replace(/^[是为吃了喝了吃的喝的有打了用了::,,、。\s]+/, '') rest = rest.replace(/^[是为吃了喝了吃的喝的有打了用了::,,、。\s]+/, '')
let end = rest.length let end = rest.length
const stopRe = /[,。.;!?\n]/ const mStop = rest.match(/[,。.;!?\n]/)
const mStop = rest.match(stopRe)
if (mStop && mStop.index < end) end = mStop.index if (mStop && mStop.index < end) end = mStop.index
for (const sk of stopKeys) { for (const sk of stopKeys) {
const si = rest.indexOf(sk) const si = rest.indexOf(sk)
@@ -242,7 +249,7 @@ export function parseExercise(raw) {
const t = normalizeNumbers(raw) const t = normalizeNumbers(raw)
const result = {} const result = {}
let durMatch = t.match(/(\d+(?:\.\d+)?)\s*(个小时|小时|钟头|时|分钟|分)/) const durMatch = t.match(/(\d+(?:\.\d+)?)\s*(个小时|小时|钟头|时|分钟|分)/)
if (durMatch) { if (durMatch) {
const val = parseFloat(durMatch[1]) const val = parseFloat(durMatch[1])
const isHour = /个小时|小时|钟头|时/.test(durMatch[2]) const isHour = /个小时|小时|钟头|时/.test(durMatch[2])