MCPcopy Create free account
hub / github.com/Enfoirer/Text2GraphRAG / parse_amount_with_unit

Method parse_amount_with_unit

agent/amount_normalizer.py:123–158  ·  view source on GitHub ↗

解析包含单位的用量字符串 Args: amount_str: 如"300毫升"、"适量盐"等 Returns: Tuple[数量, 单位, 估算数值]

(self, amount_str: str)

Source from the content-addressed store, hash-verified

121 return normalized, estimated
122
123 def parse_amount_with_unit(self, amount_str: str) -> Tuple[str, str, Optional[float]]:
124 """
125 解析包含单位的用量字符串
126
127 Args:
128 amount_str: 如"300毫升"、"适量盐"等
129
130 Returns:
131 Tuple[数量, 单位, 估算数值]
132 """
133 if not amount_str:
134 return "", "", None
135
136 # 常见单位模式
137 unit_patterns = [
138 r'(\d+(?:\.\d+)?)\s*(克|千克|斤|两|钱)', # 重量
139 r'(\d+(?:\.\d+)?)\s*(毫升|升|杯|勺|茶匙|汤匙)', # 体积
140 r'(\d+(?:\.\d+)?)\s*(个|只|条|片|根|瓣|颗)', # 数量
141 r'(\d+(?:\.\d+)?)\s*(把|撮|滴)', # 特殊
142 ]
143
144 # 尝试匹配数字+单位
145 for pattern in unit_patterns:
146 match = re.search(pattern, amount_str)
147 if match:
148 amount = match.group(1)
149 unit = match.group(2)
150 try:
151 numeric_value = float(amount)
152 return amount, unit, numeric_value
153 except ValueError:
154 pass
155
156 # 处理纯文字表达
157 normalized, estimated = self.normalize_amount(amount_str)
158 return normalized, "", estimated
159
160 def get_comparable_value(self, amount: str, unit: str = "") -> Optional[float]:
161 """

Callers

nothing calls this directly

Calls 1

normalize_amountMethod · 0.95

Tested by

no test coverage detected