From 41d9aba40ab06b1e897d46df62251730c8562e7a Mon Sep 17 00:00:00 2001 From: Jason LaPorte Date: Thu, 25 Jul 2013 16:09:33 -0400 Subject: [PATCH] Yay! Tests pass. --- TODO | 19 ------- lib/lang/english.js | 74 +++++++++++++++---------- test-cases/english.json | 119 +++++++++++++++++++++++----------------- test.js | 3 + 4 files changed, 116 insertions(+), 99 deletions(-) delete mode 100644 TODO diff --git a/TODO b/TODO deleted file mode 100644 index 413658f9..00000000 --- a/TODO +++ /dev/null @@ -1,19 +0,0 @@ -we need to test a few more "X, then Y" cases, where X and Y are any of the -below: - - (starting X Y) - (until X Y) - (during X Y) - (during X (and Y Z)) - (clauses (until X Y) (starting-again (in Z))) - (clauses (starting X Y) (continuing-until Z)) - -The following periods still need to get tested: - -today-morning -today-afternoon -today-night -later-today-night -tomorrow-afternoon -tomorrow-evening -tomorrow-night diff --git a/lib/lang/english.js b/lib/lang/english.js index fccc450a..3928144b 100644 --- a/lib/lang/english.js +++ b/lib/lang/english.js @@ -1,14 +1,19 @@ -function join_with_shared_prefix(joiner) { - return function(a, b) { - var i = 0; +function join_with_shared_prefix(a, b, joiner) { + var i = 0; - while(i !== a.length && - i !== b.length && - a.charCodeAt(i) === b.charCodeAt(i)) - ++i; + while(i !== a.length && + i !== b.length && + a.charCodeAt(i) === b.charCodeAt(i)) + ++i; - return a.slice(0, i) + a.slice(i) + joiner + b.slice(i); - }; + return a.slice(0, i) + a.slice(i) + joiner + b.slice(i); +} + +function strip_prefix(period) { + return period.slice(0, 9) === "overnight" ? period.slice(4) : + period.slice(0, 7) === "in the " ? period.slice(7) : + period.slice(0, 3) === "on " ? period.slice(3) : + period; } module.exports = require("../template")({ @@ -41,7 +46,6 @@ module.exports = require("../template")({ "light-clouds": "partly cloudy", "medium-clouds": "mostly cloudy", "heavy-clouds": "overcast", - "temperatures": "temperatures", "today-morning": "this morning", "later-today-morning": "later this morning", "today-afternoon": "this afternoon", @@ -70,31 +74,41 @@ module.exports = require("../template")({ "minutes": "$1 min.", "fahrenheit": "$1\u00B0F", "celsius": "$1\u00B0C", - "and": join_with_shared_prefix(" and "), - "range": join_with_shared_prefix(" through "), - "then": "$1, then $2", - "clauses": function(one, two) { - return one + - (one.indexOf(",") === -1 && two.indexOf(",") === -1 ? ", " : "; ") + - two; + "and": function(a, b) { + return join_with_shared_prefix( + a, + b, + a.indexOf(",") !== -1 ? ", and " : " and " + ); }, - "during": "$1 $2", + "through": function(a, b) { + return join_with_shared_prefix(a, b, " through "); + }, + "with": "$1, with $2", "for-hour": "$1 for the hour", + "starting-in": "$1 starting in $2", + "stopping-in": "$1 stopping in $2", + "starting-then-stopping-later": "$1 starting in $2, stopping $3 later", + "stopping-then-starting-later": "$1 stopping in $2, starting again $3 later", "for-day": "$1 throughout the day", + "starting": "$1 starting $2", + "until": function(condition, period) { + return condition + " until " + strip_prefix(period); + }, + "until-starting-again": function(condition, a, b) { + return condition + " until " + strip_prefix(a) + ", starting again " + b; + }, + "starting-continuing-until": function(condition, a, b) { + return condition + " starting " + a + ", continuing until " + + strip_prefix(b); + }, + "during": "$1 $2", "for-week": "$1 throughout the week", "over-weekend": "$1 over the weekend", - "continuing": "continuing $1", - "starting-again": "starting again $1", - "starting-again-later": "starting again $1 later", - "stopping-later": "stopping $1 later", - "starting": "$1 starting $2", - "starting-in": "$1 starting in $2", - "stopping-in": "$1 stopping in $2", - "until": "$1 until $2", - "peaking": "$1 peaking at $2", - "rising": "$1 rising to $2", - "valleying": "$1 bottoming out at $2", - "falling": "$1 falling to $2", + "temperatures-peaking": "temperatures peaking at $1 $2", + "temperatures-rising": "temperatures rising to $1 $2", + "temperatures-valleying": "temperatures bottoming out at $1 $2", + "temperatures-falling": "temperatures falling to $1 $2", /* Capitalize the first letter of every word, except if that word is * "and". (This is a very crude bastardization of proper English titling * rules, but it is adequate for the purposes of this module.) */ diff --git a/test-cases/english.json b/test-cases/english.json index 4e4ce5c6..06413aa5 100644 --- a/test-cases/english.json +++ b/test-cases/english.json @@ -17,9 +17,6 @@ "Heavy Snow": ["title", "heavy-snow"], - "Windy": - ["title", "medium-wind"], - "Dangerously Windy": ["title", "heavy-wind"], @@ -29,9 +26,6 @@ "Dry": ["title", "low-humidity"], - "Foggy": - ["title", "fog"], - "Humid and Partly Cloudy": ["title", ["and", "high-humidity", "light-clouds"]], @@ -47,14 +41,18 @@ ["sentence", ["stopping-in", "light-rain", ["minutes", 15]]], "Heavy sleet starting in 20 min., stopping 30 min. later.": - ["sentence", ["clauses", - ["starting-in", "heavy-sleet", ["minutes", 20]], - ["stopping-later", ["minutes", 30]]]], + ["sentence", + ["starting-then-stopping-later", + "heavy-sleet", + ["minutes", 20], + ["minutes", 30]]], "Rain stopping in 25 min., starting again 8 min. later.": - ["sentence", ["clauses", - ["stopping-in", "medium-rain", ["minutes", 25]], - ["starting-again-later", ["minutes", 8]]]], + ["sentence", + ["stopping-then-starting-later", + "medium-rain", + ["minutes", 25], + ["minutes", 8]]], "Mostly cloudy throughout the day.": @@ -66,6 +64,9 @@ "Windy until tonight.": ["sentence", ["until", "medium-wind", "today-night"]], + "Foggy until afternoon.": + ["sentence", ["until", "fog", "afternoon"]], + "Breezy in the afternoon.": ["sentence", ["during", "light-wind", "afternoon"]], @@ -75,60 +76,78 @@ ["and", "later-today-evening", "tomorrow-morning"]]], "Snow until later this morning, starting again this evening.": - ["sentence", ["clauses", - ["until", "medium-snow", "later-today-morning"], - ["starting-again", "today-evening"]]], - - "Overcast starting in the evening, continuing overnight.": - ["sentence", ["clauses", - ["starting", "heavy-clouds", "evening"], - ["continuing", "night"]]], + ["sentence", ["until-starting-again", + "medium-snow", + "later-today-morning", + "today-evening"]], - "Light sleet later this afternoon, then snow tomorrow morning.": - ["sentence", ["then", - ["during", "light-sleet", "later-today-afternoon"], - ["during", "medium-snow", "tomorrow-morning"]]], + "Overcast starting in the evening, continuing until night.": + ["sentence", ["starting-continuing-until", + "heavy-clouds", + "evening", + "night"]], - "Light sleet later this afternoon, then snow tomorrow morning.": - ["sentence", ["then", + "Light sleet later this afternoon and foggy tomorrow morning.": + ["sentence", ["and", ["during", "light-sleet", "later-today-afternoon"], - ["during", "medium-snow", "tomorrow-morning"]]], - - - "No precipitation throughout the week, temperatures peaking at 85°F tomorrow.": - ["sentence", ["clauses", + ["during", "fog", "tomorrow-morning"]]], + + "Foggy starting this morning, continuing until this afternoon, and windy tomorrow morning.": + ["sentence", ["and", + ["starting-continuing-until", + "fog", + "today-morning", + "today-afternoon"], + ["during", "medium-wind", "tomorrow-morning"]]], + + "Overcast starting later tonight and snow tomorrow afternoon.": + ["sentence", ["and", + ["starting", "heavy-clouds", "later-today-night"], + ["during", "medium-snow", "tomorrow-afternoon"]]], + + "Dry tonight and rain starting tomorrow evening, continuing until tomorrow night.": + ["sentence", ["and", + ["during", "low-humidity", "today-night"], + ["starting-continuing-until", + "medium-rain", + "tomorrow-evening", + "tomorrow-night"]]], + + + "No precipitation throughout the week, with temperatures peaking at 85°F tomorrow.": + ["sentence", ["with", ["for-week", "no-precipitation"], - ["during", - ["peaking", "temperatures", ["fahrenheit", 85]], + ["temperatures-peaking", + ["fahrenheit", 85], "tomorrow"]]], - "Mixed precipitation over the weekend, temperatures rising to 32°C on Thursday.": - ["sentence", ["clauses", + "Mixed precipitation over the weekend, with temperatures rising to 32°C on Thursday.": + ["sentence", ["with", ["over-weekend", "mixed-precipitation"], - ["during", - ["rising", "temperatures", ["celsius", 32]], + ["temperatures-rising", + ["celsius", 32], "thursday"]]], - "Drizzle today, temperatures bottoming out at 15°F on Friday.": - ["sentence", ["clauses", + "Drizzle today, with temperatures bottoming out at 15°F on Friday.": + ["sentence", ["with", ["during", "very-light-rain", "today"], - ["during", - ["valleying", "temperatures", ["fahrenheit", 15]], + ["temperatures-valleying", + ["fahrenheit", 15], "friday"]]], - "Light snow on Monday and Tuesday, temperatures falling to 0°C on Sunday.": - ["sentence", ["clauses", + "Light snow on Monday and Tuesday, with temperatures falling to 0°C on Sunday.": + ["sentence", ["with", ["during", "light-snow", ["and", "monday", "tuesday"]], - ["during", - ["falling", "temperatures", ["celsius", 0]], + ["temperatures-falling", + ["celsius", 0], "sunday"]]], - "Precipitation on Wednesday through Saturday, temperatures peaking at 100°F on Monday.": - ["sentence", ["clauses", + "Precipitation on Wednesday through Saturday, with temperatures peaking at 100°F on Monday.": + ["sentence", ["with", ["during", "medium-precipitation", - ["range", "wednesday", "saturday"]], - ["during", - ["peaking", "temperatures", ["fahrenheit", 100]], + ["through", "wednesday", "saturday"]], + ["temperatures-peaking", + ["fahrenheit", 100], "monday"]]] } diff --git a/test.js b/test.js index 8d350548..18bb2e3d 100644 --- a/test.js +++ b/test.js @@ -6,6 +6,9 @@ var expect = require("chai").expect, describe("translation", function() { fs.readdirSync(path.join(__dirname, "test-cases")).forEach(function(lang) { + if(lang.charAt(0) === ".") + return; + var name = path.basename(lang, ".json"), translate = translation[name];