a
     ˜ëjl?  ã                   @   s°   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m	Z	m
Z
 d dlmZ d dlmZ d dlmZ dd„ Zd	d
„ Zdd„ ZG dd„ deƒZedkr¬d dlZe ¡  dS )é    N)ÚZipFilePathPointer)Úfind_dirÚ	find_fileÚfind_jars_within_path)ÚParserI)ÚDependencyGraph)Útaggedsents_to_conllc                  C   s   ddl m}  | g d¢ƒ}|jS )Nr   )ÚRegexpTagger))z\.$Ú.)z\,$ú,)z\?$ú?)z\($ú()z\)$ú))z\[$ú[)z\]$ú])z^-?[0-9]+(\.[0-9]+)?$ZCD)z(The|the|A|a|An|an)$ZDT)z&(He|he|She|she|It|it|I|me|Me|You|you)$ZPRP)z(His|his|Her|her|Its|its)$úPRP$)z(my|Your|your|Yours|yours)$r   )z (on|On|in|In|at|At|since|Since)$ÚIN)z (for|For|ago|Ago|before|Before)$r   )z(till|Till|until|Until)$r   )z(by|By|beside|Beside)$r   )z(under|Under|below|Below)$r   )z(over|Over|above|Above)$r   )z (across|Across|through|Through)$r   )z(into|Into|towards|Towards)$r   )z(onto|Onto|from|From)$r   )z.*able$ZJJ)z.*ness$ÚNN)z.*ly$ZRB)z.*s$ZNNS)z.*ing$ZVBG)z.*ed$ZVBD)z.*r   )Znltk.tagr	   Útag)r	   Z_tagger© r   úN/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/parse/malt.pyÚmalt_regex_tagger   s
    ÿ r   c                 C   st   t j | ¡r| }nt| dd�}g d¢}tt|ƒƒ}dd„ |D ƒ}h d£}| |¡sVJ ‚ttdd„ |ƒƒslJ ‚t	|ƒS )	zE
    A module to find MaltParser .jar file and its dependencies.
    )ZMALT_PARSER)Úenv_vars)Ú r   r   c                 S   s   h | ]}t j |¡d  ’qS )é   )ÚosÚpathÚsplit)Ú.0Újarr   r   r   Ú	<setcomp>H   ó    z"find_maltparser.<locals>.<setcomp>>   zliblinear-1.8.jarz
libsvm.jarz	log4j.jarc                 S   s   |   d¡o|  d¡S )Nzmaltparser-z.jar)Ú
startswithÚendswith)Úir   r   r   Ú<lambda>M   r!   z!find_maltparser.<locals>.<lambda>)
r   r   Úexistsr   Úsetr   ÚissubsetÚanyÚfilterÚlist)Úparser_dirnameZ	_malt_dirZmalt_dependenciesZ
_malt_jarsZ_jarsr   r   r   Úfind_maltparser=   s    ÿr-   c                 C   s.   | du rdS t j | ¡r| S t| ddd�S dS )z8
    A module to find pre-trained MaltParser model.
    Númalt_temp.mco)Z
MALT_MODELF)r   Úverbose)r   r   r&   r   )Úmodel_filenamer   r   r   Úfind_malt_modelR   s
    r1   c                   @   sZ   e Zd ZdZddd„Zddd	„Zdd
