<jupyter_start><jupyter_text>Ne vous endormez pas !Concevez une fonction nommée `nb_words()` qui comptabilise le nombre de mots d’un texte français en vous appuyant sur *Le dormeur du val* de Rimbaud.<jupyter_code>path_to_text = '../data/dormeur-du-val.txt'
# Modules to import
import nltk
from nltk.tokenize import RegexpTokenizer
# Design your function
def nb_words(path):
    """Count the number of words in a text,
    regardless of punctuation.
    
    path -- path to the text
    """
    # Matches only the words
    tokenizer = RegexpTokenizer('\w+')
    
    # Read the file as raw
    with open(path_to_text) as file:
        text = file.read()

    return len(tokenizer.tokenize(text))
# Check the result
nb = nb_words(path_to_text)
if nb != 121:
    print(f"Vous avez trouvé {nb} mots. La bonne réponse était : 121 !")
else:
    print("Résultat correct !")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09788654060066741, 'fraction_numerical': 0.006674082313681869, 'mean_word_length': 3.6632124352331608, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '544257', 'license': 'MIT', 'n_tokens_mistral': 322, 'n_tokens_neox': 316, 'n_words': 100, 'path': '6.text-processing/answers/1.a-sleeper-in-a-valley.ipynb', 'repo_name': 'mdjamina/python-M1TAL', 'size': '899'}