In: Computer Science
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
Solution:
I think the problem is with the creation of large_words. In the description, it says filter the words with length > 15 from the COMPLETE set of words, not UNIQUE. So instead of using set(text), you have to use list(text) so that duplicates are not removed.
Here is the updated code for this problem. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
import math
import os
import random
import re
import sys
import zipfile
from nltk.corpus import gutenberg
from nltk.text import Text
def filterWords(text):
ing_words = [word for word in set(text) if
word.endswith('ing')]
# we have to add ALL words that have length>15 from the text,
not ALL UNIQUE words.
# so instead of set(text), we have to use list(text)
large_words = [word for word in list(text) if len(word) >
15]
upper_words = [word for word in set(text) if word.isupper()]
return ing_words, large_words, upper_words
if __name__ == '__main__':
text = input()
if not os.path.exists(os.getcwd() + "/nltk_data"):
with zipfile.ZipFile("nltk_data.zip", 'r') as zip_ref:
zip_ref.extractall(os.getcwd())
os.environ['NLTK_DATA'] = os.getcwd() + "/nltk_data"
text = Text(gutenberg.words(text))
ing_words, big_words, upper_words = filterWords(text)
print(sorted(ing_words))
print(sorted(big_words))
print(sorted(upper_words))
#please consider my effort and give me a like...thank u.....