
    /h2?                         S SK r S SKrS SKrS SKrS SKrS SKJr  S SKJrJ	r	J
r
  S SKJr  S SKJr  S SKJr  S rS rS	 r " S
 S\5      r\S:X  a   S SKr\R.                  " 5         gg)    N)ZipFilePathPointer)find_dir	find_filefind_jars_within_path)ParserI)DependencyGraph)taggedsents_to_conllc                  :    SSK Jn   U " / SQ5      nUR                  $ )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     G/var/www/auris/envauris/lib/python3.13/site-packages/nltk/parse/malt.pymalt_regex_taggerr"      s!    %	
G@ ;;    c                    [         R                  R                  U 5      (       a  U nO
[        U SS9n/ SQn[	        [        U5      5      nU Vs1 s H%  n[         R                  R                  U5      S   iM'     nn1 SknUR                  U5      (       d   e[        [        S U5      5      (       d   e[        U5      $ s  snf )z=
A module to find MaltParser .jar file and its dependencies.
)MALT_PARSER)env_vars) r'   r'      >   	log4j.jar
libsvm.jarliblinear-1.8.jarc                 T    U R                  S5      =(       a    U R                  S5      $ )Nzmaltparser-z.jar)
startswithendswith)is    r!   <lambda>!find_maltparser.<locals>.<lambda>M   s    m4KF9KKr#   )ospathexistsr   setr   splitissubsetanyfilterlist)parser_dirname	_malt_dirmalt_dependencies
_malt_jarsjar_jarss         r!   find_maltparserrA   =   s     
ww~~n%%"	^6FG	$*956J.89jsRWW]]3"jE9H%%e,,,,KUS    
 :s   ,C c                 l    U c  g[         R                  R                  U 5      (       a  U $ [        U SSS9$ )z0
A module to find pre-trained MaltParser model.
malt_temp.mco)
MALT_MODELF)r&   verbose)r2   r3   r4   r   )model_filenames    r!   find_malt_modelrG   R   s5     		'	'/5QQr#   c                   p    \ rS rSrSr    SS jrSS jrSS jrSS jr\	SS j5       r
SS	 jrSS
 jrSrg)
