From 0a09dcd0a72247c0be075e68afebe50cf63f0d11 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Thu, 30 May 2019 12:38:43 +0200 Subject: [PATCH] Moved the word counting bit to a separate file --- nw/gui/wordcounter.py | 52 +++++------------------------------- nw/tools/wordcount.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 nw/tools/wordcount.py diff --git a/nw/gui/wordcounter.py b/nw/gui/wordcounter.py index 4e044bca..f1c53497 100644 --- a/nw/gui/wordcounter.py +++ b/nw/gui/wordcounter.py @@ -15,6 +15,8 @@ import nw from PyQt5.QtCore import QThread +from nw.tools.wordcount import countWords + logger = logging.getLogger(__name__) class WordCounter(QThread): @@ -29,52 +31,12 @@ class WordCounter(QThread): def run(self): - self.charCount = 0 - self.wordCount = 0 - self.paraCount = 0 + theText = self.theParent.getText() + cC, wC, pC = countWords(theText) - prevEmpty = True - - for n in range(self.theParent.theDoc.blockCount()): - - theBlock = self.theParent.theDoc.findBlockByNumber(n) - if not theBlock.isValid(): - continue - - countPara = True - theText = theBlock.text() - theLen = len(theText) - - if theLen == 0: - prevEmpty = True - continue - if theText[0] == "@" or theText[0] == "%": - prevEmpty = True - continue - - if theText[0:5] == "#### ": - self.wordCount -= 1 - self.charCount -= 5 - countPara = False - elif theText[0:4] == "### ": - self.wordCount -= 1 - self.charCount -= 4 - countPara = False - elif theText[0:3] == "## ": - self.wordCount -= 1 - self.charCount -= 3 - countPara = False - elif theText[0:2] == "# ": - self.wordCount -= 1 - self.charCount -= 2 - countPara = False - - theBuff = theText.replace("–"," ").replace("—"," ") - self.wordCount += len(theBuff.split()) - self.charCount += theLen - if countPara and prevEmpty: - self.paraCount += 1 - prevEmpty = countPara == False + self.charCount = cC + self.wordCount = wC + self.paraCount = pC return diff --git a/nw/tools/wordcount.py b/nw/tools/wordcount.py new file mode 100644 index 00000000..d6fad51a --- /dev/null +++ b/nw/tools/wordcount.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +"""novelWriter Word Counter + + novelWriter – Word Counter +============================ + Simple word counter + + File History: + Created: 2019-04-22 [0.0.1] + Moved: 2019-05-30 [0.1.4] + +""" + +import logging +import nw + +logger = logging.getLogger(__name__) + +def countWords(theText): + + charCount = 0 + wordCount = 0 + paraCount = 0 + prevEmpty = True + + for aLine in theText.splitlines(): + + countPara = True + theLen = len(aLine) + + if theLen == 0: + prevEmpty = True + continue + if aLine[0] == "@" or aLine[0] == "%": + continue + + if aLine[0:5] == "#### ": + wordCount -= 1 + charCount -= 5 + countPara = False + elif aLine[0:4] == "### ": + wordCount -= 1 + charCount -= 4 + countPara = False + elif aLine[0:3] == "## ": + wordCount -= 1 + charCount -= 3 + countPara = False + elif aLine[0:2] == "# ": + wordCount -= 1 + charCount -= 2 + countPara = False + + theBuff = aLine.replace("–"," ").replace("—"," ") + wordCount += len(theBuff.split()) + charCount += theLen + if countPara and prevEmpty: + paraCount += 1 + prevEmpty = countPara == False + + return charCount, wordCount, paraCount