o
    q¨Êh~  ã                	   @   sb  d dl mZ d dlmZmZ d dlmZmZ d dlZ	d dl
Z
eƒ addddd	d
dddœZeeƒZe eeƒ¡ e eg d¢ƒ¡ dadedefdd„Zg fdefdd„Zdedee fdd„Zdee fdd„Zdedefdd„Zdedefd d!„Zd-ded"ed#edefd$d%„Zdededefd&d'„Zd.dededefd)d*„Zed+kr¯d,ZeeƒZe eƒ dS dS )/é    )Úproduct)ÚSetÚList)Úascii_lowercaseÚdigitsN)Úaú@Ú*Ú4ú&)Úir	   Úlú!Ú1)Úor	   Ú0r   )Úur	   Úv)r   r	   r   )r   r   )Úer	   Ú3)Úsú$)r   r   r   r   r   r   r   r   )r   r   r   ú^r	   r   ú"ú'é   ÚfilenameÚreturnc                 C   s"   t j t j t¡¡}t j || ¡S )z?Join the path of the current directory with the input filename.)ÚosÚpathÚabspathÚdirnameÚ__file__Újoin)r   Úroot© r%   úL/var/www/html/env/lib/python3.10/site-packages/better_profanity/profanity.pyÚget_complete_path_of_file    s   r'   Úcustom_wordsc                 C   sP   | r| }nt ƒ }tƒ }|D ]}| d¡}|tkr|a| tt|ƒƒ¡ q|adS )z1Generate a set of words that need to be censored.ú N)Úread_wordlistÚsetÚcountÚMAX_NUMBER_COMBINATIONSÚupdateÚgenerate_patterns_from_wordÚCENSOR_WORDSET)r(   Ú
temp_wordsÚall_censor_wordsÚwordÚnum_of_spacesr%   r%   r&   Úload_censor_words%   s   

ÿr5   r3   c                 C   s$   dd„ t | ƒD ƒ}dd„ t|Ž D ƒS )z3Return all patterns can be generated from the word.c                 S   s"   g | ]}|t vr|fnt | ‘qS r%   )ÚCHARS_MAPPING)Ú.0Úcharr%   r%   r&   Ú
<listcomp>?   s
    þþz/generate_patterns_from_word.<locals>.<listcomp>c                 s   s   � | ]}d   |¡V  qdS )Ú N)r#   )r7   Úpatternr%   r%   r&   Ú	<genexpr>D   s   € z.generate_patterns_from_word.<locals>.<genexpr>)Úiterr   )r3   Úcombosr%   r%   r&   r/   =   s   ýr/   c                  c   s†   � d} t | ƒ}z-t|dd��}t|ƒD ]}| ¡ }|dkr |V  qW d  ƒ W dS 1 s-w   Y  W dS  tyB   tdƒ Y dS w )z0Return words from file `profanity_wordlist.txt`.zprofanity_wordlist.txtzutf-8)Úencodingr:   Nz%Unable to find profanity_wordlist.txt)r'   Úopenr=   ÚstripÚFileNotFoundErrorÚprint)Úwordlist_filenameÚwordlist_pathÚwordlist_fileÚrowr%   r%   r&   r*   F   s    €€ý&þþr*   Úcensor_charc                 C   s   | d S )Né   r%   )rH   r%   r%   r&   Úget_replacement_for_swear_wordV   s   rJ   Útextc                 C   s   | t | ƒkS )z3Return True if  the input text has any swear words.)Úcensor)rK   r%   r%   r&   Úcontains_profanityY   s   rM   Ú	start_idxÚnum_of_next_wordsc                 C   s¶   t | ƒ}tt|t | ƒƒƒD ]}| | tvrq|} |t | ƒd kr&d|fgS d}|}tt|t | ƒƒƒD ]}| |  ¡ }|tv rD||7 }q3 ||fg}|dkrY| t| ||d ƒ¡ |S )Nr   r:   )Úlenr=   ÚrangeÚALLOWED_CHARACTERSÚlowerÚextendÚget_next_words)rK   rN   rO   Ústart_idx_of_next_wordÚindexÚcur_wordr8   Úwordsr%   r%   r&   rU   ]   s,   

ÿrU   c                 C   sø   d}d}d}d}t t| ƒƒD ]S\}}||krq| ¡ tv r"||7 }qt| |d tƒ}| ¡ }	t |ƒD ]\}
}d|	|
 ¡ f }	|	tv rLt|ƒ}|}d} nq2| ¡ tv rWt|ƒ}||7 }||7 }d}q|dkrz|t| ƒk rz| ¡ tv rvt|ƒ}||7 }|S )z/Replace the swear words with censor characters.r:   éÿÿÿÿFr   z%s %s)	r=   Ú	enumeraterS   rR   rU   r-   r0   rJ   rP   )rK   rH   Úcensored_textrX   Ú
skip_indexÚskip_cur_charrW   r8   Únext_words_indicesÚfull_next_wordÚ	next_wordÚ	end_indexr%   r%   r&   Úhide_swear_words|   s<   ürc   r	   c                 C   s8   t | tƒs	t| ƒ} t |tƒst|ƒ}tstƒ  t| |ƒS )z7Replace the swear words in the text with `censor_char`.)Ú
isinstanceÚstrr0   r5   rc   )rK   rH   r%   r%   r&   rL   ¦   s   


rL   Ú__main__ÚCorki)r   )r	   )!Ú	itertoolsr   Útypingr   r   Ústringr   r   Úos.pathr   Úsysr+   r0   r6   rR   r.   r-   re   r'   r5   r/   r*   rJ   ÚboolrM   ÚintrU   rc   rL   Ú__name__Úbad_textr\   rC   r%   r%   r%   r&   Ú<module>   sF    ø
ÿ	*ü