d„Zddd„Zeddd„ƒZ	ddd„Z
ddd„ZdS )Ú
MaltParseraà  
    A class for dependency parsing with MaltParser. The input is the paths to:
    - (optionally) a maltparser directory
    - (optionally) the path to a pre-trained MaltParser .mco model file
    - (optionally) the tagger to use for POS tagging before parsing
    - (optionally) additional Java arguments

    Example:
        >>> from nltk.parse import malt
        >>> # With MALT_PARSER and MALT_MODEL environment set.
        >>> mp = malt.MaltParser(model_filename='engmalt.linear-1.7.mco') # doctest: +SKIP
        >>> mp.parse_one('I shot an elephant in my pajamas .'.split()).tree() # doctest: +SKIP
        (shot I (elephant an) (in (pajamas my)) .)
        >>> # Without MALT_PARSER and MALT_MODEL environment.
        >>> mp = malt.MaltParser('/home/user/maltparser-1.9.2/', '/home/user/engmalt.linear-1.7.mco') # doctest: +SKIP
        >>> mp.parse_one('I shot an elephant in my pajamas .'.split()).tree() # doctest: +SKIP
        (shot I (elephant an) (in (pajamas my)) .)
    r   Nc                 C   sT   t |ƒ| _|dur|ng | _t|ƒ| _| jdk| _t ¡ | _|durH|nt	ƒ | _
dS )a  
        An interface for parsing with the Malt Parser.

        :param parser_dirname: The path to the maltparser directory that
            contains the maltparser-1.x.jar
        :type parser_dirname: str
        :param model_filename: The name of the pre-trained model with .mco file
            extension. If provided, training will not be required.
            (see http://www.maltparser.org/mco/mco.html and
            see http://www.patful.com/chalk/node/185)
        :type model_filename: str
        :param tagger: The tagger used to POS tag the raw string before
            formatting to CONLL format. It should behave like `nltk.pos_tag`
        :type tagger: function
        :param additional_java_args: This is the additional Java arguments that
            one can use when calling Maltparser, usually this is the heapsize
            limits, e.g. `additional_java_args=['-Xmx1024m']`
            (see https://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html)
        :type additional_java_args: list
        Nr.   )r-   Ú	malt_jarsÚadditional_java_argsr1   ÚmodelÚ_trainedÚtempfileÚ
gettempdirÚworking_dirr   Útagger)Úselfr,   r0   r:   r4   r   r   r   Ú__init__r   s    
ÿ

zMaltParser.__init__FÚnullc              
   c   sŒ  | j stdƒ‚tjd| jddd���:}tjd| jddd���}t|ƒD ]}| t|ƒ¡ qF| ¡  | j	|j
|j
dd�}t ¡ }zt tj | j¡d	 ¡ W n   Y n0 |  ||¡}	t |¡ |	d	krÜtd
d |¡|	f ƒ‚t|j
ƒ�8}
|
 ¡  d¡D ]}tt||d�gƒV  qöW d  ƒ n1 �s&0    Y  W d  ƒ n1 �sF0    Y  W d  ƒ n1 �sf0    Y  t |j
¡ t |j
¡ dS )a»  
        Use MaltParser to parse multiple POS tagged sentences. Takes multiple
        sentences where each sentence is a list of (word, tag) tuples.
        The sentences must have already been tokenized and tagged.

        :param sentences: Input sentences to parse
        :type sentence: list(list(tuple(str, str)))
        :return: iter(iter(``DependencyGraph``)) the dependency graph
            representation of each sentence
        z0Parser has not been trained. Call train() first.zmalt_input.conll.ÚwF©ÚprefixÚdirÚmodeÚdeletezmalt_output.conll.Úparse©rB   r   z0MaltParser parsing (%s) failed with exit code %dú z

©Útop_relation_labelN)r6   Ú	Exceptionr7   ÚNamedTemporaryFiler9   r   ÚwriteÚstrÚcloseÚgenerate_malt_commandÚnamer   ÚgetcwdÚchdirr   r   r5   Ú_executeÚjoinÚopenÚreadÚiterr   Úremove)r;   Ú	sentencesr/   rH   Ú
input_fileZoutput_fileÚlineÚcmdZ_current_pathÚretÚinfileZtree_strr   r   r   Úparse_tagged_sentsœ   sV    
ÿü
ÿ
ÿÿÿÿÿÿf
zMaltParser.parse_tagged_sentsc                    s"   ‡ fdd„|D ƒ}ˆ j |||d�S )an  
        Use MaltParser to parse multiple sentences.
        Takes a list of sentences, where each sentence is a list of words.
        Each sentence will be automatically tagged with this
        MaltParser instance's tagger.

        :param sentences: Input sentences to parse
        :type sentence: list(list(str))
        :return: iter(DependencyGraph)
        c                 3   s   | ]}ˆ   |¡V  qd S )N)r:   )r   Zsentence©r;   r   r   Ú	<genexpr>é   r!   z)MaltParser.parse_sents.<locals>.<genexpr>rG   )r^   )r;   rX   r/   rH   Ztagged_sentencesr   r_   r   Úparse_sentsÞ   s    ÿzMaltParser.parse_sentsc                 C   sª   dg}|| j 7 }tj d¡r dnd}|d| | j¡g7 }|dg7 }tj | j	¡rl|dtj 
