Files
Investidor-V2/tools/prepare-mining-reference.py
T

25 lines
3.2 KiB
Python

from pathlib import Path
import argparse,zipfile,xml.etree.ElementTree as E,hashlib,json,re
parser=argparse.ArgumentParser();parser.add_argument('source',type=Path);parser.add_argument('--out',type=Path,required=True);args=parser.parse_args()
ns={'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
def clean(s):return re.sub(r'\s+([,;)])',r'\1',re.sub(r'([(])\s+',r'\1',re.sub(r'\s+',' ',s))).strip()
def text(el):return clean(' '.join(x.text or '' for x in el.findall('.//w:t',ns)))
z=zipfile.ZipFile(args.source);root=E.fromstring(z.read('word/document.xml'));body=root.find('w:body',ns);tables=[];sections={};heading='';paragraphs=[]
for node in body:
if node.tag.endswith('}p'):
value=text(node);paragraphs.append(value)
if re.match(r'^[1-8]\s*\.0 ',value):heading=re.match(r'^([1-8])\s*\.0 ',value).group(1)+'.0';sections[heading]=[]
elif value:sections.setdefault(heading,[]).append(value)
elif node.tag.endswith('}tbl'):tables.append([[text(cell) for cell in row.findall('w:tc',ns)] for row in node.findall('w:tr',ns)])
assert len(tables)==13 and len(tables[0])==12 and len(tables[2])==11
provinces=['Cabo Delgado','Gaza','Inhambane','Manica','Maputo','Nampula','Niassa','Sofala','Tete','Zambézia']
def locations(value):return [p for p in provinces if re.search(r'(?<!\w)'+re.escape(p)+r'(?!\w)',value,re.I)]
source_hash=hashlib.sha256(args.source.read_bytes()).hexdigest()
minerals=[{'id':f'MINERAL-{i:02}','name':r[0],'locations_text':r[1],'province_names':locations(r[1]),'source_row':f'table1:row{i}','coordinates':None,'classification':'reported_occurrence_group'} for i,r in enumerate(tables[0][1:],1)]
areas=[]
for i,(row,detail) in enumerate(zip(tables[2][1:],tables[3:]),1):
fields=dict(detail[1:]);mineral_groups={1:[1],2:[4],3:[3,5],4:[2],5:[10],6:[11],7:[6],8:[7],9:[8],10:[9]};areas.append({'id':f'MIN-{i:02}','title':row[0],'mineral_ids':[f'MINERAL-{number:02}' for number in mineral_groups[i]],'locations_text':row[1],'province_names':locations(row[1]),'market_evidence':row[2],'value_chain':row[3],'source_state':row[4],'indicative_location':fields.get('Localização indicativa (distrito, não oficial)',''),'source_fields':fields,'source_row':f'table3:row{i}','coordinates':None,'availability':'reference_only','promotion_ready':False})
result={'format':'investmoz-mining-reference-v1','source_sha256':source_hash,'source_document_name':args.source.name,'reference_date':'2026-11-11','title':'Mineração e cadeias de valor em Moçambique','minerals':minerals,'areas':areas,'indicators':[{'mineral':r[0],'value':r[1],'source':r[2],'source_row':f'table2:row{i}'} for i,r in enumerate(tables[1][1:],1)],'projects':sections.get('3.0',[]),'risks':sections.get('7.0',[]),'sources':sections.get('8.0',[]),'original_paragraphs':paragraphs,'source_instructions_are_not_product_rules':True}
assert len(areas)==10 and len(minerals)==11 and len(result['indicators'])==12 and len(result['projects'])==6 and result['sources']
args.out.parent.mkdir(parents=True,exist_ok=True);args.out.write_text(json.dumps(result,ensure_ascii=False,indent=2),encoding='utf-8');print(json.dumps({'sha256':source_hash,'minerals':len(minerals),'areas':len(areas),'projects':len(result['projects']),'indicators':len(result['indicators']),'sources':len(result['sources']),'coordinates_created':0}))