Navigation Menu

Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 1164421 - autocorrect im to I'm not in
Browse files Browse the repository at this point in the history
address review comments
  • Loading branch information
David Flanagan committed May 22, 2015
1 parent d3d9e55 commit 79e987f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
46 changes: 46 additions & 0 deletions apps/keyboard/js/imes/latin/latin.js
Expand Up @@ -762,6 +762,52 @@
return;
}

//
// If there was an exact match for the user input in the suggestions
// it has already been removed. Now we need to loop through again
// looking for approximate matches and bump those to the start of the
// list. A word is an approximate match if all the letters match
// when we ignore case, apostrophes and hyphens. The assumption here
// is that the user is not expected to have to use the shift key or
// the alternate forms menu or the punctuation menus. If they type all
// the right letters, a matching word should have higher priority than
// a non-matching word. In particular this code ensures that "im" gets
// autocorrected to "I'm" instead of "in" and "id" gets autocorrected to
// "I'd" instead of "is". (Bug 1164421)
//
// XXX In English, apostrophes and hyphens are the only punctuation that
// commonly occurs within words. If other wordlists include other
// punctuation, we may need to add them to the regular expression below.
//
// XXX In the future, we might want to generalize this to consider
// accented forms when looking for approximate matches. If "jose"
// was autocorrecting to "hose" instead of "José", for example, then
// we might need to extend the definition of an approximate match here.
// For now, I don't have any examples of corrections that are coming
// out incorrectly for accented letters, though, so am keeping this
// simple.
//
// We loop backward through the suggestions, bumping any approximate
// match to the start of the list. This ensures that the highest ranked
// approximate match comes before lower ranked approximate matches,
// and all approximate matches come before non-matching suggestions.
//
var i = suggestions.length - 1;
var approximateMatches = 0; // how many approximate matches we've bumped
var lcInput = input.toLowerCase();
while(i > approximateMatches) {
if (suggestions[i][0].toLowerCase().replace(/[-']/g, '') === lcInput) {
// If this suggestion was an approximate match for the users input..
var match = suggestions[i]; // get the match,
suggestions.splice(i, 1); // remove it,
suggestions.unshift(match); // and put it back at the start.
approximateMatches++; // Increment this instead of i-- here.
}
else {
i--; // if not an approximate match move to next suggestion
}
}

// Make sure we have no more than three words
if (suggestions.length > 3) {
// We want to keep at least a user dictionary word here (if the heighest
Expand Down
26 changes: 26 additions & 0 deletions apps/keyboard/test/unit/imes/latin/latin_test.js
Expand Up @@ -508,6 +508,32 @@ suite('latin.js', function() {
}).then(done, done);
});

test('Input "im" should autocorrect to "I\'m", not "in"',
function(done) {
activateAndTestPrediction('im', 'im', [
['in', 21],
['I\'m', 16],
['km', 9],
['um', 9]
]).then(function() {
sinon.assert.calledWith(
glue.sendCandidates, ['*I\'m', 'in', 'km']);
}).then(done, done);
});

test('Input "id" should autocorrect to "I\'d", not "is"',
function(done) {
activateAndTestPrediction('id', 'id', [
['is', 20],
['I\'d', 19],
['if', 17],
['ID', 16]
]).then(function() {
sinon.assert.calledWith(
glue.sendCandidates, ['*I\'d', 'ID', 'is']);
}).then(done, done);
});

test('Space to accept suggestion', function(done) {
activateAndTestPrediction('jan', 'jan', [
['Jan'],
Expand Down

0 comments on commit 79e987f

Please sign in to comment.