a
     ˜ëj˜I  ã                   @   s¬   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m	Z	m
Z
 d dlmZ d dlmZ d dlmZ dZG dd	„ d	eƒZG d
d„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)ÚPIPE)Ú_java_optionsÚconfig_javaÚfind_jar_iterÚfind_jars_within_pathÚjava)ÚParserI©ÚDependencyGraph)ÚTreez2https://nlp.stanford.edu/software/lex-parser.shtmlc                   @   sr   e Zd ZdZdZdZdZdZdZddd„Z	dd„ Z
ddd„Zddd„Zddd„Zddd„Zd dd„Zd!dd„ZdS )"ÚGenericStanfordParserz Interface to the Stanford Parserz+stanford-parser-(\d+)(\.(\d+))+-models\.jarzstanford-parser\.jarz3edu.stanford.nlp.parser.lexparser.LexicalizedParserFNú4edu/stanford/nlp/models/lexparser/englishPCFG.ser.gzÚutf8ú-mx4gÚ c              
   C   sˆ   t t| j|ddt|dd�dd„ d�}t t| j|ddt|dd�d	d„ d�}	tj |¡d
 }
t|	gt	|
ƒ ƒ| _
|| _|| _|| _|| _d S )N)ZSTANFORD_PARSERÚSTANFORD_CORENLP© T)Zenv_varsZ
searchpathÚurlÚverboseZis_regexc                 S   s   t j | ¡S ©N©ÚosÚpathÚdirname©Ú
model_pathr   r   úR/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/parse/stanford.pyÚ<lambda>;   ó    z0GenericStanfordParser.__init__.<locals>.<lambda>)Úkey)ZSTANFORD_MODELSr   c                 S   s   t j | ¡S r   r   r   r   r   r   r   H   r   r   )Úmaxr   Ú_JARÚ_stanford_urlÚ_MODEL_JAR_PATTERNr   r   ÚsplitÚtupler   Ú
_classpathr   Ú	_encodingÚcorenlp_optionsÚjava_options)ÚselfZpath_to_jarZpath_to_models_jarr   Úencodingr   r)   r(   Zstanford_jarZ	model_jarZstanford_dirr   r   r   Ú__init__&   s<    ù	öù	özGenericStanfordParser.__init__c              	   C   s¢   g }g }g }d}|  d¡D ]~}|dkrŠ|rB| t|ƒ¡ g }d}q˜| jrh| |  d |¡¡¡ g }d}q˜| t|  d |¡¡gƒ¡ g }q| |¡ d}qt|ƒS )NFr   Ú
T)Ú
splitlinesÚappendÚiterÚ_DOUBLE_SPACED_OUTPUTÚ
_make_treeÚjoin)r*   Zoutput_ÚresZ	cur_linesZ	cur_treesZblankÚliner   r   r   Ú_parse_trees_outputV   s&    
z)GenericStanfordParser._parse_trees_outputc              
   C   sB   | j d| jddd| jdddg
}|  |  |d d	d
„ |D ƒ¡|¡¡S )aâ  
        Use StanfordParser to parse multiple sentences. Takes multiple sentences as a
        list where each sentence is a list of words.
        Each sentence will be automatically tagged with this StanfordParser instance's
        tagger.
        If whitespaces exists inside a token, then the token will be treated as
        separate tokens.

        :param sentences: Input sentences to parse
        :type sentences: list(list(str))
        :rtype: iter(iter(Tree))
        ú-modelú
-sentencesÚnewlineú-outputFormatú
-tokenizedz-escaperz-edu.stanford.nlp.process.PTBEscapingProcessorr-   c                 s   s   | ]}d   |¡V  qdS )ú N©r3   ©Ú.0Úsentencer   r   r   Ú	<genexpr>ˆ   r   z4GenericStanfordParser.parse_sents.<locals>.<genexpr>©Ú_MAIN_CLASSr   Ú_OUTPUT_FORMATr6   Ú_executer3   ©r*   Ú	sentencesr   Úcmdr   r   r   Úparse_sentsm   s     öÿÿz!GenericStanfordParser.parse_sentsc                 C   s   t |  |g|¡ƒS )a&  
        Use StanfordParser to parse a sentence. Takes a sentence as a string;
        before parsing, it will be automatically tokenized and tagged by
        the Stanford Parser.

        :param sentence: Input sentence to parse
        :type sentence: str
        :rtype: iter(Tree)
        )ÚnextÚraw_parse_sents©r*   r@   r   r   r   r   Ú	raw_parseŒ   s    
