"""Separate metal chargeable-weight declarations from the shipment's gross weight.""" import re from decimal import Decimal def material_intent(text,weight_pattern,to_kg): declarations=re.sub(r'铁铝(?:附加费|加费|费用)','',text) metal=bool(re.search(r'铁制|钢制|铝制|铁铝|不锈钢|(?:铁|钢|铝)(?:件|货|扣|罐|盒|板|管|架|材)',declarations)) generic='金属' in text ordinary=bool(re.search(r'普通货|普货|非铁铝',text)) partial=bool(re.search(r'只有|部分|其中|有些|只是|一部分',text) or metal and re.search(r'塑料|木制|布料',text)) scope='partial' if partial and (metal or generic) else ('all' if re.search(r'全是|全部是|都是|整票是|货物是|换成|改成',text) and metal else None) material=('ordinary' if '非铁铝' in text else 'metal' if metal else 'unknown_metal' if generic else 'ordinary' if ordinary or re.search(r'塑料|木制',text) else None) name=r'(?:铁铝|铁制品|钢制品|铝制品|铁件|钢件|铝件|金属)(?:部分|货物)?(?:的)?' patterns=(name+r'\s*计费重(?:量)?\s*(?:为|是|[::])?\s*'+weight_pattern.pattern, name+r'\s*按\s*'+weight_pattern.pattern+r'\s*计费', r'(?:铁铝|铁制品|钢制品|铝制品|铁件|钢件|铝件|金属)部分(?:的计费重(?:量)?)?\s*' r'(?:还是|仍然是|仍是|改为|改成|换成)\s*'+weight_pattern.pattern) charged=None;cleaned=text;errors=[];oral=False for index,pattern in enumerate(patterns): matches=list(re.finditer(pattern,text,re.I)) for match in matches: value=to_kg(*match.groups()) if charged is not None and Decimal(charged)!=value:errors.append('铁铝货物的计费重不一致,请确认。') charged=str(value);scope='partial' oral=oral or (index==2 and '计费重' not in match[0]) if not 0