Home > Back-end >  Sentence case tool - Not all sentences start with capital letter
Sentence case tool - Not all sentences start with capital letter

Time:08-15

could you look at this code. I need to fix the Sentence Case option. The first letter of each sentence should be capitalized.

e.g. This is test. This is test. This is test.

The problem is that only the first sentence starts with a capital letter and other sentences do not.

https://github.com/freewarelovers/CaseConverter


// LowerCase, Title And Sentence Case Converter Tool

var stringbox = document.getElementById('stringbox')
var wordcountspan = document.getElementById('wordcount')
var charcountspan = document.getElementById('charcount')

function convertstring(textarea, action) {
  if (action == 'sentencecase') {
    textarea.value = sentenceCase(textarea.value)
  } else if (action == 'titlecase') {
    textarea.value = toTitleCase(textarea.value)
  } else if (action == 'capitalcase') {
    textarea.value = CapitalCase(textarea.value)
  } else if (action == 'lowercase') {
    textarea.value = lowerCase(textarea.value)
  } else if (action == 'uppercase') {
    textarea.value = upperCase(textarea.value)
  }
  return false
}

function sentenceCase(str) {
  var str = str.toLowerCase().replace(/\si\s/g, ' I ');
  str = str.charAt(0).toUpperCase()   str.slice(1);
  return str
}

//reference: https://github.com/gouch/to-title-case
function toTitleCase(str) {
  var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
  var str = str.toLowerCase()
  return str.replace(/[A-Za-z0-9\u00C0-\u00FF] [^\s-]*/g, function(match, index, title) {
    if (index > 0 && index   match.length !== title.length &&
      match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
      (title.charAt(index   match.length) !== '-' || title.charAt(index - 1) === '-') &&
      title.charAt(index - 1).search(/[^\s-]/) < 0) {
      return match.toLowerCase();
    }

    if (match.substr(1).search(/[A-Z]|\../) > -1) {
      return match;
    }

    return match.charAt(0).toUpperCase()   match.substr(1);
  });
};

//reference: https://medium.freecodecamp.com/three-ways-to-title-case-a-sentence-in-javascript-676a9175eb27
function CapitalCase(str) {
  return str.toLowerCase().split(' ').map(function(word) {
    return (word.charAt(0).toUpperCase()   word.slice(1));
  }).join(' ');
}

function lowerCase(str) {
  return str.toLowerCase();
}

function upperCase(str) {
  return str.toUpperCase();
}

function wordandcharcount() {
  wordcountspan.innerHTML = stringbox.value.split(' ').length
  charcountspan.innerHTML = stringbox.value.length
}


stringbox.addEventListener('input', function() {
  wordandcharcount()
}, false)

wordandcharcount()
<textarea id="stringbox"></textarea>
<span id="wordcount"></span>
<span id="charcount"></span>

CodePudding user response:

I assume a sentence always begins at the start of the input string or immediately after a period.

var text = "this is a test. this is a test. this is a test.";
text = text.replace(/(^|\.\s*)([a-z])/g, function(match,c1,c2,offset,str) {
    return c1   c2.toUpperCase();
});
console.log(text)

This will output This is a test. This is a test. This is a test.

CodePudding user response:

Here is a way to do it.

const sentence = "this is talha. i am 8. i am a boy. I like to play soccer";

//  convert to an array
const arr = sentence.split(".");

// capitalize first word for each sentence
let capitalized = arr.map((str, index) => {
  if (index === arr.length - 1) {
    str = str.trim().charAt(0).toUpperCase()   str.trim().slice(1);
    return (str  = " ");
  }
  str = str.trim().charAt(0).toUpperCase()   str.trim().slice(1)   ".";
  return (str  = " ");
});

// convert back to string and log
console.log(capitalized.join(""));
  • Related