
    Ng                     n    d dl Z 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l	m
Z
  G d de
          ZdS )    N)ZipFilePathPointer)find_dir)
TokenizerIc                   `    e Zd ZdZddZd ZddZd Zed             Z	ed	             Z
d
 ZdS )ReppTokenizera  
    A class for word tokenization using the REPP parser described in
    Rebecca Dridan and Stephan Oepen (2012) Tokenization: Returning to a
    Long Solved Problem - A Survey, Contrastive  Experiment, Recommendations,
    and Toolkit. In ACL. http://anthology.aclweb.org/P/P12/P12-2.pdf#page=406

    >>> sents = ['Tokenization is widely regarded as a solved problem due to the high accuracy that rulebased tokenizers achieve.' ,
    ... 'But rule-based tokenizers are hard to maintain and their rules language specific.' ,
    ... 'We evaluated our method on three languages and obtained error rates of 0.27% (English), 0.35% (Dutch) and 0.76% (Italian) for our best models.'
    ... ]
    >>> tokenizer = ReppTokenizer('/home/alvas/repp/') # doctest: +SKIP
    >>> for sent in sents:                             # doctest: +SKIP
    ...     tokenizer.tokenize(sent)                   # doctest: +SKIP
    ...
    (u'Tokenization', u'is', u'widely', u'regarded', u'as', u'a', u'solved', u'problem', u'due', u'to', u'the', u'high', u'accuracy', u'that', u'rulebased', u'tokenizers', u'achieve', u'.')
    (u'But', u'rule-based', u'tokenizers', u'are', u'hard', u'to', u'maintain', u'and', u'their', u'rules', u'language', u'specific', u'.')
    (u'We', u'evaluated', u'our', u'method', u'on', u'three', u'languages', u'and', u'obtained', u'error', u'rates', u'of', u'0.27', u'%', u'(', u'English', u')', u',', u'0.35', u'%', u'(', u'Dutch', u')', u'and', u'0.76', u'%', u'(', u'Italian', u')', u'for', u'our', u'best', u'models', u'.')

    >>> for sent in tokenizer.tokenize_sents(sents): # doctest: +SKIP
    ...     print(sent)                              # doctest: +SKIP
    ...
    (u'Tokenization', u'is', u'widely', u'regarded', u'as', u'a', u'solved', u'problem', u'due', u'to', u'the', u'high', u'accuracy', u'that', u'rulebased', u'tokenizers', u'achieve', u'.')
    (u'But', u'rule-based', u'tokenizers', u'are', u'hard', u'to', u'maintain', u'and', u'their', u'rules', u'language', u'specific', u'.')
    (u'We', u'evaluated', u'our', u'method', u'on', u'three', u'languages', u'and', u'obtained', u'error', u'rates', u'of', u'0.27', u'%', u'(', u'English', u')', u',', u'0.35', u'%', u'(', u'Dutch', u')', u'and', u'0.76', u'%', u'(', u'Italian', u')', u'for', u'our', u'best', u'models', u'.')
    >>> for sent in tokenizer.tokenize_sents(sents, keep_token_positions=True): # doctest: +SKIP
    ...     print(sent)                                                         # doctest: +SKIP
    ...
    [(u'Tokenization', 0, 12), (u'is', 13, 15), (u'widely', 16, 22), (u'regarded', 23, 31), (u'as', 32, 34), (u'a', 35, 36), (u'solved', 37, 43), (u'problem', 44, 51), (u'due', 52, 55), (u'to', 56, 58), (u'the', 59, 62), (u'high', 63, 67), (u'accuracy', 68, 76), (u'that', 77, 81), (u'rulebased', 82, 91), (u'tokenizers', 92, 102), (u'achieve', 103, 110), (u'.', 110, 111)]
    [(u'But', 0, 3), (u'rule-based', 4, 14), (u'tokenizers', 15, 25), (u'are', 26, 29), (u'hard', 30, 34), (u'to', 35, 37), (u'maintain', 38, 46), (u'and', 47, 50), (u'their', 51, 56), (u'rules', 57, 62), (u'language', 63, 71), (u'specific', 72, 80), (u'.', 80, 81)]
    [(u'We', 0, 2), (u'evaluated', 3, 12), (u'our', 13, 16), (u'method', 17, 23), (u'on', 24, 26), (u'three', 27, 32), (u'languages', 33, 42), (u'and', 43, 46), (u'obtained', 47, 55), (u'error', 56, 61), (u'rates', 62, 67), (u'of', 68, 70), (u'0.27', 71, 75), (u'%', 75, 76), (u'(', 77, 78), (u'English', 78, 85), (u')', 85, 86), (u',', 86, 87), (u'0.35', 88, 92), (u'%', 92, 93), (u'(', 94, 95), (u'Dutch', 95, 100), (u')', 100, 101), (u'and', 102, 105), (u'0.76', 106, 110), (u'%', 110, 111), (u'(', 112, 113), (u'Italian', 113, 120), (u')', 120, 121), (u'for', 122, 125), (u'our', 126, 129), (u'best', 130, 134), (u'models', 135, 141), (u'.', 141, 142)]
    utf8c                 x    |                      |          | _        t          j                    | _        || _        d S )N)find_repptokenizerrepp_dirtempfile
gettempdirworking_direncoding)selfr   r   s      N/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/tokenize/repp.py__init__zReppTokenizer.__init__6   s3    //99#.00     c                 H    t          |                     |g                    S )z
        Use Repp to tokenize a single sentence.

        :param sentence: A single sentence string.
        :type sentence: str
        :return: A tuple of tokens.
        :rtype: tuple(str)
        )nexttokenize_sents)r   sentences     r   tokenizezReppTokenizer.tokenize=   s"     D''
33444r   Fc              #     K   t          j        d| j        dd          5 }|D ]'}|                    t	          |          dz              (|                                 |                     |j                  }|                     |          	                    | j
                                                  }|                     |          D ]}|st          | \  }}}	|V  	 ddd           dS # 1 swxY w Y   dS )z
        Tokenize multiple sentences using Repp.

        :param sentences: A list of sentence strings.
        :type sentences: list(str)
        :return: A list of tuples of tokens
        :rtype: iter(tuple(str))
        zrepp_input.wF)prefixdirmodedelete
N)r   NamedTemporaryFiler   writestrclosegenerate_repp_commandname_executedecoder   stripparse_repp_outputszip)
r   	sentenceskeep_token_positions
input_filesentcmdrepp_outputtokenized_sentstartsendss
             r   r   zReppTokenizer.tokenize_sentsH   s^      ( d&6S
 
 
 	%! 3 3  TT!12222,,Z_==C--,,33DMBBHHJJK"&"9"9+"F"F % %+ H363G0NFD$$$$$	%	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   CC11C58C5c                 T    | j         dz   g}|d| j         dz   gz  }|ddgz  }||gz  }|S )z
        This module generates the REPP command to be used at the terminal.

        :param inputfilename: path to the input file
        :type inputfilename: str
        	/src/reppz-c/erg/repp.setz--formattriple)r   )r   inputfilenamer/   s      r   r$   z#ReppTokenizer.generate_repp_commandb   sI     }{*+dmo566
H%%
r   c                     t          j        | t           j        t           j                  }|                                \  }}|S )N)stdoutstderr)
subprocessPopenPIPEcommunicate)r/   pr:   r;   s       r   r&   zReppTokenizer._executeo   s3    SQQQr   c              #      K   t          j        dt           j                  }|                     d          D ]>}d |                    |          D             }t          d |D                       }|V  ?dS )aZ  
        This module parses the tri-tuple format that REPP outputs using the
        "--format triple" option and returns an generator with tuple of string
        tokens.

        :param repp_output:
        :type repp_output: type
        :return: an iterable of the tokenized sentences as tuples of strings
        :rtype: iter(tuple)
        z^\((\d+), (\d+), (.+)\)$z

c                 T    g | ]%\  }}}|t          |          t          |          f&S  )int).0startendtokens       r   
<listcomp>z4ReppTokenizer.parse_repp_outputs.<locals>.<listcomp>   sA     $ $ $%E3 E

CHH-$ $ $r   c              3   &   K   | ]}|d          V  dS )   NrC   )rE   ts     r   	<genexpr>z3ReppTokenizer.parse_repp_outputs.<locals>.<genexpr>   s&      ==1!A$======r   N)recompile	MULTILINEsplitfindalltuple)r0   
line_regexsectionwords_with_positionswordss        r   r)   z ReppTokenizer.parse_repp_outputsu   s       Z ;R\JJ
"((00 	' 	'G$ $)3););G)D)D$ $ $  ==(<=====E&&&&&	' 	'r   c                     t           j                            |          r|}nt          |d          }t           j                            |dz             sJ t           j                            |dz             sJ |S )zX
        A module to find REPP tokenizer binary and its *repp.set* config file.
        )REPP_TOKENIZER)env_varsr5   r6   )ospathexistsr   )r   repp_dirname	_repp_dirs      r   r
   z ReppTokenizer.find_repptokenizer   sy     7>>,'' 	M$II 8KLLLIw~~i+566666w~~i/9:::::r   N)r   )F)__name__
__module____qualname____doc__r   r   r   r$   staticmethodr&   r)   r
   rC   r   r   r   r      s         @! ! ! !	5 	5 	5% % % %4     \
 ' ' \'(    r   r   )r[   rN   r<   sysr   	nltk.datar   nltk.internalsr   nltk.tokenize.apir   r   rC   r   r   <module>ri      s    
			 				     



  ( ( ( ( ( ( # # # # # # ( ( ( ( ( (@ @ @ @ @J @ @ @ @ @r   