** 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.
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.
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.
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)
OUTPUT IMAGE
PART 2
PART 4
.
.
.
** Language Used : Python ** PART 2 : Create a list of unique words This...
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 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 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 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 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, 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 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 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 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 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...