mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-06-01 10:39:34 +12:00
Optimized counting words and characters via regex
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
This commit is contained in:
parent
9dc2544d1c
commit
dedd8e043a
|
@ -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):
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue