-
Notifications
You must be signed in to change notification settings - Fork 0
/
pronoun.js
24 lines (22 loc) · 859 Bytes
/
pronoun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function pronoun(str) {
const pronouns = ['i', 'you', 'he', 'she', 'it', 'they', 'we'];
const words = str.toLowerCase().split(/\s+/);
const result = {};
words.forEach((word, index) => {
const cleanWord = word.replace(/[^a-z]/g, '');
if (pronouns.includes(cleanWord)) {
if (!result[cleanWord]) {
result[cleanWord] = { word: [], count: 0 };
}
result[cleanWord].count++;
const nextWord = words[index + 1];
if (nextWord) {
const cleanNextWord = nextWord.replace(/[^a-z]/g, '');
if (cleanNextWord && !pronouns.includes(cleanNextWord) && !result[cleanWord].word.includes(cleanNextWord)) {
result[cleanWord].word.push(cleanNextWord);
}
}
}
});
return result;
}