How do I fix my code to collect ONLY hashtag instead of accumulating all the information about the number of occurrences of all words from the textfile?
Please help to fix below codes. tweetcount=0 maxcount=0 count = 0 with open('elon-musk.txt') as book: for tweet in book: count += 1 print("Number of tweets:", count) print() with open('elon-musk.txt') as book: for line in book: s count = len(line.split()) if count>maxcount: maxline = line maxcount = count tweetcount += 1 print("Tweet with max number of words:",maxline) def cleanedup(s): alphabet = 'abcdefghijklmnopqrstuvwxyz#' cleantext = '' for character in s.lower(): if character in alphabet: cleantext += character else: cleantext += ' ' return cleantext concordance = {} with open('elon-musk.txt') as book: linenum = 1 for line in book: for word in cleanedup(line).split(): if word in concordance: concordance[word] +=1 else: concordance[word]= 1 linenum += 1 while True: word = input('Enter word: ') if word in concordance: print('Mentioned', concordance[word],'times') else: print('Not mentioned.')
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
def cleanedup(s):
alphabet = 'abcdefghijklmnopqrstuvwxyz#'
cleantext = ''
for character in s.lower():
if character in alphabet:
cleantext += character
else:
cleantext += ' '
return cleantext
hashtag_words = []
occurrences = []
with open('elon_musk.txt') as book:
for line in book:
for i in line.split():
if "#" in i:
i = i.replace('#','')
hashtag_words.append(i)
for i in range(len(hashtag_words)):
words = hashtag_words[i]
occurrences.append(words)
while True:
word = input('Enter word:')
word = word.replace('#','')
if word in occurrences:
print('Mentioned', occurrences.count(word), 'times')
else:
print('Not mentioned.')
Get Answers For Free
Most questions answered within 1 hours.