"""Create the aggregate confirmation figure from the supplied public summary."""
import argparse
import json
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt


def main():
    p=argparse.ArgumentParser(description=__doc__)
    p.add_argument('--results',type=Path,default=Path(__file__).with_name('results.json'))
    p.add_argument('--output',type=Path,default=Path(__file__).with_name('confirmation.svg'))
    a=p.parse_args();d=json.loads(a.results.read_text())
    n=d['eligible_mentions'];counts=[d['plain_top1'],d['system_top1']]
    fig,ax=plt.subplots(figsize=(7,3.4),layout='constrained')
    bars=ax.barh(['Plain SapBERT','SapBERT + descriptor lookup'],[100*x/n for x in counts],color=['#718096','#167a67'])
    for bar,count in zip(bars,counts):
        ax.text(bar.get_width()+1,bar.get_y()+bar.get_height()/2,f'{count/n:.1%} ({count:,}/{n:,})',va='center')
    ax.set_xlim(0,105);ax.set_xlabel('Top-1 accuracy across eligible mentions (%)')
    ax.set_title('Held-out linking accuracy')
    ax.invert_yaxis();ax.spines[['top','right']].set_visible(False)
    fig.savefig(a.output,metadata={'Date':None})

if __name__=='__main__':main()
