
    Wi                         d dl Zd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZmZ d dlmZ d dlmZ  G d	 d
e      Zy)    N)Matcher)Language)version)
csr_matrix)ListMappingTupleUnion)__version__)BaseRepresentationc                       e Zd ZdZ	 	 	 ddeeef   dedee   dz  fdZ	de
j                  ded	eeeeeef      f   d
eeeeeef      f   fdZy)PartOfSpeechaJ  Extract Topic Keywords based on their Part-of-Speech.

    DEFAULT_PATTERNS = [
                [{'POS': 'ADJ'}, {'POS': 'NOUN'}],
                [{'POS': 'NOUN'}],
                [{'POS': 'ADJ'}]
    ]

    From candidate topics, as extracted with c-TF-IDF,
    find documents that contain keywords found in the
    candidate topics. These candidate documents then
    serve as the representative set of documents from
    which the Spacy model can extract a set of candidate
    keywords for each topic.

    These candidate keywords are first judged by whether
    they fall within the DEFAULT_PATTERNS or the user-defined
    pattern. Then, the resulting keywords are sorted by
    their respective c-TF-IDF values.

    Arguments:
        model: The Spacy model to use
        top_n_words: The top n words to extract
        pos_patterns: Patterns for Spacy to use.
                      See https://spacy.io/usage/rule-based-matching

    Usage:

    ```python
    from bertopic.representation import PartOfSpeech
    from bertopic import BERTopic

    # Create your representation model
    representation_model = PartOfSpeech("en_core_web_sm")

    # Use the representation model in BERTopic on top of the default pipeline
    topic_model = BERTopic(representation_model=representation_model)
    ```

    You can define custom POS patterns to be extracted:

    ```python
    pos_patterns = [
                [{'POS': 'ADJ'}, {'POS': 'NOUN'}],
                [{'POS': 'NOUN'}], [{'POS': 'ADJ'}]
    ]
    representation_model = PartOfSpeech("en_core_web_sm", pos_patterns=pos_patterns)
    ```
    Nmodeltop_n_wordspos_patternsc                     t        |t              rt        j                  |      | _        n#t        |t
              r|| _        nt        d      || _        |ddiddigddigddigg| _        y || _        y )NzoMake sure that the Spacy model that youpass is either a string referring to aSpacy model or a Spacy nlp object.POSADJNOUN)	
isinstancestrspacyloadr   r   
ValueErrorr   r   )selfr   r   r   s       h/home/sietch6/trending-topics-pipeline/venv/lib/python3.12/site-packages/bertopic/representation/_pos.py__init__zPartOfSpeech.__init__B   s     eS!E*DJx(DJ5  '%1! !D !-D    	documentsc_tf_idftopicsreturnc                    t        | j                  j                        }|j                  d| j                         i }|j                         D ]   \  }}t        t        |       }	g }
|	D ]  }|j                  |j                  |k(  ddf   }|j                  |j                  j                  j                  |d      df   }t        |      dkD  sh|dd D ]  }|
j                  |         t        t!        |
            }
| j                  j#                  |
      }g }|D ]3  } ||      }|D ]$  \  }}}|j                  ||| j$                         & 5 t        t!        |            ||<   # t'        j(                  t*              t'        j(                  d      k\  r$t        |j,                  j/                               }n#t        |j,                  j1                               }t3        t        |t5        t        |                        }|j7                         D ci c]  }|g  }}|j                         D ]  \  }}t9        j:                  |D cg c]  }||v s|j=                  |       c}      }|j>                  dd|f   ||j@                  z      }t9        jB                  t9        jD                  |jG                         jI                  d	d
            d         | jJ                   d ddd
   }t9        j:                  t9        jD                  |jG                         jI                  d	d
            d         | jJ                   d ddd
   }t        ||      D cg c]  \  }}|||      |f }}}|||<   t        ||         | jJ                  k  sc||xx   t5        | jJ                  t        ||         z
        D cg c]  }d c}z  cc<    |S c c}w c c}w c c}}w c c}w )a1  Extract topics.

        Arguments:
            topic_model: A BERTopic model
            documents: All input documents
            c_tf_idf: Not used
            topics: The candidate topics as calculated with c-TF-IDF

        Returns:
            updated_topics: Updated topic representations
        PatternNF)regexDocumentr      z1.0.0   ) r   )&r   r   vocabaddr   itemsnextziplocTopicr&   r   containslenappendlistsetpipetextr   parsesklearn_versionvectorizer_modelget_feature_names_outget_feature_namesdictrangekeysnpsortget	c_tf_idf_	_outliersargsortarraytodensereshaper   )r   topic_modelr   r    r!   matchercandidate_topicstopicvalueskeywordscandidate_documentskeyword	selectiondocumentdocs_pipelineupdated_keywordsdocmatches_startendwordswords_lookupupdated_topicscandidate_keywordsword_indicesvalsindicesindexvaltopic_wordss                                  r   extract_topicszPartOfSpeech.extract_topics^   s   $ $****+It001#\\^ 	BME6CL)H #%# =%MM)//U*BA*EF	%MM)*<*<*@*@*I*I'Y^*I*_ak*kl	y>A%$-bqM =+228<=	= #'s+>'?"@ !JJOO,?@M!$ A!#,%, AMAuc$++CcN,?,?@AA '+3/?+@&AU#)	B0 ==)W]]7-CC55KKMNE55GGIJE CuSZ'89:17?%)??)9)?)?)A 
	q%E%77:LhwPW[gPg!!'*hL ((L9%+BWBW:WXDjj$,,.*@*@B*G!H!KLdN^N^M^M`abfdfbfgG77288DLLN$:$:1b$AB1EFHXHXGXGZ[\`^`\`aDORSZ\`OabE,u"56<bKb$/N5!>%()D,<,<<u%5AQAQTWXfglXmTnAn;o)pa')pp%
	q  @ i
 c *qs   !
O"	O'
!O'
'O,	O2)en_core_web_sm
   N)__name__
__module____qualname____doc__r
   r   r   intr   r   pd	DataFramer   r   r	   floatre    r   r   r   r      s    0h '7)-	-S(]#- - 3i$&	-8C <<C 	C
 T%U
"3445C 
d5e,--	.Cr   r   )numpyrA   pandasrm   r   spacy.matcherr   spacy.languager   	packagingr   scipy.sparser   typingr   r   r	   r
   sklearnr   r:   bertopic.representation._baser   r   rp   r   r   <module>rz      s5       ! #  # . . 2 <R% Rr   