
    rhj              	         S r SSKJr  SSKJrJr  SSKrSSKrSSK	r	SSK
Jr  SSKJr  \R                  (       a  SSKJr  SrS	R%                  5       r " S
 S\" S/ SQ5      5      r " S S\" S/ SQ5      5      r " S S\5      r " S S5      r " S S\	R0                  5      r\/r\S:X  a  SSKr\R8                  " \5        gg)z*
Classes for searching for Lyric objects.
    )annotations)
namedtupleOrderedDictN)Music21Exception)note)
StreamTypez // zDel start end measure lyric text identifier absoluteStart absoluteEndc            
      L    \ rS rSr% SrSrSSSSSS	S
SSS.	rS\S'   S rS r	Sr
g)IndexedLyric    zR
A Lyric that has been indexed to its attached element and position in a Stream.

 z)the element that the lyric is attached tozSuppose that the entire lyric for the stream were a single string:
                 this is the index of the position in the string that this
                 lyric starts at.zSuppose that the entire lyric for the stream were a single string:
                 this is the index of the position in the string that this
                 lyric ends at.zrThe measureNumber of the measure that the element is in
                 in the stream.  Same as .el.measureNumberz.The :class:`~music21.note.Lyric` object itselfz"The text of the lyric as a string.zThe identifier of the lyriczBthe position, not in the current identifier, but in all the lyricsz"the end position in all the lyrics	elstartendmeasurelyrictext
identifierabsoluteStartabsoluteEnddict[str, str]	_DOC_ATTRc                    SU R                   < SU R                  < SU R                  < S3SU R                  < SU R                  < SU R
                  < S3-   SU R                  < S	3-   $ )
NzIndexedLyric(el=z, start=z, end=, zmeasure=z, lyric=z, text=identifier=))r   r   r   r   r   r   r   selfs    O/home/james-whalen/.local/lib/python3.13/site-packages/music21/search/lyrics.py__repr__IndexedLyric.__repr__:   sn    "477+Xdjj^6$((UWXT\\,HTZZN'$))VXYZ2!45 	6    c           
         [          Vs/ s H  o!R                  U[        X5      5      PM     nnU R                  " U6 $ s  snf )z+
see docs for SortTuple for what this does
)	_attrListgetgetattr	__class__)r   keywordsattroutLists       r   modifyIndexedLyric.modify?   s=     HQQyt<<gd&9:yQ~~w'' Rs   $>N)__name__
__module____qualname____firstlineno____doc__	__slots__r   __annotations__r    r+   __static_attributes__r   r"   r   r
   r
       sG    
 I9%#>E87a?!I~  6
(r"   r
   r   c                  @    \ rS rSr% SrSrSSSSSS	S
.rS\S'   S rSr	g)SearchMatchH   zD
A lightweight object representing the match (if any) for a search.
r   z_
            The measureNumber of the measure that the first matching lyric is in.
            z^
            The measureNumber of the measure that the last matching lyric is in.
            a  
            The text of the lyric that matched the search.  For a
            plaintext search, this will be the same as the search
            term, but for a regular expression
            search this will be the text that matched the regular
            expression.
            zY
            A list of all lyric-containing elements that matched this text.
            zD
            A list of IndexedLyric objects that match.
            zd
            The identifier of (presumably all, but at least the first) lyric to match.
            mStartmEnd	matchTextelsindicesr   r   r   c                    SU R                   < SU R                  < S3SU R                  < SU R                  < S3-   SU R                  < S3-   $ )	NzSearchMatch(mStart=z, mEnd=r   z
matchText=z, els=z, indices=[...], r   r   )r9   r:   r;   r<   r   r   s    r   r    SearchMatch.__repr__g   sY    %dkk_GDII=Kt~~0txxlBSTU2!45 	6r"   N)
r-   r.   r/   r0   r1   r2   r   r3   r    r4   r   r"   r   r6   r6   H   s?     I)!I~ 26r"   r6   r8   c                      \ rS rSrSrg)LyricSearcherExceptionm   r   N)r-   r.   r/   r0   r4   r   r"   r   rA   rA   m   s    r"   rA   c                      \ rS rSrSrSSS.SS jjjr\SS j5       r\SS j5       rSSS	 jjr	SSS
 jjr
SS jrSSS jjrSS jrSrg)LyricSearcherq   at  
An object that can find lyrics that match a certain regular expression
and return relevant information about the match.

Construct the LyricSearcher by passing in a Stream object (it can be
a Score or Part or other nested item), and then call ".search()" on it.

See :ref:`User's Guide, Chapter 28, Lyric Searching <usersGuide_28_lyricSearcher>` for
full details.

TODO: Bug that occasionally the previous note will be included; Search luca/gloria for
   "riam tuam." (From Gloriam tuam).  For some reason, the whole "Gloria" is included.
   Does not occur if only "iam tuam." is searched.

TODO: allow for all intermediate notes during a search to be found.
    includeIntermediateElements.

TODO: allow for trailing melismas to also be included.

TODO: Note that because of recursive searching w/ voices, there may be "phantom" lyrics
    found if a work contains multiple voices.
N wordSeparatorc               T    Xl         SU l        SU l        S U l        / U l        X l        g )NF)streamincludeIntermediateElementsincludeTrailingMelisma
_indexText_indexTuplesrH   )r   srH   s      r   __init__LyricSearcher.__init__   s,    '(+0(&+#$(02*r"   c                f    U R                   c  U R                  5         U R                   =(       d    S$ )z
Returns the text that has been indexed (a la, :func:`~music21.text.assembleLyrics`):

>>> p0 = corpus.parse('luca/gloria').parts[0]
>>> ls = search.lyrics.LyricSearcher(p0)
>>> ls.indexText[0:25]
'Et in terra pax hominibus'
 )rM   indexr   s    r   	indexTextLyricSearcher.indexText   s&     ??"JJL$"$r"   c                T    U R                   c  U R                  5         U R                  $ N)rM   rT   rN   r   s    r   indexTuplesLyricSearcher.indexTuples   s!    ??"JJL   r"   c                
   Uc  U R                   nOXl         [        5       n[        5       n[        5       nUR                  5       R                   GHH  nUR                  nU(       d  M  UR
                  nU GH  nUR                  (       d  M  UR                  n	X;  a  SX9'   SXI'   / X)'   X9   n
XI   nX)   n[        U
5      nUR                  nUS;   a  X-  n
O)XR                  U-   -  n
U[        U R                  5      -  nXU	'   [        X]U[        U5      -   XxUU	SS5	      nUR                  U5        UR                  (       d  UR                  nO=[        R                  (       a  UR                   c   eUR                   S   R                  nXU	'   GM     GMK     / nUR#                  5        H  nUR%                  U5        M     SnSnSn/ nU H  nUR                  U:w  a  UnUS:w  a  U[        [&        5      -  nUR                  nUR)                  UR*                  U-   UR,                  U-   S9nUR.                  nUR                  U5        M     Xl        [&        R3                  UR#                  5       5      n
Xl        U$ )a  
A method that indexes the Stream's lyrics and returns the list
of IndexedLyric objects.

This does not actually need to be run, since calling .search() will call this if
it hasn't already been called.

>>> from pprint import pprint as pp

>>> p0 = corpus.parse('luca/gloria').parts[0]
>>> ls = search.lyrics.LyricSearcher(p0)
>>> pp(ls.index()[0:5])
[IndexedLyric(el=<music21.note.Note C>, start=0, end=2, measure=1,
     lyric=<music21.note.Lyric number=1 syllabic=single text='Et'>, text='Et',
     identifier=1),
 IndexedLyric(el=<music21.note.Note D>, start=3, end=5, measure=2,
     lyric=<music21.note.Lyric number=1 syllabic=single text='in'>, text='in',
     identifier=1),
 IndexedLyric(el=<music21.note.Note F>, start=6, end=9, measure=2,
     lyric=<music21.note.Lyric number=1 syllabic=begin text='ter'>, text='ter',
     identifier=1),
 IndexedLyric(el=<music21.note.Note F>, start=9, end=11, measure=3,
     lyric=<music21.note.Lyric number=1 syllabic=end text='ra'>, text='ra',
     identifier=1),
 IndexedLyric(el=<music21.note.Note A>, start=12, end=15, measure=3,
     lyric=<music21.note.Lyric number=1 syllabic=single text='pax'>, text='pax',
     identifier=1)]

* Changed in v6.7: indexed lyrics get an identifier.
NrS   )beginmiddleNr   )r   r   )rJ   r   recursenoteslyricsmeasureNumberr   r   lenrH   r
   appendisCompositesyllabictTYPE_CHECKING
