Question

In: Computer Science

Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...

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

Solutions

Expert Solution

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.....


Related Solutions

C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
array • First, create a function called addNumber, which has a formal parameter for an array...
array • First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from...
In python Define a function called cfb_graph which takes no arguments. Form a directed graph from the file cfb2010.csv by considering the teams as vertices and creating an edge between team1 and team2 only if team1 defeated team2. You should familiarize yourself with this file before attempting this part. cfb_graph will return a dictionary giving this representation.
- Write a function with a parameter called A (which is between 1 and 10) and...
- Write a function with a parameter called A (which is between 1 and 10) and prints the multiplication table for 1 to A. • Note: Do not need to draw the horizontal and vertical lines. • Example for A = 10.
\ Implement function find_key that takes a dictionary with int:int pairs and a second parameter which...
\ Implement function find_key that takes a dictionary with int:int pairs and a second parameter which is an int that corresponds to one of the dictionary values, and returns the key that corresponds to this value. If there are more than one keys, it returns the smallest key. If there is no key that maps to this value, the functions returns False. You may not use the .values and .items methods. You may not create lists, tuples, strings, sets, dictionaries....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT