a
     go                     @   s`   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ dd ZG dd dZ	G dd	 d	Z
dS )
    N)cli)word_tokenizationc                 C   sH   t | ddd"}tdd | }W d    n1 s60    Y  t|S )Nrutf-8encodingc                 S   s   |   S )N)strip)r    r	   A/usr/local/lib/python3.9/dist-packages/pythainlp/cli/benchmark.py<lambda>       z_read_file.<locals>.<lambda>)openmap	readlineslist)pathflinesr	   r	   r
   
_read_file   s    0r   c                   @   s   e Zd Zdd ZdS )Appc                 C   sn   t jdddd}|jdtdd ||dd	 }t|j| t|j}|d	d  }|d
krjt	|| d S )N	benchmarkzKBenchmark for various tasks;
currently, we have only for word tokenization.zgthainlp benchmark [task] [task-options]

tasks:

word-tokenization      benchmark word tokenization

--)progdescriptionusagetaskz[word-tokenization])typehelp      zword-tokenization)
argparseArgumentParseradd_argumentstr
parse_argsr   Zexit_if_emptyr   lowerWordTokenizationBenchmark)selfargvparserargsr   Z	task_argvr	   r	   r
   __init__   s    zApp.__init__N__name__
__module____qualname__r*   r	   r	   r	   r
   r      s   r   c                   @   s   e Zd Zdd ZdS )r%   c              	   C   s  t jf i td| }|jdddd |jdddd |jdd	d
dd ||}t|j}t|j}t	|t	|ksJ dt
d|j|jt	|f  t||}g d}i }	|D ]}
t||
  |	|
< q|	d |	d |	d   |	d< |	d |	d |	d   |	d< |	d |	d  |	d< |	d |	d  |	d< t
d dD ].}
d|
 }
|	|
 }t
|
dd|d q>d D ].}
d!|
 }
|	|
 }t
|
dd|d qr|jrtj|j}|jd"d# d$d% }d&||f }t
d'|  t|d(d)d* }tj|	|d	d+ W d    n1 s 0    Y  d,||f }t
d-|  t|d(d)d*z}g }t|d.D ]<\}}|d/ |d0  }}|d/= |d0= |||||d1 qd|	|d2}tj||d	d3 W d    n1 s0    Y  d S )4Nz
benchmark z--input-filestorez3Path to input file to compare against the test file)actionr   z--test-filez#Path to test file i.e. ground truthz--save-detailsF
store_truezJSave comparison details to files (eval-XXX.json and eval-details-XXX.json))defaultr0   r   z;Input and test files do not have the same number of samplesz3Benchmarking %s against %s with %d samples in total)char_level:tpchar_level:fpzchar_level:tnchar_level:fn$word_level:correctly_tokenised_words word_level:total_words_in_sample$word_level:total_words_in_ref_sampler3   r4   zchar_level:precisionr5   zchar_level:recallr6   r7   zword_level:precisionr8   zword_level:recallz.============== Benchmark Result ==============)tpfntnfp	precisionrecallzchar_level:z>40s z.4f)Ztotal_words_in_sampleZtotal_words_in_ref_sampleZcorrectly_tokenised_wordsr=   r>   zword_level:/.r   z%s/eval-%s.ymlz Evaluation result is saved to %swr   r   )Zdefault_flow_stylez%s/eval-details-%s.jsonz%Details of comparisons is saved to %srecordsexpectedactual)metricsrE   rF   id)rG   samples)ensure_ascii)r   r    r   Z
make_usager!   r#   r   Z
input_fileZ	test_filelenprintr   r   floatsumZsave_detailsosr   dirnamesplitr   yamldump	enumerateto_dictappendjson)r&   namer'   r(   r)   rF   rE   Zdf_rawcolumns
statisticscvdir_name	file_nameZres_pathoutfiler   rI   ir   detailsr	   r	   r
   r*   1   s    






0
z"WordTokenizationBenchmark.__init__Nr+   r	   r	   r	   r
   r%   0   s   r%   )r   rW   rO   rR   Z	pythainlpr   Zpythainlp.benchmarksr   r   r   r%   r	   r	   r	   r
   <module>   s   