标准化用量表达 Args: amount: 原始用量 unit: 单位 Returns: Tuple[标准化后的用量, 估算数值]
(self, amount: str, unit: str = "")
| 88 | } |
| 89 | |
| 90 | def normalize_amount(self, amount: str, unit: str = "") -> Tuple[str, Optional[float]]: |
| 91 | """ |
| 92 | 标准化用量表达 |
| 93 | |
| 94 | Args: |
| 95 | amount: 原始用量 |
| 96 | unit: 单位 |
| 97 | |
| 98 | Returns: |
| 99 | Tuple[标准化后的用量, 估算数值] |
| 100 | """ |
| 101 | if not amount: |
| 102 | return "", None |
| 103 | |
| 104 | # 清理输入 |
| 105 | amount = amount.strip() |
| 106 | |
| 107 | # 尝试提取数字 |
| 108 | number_match = re.match(r'^(\d+(?:\.\d+)?)', amount) |
| 109 | if number_match: |
| 110 | # 如果是纯数字,直接返回 |
| 111 | try: |
| 112 | numeric_value = float(number_match.group(1)) |
| 113 | return amount, numeric_value |
| 114 | except ValueError: |
| 115 | pass |
| 116 | |
| 117 | # 标准化非数值表达 |
| 118 | normalized = self.amount_mappings.get(amount, amount) |
| 119 | estimated = self.estimated_values.get(normalized, None) |
| 120 | |
| 121 | return normalized, estimated |
| 122 | |
| 123 | def parse_amount_with_unit(self, amount_str: str) -> Tuple[str, str, Optional[float]]: |
| 124 | """ |
no outgoing calls
no test coverage detected