<jupyter_start><jupyter_text>Question:Write code that acquires in input some text, represented as a single string, and prints out the 10 most frequent words in the text. A word is a sequence of characters delimited by whitespace. For simplicity, let’s not consider punctuation and capitalisation.e.g.histogram(“count the words in this sample text and print words count for the 10 most frequent words”)words 3count 2the 2this 1<jupyter_code>sample_text = 'count the words in this sample text and print words count for the 10 most frequent words'
list_words = sample_text.split()
list_words
distinct_words = list(set(list_words))
word_counts = {}

for word in list_words:
    if word in distinct_words:
        # if word_counts:
        if word not in word_counts.keys():
            word_counts[word] = 1
        else:
            word_counts[word] += 1
        # else:
        #     word_counts[word] = 1

print(word_counts)
word_counts
# for key in word_counts.keys():

help(sorted)
sorted(word_counts, reverse=True)
sorted_list = sorted(word_counts.items(), key=lambda x: x[1], reverse=True)
sorted_list
counter = 0

if len(sorted_list) > 10:
    for tup in sorted_list[:10]:
        print(tup[0], tup[1])
else:
    for tup in sorted_list:
        print(tup[0], tup[1])

# for tup in sorted_list:
    
#         while counter < 10:
#             print(tup[0], tup[1])
#             counter += 1
#     else:
#         print(tup[0], tup[1])
list(word_counts.items())
if not word_counts:
    print('True')
word_counts.keys()
list_words
list_words.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09644670050761421, 'fraction_numerical': 0.01903553299492386, 'mean_word_length': 3.4674220963172804, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '482824', 'license': 'MIT', 'n_tokens_mistral': 542, 'n_tokens_neox': 520, 'n_words': 164, 'path': 'interviewee/00_screenshots/interview_questions.ipynb', 'repo_name': 'Anshul-GH/interview_prep', 'size': '1576'}