zGenericStanfordParser.raw_parsec                 C   s2   | j d| jddd| jg}|  |  |d |¡|¡¡S )aI  
        Use StanfordParser to parse multiple sentences. Takes multiple sentences as a
        list of strings.
        Each sentence will be automatically tokenized and tagged by the Stanford Parser.

        :param sentences: Input sentences to parse
        :type sentences: list(str)
        :rtype: iter(iter(Tree))
        r7   r8   r9   r:   r-   rB   rF   r   r   r   rK   ˜   s    ù	ÿz%GenericStanfordParser.raw_parse_sentsc                 C   s   t |  |g|¡ƒS )a0  
        Use StanfordParser to parse a sentence. Takes a sentence as a list of
        (word, tag) tuples; the sentence must have already been tokenized and
        tagged.

        :param sentence: Input sentence to parse
        :type sentence: list(tuple(str, str))
        :rtype: iter(Tree)
        )rJ   Útagged_parse_sentsrL   r   r   r   Útagged_parse¯   s    
z"GenericStanfordParser.tagged_parsec                    sR   d‰ | j d| jddd| jddˆ dd	d
dg}|  |  |d ‡ fdd„|D ƒ¡|¡¡S )ad  
        Use StanfordParser to parse multiple 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 sentences: list(list(tuple(str, str)))
        :rtype: iter(iter(Tree))
        ú/r7   r8   r9   r:   r;   z-tagSeparatorz-tokenizerFactoryz,edu.stanford.nlp.process.WhitespaceTokenizerz-tokenizerMethodZnewCoreLabelTokenizerFactoryr-   c                 3   s&   | ]}d   ‡ fdd„|D ƒ¡V  qdS )r<   c                 3   s   | ]}ˆ   |¡V  qd S r   r=   )r?   Ztagged©Ztag_separatorr   r   rA   Û   r   zEGenericStanfordParser.tagged_parse_sents.<locals>.<genexpr>.<genexpr>Nr=   r>   rQ   r   r   rA   Ú   s   ÿz;GenericStanfordParser.tagged_parse_sents.<locals>.<genexpr>rB   rF   r   rQ   r   rN   »   s2    
òþúÿz(GenericStanfordParser.tagged_parse_sentsc           	      C   s$  | j }| d|g¡ | jr*| | j ¡ ¡ d t¡}t| j|d� tj	ddd��¨}t
|tƒrj|rj| |¡}| |¡ | ¡  | jr¦| d¡ t|| j|ttd�\}}n"| |j¡ t|| jttd	�\}}| d
d¡}| dd¡}| |¡}W d   ƒ n1 sþ0    Y  t |j¡ t|dd� |S )Nz	-encodingr<   )Úoptionsr   ÚwbF)ÚmodeÚdeleter   )Ú	classpathÚstdinÚstdoutÚstderr)rV   rX   rY   s   Â ó    s     )r'   Úextendr(   r$   r3   r   r   r)   ÚtempfileÚNamedTemporaryFileÚ
isinstanceÚstrÚencodeÚwriteÚflushÚ
_USE_STDINÚseekr   r&   r   r/   ÚnameÚreplaceÚdecoder   Úunlink)	r*   rH   Zinput_r   r+   Zdefault_optionsZ
input_filerX   rY   r   r   r   rE   â   s<    



û
ÿ
(zGenericStanfordParser._execute)NNr   r   Fr   r   )F)F)F)F)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r!   rC   rc   r1   r,   r6   rI   rM   rK   rO   rN   rE   r   r   r   r   r      s*          ø
0




'r   c                       s,   e Zd ZdZdZ‡ fdd„Zdd„ Z‡  ZS )ÚStanfordParsera¤  
    >>> parser=StanfordParser(
    ...     model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"
    ... ) # doctest: +SKIP

    >>> list(parser.raw_parse("the quick brown fox jumps over the lazy dog")) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('ROOT', [Tree('NP', [Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['quick']), Tree('JJ', ['brown']),
    Tree('NN', ['fox'])]), Tree('NP', [Tree('NP', [Tree('NNS', ['jumps'])]), Tree('PP', [Tree('IN', ['over']),
    Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['lazy']), Tree('NN', ['dog'])])])])])])]

    >>> sum([list(dep_graphs) for dep_graphs in parser.raw_parse_sents((
    ...     "the quick brown fox jumps over the lazy dog",
    ...     "the quick grey wolf jumps over the lazy fox"
    ... ))], []) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('ROOT', [Tree('NP', [Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['quick']), Tree('JJ', ['brown']),
    Tree('NN', ['fox'])]), Tree('NP', [Tree('NP', [Tree('NNS', ['jumps'])]), Tree('PP', [Tree('IN', ['over']),
    Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['lazy']), Tree('NN', ['dog'])])])])])]), Tree('ROOT', [Tree('NP',
    [Tree('NP', [Tree('DT', ['the']), Tree('JJ', ['quick']), Tree('JJ', ['grey']), Tree('NN', ['wolf'])]), Tree('NP',
    [Tree('NP', [Tree('NNS', ['jumps'])]), Tree('PP', [Tree('IN', ['over']), Tree('NP', [Tree('DT', ['the']),
    Tree('JJ', ['lazy']), Tree('NN', ['fox'])])])])])])]

    >>> sum([list(dep_graphs) for dep_graphs in parser.parse_sents((
    ...     "I 'm a dog".split(),
    ...     "This is my friends ' cat ( the tabby )".split(),
    ... ))], []) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('ROOT', [Tree('S', [Tree('NP', [Tree('PRP', ['I'])]), Tree('VP', [Tree('VBP', ["'m"]),
    Tree('NP', [Tree('DT', ['a']), Tree('NN', ['dog'])])])])]), Tree('ROOT', [Tree('S', [Tree('NP',
    [Tree('DT', ['This'])]), Tree('VP', [Tree('VBZ', ['is']), Tree('NP', [Tree('NP', [Tree('NP', [Tree('PRP$', ['my']),
    Tree('NNS', ['friends']), Tree('POS', ["'"])]), Tree('NN', ['cat'])]), Tree('PRN', [Tree('-LRB-', [Tree('', []),
    Tree('NP', [Tree('DT', ['the']), Tree('NN', ['tabby'])]), Tree('-RRB-', [])])])])])])])]

    >>> sum([list(dep_graphs) for dep_graphs in parser.tagged_parse_sents((
    ...     (
    ...         ("The", "DT"),
    ...         ("quick", "JJ"),
    ...         ("brown", "JJ"),
    ...         ("fox", "NN"),
    ...         ("jumped", "VBD"),
    ...         ("over", "IN"),
    ...         ("the", "DT"),
    ...         ("lazy", "JJ"),
    ...         ("dog", "NN"),
    ...         (".", "."),
    ...     ),
    ... ))],[]) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('ROOT', [Tree('S', [Tree('NP', [Tree('DT', ['The']), Tree('JJ', ['quick']), Tree('JJ', ['brown']),
    Tree('NN', ['fox'])]), Tree('VP', [Tree('VBD', ['jumped']), Tree('PP', [Tree('IN', ['over']), Tree('NP',
    [Tree('DT', ['the']), Tree('JJ', ['lazy']), Tree('NN', ['dog'])])])]), Tree('.', ['.'])])])]
    Zpennc                    s&   t jdtdd� tƒ j|i |¤Ž d S )NzcThe StanfordParser will be deprecated
