
    Ng2?                         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 Zd Zd	 Z G d
 de          Zedk    r	 d dlZ ej                     dS dS )    N)ZipFilePathPointer)find_dir	find_filefind_jars_within_path)ParserI)DependencyGraph)taggedsents_to_conllc                  6    ddl m}   | g d          }|j        S )Nr   )RegexpTagger))z\.$.)z\,$,)z\?$?)z\($()z\)$))z\[$[)z\]$])z^-?[0-9]+(\.[0-9]+)?$CD)z(The|the|A|a|An|an)$DT)z&(He|he|She|she|It|it|I|me|Me|You|you)$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$JJ)z.*ness$NN)z.*ly$RB)z.*s$NNS)z.*ing$VBG)z.*ed$VBD)z.*r   )nltk.tagr   tag)r   _taggers     K/var/www/html/ai-engine/env/lib/python3.11/site-packages/nltk/parse/malt.pymalt_regex_taggerr"      s?    %%%%%%l	
 	
 	
 G@ ;    c                 T   t           j                            |           r| }nt          | d          }g d}t	          t          |                    }d |D             }h d}|                    |          sJ t          t          d |                    sJ t          |          S )zE
    A module to find MaltParser .jar file and its dependencies.
    )MALT_PARSER)env_vars) r'   r'   c                 X    h | ]'}t           j                            |          d          (S )   )ospathsplit).0jars     r!   	<setcomp>z"find_maltparser.<locals>.<setcomp>H   s+    999sRW]]3"999r#   >   	log4j.jar
libsvm.jarliblinear-1.8.jarc                 V    |                      d          o|                     d          S )Nzmaltparser-z.jar)
startswithendswith)is    r!   <lambda>z!find_maltparser.<locals>.<lambda>M   s#    m44KF9K9K r#   )
r*   r+   existsr   setr   issubsetanyfilterlist)parser_dirname	_malt_dirmalt_dependencies
_malt_jars_jarss        r!   find_maltparserrC   =   s     
w~~n%% H"		^6FGGG	$*95566J99j999EHHH%%e,,,,,KKUSS     
r#   c                 p    | dS t           j                            |           r| S t          | dd          S )z8
    A module to find pre-trained MaltParser model.
    Nmalt_temp.mco)
MALT_MODELF)r&   verbose)r*   r+   r8   r   )model_filenames    r!   find_malt_modelrI   R   sA     		'	' R/5QQQQr#   c                   b    e Zd ZdZ	 	 	 	 ddZddZddZdd	Zedd
            Z	ddZ
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                     t          |          | _        ||ng | _        t          |          | _        | j        dk    | _        t          j                    | _        ||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://goo.gl/mpDBvQ)
        :type additional_java_args: list
        NrE   )rC   	malt_jarsadditional_java_argsrI   model_trainedtempfile
gettempdirworking_dirr"   tagger)selfr>   rH   rT   rN   s        r!   __init__zMaltParser.__init__r   sv    : )88 %9$D  " 	! %^44

o5#.00 & 2ff8I8K8Kr#   Fnullc           
   #     K   | j         st          d          t          j        d| j        dd          5 }t          j        d| j        dd          5 }t          |          D ]$}|                    t          |                     %|                                 | 	                    |j
        |j
        d          }t          j                    }	 t          j        t          j                            | j                  d	                    n#  Y nxY w|                     ||          }	t          j        |           |	d	k    r't          d
d                    |          |	fz            t'          |j
                  5 }
|
                                                    d          D ]#}t+          t-          ||          g          V  $	 ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   ddd           n# 1 swxY w Y   t          j        |j
                   t          j        |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r]   r   z0MaltParser parsing (%s) failed with exit code %d z

top_relation_labelN)rP   	ExceptionrQ   NamedTemporaryFilerS   r	   writestrclosegenerate_malt_commandnamer*   getcwdchdirr+   r,   rO   _executejoinopenreaditerr   remove)rU   	sentencesrG   rc   
input_fileoutput_filelinecmd_current_pathretinfiletree_strs               r!   parse_tagged_sentszMaltParser.parse_tagged_sents   s!      } 	PNOOO(&D,<3u
 
 
 /	,+$	   ,
 0;; 0 0D$$SYY////  """ 00O[%5G 1   !#	HRW]]4:66q9::::DmmC11'''!88#"%(XXc]]C$89   +*++ 
v$*KKMM$7$7$?$? 	 	 $3(0EW%& %& %&!"    	
 
 
 
 
 
 
 
 
 
 
 
 
 
 
E, , , , , , , , , , , , , , ,/	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	 /	b 		*/"""
	+"#####st   H	A>G2<DG2DA0G2AGG2GG2"G#G2&H	2G6	6H	9G6	:H		HHc                 P      fd|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   B   K   | ]}                     |          V  d S )N)rT   )r-   sentencerU   s     r!   	<genexpr>z)MaltParser.parse_sents.<locals>.<genexpr>   s/      LLhDKK11LLLLLLr#   rb   )r|   )rU   rs   rG   rc   tagged_sentencess   `    r!   parse_sentszMaltParser.parse_sents   sE     MLLL)LLL&&g:L ' 
 
 	
r#   c                    dg}|| j         z  }t          j                            d          rdnd}|d|                    | j                  gz  }|dgz  }t          j                            | j	                  r0|dt          j        
                    | j	                  d         gz  }n|d| j	        gz  }|d	|gz  }|d
k    r|d|gz  }|d|gz  }|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-cz-ir_   z-oz-m)rN   sysplatformr4   rn   rM   r*   r+   r8   rO   r,   )rU   inputfilenameoutputfilenamer]   rw   classpaths_separators         r!   ri   z MaltParser.generate_malt_command   s     ht((&)l&=&=e&D&DMss# %%dn55
 	
 	%&& 7>>$*%% 	&D"'--
33B788CCD$*%%Cm$$7??D.))Cd|
r#   c                 x    |rd nt           j        }t          j        | ||          }|                                S )N)stdoutstderr)
subprocessPIPEPopenwait)rw   rG   outputps       r!   rm   zMaltParser._execute  s6     5joS???vvxxr#   c                 \   t          j        d| j        dd          5 }d                    d |D                       }|                    t          |                     ddd           n# 1 swxY w Y   |                     |j        |           t          j	        |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.rY   FrZ   
c              3   @   K   | ]}|                     d           V  dS )
   N)to_conll)r-   dgs     r!   r   z#MaltParser.train.<locals>.<genexpr>   s,      !F!Fb"++b//!F!F!F!F!F!Fr#   NrG   )
rQ   re   rS   rn   rf   rg   train_from_filerj   r*   rr   )rU   	depgraphsrG   rt   	input_strs        r!   trainzMaltParser.train  s     (&D,<3u
 
 
 	-		!F!FI!F!F!FFFIS^^,,,		- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	Z_g>>>
	*/"""""s   AA,,A03A0c                 X   t          |t                    rt          j        d| j        dd          5 }|                                5 }|                                }|                    t          |                     ddd           n# 1 swxY w Y   | 	                    |j
        |          cddd           S # 1 swxY w Y   |                     |d          }|                     ||          }|d	k    r't          d
d                    |          |fz            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
        r   rY   FrZ   Nr   learnr`   r   z1MaltParser training (%s) failed with exit code %dra   T)
isinstancer   rQ   re   rS   ro   rp   rf   rg   r   rj   ri   rm   rd   rn   rP   )rU   
conll_filerG   rt   conll_input_file	conll_strrw   ry   s           r!   r   zMaltParser.train_from_file'  s    j"455 	N,*0@sSX   N__&& 5*: 0 5 5 7 7I$$S^^4445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ++JOW+MMN N N N N N N N N N N N N N N N (('(BBmmC))!88 XXc]]C01   s5   B>7B?B>B	B>B	B>>CC)r'   NNN)FrW   )NN)F)__name__
__module____qualname____doc__rV   r|   r   ri   staticmethodrm   r   r    r#   r!   rK   rK   ^   s         * !(L (L (L (LT@$ @$ @$ @$D
 
 
 
    @    \
# # # #&     r#   rK   __main__)inspectr*   r   r   rQ   	nltk.datar   nltk.internalsr   r   r   nltk.parse.apir   nltk.parse.dependencygraphr   nltk.parse.utilr	   r"   rC   rI   rK   r   doctesttestmodr   r#   r!   <module>r      s?    				     



  ( ( ( ( ( ( E E E E E E E E E E " " " " " " 6 6 6 6 6 6 0 0 0 0 0 0# # #L  *	R 	R 	Rc c c c c c c cL z@D NNNGOK r#   