def count_words(text):
= text.split()
words return len(words)
= "Natural language processing is fun!"
sentence print(count_words(sentence)) # Output: 5
5
Task: Write a function called count_words(text)
that takes a string of text as input and returns the number of words in it.
Instructions:
count_words(text)
.text
into words using split()
.Task: Write a function called convert_to_lowercase(text)
that takes a string of text and returns it with all characters in lowercase.
Instructions:
convert_to_lowercase(text)
..lower()
method to convert the text to lowercase.Task: Create a function called replace_word(text, old_word, new_word)
that takes a string of text, a word to be replaced, and the new word, then returns the modified text.
Instructions:
replace_word(text, old_word, new_word)
..replace()
method to replace old_word
with new_word
.Task: Create a function to count the frequency of a specific word in a given sentence.
Instructions:
word_frequency(sentence, word)
that takes a string parameter sentence
and a string parameter word
.split()
method to break the sentence into words, then count how many times word
appears in the list. Hint: Use the list method .count()
.def word_frequency(sentence, word):
words = sentence.split()
return words.count(word)
# Sample usage
sentence = "NLP is great and NLP is fun"
word = "NLP"
result = word_frequency(sentence, word)
print(f"The word '{word}' appears {result} times.") # Output: The word 'NLP' appears 2 times.
The word 'NLP' appears 2 times.
Task: Write a function that reverses the words in a sentence.
Instructions:
reverse_sentence(sentence)
that takes a string parameter sentence
.def reverse_sentence(sentence):
words = sentence.split()
reversed_words = words[::-1]
return ' '.join(reversed_words)
# Sample usage
sentence = "Natural Language Processing is fun"
result = reverse_sentence(sentence)
print("Reversed sentence:", result) # Output: Reversed sentence: fun is Processing Language Natural
Reversed sentence: fun is Processing Language Natural
Task: Create a function to remove punctuation from a given string.
Instructions:
remove_punctuation(text)
that takes a string parameter text
..isalnum()
and .ischar()
.def remove_punctuation(text: str):
# Use list comprehension to filter out punctuation
cleaned_text = ''.join(char for char in text if char.isalnum() or char.isspace())
return cleaned_text
# Sample usage
text = "NLP is great! Isn't it?"
result = remove_punctuation(text)
print("Cleaned text:", result) # Output: Cleaned text: NLP is great Isnt it
Cleaned text: NLP is great Isnt it
Task: Write a function to calculate the average length of words in a sentence.
Instructions:
average_word_length(sentence)
that takes a string parameter sentence
.def average_word_length(sentence):
words = sentence.split()
if len(words) == 0:
return 0
total_length = sum(len(word) for word in words)
return total_length / len(words)
# Sample usage
sentence = "Natural Language Processing is powerful"
result = average_word_length(sentence)
print("Average word length:", result)
Average word length: 7.0