o
    rZh                      @   s   d Z ddlmZ ddlmZmZmZ ddlmZm	Z	 ddl
mZmZ ddlmZ 	ddd	Zd
d Zdd ZG dd dZdddZdS )z 
Utility functions for parsers.
    )load)CFGPCFGFeatureGrammar)ChartChartParser)FeatureChartFeatureChartParser)InsideChartParserNc                 K   s   t | fi |}t|tstdt|tr#|du rt}||||dS t|tr;|du r.t}|du r4t}||||dS |du rAt	}|du rGt
}||||dS )a  
    Load a grammar from a file, and build a parser based on that grammar.
    The parser depends on the grammar format, and might also depend
    on properties of the grammar itself.

    The following grammar formats are currently supported:
      - ``'cfg'``  (CFGs: ``CFG``)
      - ``'pcfg'`` (probabilistic CFGs: ``PCFG``)
      - ``'fcfg'`` (feature-based CFGs: ``FeatureGrammar``)

    :type grammar_url: str
    :param grammar_url: A URL specifying where the grammar is located.
        The default protocol is ``"nltk:"``, which searches for the file
        in the the NLTK data package.
    :type trace: int
    :param trace: The level of tracing that should be used when
        parsing a text.  ``0`` will generate no tracing output;
        and higher numbers will produce more verbose tracing output.
    :param parser: The class used for parsing; should be ``ChartParser``
        or a subclass.
        If None, the class depends on the grammar format.
    :param chart_class: The class used for storing the chart;
        should be ``Chart`` or a subclass.
        Only used for CFGs and feature CFGs.
        If None, the chart class depends on the grammar format.
    :type beam_size: int
    :param beam_size: The maximum length for the parser's edge queue.
        Only used for probabilistic CFGs.
    :param load_args: Keyword parameters used when loading the grammar.
        See ``data.load`` for more information.
    z1The grammar must be a CFG, or a subclass thereof.N)trace	beam_size)r   chart_class)r   
isinstancer   
ValueErrorr   r
   r   r	   r   r   r   )Zgrammar_urlr   parserr   r   Z	load_argsgrammar r   >/var/www/auris/lib/python3.10/site-packages/nltk/parse/util.pyload_parser   s$   "


r   c                 c   sP    t | ddD ]\}\}}t||d||dddddg
}d|d }|V  qdS )	a  
    A module to convert a single POS tagged sentence into CONLL format.

    >>> from nltk import word_tokenize, pos_tag
    >>> text = "This is a foobar sentence."
    >>> for line in taggedsent_to_conll(pos_tag(word_tokenize(text))): # doctest: +NORMALIZE_WHITESPACE
    ... 	print(line, end="")
        1	This	_	DT	DT	_	0	a	_	_
        2	is	_	VBZ	VBZ	_	0	a	_	_
        3	a	_	DT	DT	_	0	a	_	_
        4	foobar	_	JJ	JJ	_	0	a	_	_
        5	sentence	_	NN	NN	_	0	a	_	_
        6	.		_	.	.	_	0	a	_	_

    :param sentence: A single input sentence to parse
    :type sentence: list(tuple(str, str))
    :rtype: iter(str)
    :return: a generator yielding a single sentence in CONLL format.
       )start_0a	
N)	enumeratestrjoin)sentenceiwordtagZ	input_strr   r   r   taggedsent_to_conllO   s   r#   c                 c   s$    | D ]}t |E dH  dV  qdS )aV  
    A module to convert the a POS tagged document stream
    (i.e. list of list of tuples, a list of sentences) and yield lines
    in CONLL format. This module yields one line per word and two newlines
    for end of sentence.

    >>> from nltk import word_tokenize, sent_tokenize, pos_tag
    >>> text = "This is a foobar sentence. Is that right?"
    >>> sentences = [pos_tag(word_tokenize(sent)) for sent in sent_tokenize(text)]
    >>> for line in taggedsents_to_conll(sentences): # doctest: +NORMALIZE_WHITESPACE
    ...     if line:
    ...         print(line, end="")
    1	This	_	DT	DT	_	0	a	_	_
    2	is	_	VBZ	VBZ	_	0	a	_	_
    3	a	_	DT	DT	_	0	a	_	_
    4	foobar	_	JJ	JJ	_	0	a	_	_
    5	sentence	_	NN	NN	_	0	a	_	_
    6	.		_	.	.	_	0	a	_	_
    <BLANKLINE>
    <BLANKLINE>
    1	Is	_	VBZ	VBZ	_	0	a	_	_
    2	that	_	IN	IN	_	0	a	_	_
    3	right	_	NN	NN	_	0	a	_	_
    4	?	_	.	.	_	0	a	_	_
    <BLANKLINE>
    <BLANKLINE>

    :param sentences: Input sentences to parse
    :type sentence: list(list(tuple(str, str)))
    :rtype: iter(str)
    :return: a generator yielding sentences in CONLL format.
    Nz

)r#   )	sentencesr   r   r   r   taggedsents_to_conlli   s
   !r%   c                   @   s$   e Zd ZdZdddZd	ddZdS )
TestGrammarz
    Unit tests for  CFG.
    Nc                 C   s*   || _ t|dd| _|| _|| _|| _d S )Nr   )r   )Ztest_grammarr   cpsuite_acceptZ_reject)selfr   r(   acceptrejectr   r   r   __init__   s
   
zTestGrammar.__init__Fc           
      C   s   | j D ][}t|d d dd dD ]D}|| D ]=}| }t| j|}|r9|r9t  t| |D ]}t| q2|dkrJ|g krGtd| d}q|rRtd	| d}	qq|r^|	r^td
 qdS )a}  
        Sentences in the test suite are divided into two classes:

        - grammatical (``accept``) and
        - ungrammatical (``reject``).

        If a sentence should parse according to the grammar, the value of
        ``trees`` will be a non-empty list. If a sentence should be rejected
        according to the grammar, then the value of ``trees`` will be None.
        doc: )end)r+   r,   r+   zSentence '%s' failed to parse'TzSentence '%s' received a parse'zAll tests passed!N)r(   printsplitlistr'   parser   )
r*   Z
show_treestestkeysenttokensZtreestreeacceptedZrejectedr   r   r   run   s.   

zTestGrammar.run)NN)F)__name__
__module____qualname____doc__r-   r<   r   r   r   r   r&      s    
r&   #%;c                 C   s   |dur	|  |} g }| dD ]F}|dks|d |v rq|dd}d}t|dkrF|d dv r<|d d	v }|d }n
t|d }|d }| }|g krOq|||fg7 }q|S )
a  
    Parses a string with one test sentence per line.
    Lines can optionally begin with:

    - a bool, saying if the sentence is grammatical or not, or
    - an int, giving the number of parse trees is should have,

    The result information is followed by a colon, and then the sentence.
    Empty lines and lines beginning with a comment char are ignored.

    :return: a list of tuple of sentences and expected results,
        where a sentence is a list of str,
        and a result is None, or bool, or int

    :param comment_chars: ``str`` of possible comment characters.
    :param encoding: the encoding of the string, if it is binary
    Nr    r   r/   r      )TruetrueFalsefalse)rD   rE   )decoder3   lenint)stringZcomment_charsencodingr$   r   Z
split_inforesultr9   r   r   r   extract_test_sentences   s&   

rN   )r   NNr   )rA   N)r@   Z	nltk.datar   Znltk.grammarr   r   r   Znltk.parse.chartr   r   Znltk.parse.featurechartr   r	   Znltk.parse.pchartr
   r   r#   r%   r&   rN   r   r   r   r   <module>   s   

9+1