On visual inspection we can see two contractions and an improper use of the ndash. There is not supposed to be white space around an emdash
. Since we are only counting words and sentence length, we could also remove the double quotes. We can manually replace these…
sampleText = exampleText.replace("It's been", "It has been").replace("What's", "What is").replace(" - ", ": ").replace(/"/g, '')
The colon is the grammatical equivalent of an emdash.
That gives us:
'It has been said that everybody loves Raymond. However, new studies suggest that Raymond is less well liked than we previously assumed: of out a hundred applicants, only eighteen individuals responded to a question about their love for Raymond positively! Almost half of applicants requested more information, asking Who the ■■■■ is this Raymond guy? What is so great about him? Why should I give him my heart so readily? More on this emerging story at eight.'
Now we want to split on full stop characters to break out the sentences.
sentenceArray = exampleText.split(/[.:!?]/g)
That will give us eight sentences of which one is empty. We can break out the words into their own arrays and trim any residual white space…
sentenceArrays = []
sentenceArray.forEach(function (sentence) {
this.push(sentence.trim().split(' '))
}, sentenceArrays)
which gives us,
[
['It', 'has', 'been', 'said', 'that', 'everybody', 'loves', 'Raymond']
['However,', 'new', 'studies', 'suggest', 'that', 'Raymond', 'is', 'less', 'well', 'liked', 'than', 'we', 'previously', 'assumed']
['of', 'out', 'a', 'hundred', 'applicants,', 'only', 'eighteen', 'individuals', 'responded', 'to', 'a', 'question', 'about', 'their', 'love', 'for', 'Raymond', 'positively']
['Almost', 'half', 'of', 'applicants', 'requested', 'more', 'information,', 'asking', 'Who', 'the', '■■■■', 'is', 'this', 'Raymond', 'guy']
['What', 'is', 'so', 'great', 'about', 'him']
['Why', 'should', 'I', 'give', 'him', 'my', 'heart', 'so', 'readily']
['More', 'on', 'this', 'emerging', 'story', 'at', 'eight']
['']
]
We can discard the last element…
sentenceArrays.pop()
Now we can build a length index…
lenCounts = sentenceArrays.map((x, i) => [x.length, i])
which gives,
[
[8, 0]
[14, 1]
[18, 2]
[15, 3]
[6, 4]
[9, 5]
[7, 6]
]
To find the longest sentence we just need to sort, and the one at the end will give us both index and length.
lenCounts.sort(function(a, b) {return a[0] - b[0]})
which gives us,
[
[6, 4]
[7, 6]
[8, 0]
[9, 5]
[14, 1]
[15, 3]
[18, 2]
]
The longest sentence is,
longest = lenCounts.slice(-1)
console.log(longest)
// [18, 2]
console.log(sentenceArray[2]])