
     h;%                     \    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
  G d d          ZdS )	    )Iterable   )ALLOWED_CHARACTERS)any_next_words_form_swear_wordget_complete_path_of_fileget_replacement_for_swear_wordread_wordlist)VaryingStringc                   j    e Zd ZddZddZd ZddZd Zd Zdd	d
Z	d Z
d Zd Zd Zd ZddZdS )	ProfanityNc           
         |9t          |t                    s$t          |t                    st          d          g | _        ddddddd	d
dd	| _        d| _        t          | _        t          d          | _	        t          |          t          k    r|                     |           dS |                     |           dS )aI  
        Args:
            words (Iterable/str): Collection of words or file path for a list of
                words to censor. `None` to use the default word list.

        Raises:
            TypeError: If `words` is not a valid type.
            FileNotFoundError: If `words` is a `str` and is not a valid file path.
        Nz(words must be of type str, list, or None)a@*4)ir   l1)or   0r   )ur   v)r   r   r   )r   r   )er   3)s$5)t7)	r   r   r   r   r   r   r   r   r   r   zprofanity_wordlist.txt)custom_words)
isinstancestrr   	TypeErrorCENSOR_WORDSETCHARS_MAPPINGMAX_NUMBER_COMBINATIONSr   r   _default_wordlist_filenametypeload_censor_words_from_fileload_censor_words)selfwordss     ]/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/better_profanity/better_profanity.py__init__zProfanity.__init__   s     uc** uh//  FGGG %%%    

 

 ()$"4*C$+
 +
