31 lines
1.9 KiB
Python
31 lines
1.9 KiB
Python
"""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*计费')
|
||
charged=None;cleaned=text;errors=[]
|
||
for pattern in 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'
|
||
if not 0<value<=1000000:errors.append('请提供大于0的有效铁铝货物计费重。')
|
||
cleaned=cleaned[:match.start()]+' '*(match.end()-match.start())+cleaned[match.end():]
|
||
return {'material':material,'metal_scope':scope,'metal_chargeable_weight':charged,
|
||
'quantity_text':cleaned,'errors':errors}
|