-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analysis): append ordinal suffix to numeric street names
- Loading branch information
1 parent
b96f7dd
commit 8c5555a
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const _ = require('lodash'); | ||
|
||
// The ordinal function replaces all numeric street names (ie. 30 street) | ||
// with a version including ordinals (ie. 30th street). | ||
// note: this is currently only configured for the English language | ||
|
||
function ordinals(opts) { | ||
return (tokens) => { | ||
|
||
// consider all but final token | ||
for (var o = 0; o < tokens.length-1; o++) { | ||
|
||
// token must be entirely numeric | ||
if (!tokens[o].isNumeric()) { continue; } | ||
|
||
// token must be followed by a street type token | ||
if (!_.has(opts.dict.streetTypes, _.toLower(tokens[o+1].body))) { continue; } | ||
|
||
// token must either be the leftmost token or be preceeded by a directional token | ||
if(o !== 0) { | ||
if (!_.has(opts.dict.directionalExpansions, _.toLower(tokens[o-1].body))) { | ||
continue; | ||
} | ||
} | ||
|
||
// append the english ordinal suffix | ||
tokens[o].body += englishOrdinalSuffix(tokens[o].body); | ||
|
||
// maximum of one replacement | ||
break; | ||
} | ||
|
||
return tokens; | ||
}; | ||
} | ||
|
||
function englishOrdinalSuffix(i) { | ||
const j = i % 10, k = i % 100; | ||
if (j === 1 && k !== 11) { return 'st'; } | ||
if (j === 2 && k !== 12) { return 'nd'; } | ||
if (j === 3 && k !== 13) { return 'rd'; } | ||
return 'th'; | ||
} | ||
|
||
module.exports = ordinals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters