Question

In: Computer Science

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

This is python program which opens mypaper.txt file and find the occurences of words in the file and display it in alphabetical order.

#HW19.py

from collections import *
def frequency_sortedwords(n):
f = open(n, "r")
s=f.read()
l=s.split()

for i in l:

if( ord(i[-1])<65 or ord(i[-1])>90 or ord(i[-1])<97 or ord[-1]>122 or i=='.' or i==','):

i=i[ :-1]

d=Counter(l)
d1=dict(d)
print("\t\t\t\tWord\t\t\t\tNo. of occurence")
for k, v in d1.items():
print ("%40s\t%10d" % ( k, v))

#main

n=input("Enter filename:")
frequency_sortedwords(n)

mypaper.txt

Algorithms are used to manipulate the data contained in data structures
When working with data structures algorithms are used to perform operations on the stored dataA complex algorithm is often divided into smaller units called modules
This process of dividingan algorithm into modules is called modularization
The key advantages of modularization are as follows:
It makes the complex algorithm simpler to design and implement
Each module can be designed independently While designing one module the details of other modules can be ignored thereby enhancing clarity in design which in turn simplifiesimplementation debugging testing documenting and maintenance of the overall algorithm
There are two main approaches to design an algorithm top-down approach and bottom-up approachTop-down approach: A top-down design approach starts by dividing the complex algorithm into one or more modules
These modules can further be decomposed into one or more sub-modules and this process of decomposition is iterated until the desired level of module complexity is achieved
Top-down design method is a form of stepwise refinement where we begin with the
topmost module and incrementally add modules that it calls
Therefore in a top-down approach we start from an abstract design and then at each step this design is refined into more concrete levels until a level is reached that requires no further refinementBottom-up approach
A bottom-up approach is just the reverse of top-down approach In the bottom-up design we start with designing the most basic or concrete modules and then proceed towards designing higher level modules
The higher level modules are implemented by using the operations performed by lower level modules
Thus in this approach sub-modules are grouped together to form a higher level module All the higher level modules are clubbed together to form even higher level modules This process is repeated until the design of the complete algorithm is obtained


Related Solutions

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...
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:...
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...
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.
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,...
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...
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...
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice)....
Download the AddValueNewArray.java file, and open it in jGrasp (or a text editor of your choice). This program behaves similarly to the AddValueToArray program from before. However, instead of modifying the array in-place, it will return a new array holding the modification. The original array does not change. For simplicity, the array used is “hard-coded” in main, though the method you write should work with any array. Example output with the command-line argument 3 is shown below: Original array: 3...
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice)....
Download the SwapMultidimensional.java file, and open it in jGrasp (or a text editor of your choice). This program contains two methods which you will need to write: swapRows: Swaps the contents of two rows, given a two-dimensional array and the indices of the rows to swap. swapCols: Swaps the contents of two columns, given a two-dimensional array and the indices of the columns to swap. main contains some code which will call swapRows and swapCols, for the purpose of informal...
Download the Take.java file, and open it in jGrasp (or a text editor of your choice)....
Download the Take.java file, and open it in jGrasp (or a text editor of your choice). This program will “take” a given number of elements from a given array, using the command-line arguments as an array, and the hard-coded value of 3 elements to take. Example output of this program with the command-line arguments foo bar baz is shown below: foo bar baz In the above example, three elements were taken. However, it's possible that we may request to take...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT