Skip to content

Commit

Permalink
Keep capital letter if typer by user.
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekplus committed Dec 15, 2023
1 parent 3d235b6 commit 5d32faa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/presageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,13 @@ class PresageHandler {

/**
* Checks if auto capitalization should be applied based on the input tokens and punctuation marks.
* @param {string[]} tokensArray - The array of input tokens.
* @param {string[]} lastWord - Last Word of input
* @param {string[]} wordCount - Word count in the sentence
* @param {boolean} newSentence - Indicates if the input includes a new sentence.
* @param {boolean} endsWithSpace - Indicates if the input ends with a whitespace.
* @returns {Capitalization} The type of capitalization to be applied.
*/
checkAutoCapitalize(tokensArray, newSentence, endsWithSpace) {
const lastWord = tokensArray.length
? tokensArray[tokensArray.length - 1]
: "";
checkAutoCapitalize(lastWord, wordCount, newSentence, endsWithSpace) {
const firstCharacterOfLastWord = lastWord.slice(0, 1);

// Check for whole word capitalization
Expand Down Expand Up @@ -360,8 +358,8 @@ class PresageHandler {
if (
this.autoCapitalize &&
newSentence &&
((!endsWithSpace && tokensArray.length === 1) ||
(endsWithSpace && tokensArray.length === 0))
((!endsWithSpace && wordCount === 1) ||
(endsWithSpace && wordCount === 0))
)
return Capitalization.FirstLetter;

Expand All @@ -388,6 +386,11 @@ class PresageHandler {
if (!endsWithSpace && lastWord.length < this.minWordLengthToPredict)
return false;

console.log(
lastWord,
(lastWord.match(this.separatorCharRegEx) || []).length,
(lastWord.match(this.keepPredCharRegEx) || []).length
);
// If the last word includes a separator character, disable prediction
if (
!endsWithSpace &&
Expand Down Expand Up @@ -424,7 +427,10 @@ class PresageHandler {
// Replace additional separators with spaces, if necessary
const additionalSeparatorRegex = LANG_ADDITIONAL_SEPERATOR_REGEX[language];
if (additionalSeparatorRegex) {
predictionInput = predictionInput.replace(additionalSeparatorRegex, " ");
predictionInput = predictionInput.replaceAll(
RegExp(additionalSeparatorRegex, "g"),
" "
);
}

// Split the input string into an array of words, removing whitespace and empty strings
Expand Down Expand Up @@ -455,7 +461,8 @@ class PresageHandler {

// Check if auto-capitalization should be performed
const doCapitalize = this.checkAutoCapitalize(
wordArray,
lastWord,
wordArray.length,
newSentence,
endsWithSpace
);
Expand Down
4 changes: 4 additions & 0 deletions tests/presageHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ describe("features", () => {
[" xyz. xyz", false, "out"],
[" xyz. ", true, "Out"],
[" xyz. ", false, "out"],
["\"Xyz", false, "Out"],

Check failure on line 158 in tests/presageHandler.test.js

View workflow job for this annotation

GitHub Actions / build

Replace `"\"Xyz"` with `'"Xyz'`
["\"xyz", false, "out"],

Check failure on line 159 in tests/presageHandler.test.js

View workflow job for this annotation

GitHub Actions / build

Replace `"\"xyz"` with `'"xyz'`
["\"Xyz", true, "Out"],

Check failure on line 160 in tests/presageHandler.test.js

View workflow job for this annotation

GitHub Actions / build

Replace `"\"Xyz"` with `'"Xyz'`
["\"xyz", true, "out"],

Check failure on line 161 in tests/presageHandler.test.js

View workflow job for this annotation

GitHub Actions / build

Replace `"\"xyz"` with `'"xyz'`
])(
"input: '%s', autoCapitalize: %s, expected: '%s'",
(input, autoCapitalize, expected) => {
Expand Down

0 comments on commit 5d32faa

Please sign in to comment.