Please use [91mnltk.parse.corenlp.CoreNLPParser[0m instead.é   ©Ú
stacklevel©ÚwarningsÚwarnÚDeprecationWarningÚsuperr,   ©r*   ÚargsÚkwargs©Ú	__class__r   r   r,   F  s    üzStanfordParser.__init__c                 C   s
   t  |¡S r   )r   Ú
fromstring©r*   Úresultr   r   r   r2   P  s    zStanfordParser._make_tree©ri   rj   rk   rl   rD   r,   r2   Ú__classcell__r   r   ry   r   rm     s   2
rm   c                       s,   e Zd ZdZdZ‡ fdd„Zdd„ Z‡  ZS )ÚStanfordDependencyParseraƒ
  
    >>> dep_parser=StanfordDependencyParser(
    ...     model_path="edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"
    ... ) # doctest: +SKIP

    >>> [parse.tree() for parse in dep_parser.raw_parse("The quick brown fox jumps over the lazy dog.")] # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('jumps', [Tree('fox', ['The', 'quick', 'brown']), Tree('dog', ['over', 'the', 'lazy'])])]

    >>> [list(parse.triples()) for parse in dep_parser.raw_parse("The quick brown fox jumps over the lazy dog.")] # doctest: +NORMALIZE_WHITESPACE +SKIP
    [[((u'jumps', u'VBZ'), u'nsubj', (u'fox', u'NN')), ((u'fox', u'NN'), u'det', (u'The', u'DT')),
    ((u'fox', u'NN'), u'amod', (u'quick', u'JJ')), ((u'fox', u'NN'), u'amod', (u'brown', u'JJ')),
    ((u'jumps', u'VBZ'), u'nmod', (u'dog', u'NN')), ((u'dog', u'NN'), u'case', (u'over', u'IN')),
    ((u'dog', u'NN'), u'det', (u'the', u'DT')), ((u'dog', u'NN'), u'amod', (u'lazy', u'JJ'))]]

    >>> sum([[parse.tree() for parse in dep_graphs] for dep_graphs in dep_parser.raw_parse_sents((
    ...     "The quick brown fox jumps over the lazy dog.",
    ...     "The quick grey wolf jumps over the lazy fox."
    ... ))], []) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('jumps', [Tree('fox', ['The', 'quick', 'brown']), Tree('dog', ['over', 'the', 'lazy'])]),
    Tree('jumps', [Tree('wolf', ['The', 'quick', 'grey']), Tree('fox', ['over', 'the', 'lazy'])])]

    >>> sum([[parse.tree() for parse in dep_graphs] for dep_graphs in dep_parser.parse_sents((
    ...     "I 'm a dog".split(),
    ...     "This is my friends ' cat ( the tabby )".split(),
    ... ))], []) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('dog', ['I', "'m", 'a']), Tree('cat', ['This', 'is', Tree('friends', ['my', "'"]), Tree('tabby', ['the'])])]

    >>> sum([[list(parse.triples()) for parse in dep_graphs] for dep_graphs in dep_parser.tagged_parse_sents((
    ...     (
    ...         ("The", "DT"),
    ...         ("quick", "JJ"),
    ...         ("brown", "JJ"),
    ...         ("fox", "NN"),
    ...         ("jumped", "VBD"),
    ...         ("over", "IN"),
    ...         ("the", "DT"),
    ...         ("lazy", "JJ"),
    ...         ("dog", "NN"),
    ...         (".", "."),
    ...     ),
    ... ))],[]) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [[((u'jumped', u'VBD'), u'nsubj', (u'fox', u'NN')), ((u'fox', u'NN'), u'det', (u'The', u'DT')),
    ((u'fox', u'NN'), u'amod', (u'quick', u'JJ')), ((u'fox', u'NN'), u'amod', (u'brown', u'JJ')),
    ((u'jumped', u'VBD'), u'nmod', (u'dog', u'NN')), ((u'dog', u'NN'), u'case', (u'over', u'IN')),
    ((u'dog', u'NN'), u'det', (u'the', u'DT')), ((u'dog', u'NN'), u'amod', (u'lazy', u'JJ'))]]

    Z	conll2007c                    s&   t jdtdd� tƒ j|i |¤Ž d S )NzwThe StanfordDependencyParser will be deprecated
