Skip to content

Commit

Permalink
Yay! Tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason LaPorte committed Jul 25, 2013
1 parent 664abb9 commit 41d9aba
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 99 deletions.
19 changes: 0 additions & 19 deletions TODO

This file was deleted.

74 changes: 44 additions & 30 deletions lib/lang/english.js
Original file line number Diff line number Diff line change
@@ -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")({
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.) */
Expand Down
119 changes: 69 additions & 50 deletions test-cases/english.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
"Heavy Snow":
["title", "heavy-snow"],

"Windy":
["title", "medium-wind"],

"Dangerously Windy":
["title", "heavy-wind"],

Expand All @@ -29,9 +26,6 @@
"Dry":
["title", "low-humidity"],

"Foggy":
["title", "fog"],

"Humid and Partly Cloudy":
["title",
["and", "high-humidity", "light-clouds"]],
Expand All @@ -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.":
Expand All @@ -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"]],

Expand All @@ -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"]]]
}
3 changes: 3 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down

0 comments on commit 41d9aba

Please sign in to comment.