Question

In: Computer Science

Python: Word Frequencies (Concordance) 1. Use a text editor to create a text file (ex: myPaper.txt)...

Python:

Word Frequencies (Concordance)

1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words.

2. Write a Python program (HW19.py) that

  • asks the user to provide the name of the text file. Be SURE to check that it exists!
    Do NOT hard-code the name of the file! Use the entry provided by the user!
  • read from the text file
  • NOTE: (write your program so that it ONLY works if it reads from the file specified by the user).

3. Your program will produce a dictionary of words with the number of times each occurs. It maps a word to the number of times the word occurs

  • Key: a word
  • Value: the number of times the word occurs in the text. The report should be an ALPHABETIZED output table, listing the word and the number of occurrences.

NOTES:

  • The input sequence can be words, numbers or any other immutable Python object, suitable for a dict key.
  • Ignore case – apple is the same as Apple is the same as APPLE, etc.
  • Discard all punctuation and extra white space (newlines, tabs, blanks, etc.).
  • Put the words into a dictionary. The first time a word is seen, the frequency is 1. Each time the word is seen again, increment the frequency. NOTE: you are REQUIRED to use the dictionary class for this assignment.
  • Produce a frequency table. To alphabetize the frequency table, extract just the keys and sort them. This sorted sequence of keys can be used to extract the counts from the dict.
  • Make sure that your output (the table) is formatted so that the columns are aligned, and so that there are titles for each column.
  • Be sure to modularize your code, using functions appropriately.
  • The main function should be clean and easy to read, calling the functions with meaningful names. Pass parameters – do not use global variables.

BONUS +5 pts:

  • Create a menu-driven front-end to this application.
    • First, populate the dictionary from the file
    • Next, provide a menu that allows the user to
      • Add words to the dictionary
                after asking the user for the word to add
                Note: the count should increase if the word is already there)
      • Check if a word is already in the dictionary
      • Delete a word from the dictionary after asking the user for the word to delete
      • Print the dictionary entries in table form as described above.
  • Allow the user to continue using the menu until they choose the option to quit.

Solutions

Expert Solution

# -*- coding: utf-8 -*-
"""
Created on Thu Oct  8 11:05:06 2020

@author: SYED ZUBAIR
"""

import os
import string
filename = str(input("enter the name of the file : "))
if  os.path.isfile("C:/Users/Syed Zubair/Documents/{0}.txt".format(filename)):
    print("FILE EXISTS ")
    f= open(filename, 'r') 
    d = dict()
    for line in f: 
    
        line = line.strip() 
  
    # Convert the characters in line to  
    # lowercase to avoid case mismatch 
        line = line.lower() 
  
    # Remove the punctuation marks from the line 
        line = line.translate(line.maketrans("", "", string.punctuation)) 
  
    # Split the line into words 
        words = line.split(" ") 
  
     
        for word in words: 
     
            if word in d: 
            
               d[word] = d[word] + 1
            else: 
            
               d[word] = 1
  

     for key in list(d.keys()): 
         print(key, ":", d[key]) 
else:
    print("FILE DOES NOT EXIST")
    

  
 
  



Related Solutions

Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
Python File program 5: Word Frequencies (Concordance)    20 pts 1. Use a text editor to create...
Python File program 5: Word Frequencies (Concordance)    20 pts 1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE:...
In Python. A file concordance tracks the unique words in a file and their frequencies. Write...
In Python. A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies. Below is an example file along with the program input and output: Input : test.txt output : 3/4 1, 98 1, AND 2, GUARANTEED 1,...
Use Vi text editor or ATOM to create a bash script file. Use the file name...
Use Vi text editor or ATOM to create a bash script file. Use the file name ayaan.sh. The script when ran it will execute the following commands all at once as script. Test your script file make sure it works. Here is the list of actions the script will do: It will create the following directory structure data2/new2/mondaynews off your home directory. inside the mondaynews, create 4 files sports.txt, baseball.txt, file1.txt and file2.txt. Make sure file1.txt and file2.txt are hidden...
In linux , Using a simple text editor, create a text file with the following name...
In linux , Using a simple text editor, create a text file with the following name "Test" and content: GNU GENERAL PUBLIC LICENSE The GNU General Public License is a free, copy left license for the GNU General Public License is intended to guarantee your freedom to GNU General Public License for most of our software; it applies … 2-Write the command to create the text file. 3-Print the file permissions. 4-Create a directory named "361" 5-Copy file "Test" to...
A file concordance tracks the unique words in a file and their frequencies. Write a program...
A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies.
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
A concordance is a table that tells how often a word appears in a text (or...
A concordance is a table that tells how often a word appears in a text (or where in a text it appears; but we are not using this definition). You are going to write a program for a concordance that: reads a text separates it into words (any collection of letters appearing between non-alphabetic characters) places them into a table (if they are not already there with an initial count of 1) or adds one to its count in the...
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to an output file.
Concepts tested by this program            Hash Table,            Link List,hash code, buckets/chaining,exception handling, read/write files (FileChooser)A concordance lists every word that occurs in a document in alphabetical order, and for each word it gives the line number of every line in the document where the word occurs.Write a program that creates a concordance. There will be two ways to create a concordance. The first requires a document to be read from an input file, and the concordance data is written to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT