-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpronoun.js
73 lines (72 loc) · 3.16 KB
/
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function pronoun(str) {
var obj = {};
var arr = str.split("\n").join(" ").split(" ");
for (var i = 0; i < arr.length; i++) {
if (/^i$/i.test(arr[i])) {
obj["i"] === undefined ? (obj["i"] = {}) : null;
obj["i"]["count"] === undefined
? (obj["i"]["count"] = 1)
: obj["i"]["count"]++;
obj["i"]["word"] === undefined ? (obj["i"]["word"] = []) : null;
obj["i"]["word"].push(findNextWord(arr.slice(i)));
} else if (/^you$/i.test(arr[i])) {
obj["you"] === undefined ? (obj["you"] = {}) : null;
obj["you"]["count"] === undefined
? (obj["you"]["count"] = 1)
: obj["you"]["count"]++;
obj["you"]["word"] === undefined ? (obj["you"]["word"] = []) : null;
obj["you"]["word"].push(findNextWord(arr.slice(i)));
} else if (/^he$/i.test(arr[i])) {
obj["he"] === undefined ? (obj["he"] = {}) : null;
obj["he"]["count"] === undefined
? (obj["he"]["count"] = 1)
: obj["he"]["count"]++;
obj["he"]["word"] === undefined ? (obj["he"]["word"] = []) : null;
obj["he"]["word"].push(findNextWord(arr.slice(i)));
} else if (/^she$/i.test(arr[i])) {
obj["she"] === undefined ? (obj["she"] = {}) : null;
obj["she"]["count"] === undefined
? (obj["she"]["count"] = 1)
: obj["she"]["count"]++;
obj["she"]["word"] === undefined ? (obj["she"]["word"] = []) : null;
obj["she"]["word"].push(findNextWord(arr.slice(i)));
} else if (/^it$/i.test(arr[i])) {
obj["it"] === undefined ? (obj["it"] = {}) : null;
obj["it"]["count"] === undefined
? (obj["it"]["count"] = 1)
: obj["it"]["count"]++;
obj["it"]["word"] === undefined ? (obj["it"]["word"] = []) : null;
obj["it"]["word"].push(findNextWord(arr.slice(i)));
} else if (/^they$/i.test(arr[i])) {
obj["they"] === undefined ? (obj["they"] = {}) : null;
obj["they"]["count"] === undefined
? (obj["they"]["count"] = 1)
: obj["they"]["count"]++;
obj["they"]["word"] === undefined
? (obj["they"]["word"] = [])
: null;
obj["they"]["word"].push(findNextWord(arr.slice(i)));
} else if (/^we$/i.test(arr[i])) {
obj["we"] === undefined ? (obj["we"] = {}) : null;
obj["we"]["count"] === undefined
? (obj["we"]["count"] = 1)
: obj["we"]["count"]++;
obj["we"]["word"] === undefined ? (obj["we"]["word"] = []) : null;
obj["we"]["word"].push(findNextWord(arr.slice(i)));
}
}
for (var key in obj) {
obj[key]["word"] = obj[key]["word"].filter((x) => x !== undefined);
}
return obj;
}
function findNextWord(arr) {
var pronouns = /^(i|you|he|she|it|they|we)$/i;
for (var i = 1; i < arr.length; i++) {
if (pronouns.test(arr[i])) {
return;
} else {
return arr[i].replace(/,/, "");
}
}
}