' ;;#,,U33333"""66666    r   c                     t          |t                    st          |          }t          |t                    st          |          }| j        s|                                  |                     ||          S )z7Replace the swear words in the text with `censor_char`.)r!   r"   r$   r*   _hide_swear_words)r+   textcensor_chars      r-   censorzProfanity.censor8   ss     $$$ 	t99D+s++ 	+k**K" 	%""$$$%%dK888r/   c                 @    t          |          } | j        |fi | d S N)r	   _populate_words_to_wordset)r+   filenamekwargsr,   s       r-   r)   z%Profanity.load_censor_words_from_fileD   s0    h''''8888888r/   c                 N    |pt          | j                  } | j        |fi | dS )z1Generate a set of words that need to be censored.N)r	   r'   r7   )r+   r    r9   s      r-   r*   zProfanity.load_censor_wordsH   s:     $U}T5T'U'U''???????r/   c                     t          |t          t          t          f          st	          d          |D ]0}| j                            t          || j                             1d S )Nz<Function 'add_censor_words' only accepts list, tuple or set.char_map)	r!   listtuplesetr#   r$   appendr
   r%   )r+   r    ws      r-   add_censor_wordszProfanity.add_censor_wordsN   sy    ,uc(:;; 	N    	V 	VA&&}QAS'T'T'TUUUU	V 	Vr/   c                 4    ||                      |          k    S )z3Return True if  the input text has any swear words.)r4   )r+   r2   s     r-   contains_profanityzProfanity.contains_profanityV   s    t{{4((((r/   )whitelist_wordsc                |   |1t          |t          t          t          f          st	          d          |pg }t          |          D ]a\  }}t          |t                    s0t          d                    t          |                              |
                                ||<   bt          |          }g }t          |          D ]k}|
                                }||v r|                     |          }|| j        k    r|| _        |                    t          || j                             l|| _        d S )NzGThe 'whitelist_words' keyword argument only accepts list, tuple or set.zFEach word in 'whitelist_words' must be 'str' type, but '{word}' found.)wordr<   )r!   r>   r@   r?   r#   	enumerater"   
ValueErrorformatr(   lower_count_non_allowed_charactersr&   rA   r
   r%   r$   )r+   r,   rF   indexrH   all_censor_wordsnum_of_non_allowed_charss          r-   r7   z$Profanity._populate_words_to_wordset\   s^   &zdC/0
 0
& Y  
 */R$_55 	2 	2KE4dC((  **0&d4jj&*A*A   &*ZZ\\OE"" o..JJ 	V 	VD::<<D&&'+'I'I$'O'O$'$*FFF/G,##M$AS$T$T$TUUUU /r/   c                 J    d}t          |          D ]}|| j        vr|dz  }|S )Nr   r   )iterr   )r+   rH   countchars       r-   rM   z'Profanity._count_non_allowed_characters   s9    JJ 	 	D4222
r/   c                     |s|                      ||| j                  }n?|dd= |r8|d         d         dk    r&||                      ||d         d         d          z  }|S )z:Return a list of next words_indices after the input index.N   r    r   )_get_next_wordsr&   )r+   r2   words_indices	start_idxs       r-   _update_next_words_indicesz$Profanity._update_next_words_indices   s     	U 00i!= MM bqb! Ur!21!5!;!;!5!5dM"<Ma<PRS!T!TTr/   c                    d}d}d}g }|                      |d          }|t          |          dz
  k    r|S |dk    r|d|         }||d         }t          t          |                    D ]\  }}	||k     r|	t          v r||	z  }|                                dk    r||	z  }d};|                     |||          }t          ||| j                  \  }
}|
rt          |          }|}d}	g }|
                                | j        v rt          |          }|||	z   z  }d}|dk    rE|t          |          dz
  k     r/|
                                | j        v rt          |          }||z  }|S )z/Replace the swear words with censor characters.rX   rW   r   r   N)_get_start_index_of_next_wordlenrR   rI   r   stripr\   r   r$   r   rL   )r+   r2   r3   censored_textcur_word
skip_indexnext_words_indicesstart_idx_of_next_wordrN   rT   contains_swear_word	end_indexs               r-   r1   zProfanity._hide_swear_words   s   
!%!C!CD!!L!L "SYY]22K "A%% !8"8!89M.//0D  	$00  	  	KE4z!!)))D  ~~2%%% "&!@!@(%" " .L,d.A. .* # (9+FF&
%'" ~~4#6669+FFX_,MHH r>>j3t99q=88~~4#6669+FFX%Mr/   c                     t          |          }t          t          |t          |                              D ]}||         | j        vr|} |S )zKReturn the index of the first character of the next word in the given text.)r_   rR   ranger   )r+   r2   r[   re   rN   s        r-   r^   z'Profanity._get_start_index_of_next_word   sY    !$T%	3t995566 	 	EE{$"999%*"%%r/   c                     d}|}t          t          |t          |                              D ]}||         }|| j        v r||z  } ||fS )zLReturn the next word in the given text, and the index of its last character.rX   )rR   ri   r_   r   )r+   r2   r[   	next_wordrN   rT   s         r-   _get_next_word_and_end_indexz&Profanity._get_next_word_and_end_index   se    	%	3t995566 	 	E;Dt...T!	%r/   r   c                 .   |                      ||          }|t          |          dz
  k    rd|fd|fgS |                     ||          \  }}||f|||         ||fg}|dk    r-|                    |                     |||dz
                       |S )z
        Return a list of pairs of next words and next words included with separators,
        combined with their end indices.
        For example: Word `hand_job` has next words pairs: `job`, `_job`.
        r   rX   )r^   r_   rl   extendrY   )r+   r2   r[   num_of_next_wordsre   rk   rg   r,   s           r-   rY   zProfanity._get_next_words   s     "&!C!CD)!T!T "SYY]22/027M2NOO  $@@( 
  
	9
 	"I&<<==yyI9U
 q  LL--dI?PST?TUUVVVr/   r6   )r   )r   )__name__
__module____qualname__r.   r4   r)   r*   rC   rE   r7   rM   r\   r1   r^   rl   rY    r/   r-   r   r      s       $7 $7 $7 $7P
9 
9 
9 
99 9 9@ @ @ @V V V) ) ) DH #/ #/ #/ #/ #/J  
 
 
9 9 9v	& 	& 	&
  
  
      r/   r   N)collections.abcr   	constantsr   utilsr   r   r   r	   varying_stringr
   r   rs   r/   r-   <module>rx      s    % $ $ $ $ $ ) ) ) ) ) )            * ) ) ) ) )q q q q q q q q q qr/   