Ë
    çÍ:jï   ã                   ó†   — 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„Zd	„ Zd
„ Z G d„ d«      Zdd„Zy)z 
Utility functions for parsers.
é    )Úload)ÚCFGÚPCFGÚFeatureGrammar)ÚChartÚChartParser)ÚFeatureChartÚFeatureChartParser)ÚInsideChartParserNc                 ó"  — t        | fi |¤Ž}t        |t        «      st        d«      ‚t        |t        «      r|€t
        } ||||¬«      S t        |t        «      r|€t        }|€t        } ||||¬«      S |€t        }|€t        } ||||¬«      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.)ÚtraceÚ	beam_size)r   Úchart_class)r   Ú
isinstancer   Ú
ValueErrorr   r   r   r
   r	   r   r   )Úgrammar_urlr   Úparserr   r   Ú	load_argsÚgrammars          úd/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/nltk/parse/util.pyÚload_parserr      s¡   € ôD �;Ñ, )Ñ,€GÜ�gœsÔ#ÜÐOÓPÐPÜ�'œ4Ô Øˆ>Ü&ˆFÙ�g U°iÔ@Ð@ä	�Gœ^Ô	,Øˆ>Ü'ˆFØÐÜ&ˆKÙ�g U¸ÔDÐDð ˆ>Ü ˆFØÐÜˆKÙ�g U¸ÔDÐDó    c              #   ó–   K  — t        | d¬«      D ]5  \  }\  }}t        |«      |d||dddddg
}dj                  |«      dz   }|–— Œ7 y­w)	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ÚtagÚ	input_strs        r   Útaggedsent_to_conllr)   O   s\   è ø€ ô( $ H°AÔ6ò ‰ˆ‰;ˆD�#Ü˜“V˜T 3¨¨S°#°s¸CÀÀcÐJˆ	Ø—I‘I˜iÓ(¨4Ñ/ˆ	Ø‹ñùs   ‚AA	c              #   óL   K  — | D ]  }t        |«      E d{  –—†  d–— Œ y7 Œ­w)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$   s     r   Útaggedsents_to_conllr,   i   s.   è ø€ ðB ò ˆÜ& xÓ0×0Ð0Ø‹ñØ0ús   ‚$–"—$c                   ó    — e Zd ZdZdd„Zdd„Zy)ÚTestGrammarz
    Unit tests for  CFG.
    Nc                 ó`   — || _         t        |d¬«      | _        || _        || _        || _        y )Nr   )r   )Útest_grammarr   ÚcpÚsuiteÚ_acceptÚ_reject)Úselfr   r2   ÚacceptÚrejects        r   Ú__init__zTestGrammar.__init__™   s-   € Ø#ˆÔä˜g¨QÔ/ˆŒØˆŒ
ØˆŒØˆ�r   c                 ó¨  — | j                   D ]Ã  }t        |d   dz   d¬«       dD ]˜  }||   D ]Ž  }|j                  «       }t        | j                  j                  |«      «      }|r)|r't        «        t        |«       |D ]  }t        |«       Œ |dk(  r|g k(  rt        d|z  «      ‚d}Œ}|rt        d	|z  «      ‚d}	Œ� Œš sŒ¶	sŒ¹t        d
«       ŒÅ y)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)r6   r7   r6   zSentence '%s' failed to parse'TzSentence '%s' received a parse'zAll tests passed!N)r2   ÚprintÚsplitÚlistr1   Úparser   )
r5   Ú
show_treesÚtestÚkeyÚsentÚtokensÚtreesÚtreeÚacceptedÚrejecteds
             r   ÚrunzTestGrammar.run¡   sä   € ð —J‘Jò 	+ˆDÜ�$�u‘+ Ñ#¨Õ-Ø+ò ,�Ø  ™Iò ,�DØ!ŸZ™Z›\�FÜ  §¡§¡¨vÓ!6Ó7�EÙ!¡eÜœÜ˜dœØ$)ò (˜DÜ! $�Kð(à˜h’Ø  Bš;Ü",Ð-MÐPTÑ-TÓ"UÐUà'+™Há Ü",Ð-NÐQUÑ-UÓ"VÐVà'+™Hñ#,ð,ò& šHÜÐ)Õ*ñ-	+r   )NN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   rK   © r   r   r.   r.   ”   s   „ ñóô!+r   r.   c                 óD  — |�| j                  |«      } g }| j                  d«      D ]v  }|dk(  s|d   |v rŒ|j                  dd«      }d}t        |«      dk(  r'|d   dv r|d   d	v }|d   }nt        |d   «      }|d   }|j                  «       }|g k(  rŒo|||fgz  }Œx |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)rT   rU   )Údecoder?   ÚlenÚint)ÚstringÚcomment_charsÚencodingr+   r$   Ú
split_infoÚresultrF   s           r   Úextract_test_sentencesr`   Å   s×   € ð$ ÐØ—‘˜xÓ(ˆØ€IØ—L‘L Ó&ò (ˆØ�rŠ>˜X a™[¨MÑ9ØØ—^‘^ C¨Ó+ˆ
ØˆÜˆz‹?˜aÒØ˜!‰}Ð BÑBØ# A™Ð*:Ð:�Ø% a™=‘ä˜Z¨™]Ó+�Ø% a™=�Ø—‘Ó!ˆØ�RŠ<ØØ�v˜vÐ&Ð'Ñ'‰	ð(ð  Ðr   )r   NNr   )z#%;N)rO   Ú	nltk.datar   Únltk.grammarr   r   r   Únltk.parse.chartr   r   Únltk.parse.featurechartr	   r
   Únltk.parse.pchartr   r   r)   r,   r.   r`   rP   r   r   ú<module>rf      sG   ðñõ ß 2Ñ 2ß /ß DÝ /ð DEó6Eòrò4#÷V.+ñ .+ôb%r   