componentsvaluesextendLINEBREAK_TOKENr+   r   r   r   rN   joinrM   )r   rO   indexByIdentifieriTextByIdentifierlastSyllabicByIdentifiernlsmNumlylyIdentifieriTextlastSyllabicrT   posStarttxtilindexPreliminaryoneIdentifierIndexabsolutePosShiftlastIdentifierlastEndoneIndexnewIndexs                          r   rT   LyricSearcher.index   sn   @ 9AKFQm7B}<GM 	! ""A#$88B??Dww!}}868%3=A,:68%3)77E)7u:gg#<<LE//#55ED$6$6 77H27,/!!x#c(/BDc".16R ~~#%;;L!}}888#%==#4#=#=L9E6?  #L "3":":"<##$67 #= (H""n4#* a<$O(<<$%00NX^^FV5V3;<<BR3R ' TH**GLL" ) "$$%6%=%=%?@r"   c                   Uc  U R                   nX R                   Ld  U R                  (       d  U R                  U5        [        U[        5      (       a  SnO"[        US5      (       a  SnO[        U S35      eUSL a9  U R                  [        R                  " [        R                  " U5      5      5      $ U R                  U5      $ )a  
Return a list of SearchMatch objects matching a string or regular expression.

>>> import re

>>> p0 = corpus.parse('luca/gloria').parts[0]
>>> ls = search.lyrics.LyricSearcher(p0)
>>> ls.search('pax')
[SearchMatch(mStart=3, mEnd=3, matchText='pax', els=(<music21.note.Note A>,),
                indices=[...], identifier=1)]

Search a regular expression that takes into account non-word characters such as commas

>>> agnus = re.compile(r'agnus dei\W+filius patris', re.IGNORECASE)
>>> sm = ls.search(agnus)
>>> sm
[SearchMatch(mStart=49, mEnd=55, matchText='Agnus Dei, Filius Patris',
                els=(<music21.note.Note G>,...<music21.note.Note G>), indices=[...],
                identifier=1)]
>>> sm[0].mStart, sm[0].mEnd
(49, 55)

OMIT_FROM_DOCS

Make sure that regexp characters are not interpreted as such in plaintext:

This should only match Amen.

>>> ls.search('en.')
[SearchMatch(mStart=125, mEnd=125, matchText='en.', ...)]

This should match 'ene', 'eni', and 'en.'

>>> ls.search(re.compile('en.'))
[SearchMatch(mStart=13, mEnd=13, matchText='ene', ...),
 SearchMatch(mStart=38, mEnd=38, matchText='ens', ...),
 SearchMatch(mStart=42, mEnd=42, matchText='eni', ...),
 SearchMatch(mStart=125, mEnd=125, matchText='en.', ...)]