| j	¡d g7 }n|d| j	g7 }|d	|g7 }|d
krš|d|g7 }|d|g7 }|S )a  
        This function generates the maltparser command use at the terminal.

        :param inputfilename: path to the input file
        :type inputfilename: str
        :param outputfilename: path to the output file
        :type outputfilename: str
        ÚjavaÚwinú;ú:z-cpzorg.maltparser.Maltz-céÿÿÿÿz-irD   z-oz-m)r4   ÚsysÚplatformr"   rS   r3   r   r   r&   r5   r   )r;   ZinputfilenameZoutputfilenamerB   r[   Zclasspaths_separatorr   r   r   rN   î   s     


þ
z MaltParser.generate_malt_commandc                 C   s&   |rd nt j}t j| ||d�}| ¡ S )N)ÚstdoutÚstderr)Ú
subprocessÚPIPEÚPopenÚwait)r[   r/   ÚoutputÚpr   r   r   rR     s    zMaltParser._executec                 C   sv   t jd| jddd��2}d dd„ |D ƒ¡}| t|ƒ¡ W d  ƒ n1 sL0    Y  | j|j|d	� t 	|j¡ dS )
zÍ
        Train MaltParser from a list of ``DependencyGraph`` objects

        :param depgraphs: list of ``DependencyGraph`` objects for training input data
        :type depgraphs: DependencyGraph
        úmalt_train.conll.r>   Fr?   Ú
c                 s   s   | ]}|  d ¡V  qdS )é
   N)Zto_conll)r   Zdgr   r   r   r`      r!   z#MaltParser.train.<locals>.<genexpr>N©r/   )
r7   rJ   r9   rS   rK   rL   Útrain_from_filerO   r   rW   )r;   Z	depgraphsr/   rY   Z	input_strr   r   r   Útrain  s    	
ÿ,zMaltParser.trainc              	   C   sÎ   t |tƒrŒtjd| jddd��^}| ¡ �&}| ¡ }| t|ƒ¡ W d  ƒ n1 sT0    Y  | j	|j
|d�W  d  ƒ S 1 s‚0    Y  | j|dd�}|  ||¡}|d	krÄtd
d |¡|f ƒ‚d| _dS )z—
        Train MaltParser from a file
        :param conll_file: str for the filename of the training input data
        :type conll_file: str
        rq   r>   Fr?   Nrt   ZlearnrE   r   z1MaltParser training (%s) failed with exit code %drF   T)Ú
isinstancer   r7   rJ   r9   rT   rU   rK   rL   ru   rO   rN   rR   rI   rS   r6   )r;   Z
conll_filer/   rY   Zconll_input_fileZ	conll_strr[   r\   r   r   r   ru   '  s$    	

ÿ
,.ÿÿzMaltParser.train_from_file)r   NNN)Fr=   )Fr=   )NN)F)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r<   r^   ra   rN   ÚstaticmethodrR   rv   ru   r   r   r   r   r2   ^   s       û
*
B

 
r2   Ú__main__)Úinspectr   rk   rg   r7   Z	nltk.datar   Znltk.internalsr   r   r   Znltk.parse.apir   Znltk.parse.dependencygraphr   Znltk.parse.utilr   r   r-   r1   r2   rx   ÚdoctestÚtestmodr   r   r   r   Ú<module>
   s"   & gC