Add body text only counter
This commit is contained in:
@@ -5,6 +5,7 @@ novelWriter – Text Counting Functions
|
|||||||
File History:
|
File History:
|
||||||
Created: 2019-04-22 [0.0.1] standardCounter
|
Created: 2019-04-22 [0.0.1] standardCounter
|
||||||
Rewritten: 2024-02-27 [2.4b1] preProcessText, standardCounter
|
Rewritten: 2024-02-27 [2.4b1] preProcessText, standardCounter
|
||||||
|
Created: 2024-02-27 [2.4b1] bodyTextCounter
|
||||||
|
|
||||||
This file is a part of novelWriter
|
This file is a part of novelWriter
|
||||||
Copyright 2018–2024, Veronica Berglyd Olsen
|
Copyright 2018–2024, Veronica Berglyd Olsen
|
||||||
@@ -75,15 +76,14 @@ def standardCounter(text: str) -> tuple[int, int, int]:
|
|||||||
This is the standard counter that includes headers in the word and
|
This is the standard counter that includes headers in the word and
|
||||||
character counts.
|
character counts.
|
||||||
"""
|
"""
|
||||||
charCount = 0
|
cCount = 0
|
||||||
wordCount = 0
|
wCount = 0
|
||||||
paraCount = 0
|
pCount = 0
|
||||||
prevEmpty = True
|
prevEmpty = True
|
||||||
|
|
||||||
for line in preProcessText(text):
|
for line in preProcessText(text):
|
||||||
|
|
||||||
countPara = True
|
countPara = True
|
||||||
|
|
||||||
if not line:
|
if not line:
|
||||||
prevEmpty = True
|
prevEmpty = True
|
||||||
continue
|
continue
|
||||||
@@ -108,11 +108,28 @@ def standardCounter(text: str) -> tuple[int, int, int]:
|
|||||||
line = line[4:]
|
line = line[4:]
|
||||||
countPara = False
|
countPara = False
|
||||||
|
|
||||||
wordCount += len(line.split())
|
wCount += len(line.split())
|
||||||
charCount += len(line)
|
cCount += len(line)
|
||||||
if countPara and prevEmpty:
|
if countPara and prevEmpty:
|
||||||
paraCount += 1
|
pCount += 1
|
||||||
|
|
||||||
prevEmpty = not countPara
|
prevEmpty = not countPara
|
||||||
|
|
||||||
return charCount, wordCount, paraCount
|
return cCount, wCount, pCount
|
||||||
|
|
||||||
|
|
||||||
|
def bodyTextCounter(text: str | list[str]) -> tuple[int, int, int]:
|
||||||
|
"""A counter that counts body text words, characters, and characters
|
||||||
|
without white spaces.
|
||||||
|
"""
|
||||||
|
wCount = 0
|
||||||
|
cCount = 0
|
||||||
|
sCount = 0
|
||||||
|
|
||||||
|
for line in preProcessText(text, keepHeaders=False):
|
||||||
|
words = line.split()
|
||||||
|
wCount += len(words)
|
||||||
|
cCount += len(line)
|
||||||
|
sCount += len("".join(words))
|
||||||
|
|
||||||
|
return wCount, cCount, sCount
|
||||||
|
|||||||
@@ -22,18 +22,69 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from novelwriter.text.counting import standardCounter
|
from novelwriter.text.counting import bodyTextCounter, preProcessText, standardCounter
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.core
|
||||||
|
def testTextCounting_preProcessText():
|
||||||
|
"""Test the text preprocessor for counters."""
|
||||||
|
# Not Text
|
||||||
|
assert preProcessText(None) == []
|
||||||
|
|
||||||
|
# No Text
|
||||||
|
assert preProcessText("") == []
|
||||||
|
|
||||||
|
text = (
|
||||||
|
"#! Title\n"
|
||||||
|
"##! Prologue\n"
|
||||||
|
"# Heading One\n"
|
||||||
|
"## Heading Two\n"
|
||||||
|
"### Heading Three\n"
|
||||||
|
"#### Heading Four\n\n"
|
||||||
|
"@tag: value\n\n"
|
||||||
|
"% A comment.\n\n"
|
||||||
|
"A [b]paragraph[/b].\n\n"
|
||||||
|
"[vspace:3]\n\n"
|
||||||
|
"[New Page]\n\n"
|
||||||
|
"Dashes\u2013and even longer\u2014dashes.\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Process Text, w/Headers
|
||||||
|
assert preProcessText(text) == [
|
||||||
|
"#! Title",
|
||||||
|
"##! Prologue",
|
||||||
|
"# Heading One",
|
||||||
|
"## Heading Two",
|
||||||
|
"### Heading Three",
|
||||||
|
"#### Heading Four",
|
||||||
|
"", "", "",
|
||||||
|
"A paragraph.", "",
|
||||||
|
"", "", "", "",
|
||||||
|
"Dashes and even longer dashes.", ""
|
||||||
|
]
|
||||||
|
|
||||||
|
# Process Text, wo/Headers
|
||||||
|
assert preProcessText(text, keepHeaders=False) == [
|
||||||
|
"", "", "",
|
||||||
|
"A paragraph.", "",
|
||||||
|
"", "", "", "",
|
||||||
|
"Dashes and even longer dashes.", ""
|
||||||
|
]
|
||||||
|
|
||||||
|
# END Test testTextCounting_preProcessText
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testTextCounting_standardCounter():
|
def testTextCounting_standardCounter():
|
||||||
"""Test the word counter and the exclusion filers."""
|
"""Test the standard counter."""
|
||||||
# Non-Text
|
# Non-Text
|
||||||
assert standardCounter(None) == (0, 0, 0) # type: ignore
|
assert standardCounter(None) == (0, 0, 0) # type: ignore
|
||||||
assert standardCounter(1234) == (0, 0, 0) # type: ignore
|
assert standardCounter(1234) == (0, 0, 0) # type: ignore
|
||||||
|
|
||||||
# General Text
|
# General Text
|
||||||
cC, wC, pC = standardCounter((
|
cC, wC, pC = standardCounter((
|
||||||
|
"#! Title\n\n"
|
||||||
|
"##! Prologue\n\n"
|
||||||
"# Heading One\n"
|
"# Heading One\n"
|
||||||
"## Heading Two\n"
|
"## Heading Two\n"
|
||||||
"### Heading Three\n"
|
"### Heading Three\n"
|
||||||
@@ -45,8 +96,8 @@ def testTextCounting_standardCounter():
|
|||||||
"The third paragraph.\n\n"
|
"The third paragraph.\n\n"
|
||||||
"Dashes\u2013and even longer\u2014dashes."
|
"Dashes\u2013and even longer\u2014dashes."
|
||||||
))
|
))
|
||||||
assert cC == 138
|
assert cC == 151
|
||||||
assert wC == 22
|
assert wC == 24
|
||||||
assert pC == 4
|
assert pC == 4
|
||||||
|
|
||||||
# Text Alignment
|
# Text Alignment
|
||||||
@@ -125,3 +176,31 @@ def testTextCounting_standardCounter():
|
|||||||
assert pC == 2
|
assert pC == 2
|
||||||
|
|
||||||
# END Test testTextCounting_standardCounter
|
# END Test testTextCounting_standardCounter
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.core
|
||||||
|
def testTextCounting_bodyTextCounter():
|
||||||
|
"""Test the body text counter."""
|
||||||
|
# Not Text
|
||||||
|
assert bodyTextCounter(None) == (0, 0, 0)
|
||||||
|
|
||||||
|
# General Text
|
||||||
|
wC, cC, sC = bodyTextCounter((
|
||||||
|
"#! Title\n\n"
|
||||||
|
"##! Prologue\n\n"
|
||||||
|
"# Heading One\n"
|
||||||
|
"## Heading Two\n"
|
||||||
|
"### Heading Three\n"
|
||||||
|
"#### Heading Four\n\n"
|
||||||
|
"@tag: value\n\n"
|
||||||
|
"% A comment that should not be counted.\n\n"
|
||||||
|
"The first paragraph.\n\n"
|
||||||
|
"The second paragraph.\n\n\n"
|
||||||
|
"The third paragraph.\n\n"
|
||||||
|
"Dashes\u2013and even longer\u2014dashes."
|
||||||
|
))
|
||||||
|
assert wC == 14
|
||||||
|
assert cC == 91
|
||||||
|
assert sC == 81
|
||||||
|
|
||||||
|
# END Test testTextCounting_bodyTextCounter
|
||||||
|
|||||||
Reference in New Issue
Block a user