Using python, write code for program. There is a text file Gettysburg.txt used in this program and i have put a screenshot of the contents of the text file. Please also upload screenshot of your code.
29. Gettysburg Address The file Gettysburg. txt contains the entire Gettysburg Address as a single line. 1 Write lines of code that display the first 89 characters of the Gettysburg Address, the number of words in the Gettysburg Address, and the number of different words. See Fig. 5.3 Four score and seven years ago our fathers brought forth on this continent a new nation The Gettysburg Address contains 268 words The Gettysburg Address contains 139 different words. FIGURE 5.3 Outcome of Exercise 29
Expert Answer
import collections
num_words = 0
cnt=0
fp = open(‘address.txt’)
with open(‘address.txt’) as fp:
lines = fp.read()
#string = lines
#print(lines)
#print(“**Printing first 89 characters**”)
char89 = lines[:89]
print(char89)
#print(“n**Printing the total number of words**”)
with open(‘address.txt’, ‘r’) as fp:
for line in fp:
words = line.split()
num_words += len(words)
print(“The Gettysburg Address contains “,num_words, ” words”)
with open(‘address.txt’, ‘r’) as fp:
for string in fp:
words = string.split()
word_counts = collections.Counter(words)
for word, count in sorted(word_counts.items()):
if count == 1:
cnt=cnt+1;
print(“The Gettysburg Address contains “,cnt, ” different words”)