MaltParser^   a  
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)) .)
Nc                     [        U5      U l        Ub  UO/ U l        [        U5      U l        U R                  S:g  U l        [        R                  " 5       U l        Ub  X0l
        g[        5       U l
        g)aP  
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
NrC   )rA   	malt_jarsadditional_java_argsrG   model_trainedtempfile
gettempdirworking_dirr"   tagger)selfr;   rF   rS   rM   s        r!   __init__MaltParser.__init__r   sh    : )8 %9$D " 	! %^4


o5#..0 & 2f8I8Kr#   c              #     #    U R                   (       d  [        S5      e[        R                  " SU R                  SSS9 n[        R                  " SU R                  SSS9 n[        U5       H  nUR                  [        U5      5        M     UR                  5         U R                  UR                  UR                  SS9n[        R                  " 5       n [        R                  " [        R                  R                  U R                   5      S	   5        U R#                  Xr5      n	[        R                  " U5        U	S	:w  a  [        S
SR%                  U5      U	4-  5      e['        UR                  5       n
U
R)                  5       R                  S5       H  n[+        [-        XS9/5      v   M     SSS5        SSS5        SSS5        [        R.                  " WR                  5        [        R.                  " WR                  5        g!    N= f! , (       d  f       Nf= f! , (       d  f       No= f! , (       d  f       Nx= f7f)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)rO   	ExceptionrP   NamedTemporaryFilerR   r	   writestrclosegenerate_malt_commandnamer2   getcwdchdirr3   r6   rN   _executejoinopenreaditerr   remove)rT   	sentencesrE   rb   
input_fileoutput_filelinecmd_current_pathretinfiletree_strs               r!   parse_tagged_sentsMaltParser.parse_tagged_sents   s     }}NOO((&D,<,<3u
,,+$$	
 0;D$$SY/ <  " 00OO[%5%5G 1  !#		HHRWW]]4::6q9: mmC1'!8#"%(XXc]C$89  +**+v$*KKM$7$7$? $3(0%&!" %@ ,E
b 			*//"
		+""#3 ,+E 
 
si   >I
 "H9"A7H(A HA!H(;<H7H(?H9A	I
HH(
H%!H((
H6	2H99
II
c                 <   ^  U 4S jU 5       nT R                  XBUS9$ )a.  
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   F   >#    U  H  nTR                  U5      v   M     g 7f)N)rS   ).0sentencerT   s     r!   	<genexpr>)MaltParser.parse_sents.<locals>.<genexpr>   s     L)hDKK11)s   !ra   )r{   )rT   rr   rE   rb   tagged_sentencess   `    r!   parse_sentsMaltParser.parse_sents   s/     M)L&&:L ' 
 	
r#   c                    S/nX@R                   -  n[        R                  R                  S5      (       a  SOSnUSUR	                  U R
                  5      /-  nUS/-  n[        R                  R                  U R                  5      (       a2  US[        R                  R                  U R                  5      S   /-  nOUSU R                  /-  nUS	U/-  nUS
:X  a  USU/-  nUSU/-  nU$ )z
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)rM   sysplatformr-   rm   rL   r2   r3   r4   rN   r6   )rT   inputfilenameoutputfilenamer\   rv   classpaths_separators         r!   rh    MaltParser.generate_malt_command   s     h(((&)ll&=&=e&D&Ds# %%dnn5
 	
 	%&& 77>>$**%%D"''--

3B788CD$**%%Cm$$7?D.))Cd|
r#   c                 ~    U(       a  S O[         R                  n[         R                  " XUS9nUR                  5       $ )N)stdoutstderr)
subprocessPIPEPopenwait)rv   rE   outputps       r!   rl   MaltParser._execute  s,     jooS?vvxr#   c                 R   [         R                  " SU R                  SSS9 nSR                  S U 5       5      nUR	                  [        U5      5        SSS5        U R                  WR                  US9  [        R                  " UR                  5        g! , (       d  f       NI= f)	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.rX   FrY   
c              3   B   #    U  H  oR                  S 5      v   M     g7f)
   N)to_conll)r   dgs     r!   r   #MaltParser.train.<locals>.<genexpr>   s     !FIb++b//Is   NrE   )
rP   rd   rR   rm   re   rf   train_from_fileri   r2   rq   )rT   	depgraphsrE   rs   	input_strs        r!   trainMaltParser.train  s     ((&D,<,<3u
		!FI!FFIS^,	
 	Z__g>
		*//"
 
s   3B
B&c                    [        U[        5      (       a  [        R                  " SU R                  SSS9 nUR                  5        nUR                  5       nUR                  [        U5      5        SSS5        U R                  UR                  US9sSSS5        $ U R                  USS9nU R                  Xb5      nUS	:w  a  [        S
SR                  U5      U4-  5      eSU l        g! , (       d  f       N= f! , (       d  f       Nm= f)zw
Train MaltParser from a file
:param conll_file: str for the filename of the training input data
:type conll_file: str
r   rX   FrY   Nr   learnr_   r   z1MaltParser training (%s) failed with exit code %dr`   T)
isinstancer   rP   rd   rR   rn   ro   re   rf   r   ri   rh   rl   rc   rm   rO   )rT   
conll_filerE   rs   conll_input_file	conll_strrv   rx   s           r!   r   MaltParser.train_from_file'  s     j"455,,*0@0@sSX__&*: 0 5 5 7I$$S^4 ' ++JOOW+M  (('(BmmC)!8 XXc]C01   '& s#   C=+C,3!C=,
C:	6C==
D)rO   rM   rL   rN   rS   rR   )r'   NNN)Fnull)NN)F)__name__
__module____qualname____firstlineno____doc__rU   r{   r   rh   staticmethodrl   r   r   __static_attributes__ r#   r!   rI   rI   ^   sL    * !(LT@$D
 @  
#&r#   rI   __main__)inspectr2   r   r   rP   	nltk.datar   nltk.internalsr   r   r   nltk.parse.apir   nltk.parse.dependencygraphr   nltk.parse.utilr	   r"   rA   rG   rI   r   doctesttestmodr   r#   r!   <module>r      sn     	  
  ( E E " 6 0#L*	Rc cL z@D OOK r#   