Remove the old numberToWord functions and clean up the translation files

This commit is contained in:
Veronica K. B. Olsen
2021-02-16 23:47:49 +01:00
parent 290ca2178d
commit 62cc64d912
5 changed files with 183 additions and 420 deletions
+181 -214
View File
File diff suppressed because it is too large Load Diff
-1
View File
@@ -2,7 +2,6 @@ SOURCES += \
nw/constants/constants.py \
nw/core/document.py \
nw/core/project.py \
nw/core/tokenizer.py \
nw/gui/about.py \
nw/gui/build.py \
nw/gui/custom.py \
+1 -2
View File
@@ -7,12 +7,11 @@ from nw.core.spellcheck import NWSpellCheck, NWSpellEnchant, NWSpellSimple
from nw.core.tohtml import ToHtml
from nw.core.toodt import ToOdt
from nw.core.tomd import ToMarkdown
from nw.core.tools import countWords, numberToRoman, numberToWord
from nw.core.tools import countWords, numberToRoman
__all__ = [
"countWords",
"numberToRoman",
"numberToWord",
"NWDoc",
"NWIndex",
"NWProject",
-125
View File
@@ -6,8 +6,6 @@ Various core tool functions
File History:
Created: 2019-04-22 [0.0.1] countWords
Created: 2019-10-13 [0.2.3] numberToWord, _numberToWordEN
Merged: 2020-05-08 [0.4.5] All of the above into this file
Created: 2020-07-05 [0.10.0] numberToRoman
This file is a part of novelWriter
@@ -118,126 +116,3 @@ def numberToRoman(numVal, isLower=False):
break
return romNum.lower() if isLower else romNum
# =============================================================================================== #
# Convert an Integer to a Number String
# =============================================================================================== #
def numberToWord(numVal, theLang):
"""Wrapper for converting numbers to words for chapter headings.
"""
if not isinstance(numVal, int):
return "[NaN]"
if numVal < 0:
return "[<0]"
if numVal > 999:
return "[>999]"
numWord = ""
theCode = theLang.split("_")[0]
if theCode == "en":
numWord = _numberToWord_EN(numVal)
elif theCode in ("nb", "nn"):
numWord = _numberToWord_NO(numVal, theCode)
else:
# Default to return the number as a string
numWord = str(numVal)
return numWord
def _numberToWord_EN(numVal):
"""Convert numbers to English words.
"""
oneWord = ""
tenWord = ""
hunWord = ""
if numVal == 0:
return "Zero"
oneVal = numVal % 10
tenVal = (numVal - oneVal) % 100
hunVal = (numVal - tenVal - oneVal) % 1000
theWords = {
0: "", 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five",
6: "Six", 7: "Seven", 8: "Eight", 9: "Nine", 10: "Ten",
11: "Eleven", 12: "Twelve", 13: "Thirteen", 14: "Fourteen",
15: "Fifteen", 16: "Sixteen", 17: "Seventeen", 18: "Eighteen",
19: "Nineteen", 20: "Twenty", 30: "Thirty", 40: "Forty",
50: "Fifty", 60: "Sixty", 70: "Seventy", 80: "Eighty",
90: "Ninety", 100: "One Hundred", 200: "Two Hundred",
300: "Three Hundred", 400: "Four Hundred", 500: "Five Hundred",
600: "Six Hundred", 700: "Seven Hundred", 800: "Eight Hundred",
900: "Nine Hundred",
}
retVal = ""
hunWord = theWords.get(hunVal, "")
if tenVal == 10:
oneWord = theWords.get(oneVal+10, "")
retVal = f"{hunWord} {oneWord}".strip()
else:
oneWord = theWords.get(oneVal, "")
if tenVal == 0:
retVal = f"{hunWord} {oneWord}".strip()
else:
tenWord = theWords.get(tenVal, "")
if oneVal == 0:
retVal = f"{hunWord} {tenWord}".strip()
else:
retVal = f"{hunWord} {tenWord}-{oneWord}".strip()
return retVal
def _numberToWord_NO(numVal, theCode):
"""Convert numbers to Norwegian words.
"""
oneWord = ""
tenWord = ""
hunWord = ""
if numVal == 0:
return "null"
oneVal = numVal % 10
tenVal = (numVal - oneVal) % 100
hunVal = (numVal - tenVal - oneVal) % 1000
theWords = {
0: "", 1: "én", 2: "to", 3: "tre", 4: "fire", 5: "fem", 6: "seks",
7: "sju", 8: "åtte", 9: "ni", 10: "ti", 11: "elleve", 12: "tolv",
13: "tretten", 14: "fjorten", 15: "femten", 16: "seksten", 17: "sytten",
18: "atten", 19: "nitten", 20: "tjue", 30: "tretti", 40: "førti",
50: "femti", 60: "seksti", 70: "sytti", 80: "åtti", 90: "nitti",
100: "ett hundre", 200: "to hundre", 300: "tre hundre", 400: "fire hundre",
500: "fem hundre", 600: "seks hundre", 700: "sju hundre",
800: "åtte hundre", 900: "ni hundre",
}
if theCode == "nn":
theWords[1] = "ein"
theWords[100] = "eitt hundre"
retVal = ""
hunWord = theWords.get(hunVal, "")
if tenVal == 10:
oneWord = theWords.get(oneVal+10, "")
sepVal = " og " if hunWord and oneWord else " "
retVal = f"{hunWord}{sepVal}{oneWord}"
else:
oneWord = theWords.get(oneVal, "")
if tenVal == 0:
sepVal = " og " if hunWord and oneWord else " "
retVal = f"{hunWord}{sepVal}{oneWord}".strip()
else:
tenWord = theWords.get(tenVal, "")
sepVal = " og " if hunWord and tenWord else " "
if oneVal == 0:
retVal = f"{hunWord}{sepVal}{tenWord}".strip()
else:
retVal = f"{hunWord}{sepVal}{tenWord}{oneWord}".strip()
return retVal.strip()
+1 -78
View File
@@ -22,7 +22,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import pytest
from nw.core.tools import countWords, numberToRoman, numberToWord
from nw.core.tools import countWords, numberToRoman
@pytest.mark.core
def testCoreTools_CountWords():
@@ -81,80 +81,3 @@ def testCoreTools_RomanNumbers():
assert numberToRoman(999, True) == "cmxcix"
# END Test testCoreTools_RomanNumbers
@pytest.mark.core
def testCoreTools_NumberWords():
"""Test the conversion of integer to English words.
"""
# English
assert numberToWord(0, "en") == "Zero"
assert numberToWord(1, "en") == "One"
assert numberToWord(2, "en") == "Two"
assert numberToWord(3, "en") == "Three"
assert numberToWord(4, "en") == "Four"
assert numberToWord(5, "en") == "Five"
assert numberToWord(6, "en") == "Six"
assert numberToWord(7, "en") == "Seven"
assert numberToWord(8, "en") == "Eight"
assert numberToWord(9, "en") == "Nine"
assert numberToWord(10, "en") == "Ten"
assert numberToWord(11, "en") == "Eleven"
assert numberToWord(12, "en") == "Twelve"
assert numberToWord(13, "en") == "Thirteen"
assert numberToWord(14, "en") == "Fourteen"
assert numberToWord(15, "en") == "Fifteen"
assert numberToWord(16, "en") == "Sixteen"
assert numberToWord(17, "en") == "Seventeen"
assert numberToWord(18, "en") == "Eighteen"
assert numberToWord(19, "en") == "Nineteen"
assert numberToWord(20, "en") == "Twenty"
assert numberToWord(21, "en") == "Twenty-One"
assert numberToWord(29, "en") == "Twenty-Nine"
assert numberToWord(42, "en") == "Forty-Two"
assert numberToWord(114, "en") == "One Hundred Fourteen"
assert numberToWord(142, "en") == "One Hundred Forty-Two"
assert numberToWord(999, "en") == "Nine Hundred Ninety-Nine"
# Norwegian
assert numberToWord(0, "nb") == "null"
assert numberToWord(1, "nb") == "én"
assert numberToWord(10, "nb") == "ti"
assert numberToWord(20, "nb") == "tjue"
assert numberToWord(21, "nb") == "tjueén"
assert numberToWord(29, "nb") == "tjueni"
assert numberToWord(42, "nb") == "førtito"
assert numberToWord(60, "nb") == "seksti"
assert numberToWord(100, "nb") == "ett hundre"
assert numberToWord(114, "nb") == "ett hundre og fjorten"
assert numberToWord(142, "nb") == "ett hundre og førtito"
assert numberToWord(999, "nb") == "ni hundre og nittini"
assert numberToWord(0, "nn") == "null"
assert numberToWord(1, "nn") == "ein"
assert numberToWord(10, "nn") == "ti"
assert numberToWord(20, "nn") == "tjue"
assert numberToWord(21, "nn") == "tjueein"
assert numberToWord(29, "nn") == "tjueni"
assert numberToWord(42, "nn") == "førtito"
assert numberToWord(60, "nn") == "seksti"
assert numberToWord(100, "nn") == "eitt hundre"
assert numberToWord(114, "nn") == "eitt hundre og fjorten"
assert numberToWord(142, "nn") == "eitt hundre og førtito"
assert numberToWord(999, "nn") == "ni hundre og nittini"
# Check complex language setting
assert numberToWord(1, "en_GB") == "One"
assert numberToWord(2, "en_GB") == "Two"
assert numberToWord(3, "en_GB") == "Three"
# Check a few with a nonsense language setting
assert numberToWord(1, "foo") == "1"
assert numberToWord(2, "foo") == "2"
assert numberToWord(3, "foo") == "3"
# Test out of range values
assert numberToWord(12345, "en") == "[>999]"
assert numberToWord(-2345, "en") == "[<0]"
assert numberToWord("234", "en") == "[NaN]"
# END Test testCoreTools_NumberWords