Moved the word counting bit to a separate file
This commit is contained in:
+7
-45
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user