Question

How do I fix my code to collect ONLY hashtag instead of accumulating all the information...

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.')

Homework Answers

Answer #1

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.')
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
This must be answered not advance methods, focusing on String method. We are working on Ch...
This must be answered not advance methods, focusing on String method. We are working on Ch 9 in Think Java 1st Ed.I need the program to be bare bones and the coding need to be done the long way with no advanced code. in this lab you will have two methods with headings: - public static int countNumberSigns(String tweetText) - public static int countHashtags(String tweetText) 'String tweetText' means the method is expectiong a string value as an input to it....
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include...
How do I fix my error of binding on line 74? #include <iostream> #include <fstream> #include <cctype> #include <cstring> #include <iomanip> using namespace std; #include "AvlTree.h" class WordCount { public:     char *word;     int *lines;     int count;     int size;     bool operator<(const WordCount &rhs) const {return strcmp(word, rhs.word) < 0;}     bool operator!= (const WordCount &rhs) const {return strcmp(word, rhs.word) != 0;}     WordCount():lines(NULL), count(0), size(0) {word = new char[1]; word[0] = '\0';}     friend ostream& operator<<...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...