![]() |
Class and methods - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Class and methods (/thread-42110.html) |
Class and methods - Saida2024 - May-12-2024 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 0If I enter a text, I receive an error: Anyone can help me fix the error?Thanks
RE: Class and methods - ebn852_pan - May-12-2024 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. RE: Class and methods - deanhystad - May-13-2024 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 forYour 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")) Much of what was done above could be done using a Counter dictionary.
|