Please use [91mnltk.parse.corenlp.CoreNLPDependencyParser[0m instead.rn   ro   rq   rv   ry   r   r   r,   ‡  s    üz!StanfordDependencyParser.__init__c                 C   s   t |dd�S )NÚroot©Ztop_relation_labelr	   r|   r   r   r   r2   ‘  s    z#StanfordDependencyParser._make_treer~   r   r   ry   r   r€   T  s   0
r€   c                       sJ   e Zd ZdZdZdZdZdZdZdZ	‡ fdd„Z
dd
d„Zdd„ Z‡  ZS )ÚStanfordNeuralDependencyParserar  
    >>> from nltk.parse.stanford import StanfordNeuralDependencyParser # doctest: +SKIP
    >>> dep_parser=StanfordNeuralDependencyParser(java_options='-mx4g')# doctest: +SKIP

    >>> [parse.tree() for parse in dep_parser.raw_parse("The quick brown fox jumps over the lazy dog.")] # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('jumps', [Tree('fox', ['The', 'quick', 'brown']), Tree('dog', ['over', 'the', 'lazy']), '.'])]

    >>> [list(parse.triples()) for parse in dep_parser.raw_parse("The quick brown fox jumps over the lazy dog.")] # doctest: +NORMALIZE_WHITESPACE +SKIP
    [[((u'jumps', u'VBZ'), u'nsubj', (u'fox', u'NN')), ((u'fox', u'NN'), u'det',
    (u'The', u'DT')), ((u'fox', u'NN'), u'amod', (u'quick', u'JJ')), ((u'fox', u'NN'),
    u'amod', (u'brown', u'JJ')), ((u'jumps', u'VBZ'), u'nmod', (u'dog', u'NN')),
    ((u'dog', u'NN'), u'case', (u'over', u'IN')), ((u'dog', u'NN'), u'det',
    (u'the', u'DT')), ((u'dog', u'NN'), u'amod', (u'lazy', u'JJ')), ((u'jumps', u'VBZ'),
    u'punct', (u'.', u'.'))]]

    >>> sum([[parse.tree() for parse in dep_graphs] for dep_graphs in dep_parser.raw_parse_sents((
    ...     "The quick brown fox jumps over the lazy dog.",
    ...     "The quick grey wolf jumps over the lazy fox."
    ... ))], []) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('jumps', [Tree('fox', ['The', 'quick', 'brown']), Tree('dog', ['over',
    'the', 'lazy']), '.']), Tree('jumps', [Tree('wolf', ['The', 'quick', 'grey']),
    Tree('fox', ['over', 'the', 'lazy']), '.'])]

    >>> sum([[parse.tree() for parse in dep_graphs] for dep_graphs in dep_parser.parse_sents((
    ...     "I 'm a dog".split(),
    ...     "This is my friends ' cat ( the tabby )".split(),
    ... ))], []) # doctest: +NORMALIZE_WHITESPACE +SKIP
    [Tree('dog', ['I', "'m", 'a']), Tree('cat', ['This', 'is', Tree('friends',
    ['my', "'"]), Tree('tabby', ['-LRB-', 'the', '-RRB-'])])]
    Zconllz)edu.stanford.nlp.pipeline.StanfordCoreNLPz%stanford-corenlp-(\d+)(\.(\d+))+\.jarz,stanford-corenlp-(\d+)(\.(\d+))+-models\.jarTc                    s4   t jdtdd� tƒ j|i |¤Ž |  jd7  _d S )Nz}The StanfordNeuralDependencyParser will be deprecated
Please use [91mnltk.parse.corenlp.CoreNLPDependencyParser[0m instead.rn   ro   z(-annotators tokenize,ssplit,pos,depparse)rr   rs   rt   ru   r,   r(   rv   ry   r   r   r,   ¼  s    üz'StanfordNeuralDependencyParser.__init__Fc                 C   s   t dƒ‚dS )z¶
        Currently unimplemented because the neural dependency parser (and
        the StanfordCoreNLP pipeline class) doesn't support passing in pre-
        tagged tokens.
        zxtagged_parse[_sents] is not supported by StanfordNeuralDependencyParser; use parse[_sents] or raw_parse[_sents] instead.N)ÚNotImplementedError)r*   rG   r   r   r   r   rN   Ç  s    ÿz1StanfordNeuralDependencyParser.tagged_parse_sentsc                 C   s   t |dd�S )NÚROOTr‚   r	   r|   r   r   r   r2   Ó  s    z)StanfordNeuralDependencyParser._make_tree)F)ri   rj   rk   rl   rD   rC   r!   r#   rc   r1   r,   rN   r2   r   r   r   ry   r   rƒ   •  s   
rƒ   )r   r\   rr   Ú
subprocessr   Znltk.internalsr   r   r   r   r   Znltk.parse.apir   Znltk.parse.dependencygraphr
   Z	nltk.treer   r"   r   rm   r€   rƒ   r   r   r   r   Ú<module>	   s    vCA