From c575d816fa257d1eb062f3c7d97a562275b060c9 Mon Sep 17 00:00:00 2001 From: Jason LaPorte Date: Wed, 24 Jul 2013 16:25:40 -0400 Subject: [PATCH] Well, we support a bunch of the hourly cases correctly now... --- TODO | 19 ++++++++++++++++ lib/lang/english.js | 48 +++++++++++++++++++++++++---------------- lib/template.js | 34 ++++++++++++++++++----------- test-cases/english.json | 36 +++++++++++++++++++++++++++++++ test.js | 2 +- 5 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 TODO diff --git a/TODO b/TODO new file mode 100644 index 00000000..413658f9 --- /dev/null +++ b/TODO @@ -0,0 +1,19 @@ +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 991407cb..3fd81a4a 100644 --- a/lib/lang/english.js +++ b/lib/lang/english.js @@ -1,3 +1,15 @@ +function shared_prefix_length(a, b) { + var i = 0; + while(i !== a.length && i !== b.length && a.charCodeAt(i) === b.charCodeAt(i)) + ++i; + return i; +} + +function join_with_shared_prefix(a, b, joiner) { + var length = shared_prefix_length(a, b); + return a.slice(0, length) + a.slice(length) + joiner + b.slice(length); +} + module.exports = require("../template")({ "clear": "clear", "no-precipitation": "no precipitation", @@ -41,35 +53,35 @@ module.exports = require("../template")({ "tomorrow-afternoon": "tomorrow afternoon", "tomorrow-evening": "tomorrow evening", "tomorrow-night": "tomorrow night", - "morning": "morning", - "afternoon": "afternoon", - "evening": "evening", - "night": "night", + "morning": "in the morning", + "afternoon": "in the afternoon", + "evening": "in the evening", + "night": "overnight", "today": "today", "tomorrow": "tomorrow", - "sunday": "Sunday", - "monday": "Monday", - "tuesday": "Tuesday", - "wednesday": "Wednesday", - "thursday": "Thursday", - "friday": "Friday", - "saturday": "Saturday", + "sunday": "on Sunday", + "monday": "on Monday", + "tuesday": "on Tuesday", + "wednesday": "on Wednesday", + "thursday": "on Thursday", + "friday": "on Friday", + "saturday": "on Saturday", "minutes": "$1 min.", "fahrenheit": "$1\u00B0F", "celsius": "$1\u00B0C", - "and": "$1 and $2", + "and": function(a, b) { + return join_with_shared_prefix(a, b, " and "); + }, + "range": function(a, b) { + return join_with_shared_prefix(a, b, " through "); + }, "then": "$1, then $2", - "range": "$1 through $2", "clauses": function(one, two) { return one + (one.indexOf(",") === -1 && two.indexOf(",") === -1 ? ", " : "; ") + two; }, - "during": function(condition, day) { - return condition + - (day === "today" || day === "tomorrow" ? " " : " on ") + - day; - }, + "during": "$1 $2", "for-hour": "$1 for the hour", "for-day": "$1 throughout the day", "for-week": "$1 throughout the week", diff --git a/lib/template.js b/lib/template.js index f7a4b718..64eadcad 100644 --- a/lib/template.js +++ b/lib/template.js @@ -4,25 +4,33 @@ module.exports = function(template) { return expr.toString(); else if(typeof expr === "string") { - if(template.hasOwnProperty(expr)) + if(!template.hasOwnProperty(expr)) + throw new Error("\"" + expr + "\" not found in language template."); + + else return template[expr]; } else if(Array.isArray(expr)) { - if(template.hasOwnProperty(expr[0])) { - if(typeof template[expr[0]] === "string") - return template[expr[0]].replace(/\$\d+/g, function(n) { - return parse(expr[n.slice(1)|0]); - }); - - if(typeof template[expr[0]] === "function") - return template[expr[0]].apply(null, expr.slice(1).map(function(arg) { - return parse(arg); - })); - } + if(!template.hasOwnProperty(expr[0])) + throw new Error("\"" + expr[0] + "\" not found in language template."); + + else if(typeof template[expr[0]] === "string") + return template[expr[0]].replace(/\$\d+/g, function(n) { + return parse(expr[n.slice(1)|0]); + }); + + else if(typeof template[expr[0]] === "function") + return template[expr[0]].apply(null, expr.slice(1).map(function(arg) { + return parse(arg); + })); + + else + throw new Error("\"" + expr[0] + "\" is not a valid language template pattern."); } - throw new Error("Invalid expression."); + else + throw new Error("Invalid expression."); } return parse; diff --git a/test-cases/english.json b/test-cases/english.json index 3f14b8c7..2cac9761 100644 --- a/test-cases/english.json +++ b/test-cases/english.json @@ -57,6 +57,42 @@ ["starting-again-later", ["minutes", 8]]]], + "Mostly cloudy throughout the day.": + ["sentence", ["for-day", "medium-clouds"]], + + "Light sleet starting in the morning.": + ["sentence", ["starting", "very-light-sleet", "morning"]], + + "Windy until tonight.": + ["sentence", ["until", "medium-wind", "today-night"]], + + "Breezy in the afternoon.": + ["sentence", ["during", "light-wind", "afternoon"]], + + "Snow later this evening and tomorrow morning.": + ["sentence", ["during", + "medium-snow", + ["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-until", "night"]]], + + "Light sleet later this afternoon, then snow tomorrow morning.": + ["sentence", ["then", + ["during", "light-sleet", "later-today-afternoon"], + ["during", "medium-snow", "tomorrow-morning"]]], + + "Light sleet later this afternoon, then snow tomorrow morning.": + ["sentence", ["then", + ["during", "light-sleet", "later-today-afternoon"], + ["during", "medium-snow", "tomorrow-morning"]]], "No precipitation throughout the week, temperatures peaking at 85°F tomorrow.": diff --git a/test.js b/test.js index e573230f..8d350548 100644 --- a/test.js +++ b/test.js @@ -18,7 +18,7 @@ describe("translation", function() { var source = cases[summary]; it( - util.format("should translate %d to \"%s\"", source, summary), + util.format("should translate %j to \"%s\"", source, summary), function() { expect(translate(source)).to.equal(summary); }