Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class and methods
#1
I have this code but it doesn't seem to work.

class textanalysis(object):
    
Class Analysis(object):   
  def _init_(self, text):
        #enlever la ponctuation
        formater = text.replace(',','').replace('.','').replace('!','').replace('?','')
        #transformer en minuscule
        formater = formater.lower()
        self.fmt = formater
        
    def freqall(self):
        mots = self.fmt.split(' ')
        #creer un dictionnaire
        dico = {}
        
        for word in set(mots):    #set est utiliser pour qu'il n'y ait pas de repetitions
            dico[word] = self.fmt.count(word)
            
            return dico
        
    def freq0f(self, word):
        freqdico = self.freqall()
        if word in freqdico:
            return frqdico[word]
        else:
            return 0
If I enter a text, I receive an error:
Error:
" TypeError: textanalysis() takes no arguments "
Anyone can help me fix the error?
Thanks
Output:
buran write May-12-2024, 06:26 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Sorry. But I can clarify some or one point above program. Is this Italian? If so, that's not the correct translation. You'll have to type in I say.
Programs are like instructions or rules. Learning it gets us closer to a solution. Desired outcome. Computer talk.
Reply
#3
What is this supposed to do?
class textanalysis(object):
It looks like the start of a class, but there is no code that follows the class declaration. Where are the methods for class textanalysis?

Class Analysis should be class Analysis. class with lower case 'c', not capital 'C'.

When declaring a class you don't have to specify the superclass when the superclass is object. class Analysis(): is fine. No need for class Analysis(object):.

Indentation is important in Python, it is how you define a block of code. I think your indentation is wrong for the return statement here:
        for word in set(mots):    #set est utiliser pour qu'il n'y ait pas de repetitions
            dico[word] = self.fmt.count(word)
             
            return dico  # Should be indented same as for
Your class is odd. I would put the counting in __init__(), like this:
class Analysis():
    """Count word occurrances in text."""
    def __init__(self, text):
        words = "".join((c for c in text if c not in ".,?!")).lower().split()
        self.total = len(words)
        self.word_counts = {}
        for word in words:
            self.word_counts[word] = self.word_counts.get(word, 0) + 1

    def most_common(self):
        """Return list of (word, count) tuples sorted in decreasing order."""
        words = [(word, count) for word, count in self.word_counts.items()]
        words.sort(key=lambda x: x[1], reverse=True)
        return words
    
    @property
    def words(self):
        """Return list of words in text."""
        return list(self.word_counts)

    def count(self, word):
        """Return count for a word."""
        return self.word_counts.get(word, 0)

    def frequency(self, word):
        """Return frequency for a word"""
        return self.count(word) / self.total

text = """I'm blue
Da ba dee da ba di
Da ba dee da ba di
Da ba dee da ba di
Da ba dee da ba di
Da ba dee da ba di
Da ba dee da ba di
Da ba dee da ba di"""

x = Analysis(text)
print(x.words, x.total)
print(x.word_counts)
print(x.most_common())
print(x.count("ba"))
print(x.frequency("ba"))
Output:
["i'm", 'blue', 'da', 'ba', 'dee', 'di'] 44 {"i'm": 1, 'blue': 1, 'da': 14, 'ba': 14, 'dee': 7, 'di': 7} [('da', 14), ('ba', 14), ('dee', 7), ('di', 7), ("i'm", 1), ('blue', 1)] 14 0.3181818181818182
Much of what was done above could be done using a Counter dictionary.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 9 475 May-20-2024, 08:46 PM
Last Post: ebn852_pan
  Class test : good way to split methods into several files paul18fr 4 579 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Structuring a large class: privite vs public methods 6hearts 3 1,168 May-05-2023, 10:06 AM
Last Post: Gribouillis
  access share attributed among several class methods drSlump 0 1,093 Nov-18-2021, 03:02 PM
Last Post: drSlump
  a function common to methods of a class Skaperen 7 2,714 Oct-04-2021, 07:07 PM
Last Post: Skaperen
  Listing All Methods Of Associated With A Class JoeDainton123 3 2,415 May-10-2021, 01:46 AM
Last Post: deanhystad
  too many methods in class - redesign idea? Phaze90 3 2,564 Mar-05-2021, 09:01 PM
Last Post: deanhystad
  Special Methods in Class Nikhil 3 2,363 Mar-04-2021, 06:25 PM
Last Post: Nikhil
  cant able to make methods interact with each other in the class jagasrik 2 1,854 Sep-16-2020, 06:52 PM
Last Post: deanhystad
  Question about naming variables in class methods sShadowSerpent 1 2,052 Mar-25-2020, 04:51 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020