mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-06-01 10:39:34 +12:00
038e101386
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
45 lines
853 B
Python
45 lines
853 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import re
|
|
|
|
from enum import Enum, unique
|
|
|
|
_char_pattern_with_spaces = re.compile(r"[\S ]")
|
|
_char_pattern_without_spaces = re.compile(r"\S")
|
|
_word_pattern = re.compile(r"\S+")
|
|
|
|
_whitespace = " \t\r\n"
|
|
|
|
|
|
@unique
|
|
class CounterKind(Enum):
|
|
WORDS = 0
|
|
CHARACTERS = 1
|
|
PAGES = 2
|
|
|
|
|
|
class CharCounter:
|
|
|
|
@classmethod
|
|
def count(cls, text: str, use_spaces: bool = False):
|
|
if use_spaces:
|
|
return len(_char_pattern_with_spaces.findall(text))
|
|
else:
|
|
return len(_char_pattern_without_spaces.findall(text))
|
|
|
|
|
|
class WordCounter:
|
|
|
|
@classmethod
|
|
def count(cls, text: str):
|
|
return len(_word_pattern.findall(text))
|
|
|
|
|
|
class PageCounter:
|
|
|
|
@classmethod
|
|
def count(cls, text: str):
|
|
wc = WordCounter.count(text)
|
|
return int(wc / 25) / 10.
|