UNPKG

compromise

Version:

modest natural language processing

41 lines (39 loc) • 1.26 kB
const isAcronym = /[ .][A-Z]\.? *$/i //asci - 'n.s.a.' const hasEllipse = /(?:\u2026|\.{2}) *$/ // '...' const hasLetter = /\p{L}/u const hasPeriod = /\. *$/ const leadInit = /^\p{Lu}\. $/u // "W. Kensington" // an initial in any alphabet - 'А. С. Пушкин' const isInitial = /[ .]\p{Lu}\. *$/u /** does this look like a sentence? - str can be the end of it, if foundLetter is given */ const isSentence = function (str, abbrevs, foundLetter = hasLetter.test(str)) { // must have a letter if (foundLetter === false) { return false } // check for 'F.B.I.' if (isAcronym.test(str) || isInitial.test(str)) { return false } // check for leading initial - "W. Kensington" if (str.length === 3 && leadInit.test(str)) { return false } //check for '...' if (hasEllipse.test(str)) { return false } const txt = str.replace(/[.!?\u203D\u2E18\u203C\u2047-\u2049] *$/, '') const words = txt.split(' ') const lastWord = words[words.length - 1].toLowerCase() // check for 'Mr.' (and not mr?) if (Object.hasOwn(abbrevs, lastWord) && hasPeriod.test(str)) { return false } // //check for jeopardy! // if (blacklist.hasOwnProperty(lastWord)) { // return false // } return true } export default isSentence