Question

** Language Used : Python ** PART 2 : Create a list of unique words This...

** Language Used : Python **

PART 2 : Create a list of unique words

This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back.

  1. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing.

  2. Create a test program that takes a string as input and then calls the function over and over until the user enters "Done." If the string "Done" is entered, the program sorts the list and prints it out line by line.

PART 4: Parse a file into a list of unique words

The goal of this part is to create a unique list of only the words in the document and not the other characters.

  1. In this part of the project, each team should read their file into a list and then iterate through the list, line by line, spliting the lines into words in a list. Before splitting the line into parts, the data cleaning function created in Part 1 should be called to remove all unwanted characters from the line.
  2. Then use the function created in Part 2 to to add those words to a list of unique words.
  3. Words should all be converted to uppercase before being addded into the List so that "Green" and "green" are considered the same. Also, any words that begin as all caps or as all numeric should not be added to the list.
  4. Finally, once the entire text is processed, print out the contents of the list line by line. If you notice any characters that should have been removed, go back and add them to your file in Part 3 using the keyboard.
  5. The successful projects will only have words in their output. There will be no special characters or numbers.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:


I have provided the properly commented  and indented code so you can easily copy the code as well as check for correct indentation.
I have provided the output image of the code so you can easily cross-check for the correct output of the code.
Have a nice and healthy day!!

CODE

# Part 2

# function unique_words
def unique_words(word:str,words_list:list):
    # converting word to upper case
    upper = word.upper()
    # checking if word already in list
    if upper not in words_list:
        # adding upper to words_list
        words_list.append(upper)
        
    # returning list
    return words_list

# test program
# defining empty list
words_list = []

# while loop till word not equals Done
while True:
    # prompting user
    word = input("Please enter a word (To stop enter Done): ")
    # if entered Done, break
    if word == "Done":
        break
    # otherwise call function unique_words
    words_list = unique_words(word,words_list)
    
# displaying sorted list
# sorting list using sorted function of python
words_list = sorted(words_list)
print("\nWords List is:")
for word in words_list:
    print(word)
    
    
# PART 4
# reading file "file.txt"
file = open("file.txt")

# defining empty list
words_list =[]

# defining specialCharsDict, maping special char with empty char
specialCharsDict = {ord(c): "" for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"}

# looping line by line the file
for line in file:
    # removing special characters from list using specialCharsDict
    # and translate function
    line = line.translate(specialCharsDict)
    # spliting lines into words
    words = line.split()
    
    # looping to all words and calling function unique_words
    for word in words:
        words_list = unique_words(word,words_list)
        
# closing file
file.close()
        
# displaying sorted list
# sorting list using sorted function of python
words_list = sorted(words_list)
print("Words List is:")
for word in words_list:
    print(word)

INPUT IMAGE (file.txt)

1 Parse a file into a list of unique words 2 3 4 5 6 The goal of this part is to create a unique list of only the words in th

OUTPUT IMAGE

PART 2

Please enter a word (To stop enter Done): ywlo Please enter a word (To stop enter Done): yellow Please enter a word (To stop

PART 4

Words List is: GREEN 1 2 3 A ADD ADDDED ADDED ALL ALSO AND ANY ARE AS BACK BE BEEN BEFORE BEGIN BEING BY CALLED CAPS CHARAC

.

.

.

THIS THOSE THROUGH TO UNIQUE UNWANTED UPPERCASE USE USING WILL WORDS YOU YOUR

Add a comment
Know the answer?
Add Answer to:
** Language Used : Python ** PART 2 : Create a list of unique words This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need some help on this Python Problem called "unique words" the text.txt file can be any...

    Need some help on this Python Problem called "unique words" the text.txt file can be any .txt file with words in it. Write a program that opens a specified text file and then displays the number of unique words in the text file after stripping all the white spaces, comma, and the punctuation marks and list of all the unique words found in the file. The list should be sorted in alphabetical order. The program should handle the ‘file not...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • Write a program IN PYTHON that checks the spelling of all words in a file. It...

    Write a program IN PYTHON that checks the spelling of all words in a file. It should read each word of a file and check whether it is contained in a word list. A word list available below, called words.txt. The program should print out all words that it cannot find in the word list. Requirements Your program should implement the follow functions: main() The main function should prompt the user for a path to the dictionary file and a...

  • Write a spell checker that stores a set of words, W, in a hash table and...

    Write a spell checker that stores a set of words, W, in a hash table and implements a function, spellCheck(s), which performs a spell check on the string s with respect to the set of words, W. If s is in W, then the call to spellCheck(s) returns an iterable collection that contains only s, because it is assumed to be spelled correctly in this case. Otherwise, if s is not in W, then the call to spellCheck(s) returns a...

  • in python: Write a function, write_file, that expects as arguments, in order: A list of words,...

    in python: Write a function, write_file, that expects as arguments, in order: A list of words, like the one created by the previous function A path to an output file The number of words to write to the output file The seed for the random number generator and writes a randomly-generated file of that many words by randomly selecting words from the list. If you use the random module function random.choice() to select each word from the list, calling the...

  • python program- ---create a function that returns the longest word inside a list of words ---create...

    python program- ---create a function that returns the longest word inside a list of words ---create a function with two parameters (phrase,wordList) that returns a count of how many words are spelled incorrectly that cannot be found in wordList (a list of words) for both you can only use these functions --- append, len, str, float, range, strip, split, int.

  • A)) (Ransom Note Problem) in python A kidnapper kidnaps you and writes a ransom note. He...

    A)) (Ransom Note Problem) in python A kidnapper kidnaps you and writes a ransom note. He does not write it by hand to avoid having his hand writing being recognized, so he uses a magazine to create a ransom note. We need to find out, given the ransom string and magazine string, is it possible to create a given ransom note. The kidnapper can use individual characters of words. Here is how your program should work to simulate the ransom...

  • PYTHON --create a program that accepts a list of words and returns the longest word inside...

    PYTHON --create a program that accepts a list of words and returns the longest word inside that list can only use --- append, len, str, float, range, strip, split, int --create a program that accepts a list of words and a specific length. function will returns a new list that contains the words found with the specific length, and return an empty list if none are found can only use --- append, len, str, float, range, strip, split, int

  • In Python 4. outputWordPointPairs(pointWordList, filename, toFile) NO return (just prints a formatted list or writes it...

    In Python 4. outputWordPointPairs(pointWordList, filename, toFile) NO return (just prints a formatted list or writes it to file). NOTE: Your function should add the .txt extension to the filename before opening a file with that name. Write a function which will output the (pointValue, word) pairs in pointWordList to the shell or to a file depending on the bool value toFile. Note the order of elements of the tuple is (pointValue, word) not (word, pointValue). Find out why this specific...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT