Fix word counter for align and indent tags

This commit is contained in:
Veronica Berglyd Olsen
2021-06-10 23:38:11 +02:00
parent 804dd3bb7c
commit 32e9c10702
3 changed files with 83 additions and 45 deletions
+27 -19
View File
@@ -908,6 +908,9 @@ def countWords(theText):
paraCount = 0 paraCount = 0
prevEmpty = True prevEmpty = True
if not isinstance(theText, str):
return charCount, wordCount, paraCount
# We need to treat dashes as word separators for counting words. # We need to treat dashes as word separators for counting words.
# The check+replace apprach is much faster that direct replace for # The check+replace apprach is much faster that direct replace for
# large texts, and a bit slower for small texts, but in the latter # large texts, and a bit slower for small texts, but in the latter
@@ -920,33 +923,38 @@ def countWords(theText):
for aLine in theText.splitlines(): for aLine in theText.splitlines():
countPara = True countPara = True
theLen = len(aLine)
if theLen == 0: if not aLine:
prevEmpty = True prevEmpty = True
continue continue
if aLine[0] == "@" or aLine[0] == "%": if aLine[0] == "@" or aLine[0] == "%":
continue continue
if aLine[0:5] == "#### ": if aLine[0] == "#":
wordCount -= 1 if aLine[:5] == "#### ":
charCount -= 5 aLine = aLine[5:]
countPara = False countPara = False
elif aLine[0:4] == "### ": elif aLine[:4] == "### ":
wordCount -= 1 aLine = aLine[4:]
charCount -= 4 countPara = False
countPara = False elif aLine[:3] == "## ":
elif aLine[0:3] == "## ": aLine = aLine[3:]
wordCount -= 1 countPara = False
charCount -= 3 elif aLine[:2] == "# ":
countPara = False aLine = aLine[2:]
elif aLine[0:2] == "# ": countPara = False
wordCount -= 1 elif aLine[0] == ">" or aLine[-1] == "<":
charCount -= 2 if aLine[:2] == ">>":
countPara = False aLine = aLine[2:].lstrip(" ")
elif aLine[:1] == ">":
aLine = aLine[1:].lstrip(" ")
if aLine[-2:] == "<<":
aLine = aLine[:-2].rstrip(" ")
elif aLine[-1:] == "<":
aLine = aLine[:-1].rstrip(" ")
wordCount += len(aLine.split()) wordCount += len(aLine.split())
charCount += theLen charCount += len(aLine)
if countPara and prevEmpty: if countPara and prevEmpty:
paraCount += 1 paraCount += 1
+9 -9
View File
@@ -1,13 +1,13 @@
<?xml version='1.0' encoding='utf-8'?> <?xml version='1.0' encoding='utf-8'?>
<novelWriterXML appVersion="1.4a0" hexVersion="0x010400a0" fileVersion="1.2" timeStamp="2021-06-10 14:15:49"> <novelWriterXML appVersion="1.4a0" hexVersion="0x010400a0" fileVersion="1.2" timeStamp="2021-06-10 23:37:32">
<project> <project>
<name>Sample Project</name> <name>Sample Project</name>
<title>Sample Project</title> <title>Sample Project</title>
<author>Jane Smith</author> <author>Jane Smith</author>
<author>Jay Doh</author> <author>Jay Doh</author>
<saveCount>1095</saveCount> <saveCount>1096</saveCount>
<autoCount>188</autoCount> <autoCount>188</autoCount>
<editTime>52304</editTime> <editTime>52328</editTime>
</project> </project>
<settings> <settings>
<doBackup>False</doBackup> <doBackup>False</doBackup>
@@ -17,8 +17,8 @@
<autoOutline>True</autoOutline> <autoOutline>True</autoOutline>
<lastEdited>636b6aa9b697b</lastEdited> <lastEdited>636b6aa9b697b</lastEdited>
<lastViewed>636b6aa9b697b</lastViewed> <lastViewed>636b6aa9b697b</lastViewed>
<lastWordCount>1224</lastWordCount> <lastWordCount>1217</lastWordCount>
<novelWordCount>848</novelWordCount> <novelWordCount>841</novelWordCount>
<notesWordCount>376</notesWordCount> <notesWordCount>376</notesWordCount>
<autoReplace> <autoReplace>
<entry key="A">B</entry> <entry key="A">B</entry>
@@ -63,8 +63,8 @@
<status>Started</status> <status>Started</status>
<exported>True</exported> <exported>True</exported>
<layout>TITLE</layout> <layout>TITLE</layout>
<charCount>244</charCount> <charCount>241</charCount>
<wordCount>43</wordCount> <wordCount>42</wordCount>
<paraCount>3</paraCount> <paraCount>3</paraCount>
<cursorPos>252</cursorPos> <cursorPos>252</cursorPos>
</item> </item>
@@ -118,8 +118,8 @@
<status>1st Draft</status> <status>1st Draft</status>
<exported>True</exported> <exported>True</exported>
<layout>SCENE</layout> <layout>SCENE</layout>
<charCount>2450</charCount> <charCount>2434</charCount>
<wordCount>439</wordCount> <wordCount>433</wordCount>
<paraCount>14</paraCount> <paraCount>14</paraCount>
<cursorPos>61</cursorPos> <cursorPos>61</cursorPos>
</item> </item>
+47 -17
View File
@@ -1177,29 +1177,59 @@ def testCoreIndex_CheckTextCounts(dummyGUI):
def testCoreIndex_CountWords(): def testCoreIndex_CountWords():
"""Test the word counter and the exclusion filers. """Test the word counter and the exclusion filers.
""" """
testText = ( # Non-Text
assert countWords(None) == (0, 0, 0)
assert countWords(1234) == (0, 0, 0)
# General Text
cC, wC, pC = countWords((
"# Heading One\n" "# Heading One\n"
"## Heading Two\n" "## Heading Two\n"
"### Heading Three\n" "### Heading Three\n"
"#### Heading Four\n" "#### Heading Four\n\n"
"\n" "@tag: value\n\n"
"@tag: value\n" "% A comment that should not be counted.\n\n"
"\n" "The first paragraph.\n\n"
"% A comment that should n ot be counted.\n" "The second paragraph.\n\n\n"
"\n" "The third paragraph.\n\n"
"The first paragraph.\n"
"\n"
"The second paragraph.\n"
"\n"
"\n"
"The third paragraph.\n"
"\n"
"Dashes\u2013and even longer\u2014dashes." "Dashes\u2013and even longer\u2014dashes."
) ))
cC, wC, pC = countWords(testText)
assert cC == 138 assert cC == 138
assert wC == 22 assert wC == 22
assert pC == 4 assert pC == 4
# Text Alignment
cC, wC, pC = countWords((
"# Title\n\n"
"Left aligned<<\n\n"
"Left aligned <<\n\n"
"Right indent<\n\n"
"Right indent <\n\n"
))
assert cC == 53
assert wC == 9
assert pC == 4
cC, wC, pC = countWords((
"# Title\n\n"
">>Right aligned\n\n"
">> Right aligned\n\n"
">Left indent\n\n"
"> Left indent\n\n"
))
assert cC == 53
assert wC == 9
assert pC == 4
cC, wC, pC = countWords((
"# Title\n\n"
">>Centre aligned<<\n\n"
">> Centre aligned <<\n\n"
">Double indent<\n\n"
"> Double indent <\n\n"
))
assert cC == 59
assert wC == 9
assert pC == 4
# END Test testCoreIndex_CountWords # END Test testCoreIndex_CountWords