TfinditerFz3 is not a string or RE with the finditer() function)rJ   rN   rT   
isinstancestrhasattrrA   	_reSearchrecompileescape)r   textOrRerO   	plainTexts       r   searchLyricSearcher.search  s    R 9AKKt'8'8JJqMh$$IXz**I(*OPR R >>"**RYYx-@"ABB>>(++r"   c                    U R                    H,  nUR                  Us=::  a  UR                  ::  d  M&   Us  $   M.     [        SU S35      e)z
Finds an object in ._indexTuples by search position.

Raises exception if no IndexedLyric for that position.

Runs in O(n) time on number of lyrics. Would not be
hard to do in O(log(n)) for very large lyrics
Could not find position  in text)rN   r   r   rA   )r   posis      r   _findObjInIndexByPos"LyricSearcher._findObjInIndexByPosL  sJ     ""Aww#&& ' # %'?uH%MNNr"   c                    / nU R                    H8  nUR                  U:  d  M  UR                  U::  d  M'  UR                  U5        M:     U(       d  [	        SU S35      eU$ )zI
Finds a list of objects in ._indexTuples by search position (inclusive)
r   r   )rN   r   r   rd   rA   )r   rx   posEndr=   r   s        r   _findObjsInIndexByPos#LyricSearcher._findObjsInIndexByPos[  s[     ""A}}x'AOOv,Eq! # (+CH:X)VWWr"   c           
        / nU R                   c  U$ UR                  U R                   5       H  nUR                  5       u  pEUR                  S5      nU R	                  XES-
  5      nUS   nUS   n	[        UR                  U	R                  U[        S U 5       5      UUS   R                  S9n
UR                  U
5        M     U$ )Nr      r^   c              3  8   #    U  H  oR                   v   M     g 7frX   )r   ).0	thisIndexs     r   	<genexpr>*LyricSearcher._reSearch.<locals>.<genexpr>  s     &MW	||Ws   r8   )
rM   r   spangroupr   r6   r   tupler   rd   )r   r	locationsmabsoluteFoundPosabsoluteEndPosr;   r=   
indexStartindexEndsms              r   r   LyricSearcher._reSearch{  s    ')	??"DOO,A/0vvx,
I001ATUCUVG Jr{HJ$6$6"*"2"2'0!&&MW&M!M%,(/
(=(=B R  -  r"   )rM   rN   rK   rL   rJ   rH   rX   )rO   zStreamType | NonerH   r   )returnr   )r   zlist[IndexedLyric])r   list[SearchMatch])r   r
   )i?B )r   z
re.Patternr   r   )r-   r.   r/   r0   r1   rP   propertyrU   rY   rT   r   r   r   r   r4   r   r"   r   rD   rD   q   s^    .+# + + % % ! !
gR:,xO
@r"   rD   c                  (    \ rS rSr S rS rS rSrg)Testi  c                t  ^ ^^ SSK Jn  SSK Jm  SnUR                  USS9mTR	                  5       R
                  S   R                  S   nUUU 4S jnU" 5         SUR                  S   l        S	UR                  S
   l        U" 5         SUR                  S   l        SUR                  S
   l        U" 5         g)z4
This score uses a non-breaking space as an elision
r   	converterr   u  
        <score-partwise>
            <part-list>
                <score-part id="P1">
                <part-name>MusicXML Part</part-name>
                </score-part>
            </part-list>
            <part id="P1">
                <measure number="1">
                    <note>
                        <pitch>
                            <step>G</step>
                            <octave>4</octave>
                        </pitch>
                        <duration>1</duration>
                        <voice>1</voice>
                        <type>quarter</type>
                        <lyric number="1">
                            <syllabic>middle</syllabic>
                            <text>la</text>
                            <elision> </elision>
                            <syllabic>middle</syllabic>
                            <text>la</text>
                        </lyric>
                    </note>
                </measure>
            </part>
        </score-partwise>
        MusicXMLformatc                 t   > TR                   R                  T5      n TR                  U R                  S5        g )Nu   la la)ra   rD   assertEqualrU   )rr   rO   r   r   s    r   	runSearch0Test.testMultipleLyricsInNote.<locals>.runSearch  s+    ,,Q/BR\\84r"   r\   r   r   singleN)	music21r   r   parseflattenr`   ra   ri   rf   )r   r   partXMLrt   r   rO   r   s   `    @@r   testMultipleLyricsInNoteTest.testMultipleLyricsInNote  s     	&": OOGJO7YY[q!((+	5
 	$+a!$)a!$,a!$,a!r"   c                	   SSK Jn  SSK Jn  SnUR                  USS9nUR                  R                  U5      nU R                  UR                  S5        UR                  nU R                  [        U5      S5        [        UR                  5       R                  5      nU R                  US   R                  US   R                  S   5        U R                  US	   R                  US	   R                  S   5        U R                  US
   R                  US   R                  S	   5        U R                  US   R                  US	   R                  S	   5        U R                  US   R                  US
   R                  S   5        UR                  S5      nU R                  [        U5      S	5        US   n	U R                  U	R                  S
5        U R                  U	R                   S
5        U R                  U	R"                  US	   US
   45        U R                  U	R$                  S
5        U R                  [        U	R&                  5      S
5        U R                  U	R&                  S   R                  US	   R                  S	   5        U R                  U	R&                  S	   R                  US
   R                  S   5        [(        R*                  " S5      n
UR                  U
5      nU R                  [        U5      S
5        US   n	U R                  U	R                  S
5        U R                  U	R                   S
5        U R                  U	R,                  S5        U R                  U	R$                  S	5        U R                  U	R"                  US	   45        US	   nU R                  UR                  S
5        U R                  UR                   S
5        U R                  UR,                  S5        U R                  UR$                  S
5        U R                  UR"                  US
   45        UR                  S5      nU R                  [        U5      S	5        U R                  US   R                  S	5        U R                  US   R                   S
5        U R                  US   R$                  S	5        g )Nr   r   r   a
	  
        <score-partwise>
            <part-list>
                <score-part id="P1">
                <part-name>MusicXML Part</part-name>
                </score-part>
            </part-list>
            <part id="P1">
                <measure number="1">
                    <note>
                        <pitch>
                            <step>G</step>
                            <octave>4</octave>
                        </pitch>
                        <duration>2</duration>
                        <voice>1</voice>
                        <type>half</type>
                        <lyric number="1">
                            <syllabic>single</syllabic>
                            <text>hi</text>
                        </lyric>
                        <lyric number="2">
                            <syllabic>single</syllabic>
                            <text>bye</text>
                        </lyric>
                    </note>
                </measure>
                <measure number="2">
                    <note>
                        <pitch>
                            <step>A</step>
                            <octave>4</octave>
                        </pitch>
                        <duration>1</duration>
                        <voice>1</voice>
                        <type>quarter</type>
                        <lyric number="1">
                            <syllabic>begin</syllabic>
                            <text>there!</text>
                        </lyric>
                        <lyric number="2">
                            <syllabic>begin</syllabic>
                            <text>Mi</text>
                        </lyric>
                    </note>
                    <note>
                        <pitch>
                            <step>B</step>
                            <octave>4</octave>
                        </pitch>
                        <duration>1</duration>
                        <voice>1</voice>
                        <type>quarter</type>
                        <lyric number="2">
                            <syllabic>end</syllabic>
                            <text>chael.</text>
                        </lyric>
                    </note>
                </measure>
            </part>
        </score-partwise>
        r   r   zhi there! // bye Michael.   r            Michaelze[a-z]err   zi t)r   r   r   r   ra   rD   r   rU   rY   rc   listr   r`   assertIsr   r9   r:   r<   r   r=   r   r   r;   )r   r   r   r   rO   rr   tuplesr`   matchm0e_with_letterm1s               r   testMultipleVersesTest.testMultipleVerses  s   %"=| OOGJO7]]((+'BCVa(QYY[&&'fQioouQxq'9:fQioouQxq'9:fQioouQxq'9:fQioouQxq'9:fQioouQxq'9:		)$UQ'1XA&!$%(E!H!56*RZZ!,bjjm))58??1+=>bjjm))58??1+=>

9-		-(UQ'1XA&!$t,*%(-1XA&!$t,*%(-		% UQ'q!,q*q,,a0r"   c                X   SSK Jn  SSK Jn  SS KnSnUR	                  USS9n[        S5       H  nSU-  nUR                  R                  XWS	9nUR                  S
5       H  n	U	S   U-   U	S   -   n
UR                  U
5      nU R                  [        U5      S5        U R                  [        US   R                  5      S5        U R                  US   R                  S   R                  U	S   5        U R                  US   R                  S   R                  U	S   5        M     M     g )Nr   r   r   uq  <score-partwise version="4.0">
  <identification>
    <encoding>
      <software>MuseScore 4.5.2</software>
      <encoding-date>2025-06-22</encoding-date>
      <supports element="accidental" type="yes"/>
      <supports element="beam" type="yes"/>
      <supports element="print" attribute="new-page" type="no"/>
      <supports element="print" attribute="new-system" type="no"/>
      <supports element="stem" type="yes"/>
      </encoding>
    </identification>
  <part-list>
    <score-part id="P1">
      <part-name>钢琴, Track1</part-name>
      <part-abbreviation>Pno.</part-abbreviation>
      <score-instrument id="P1-I1">
        <instrument-name>钢琴</instrument-name>
        <instrument-sound>keyboard.piano</instrument-sound>
        </score-instrument>
      <midi-device id="P1-I1" port="1"></midi-device>
      <midi-instrument id="P1-I1">
        <midi-channel>1</midi-channel>
        <midi-program>1</midi-program>
        <volume>78.7402</volume>
        <pan>0</pan>
        </midi-instrument>
      </score-part>
    </part-list>
  <part id="P1">
    <measure number="1">
      <attributes>
        <divisions>2</divisions>
        <key>
          <fifths>0</fifths>
          </key>
        <time>
          <beats>4</beats>
          <beat-type>4</beat-type>
          </time>
        <clef>
          <sign>F</sign>
          <line>4</line>
          </clef>
        </attributes>
      <note dynamics="50">
        <pitch>
          <step>G</step>
          <octave>3</octave>
          </pitch>
        <duration>2</duration>
        <voice>1</voice>
        <type>quarter</type>
        <stem>down</stem>
        <lyric number="1">
          <syllabic>single</syllabic>
          <text>长</text>
          </lyric>
        </note>
      <note dynamics="50">
        <pitch>
          <step>E</step>
          <octave>3</octave>
          </pitch>
        <duration>1</duration>
        <voice>1</voice>
        <type>eighth</type>
        <stem>down</stem>
        <beam number="1">begin</beam>
        <notations>
          <slur type="start" number="1"/>
          </notations>
        <lyric number="1">
          <syllabic>single</syllabic>
          <text>亭</text>
          </lyric>
        </note>
      <note dynamics="50">
        <pitch>
          <step>G</step>
          <octave>3</octave>
          </pitch>
        <duration>1</duration>
        <voice>1</voice>
        <type>eighth</type>
        <stem>down</stem>
        <beam number="1">end</beam>
        <notations>
          <slur type="stop" number="1"/>
          </notations>
        </note>
      <note dynamics="50">
        <pitch>
          <step>C</step>
          <octave>4</octave>
          </pitch>
        <duration>2</duration>
        <voice>1</voice>
        <type>quarter</type>
        <stem>down</stem>
        <lyric number="1">
          <syllabic>single</syllabic>
          <text>外</text>
          </lyric>
        </note>
      <note>
        <rest/>
        <duration>2</duration>
        <voice>1</voice>
        <type>quarter</type>
        </note>
      </measure>
    <measure number="2">
      <note dynamics="50">
        <pitch>
          <step>A</step>
          <octave>3</octave>
          </pitch>
        <duration>2</duration>
        <voice>1</voice>
        <type>quarter</type>
        <stem>down</stem>
        <lyric number="1">
          <syllabic>single</syllabic>
          <text>古</text>
          </lyric>
        </note>
      <note dynamics="50">
        <pitch>
          <step>C</step>
          <octave>4</octave>
          </pitch>
        <duration>2</duration>
        <voice>1</voice>
        <type>quarter</type>
        <stem>down</stem>
        <lyric number="1">
          <syllabic>single</syllabic>
          <text>道</text>
          </lyric>
        </note>
      <note dynamics="50">
        <pitch>
          <step>G</step>
          <octave>3</octave>
          </pitch>
        <duration>2</duration>
        <voice>1</voice>
        <type>quarter</type>
        <stem>down</stem>
        <lyric number="1">
          <syllabic>single</syllabic>
          <text>边</text>
          </lyric>
        </note>
      <note>
        <rest/>
        <duration>2</duration>
        <voice>1</voice>
        <type>quarter</type>
        </note>
      <barline location="right">
        <bar-style>light-heavy</bar-style>
        </barline>
      </measure>
    </part>
  </score-partwise>r   r   r   rF   rG   u   长亭外古道边r   r   )r   r   r   more_itertoolsr   rangera   rD   pairwiser   rc   r<   r   )r   r   r   r   r   rO   
lenWordSepwordSeprr   pairkeywordr   s               r   testCustomSeparatorTest.testCustomSeparator<  s   %"fN OOGJO7(JJ&G,,Q,FB&//0DEq'G+d1g5		'*  UQ/  U1X\\!2A6  qa!6!6Q@  qa!6!6Q@ F #r"   r   N)r-   r.   r/   r0   r   r   r   r4   r   r"   r   r   r     s    2hn1`vAr"   r   __main__)r1   
__future__r   collectionsr   r   r   typingrg   unittestmusic21.exceptions21r   r   r   rh   music21.common.typesr   rl   splitr$   r
   r6   rA   rD   TestCaser   
_DOC_ORDERr-   mainTestr   r"   r   <module>r      s    # / 	   1  ??/RXXZ	$(:3 $(P"6*]\^ "6J	- 	^ ^H	]A8 ]AD _
 zT r"   