String manipulation enables interesting approaches to analyzing the writing of g
ID: 3916208 • Letter: S
Question
String manipulation enables interesting approaches to analyzing the writing of great authors. Much attention has been focused on whether William Shakespeare ever lived. Some scholars believe there’s substantial evidence indicating that Christopher Marlowe actually penned the masterpieces attributed to Shakespeare. Researchers have used computers to find similarities in the writing of these two authors. This exercise examines three methods for analyzing texts with a computer. Read a line of text from the keyboard and then perform the following tasks: a) Calculate and print a table indicating the number of occurrences of each letter of the alphabet in the text. b) Calculate and print the number of one-letter words, two-letter words, three-letter words, and so on, appearing in the text. c) Calculate and print the number of occurrences of each different word in the text. The program should print the words in the table in the same order in which they appear in the text. Sample Run
Explanation / Answer
# encoding=utf8
from collections import OrderedDict
text=raw_input("What is the text : ")
print text
letters=('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')
punctuations='~`!@#$^&*()+={[}]|:;"<,>.??‘’?“„’”\'
list_of_words=OrderedDict()
def clean_words(words):
new_words=[]
for i in words:
if i not in punctuations:
new_words.append(i.lower().rstrip(punctuations))
return new_words
print "-------------------Character Count--------------------"
for i in letters:
count=0
for j in text:
if i==j.lower():
count+=1
print i,count
count_1=0
count_2=0
count_3=0
count_4=0
count_5=0
count_6=0
count_7=0
count_8=0
count_9=0
count_10=0
words=[]
for i in text.split():
if len(i)==1:
count_1+=1
elif len(i)==2:
count_2+=1
elif len(i)==3:
count_3+=1
elif len(i)==4:
count_4+=1
elif len(i)==5:
count_5+=1
elif len(i)==6:
count_6+=1
elif len(i)==7:
count_7+=1
elif len(i)==8:
count_8+=1
elif len(i)==9:
count_9+=1
elif len(i)==10:
count_10+=1
print "--------------------Count of different letter words---------------------"
print "One word = ",count_1
print "Two word = ",count_2
print "Three word = ",count_3
print "Four word = ",count_4
print "Five word = ",count_5
print "Six word = ",count_6
print "Seven word = ",count_7
print "Eight word = ",count_8
print "Nine word = ",count_9
print "Ten word = ",count_10
print "--------------------Word Counts---------------------"
words=clean_words(text.split())
print words
for i in words:
if i in list_of_words:
list_of_words[i]=list_of_words[i]+1
else:
list_of_words[i]=1
for key in list_of_words:
print key, list_of_words[key]