Skip to content

Commit

Permalink
Well, we support a bunch of the hourly cases correctly now...
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason LaPorte committed Jul 24, 2013
1 parent 52f74e5 commit c575d81
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 32 deletions.
19 changes: 19 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -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
48 changes: 30 additions & 18 deletions lib/lang/english.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
34 changes: 21 additions & 13 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions test-cases/english.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.":
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit c575d81

Please sign in to comment.