Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 2 months ago

Computer science help! How do you write a new function findMention code python?

Write a new function findMentions that takes a filename as a parameter and reports 3 usernames most frequently mentioned in that file. The function should create a dictionary of counts for all username mentions (words starting with @). After reading through the file and accumulating the counts for all mentioned usernames, use the dictionary to create a list like this:

example:  [[15, '@alice'], [20, '@bob'], [7, '@carol'], ... ]

Use sort to sort the above list and print out 3 most frequently mentioned usernames.

Attachment image

2 Answers

Relevance
  • EddieJ
    Lv 7
    2 months ago
    Favorite Answer

    This doesn't do the whole thing but should get you closer:

    mentionedWords = []

    for filename in os.listdir('.'):

        if filename [-7:] == ".tweets":

            print ("fn=", filename)

            with open (filename) as file:

                for line in file:

                    x = line.find ("@")

                    if x>=0: mentionedWords.append(line)

                print (mentionedWords)

    [Edit3:  The next problem is that <counts> is not defined for

    for word in counts: ]

    [Edit2: The first problem I found was you misspelled filename in:

    print(filenane, 'contains @')  5:06p]

    [Edit: They removed comments months ago.  I'm starting to look at your code.  4:40p CST]

    You should use stepwise refinement -- don't try to write the whole function at once.

    So, start with writing the function so that all it does is displays the number of lines in the file.

    The photo you supplied is unreadable.  Also, I realize that you generally don't respect the answers that you receive.

  • 2 months ago

    @EddieJ I do respect all answers given but I'm unsure of how to reply or comment individually to you all. I can't find the option or button. Yahoo answer is different from when I last used it and it's been years. Wait is the answer section the comment section? Can you even see this when I @mention you?

    Task Step-by-step:

    Modify function cleanedup so that it keeps not only letters, but also digits 0123456789 and symbols @ and _

    Write a new function findMentions that takes a filename as a parameter and reports 3 usernames most frequently mentioned in that file. The function should create a dictionary of counts for all username mentions (words starting with @). After reading through the file and accumulating the counts for all mentioned usernames, use the dictionary to create a list like this:

      [[15, '@alice'], [20, '@bob'], [7, '@carol'], ... ]

    Use sort to sort the above list and print out 3 most frequently mentioned usernames.

    Check each file in the current folder (using os.listdir('.')), if the file name ends with .tweets, call findMentions on the file to find its most frequent mentions.

    My code so far:

    def cleanedup(s):

        alphabet='abcdefghijklmnopqrstuvwxyz@_0123456789'

        cleantext= ''

        for character in s.lower():

               if character in alphabet:

                     cleantext+= character

               else:

                     cleantext+=''

        return cleantext

    import os

    def contains(filename, pattern):

        with open(filename) as file:

            for line in file:

                if pattern in line:

                    return True

        return False

    for filename in os.listdir('.'):

        if contains(filename, '@'):

            print(filenane, 'contains @')

    mentionedWords=[]

    for word in counts:

        if word[-3:]=='@':

            mentionedWords.append([counts[word],word])

    mentionedWords.sort()

    print(mentionedWords[-5:])

    import os

    path='.'

    for filename in os.listdir(path):

        print(filename)

Still have questions? Get your answers by asking now.