Question

In: Computer Science

Problem 2(a). Letter Frequencies. ? Write Python code that reads a text file into memory and...

Problem 2(a). Letter Frequencies.

? Write Python code that reads a text file into memory and creates a dict object with a frequency count for each letter. For example, for encryptedA.txt, your output should contain the key:value pairs 'a': 78 and 'b': 31.

Notes

  • Do not distinguish between uppercase and lowercase letters.
  • Ignore punctuation. Punctuation counts must not appear in your dict
  • If a given letter does not appear in the text, there must be a key:value pair with value 0.

? Use Python to determine which letter has the highest frequency in each text file, and print the result.

Problem 2(b). Formatting for R.

? Write your two dictionaries with frequency counts from 2(a) to a pair of suitably named .csv files, with one column for the key and one column for the frequency counted. Include both .csv files with your commit to GitHub.

Solutions

Expert Solution

Solution: Code for each asked problem is as following. Each solution is commented in the code. Go through the code once, and if any query/problem do ask in comment section. In this code I have given the functionality of running code on as many as files user want using menu option. Program as for choice 1 or 2. Enter 1 to run the program, then enter file name on which program will run. Enter 2 to quit the program.


valid = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
while True:
  print('Enter 1.For Starting Program | 2.For quit')
  choice = int(input())
  if(choice==1):

    #solution of 2(a) to find frequency of each letter
    freqs = dict()
    for x in valid:
      freqs[x] = 0
    file = input('Enter File Name with \'.text\' extension:\n' )
    with open(file) as f:
        for line in f:
            for char in line.lower():
                if char in valid:
                      freqs[char] += 1

    print("Dictonary for letters is:\n",freqs)

    #Finds the letter with hisghest frequency and displays it on screen
    max  = 0
    letter = ''
    for x,y in freqs.items():
      if y>max:
        max = y
        letter = x

    print("Letter with maximum frequency is:",letter)


    #solution of 2(b) to save distonary in .csv File
    outFile = file.split('.')[0]
    outFile+='.csv'
    with open(outFile, 'w') as f:
        for key in freqs.keys():
            f.write("%s,%s\n"%(key,freqs[key]))
    print("dictonary saved in ",outFile," successfully")
  elif(choice==2):
    break
  else:
    print("Enter Valid option")

Sample Output is as following:


Related Solutions

Write a code to find the following in a text file (Letter). language: Python (a) Find...
Write a code to find the following in a text file (Letter). language: Python (a) Find the 20 most common words (b) How many unique words are used? (c) How many words are used at least 5 times? (d) Write the 200 most common words, and their counts, to a file. text file: Look in thy glass and tell the face thou viewest, Now is the time that face should form another, Whose fresh repair if now thou not renewest,...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
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...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
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,...
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT