优化中文数字的识别逻辑

This commit is contained in:
x1ao4 2025-06-19 23:21:42 +08:00
parent fea41e76fd
commit f0a05d5572
2 changed files with 87 additions and 34 deletions

View File

@ -115,6 +115,66 @@
}
}
});
// 添加中文数字转阿拉伯数字的函数
function chineseToArabic(chinese) {
if (!chinese) {
return null;
}
// 数字映射
const digitMap = {
'零': 0, '一': 1, '二': 2, '三': 3, '四': 4,
'五': 5, '六': 6, '七': 7, '八': 8, '九': 9,
'两': 2
};
// 单位映射
const unitMap = {
'十': 10,
'百': 100,
'千': 1000,
'万': 10000
};
// 如果是单个字符,直接返回对应数字
if (chinese.length === 1) {
if (chinese === '十') return 10;
return digitMap[chinese];
}
let result = 0;
let section = 0;
let number = 0;
// 从左向右处理
for (let i = 0; i < chinese.length; i++) {
const char = chinese[i];
if (char in digitMap) {
number = digitMap[char];
} else if (char in unitMap) {
const unit = unitMap[char];
// 如果前面没有数字默认为1例如"十"表示1*10=10
section += (number || 1) * unit;
number = 0;
// 如果是万级单位累加到结果并重置section
if (unit === 10000) {
result += section;
section = 0;
}
} else {
// 非法字符
return null;
}
}
// 加上最后的数字和小节
result += section + number;
return result;
}
</script>
</head>
@ -1790,13 +1850,10 @@
if (match) {
separatorMatch = match;
// 根据不同的格式,确定季序号的位置
if (match[3] && match[3].match(/[一二三四五六七八九十零]/)) {
if (match[3] && match[3].match(/[一二三四五六七八九十百千万]/)) {
// 将中文数字转换为阿拉伯数字
const chineseToNumber = {
'零': '0', '一': '1', '二': '2', '三': '3', '四': '4',
'五': '5', '六': '6', '七': '7', '八': '8', '九': '9', '十': '10'
};
seasonNumber = chineseToNumber[match[3]] || '01';
const arabicNumber = chineseToArabic(match[3]);
seasonNumber = arabicNumber !== null ? String(arabicNumber) : '01';
} else {
seasonNumber = match[3] || '01';
}
@ -1893,18 +1950,15 @@
if (pathParts.length > 0) {
const lastPart = pathParts[pathParts.length - 1];
// 匹配中文季序号格式
const chineseSeasonMatch = lastPart.match(/^(.*?)([\s\.\-_]+)第([一二三四五六七八九十零]+)季$/);
const chineseSeasonMatch = lastPart.match(/^(.*?)([\s\.\-_]+)第([一二三四五六七八九十百千万]+)季$/);
if (chineseSeasonMatch) {
const showName = chineseSeasonMatch[1].trim();
const separator = chineseSeasonMatch[2];
const chineseSeason = chineseSeasonMatch[3];
// 将中文数字转换为阿拉伯数字
const chineseToNumber = {
'零': '0', '一': '1', '二': '2', '三': '3', '四': '4',
'五': '5', '六': '6', '七': '7', '八': '8', '九': '9', '十': '10'
};
const seasonNumber = chineseToNumber[chineseSeason] || '1';
const arabicNumber = chineseToArabic(chineseSeason);
const seasonNumber = arabicNumber !== null ? String(arabicNumber) : '1';
// 更新最末级目录为标准格式
pathParts[pathParts.length - 1] = showName + separator + 'S' + seasonNumber.padStart(2, '0');

View File

@ -454,7 +454,7 @@ def chinese_to_arabic(chinese):
digit_map = {
'': 0, '': 1, '': 2, '': 3, '': 4,
'': 5, '': 6, '': 7, '': 8, '': 9,
'': 2, '': 10
'': 2
}
# 单位映射
@ -467,38 +467,37 @@ def chinese_to_arabic(chinese):
# 如果是单个字符,直接返回对应数字
if len(chinese) == 1:
if chinese == '':
return 10
return digit_map.get(chinese)
# 如果只有"十"
if chinese == '':
return 10
result = 0
temp = 0
unit = 1
section = 0
number = 0
# 从右向左处理
for i in range(len(chinese) - 1, -1, -1):
# 从向右处理
for i in range(len(chinese)):
char = chinese[i]
# 处理数字
if char in digit_map and char != '':
temp = digit_map[char]
result += temp * unit
unit = 1 # 重置单位
# 处理单位
if char in digit_map:
number = digit_map[char]
elif char in unit_map:
if char == '' and i == 0: # 处理"十X"的情况
result += 10 + digit_map.get(chinese[1], 0)
break
else:
unit = unit_map[char]
if i == 0: # 如果单位在最前面,如"十三"则前面默认为1
result += unit
unit = unit_map[char]
# 如果前面没有数字默认为1例如"十"表示1*10=10
section += (number or 1) * unit
number = 0
# 如果是万级单位累加到结果并重置section
if unit == 10000:
result += section
section = 0
else:
# 非法字符
return None
# 加上最后的数字和小节
result += section + number
return result
# 兼容青龙