Write a program called acronym.py that may be used to obtain an acronym for a gi
ID: 3693426 • Letter: W
Question
Write a program called acronym.py that may be used to obtain an acronym for a given sentence. Use Python 3.5.
Words such as ‘and’, ‘the’ and ‘for’ must be ignored in the program. Your program should begin by prompting the user to enter a list of words to be ignored. It should then ask the user to enter the sentence for which the acronym is to be generated.
----------------------------------------------------------------------------------
Sample Input/Output:
Enter words to be ignored separated by commas:
the, for, and
Enter a title to generate its acronym:
The Centre for Theoretical Physics and Astrophysics
The acronym is: CTPA
-----------------------------------------------------------------------------------------------
HINT: 'one, two'.split(', ') is ['one', 'two'].
Explanation / Answer
This program is not complete yet and gives an idea in removing unwanted words.
import re
esentence = raw_input('Enter words to be ignored separated by commas: ')
sentence = raw_input('What would you like to acronymize?: ')
exceps = esentence.split(',')
words = sentence.split() #split the sentece into words
just_first_letters = [] #a list containing just the first letter of each word
result = []
#traverse the list of words, adding the first letter of
#each word into just_first_letters
noise_re = re.compile('\b(%s)\W'%('|'.join(map(re.escape,exceps))),re.I)
[noise_re.sub('',p) for p in words]
for word in words:
for excep in exceps:
if word.startswith(excep):
word = word.replace(excep, "").strip()
result.append(word)
print result
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.