Optimized counting words and characters via regex

Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
TheJackiMonster 2021-06-01 15:16:02 +02:00
parent 9dc2544d1c
commit dedd8e043a
No known key found for this signature in database
GPG key ID: D850A5F772E880F9
2 changed files with 13 additions and 18 deletions

View file

@ -24,17 +24,14 @@ MW = None
def wordCount(text):
t = text.strip().replace(" ", "\n").split("\n")
t = [l for l in t if l]
return len(t)
return len(re.findall(r"\S+", text))
def charCount(text, use_spaces = True):
t = text.strip()
if not use_spaces:
t = t.replace(" ", "")
return len(t)
if use_spaces:
return len(re.findall(r"[\S ]", text))
else:
return len(re.findall(r"\S", text))
validate_ok = lambda *args, **kwargs: True
def uiParse(input, default, converter, validator=validate_ok):

View file

@ -1,26 +1,24 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
class CharCounter:
@classmethod
def count(cls, text: str, use_spaces: bool = False):
t = text.strip()
if not use_spaces:
t = t.replace(" ", "")
return len(t)
if use_spaces:
return len(re.findall(r"[\S ]", text))
else:
return len(re.findall(r"\S", text))
class WordCounter:
@classmethod
def count(cls, text: str):
t = text.strip().replace(" ", "\n").split("\n")
t = [l for l in t if len(l) > 0]
return len(t)
return len(re.findall(r"\S+", text))
class PageCounter: