-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCounter.py
More file actions
45 lines (31 loc) · 1.35 KB
/
Copy pathWordCounter.py
File metadata and controls
45 lines (31 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from collections import Counter
import re
import json
def with_spaces(word):
return len(word)
def without_spaces(word):
return len(word) - word.count(' ')
class Count:
def __init__(self, sentence):
self.sentence = sentence
def word_count(self):
res = len(self.sentence.split())
return res
def character_count(self):
res = Counter(self.sentence)
for key in res.copy():
if not re.search(r'[a-zA-Z]', key):
res.pop(key)
return json.dumps(sorted(res.items()), separators=(',', ':')) \
.replace('[', '{').replace(']', '}').replace('{{', '[{').replace('}}', '}]').replace('",', '":')
def text_length(self):
return json.dumps({"withSpaces": with_spaces(self.sentence),
"withoutSpaces": without_spaces(self.sentence)}, separators=(',', ':'))
class Print:
def __init__(self, sentence):
self.c = Count(sentence)
def print(self):
return json.dumps({"textLength": self.c.text_length(),
"wordCount": self.c.word_count(),
"characterCount": self.c.character_count()}, separators=(',', ':')).replace('\\', '').replace('"{', '{')\
.replace('}"', '}').replace('"[', '[').replace(']"', ']')