Question

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 fh.readlines():
linecount = linecount+1
word = re.findall('[a-zA-Z0-9\-\']+', line)
wordcount = len(word) + wordcount
unique_words_line = len(set(word))
#print(length)
length = len(word)

print("Line",linecount, ":", length, "words", unique_words_line, "unique words")

# if line in count:
# count[line] += 1
# else:
# count[line] = 1

print("Total number of world found in the file is:", wordcount)
print("Total number of unique words found in the file are:", )
# for word, times in count.items():
# print ("%s was found %d times" %(word, times))

if __name__ == "__main__": main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please Refer to the following code below:

import re


# 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.name)
    # set to store unique words of file
    unique_words = set()
    for line in fh.readlines():
        linecount = linecount + 1
        word = re.findall('[a-zA-Z0-9\-\']+', line)
        # loop for iteration in unique words in line
        for i in set(word):
            # adding unique words of file to file unique words
            unique_words.add(i)
        wordcount = len(word) + wordcount
    unique_words_line = len(set(unique_words))

    print("Line", linecount, ":", wordcount, " words:", unique_words_line, "unique words")

    print("Total number of word found in the file is:", wordcount)
    print("Total number of unique words found in the file are:", unique_words_line)


if __name__ == "__main__":
    main()

Output:-

Enter the file to search: textname.txt
Sumary of the textname.txt
Line 3 : 8 words: 4 unique words
Total number of word found in the file is: 8
Total number of unique words found in the file are: 4

Input file textname.txt:-

print print print anoop
sharma print
print abc

============================================================================

Please refer to the images below:-

Code:-

OUTPUT:-

INput file:-

=============================================================================

Please upvote

=============================================================================

Add a comment
Know the answer?
Add Answer to:
I cant get this python program to read all the unique words in a file. It...
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
  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • Counting characters, words, and lines based on a text file

    I did the assigment but inside mainWrite a C program (called counting.c) that counts the number of characters, words and lines readfrom standard input (stdin) until EOF is reached. This means counting.c must contain a mainfunction along with other function.- Assume the input is ASCII text of any length.-Every byte read from stdin counts as a character except EOF.- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has...

  • I need python help .. I need to know if a user were to input 3...

    I need python help .. I need to know if a user were to input 3 commands to a program and run it such as specify: Usage: ./filemaker INPUTCOMMANDFILE OUTPUTFILE RECORDCOUNT (./filemaker is the program)( (INPUTCOOMANDFUILE= a text file that contains key words that need to be searched through then decided what to do ) (RECORDCOUNT= number) Given an input file that contains the following: That specifies the text, then the output, and the number of times to be repeated...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

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

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • My Python file will not work below and I am not sure why, please help me...

    My Python file will not work below and I am not sure why, please help me debug! ********************************* Instructions for program: You’ll use these functions to put together a program that does the following: Gives the user sentences to type, until they type DONE and then the test is over. Counts the number of seconds from when the user begins to when the test is over. Counts and reports: The total number of words the user typed, and how many...

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