在python 3中将单词计数函数重写为OOP



我写了一个简单的函数来计算单词在句子或短语中的出现次数。

def count(phrase):
phrase = phrase.lower()    
lst = phrase.split()
dct = dict()
for word in lst:
if word not in dct:
dct[word] = 1
else:
dct[word] += 1
return dct

请注意,现在我不关心标点符号、数字或停止语。

我想做的是写一个类来接受一个对象并做同样的事情。以下是到目前为止我所掌握的内容,但现在我被困在如何将列表传递到计数和创建字典的循环上:

class Count:
dct = dict()
def __init__(self, phrase):
self.phrase = phrase
def lst(self):
return self.phrase.lower().split()

为了方便起见,您可以使用defaultdict。

from collections import defaultdict as ddict
class Count:
def __init__(self, phrase):
self.phrase = phrase
self.dct = ddict(int)
def lst(self):
return self.phrase.lower().split()
# you can do it this way
def make_dict(self):
lst_words = self.lst()
for word in lst_words:
self.dct[word] += 1
c = Count("What I want to do is write a class to take an object and do the same thing. Here's what I have so far, but now I'm stuck on how to pass the list on to the loop that counts and creates the dictionary")
c.make_dict()     # make a dictonary out of words
c.dct             # display the contents stored in the dict

输出:

defaultdict(int,
{'what': 2,
'i': 2,
'want': 1,
'to': 4,
'do': 2,
'is': 1,
'write': 1,
'a': 1,
'class': 1,
'take': 1,
'an': 1,
'object': 1,
'and': 2,
'the': 4,
'same': 1,
'thing.': 1,
"here's": 1,
'have': 1,
'so': 1,
'far,': 1,
'but': 1,
'now': 1,
"i'm": 1,
'stuck': 1,
'on': 2,
'how': 1,
'pass': 1,
'list': 1,
'loop': 1,
'that': 1,
'counts': 1,
'creates': 1,
'dictionary': 1})

更新:根据Roelant的礼貌,还有一种方法可以做同样的事情,使用Counter

from collections import Counter
class Count:
def __init__(self, phrase):
self.phrase = phrase
self.dct = None
def lst(self):
return self.phrase.lower().split()
# you can do it this way also.
def make_dict(self):
lst_words = self.lst()
self.dct = Counter(lst_words)

请注意,您可以为此使用collections.Counter;也许你已经准备好了,但我无论如何都会把这个选项放在这里。

>>> import collections
>>> phrase = "What I want to do is write a class to take an object and do the same thing. Here's what I have so far, but now I'm stuck on how to pass the list on to the loop that counts and creates the dictionary."
>>> c = collections.Counter(phrase.lower().split())
>>> for k, v in c.most_common():
...     print(v, k)
4 to
4 the
2 what
2 i
2 do
2 and
2 on
1 want
1 is
1 write
1 a
1 class
1 take
1 an
1 object
1 same
1 thing.
1 here's
1 have
1 so
1 far,
1 but
1 now
1 i'm
1 stuck
1 how
1 pass
1 list
1 loop
1 that
1 counts
1 creates
1 dictionary.

将其封装在一个类中并没有太大的优势,但您可以使用类似这样的简单方法,将Counter:子类化

import collections
import string
class MyCounter(collections.Counter):
def __init__(self, phrase):
# do some preprocessing, for example removing puntuation
phrase = ''.join(
c.lower()
for c in phrase
if c not in string.punctuation)
super().__init__(phrase.split())

用途与以前类似:

>>> mc = MyCounter(phrase)         # 'lower' and 'split' is done inside
>>> for k, v in mc.most_common():
...     print(v, k)
4 to
4 the
2 what
2 i
2 do
2 and
2 on
1 want
1 is
1 write
1 a
1 class
1 take
1 an
1 object
1 same
1 thing
1 heres
1 have
1 so
1 far
1 but
1 now
1 im
1 stuck
1 how
1 pass
1 list
1 loop
1 that
1 counts
1 creates
1 dictionary

最新更新