With Python codes
(STATE OF THE UNION SPEECHES) All U.S. Presidents’ State of the Union speeches are available online. Copy and paste one into a large multiline string, then display statistics, including the total word count, the total character count, the average word length, the average sentence length, a word distribution of all words, a word distribution of words ending in 'ly' and the top 10 longest words.
HI,
Please refer to the screenshots to understand the program and it's output. I have also copied the code here.
Code:-
from itertools import count
speech = ''' All U.S. Presidents’ State of the Union speeches
are available online .
Please follow this program to understand different operations of
Python string .
All the operatins here beautifuly demonstrated . Keep asking doubts
to understand quickly .
Happy Coding . '''
# Total count of words in this string.
words = speech.split()
words_length = len(words)
print('Words count :', words_length)
# Number of characters in this string.
def count_chars(speech):
result = 0
for char in speech:
result += 1
return result
print('Characters count :', count_chars(speech))
# Avreage word length in this string.
average_word = sum(len(word) for word in words) / len(words)
print('Average word length :', average_word)
# Avreage sentence length in this string.
sentence = speech.split('.')
average_sentence = sum(len(x) for x in sentence) /
len(sentence)
print('Average sentence length :', average_sentence)
# Word distribution of all words
words2 = []
for word in words:
if word not in words2:
words2.append(word)
for i in range(0, len(words2)):
print('Distribution of', words2[i], 'is :',
words.count(words2[i]))
# Word distribution of words ending in ly
words_ly = [word for word in speech.split() if
word.endswith('ly')]
print(words_ly)
words3 = []
for word in words_ly:
if word not in words3:
words3.append(word)
for i in range(0, len(words3)):
print('Distribution of', words3[i], 'is :',
words.count(words3[i]))
# Top 10 longest words
def longest_word(words, ten_large):
cnt = count()
return sorted(words, key = lambda w : (len(w), next(cnt)),
reverse = True)[:ten_large]
ten_large = 10
print('Top 10 longest words :', longest_word(words, ten_large))




With Python codes (STATE OF THE UNION SPEECHES) All U.S. Presidents’ State of the Union speeches...