-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.js
117 lines (107 loc) · 3.18 KB
/
answer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict'
module.exports = {
buildEmotionsAnswer: (emotions, gender) => {
let sortedEmotions = processEmotions(emotions);
if (sortedEmotions.length == 1) {
let emotionDescription = sortedEmotions[0][0].singleDesc
return resolveGenderPlaceholders(emotionDescription, gender)
}
let answer = "I think they seem to be "
sortedEmotions.map(em => {
let topEmotion = em[0].name
if (em == sortedEmotions[0]){
answer += topEmotion // first element
} else if (em == sortedEmotions[sortedEmotions.length - 1]) {
answer += ` and ${topEmotion}.` // last element{
} else {
answer += ", " + topEmotion
}
})
return answer
},
buildAgeAnswer: (faces, gender) => {
let answer = faces.length > 1 ? "They " : resolveGenderPlaceholders("<heshe> ", gender)
answer += "could be "
for(let i=0; i < faces.length; i++) {
answer += faces[i].age
answer += i != (faces.length - 1) ? " and " : ""
}
return answer + " years old"
},
buildDescriptionAnswer: (description) => {
let desc = description.captions[0].text
let answer = "That looks like "
if (desc[0] == "a" && desc[1] == " ") {
return answer + desc
} else {
return answer + "a " + desc
}
},
}
function resolveGenderPlaceholders(text, gender) {
let heshe
if (text[text.indexOf("<heshe>") - 2] === "." || text.indexOf("<heshe>") === 0) {
heshe = gender == "Male" ? "He" : "She"
} else {
heshe = gender == "Male" ? "he" : "she"
}
text = text.replace("<heshe>", heshe)
let himher
if (text[text.indexOf("<himher>") - 2] === "." || text.indexOf("<himher>") === 0) {
himher = gender == "Male" ? "Him" : "Her"
} else {
himher = gender == "Male" ? "him" : "her"
}
text = text.replace("<himher>", himher)
return text
}
function processEmotions(emotions) {
let sortedEmotions = []
emotions.map(em => {
const emotion = em.faceAttributes.emotion
let ems = []
ems.push({
name: "angry",
singleDes: "Woah. What did you do to make <himher> so angry? 😡",
value: emotion.anger
})
ems.push({
name: "contempt",
singleDesc: "Pfff. <heshe> is obiously better then you! 😒",
value: emotion.contempt
})
ems.push({
name: "disgusted",
singleDesc: "Wüah! <heshe> looks disgusted! 😖",
value: emotion.disgust
})
ems.push({
name: "in fear",
singleDesc: "<heshe> is in fear. 😱 Help <himher>!",
value: emotion.fear
})
ems.push({
name: "happy",
singleDesc: "Haha. <heshe> looks happy! 😃",
value: emotion.happiness })
ems.push({
name: "neutral",
singleDesc: "<heshe> does not show any particular emotion. 😐",
value: emotion.neutral
})
ems.push({
name: "sad",
singleDes: "Oh no. <heshe> he is sad. 😢",
value: emotion.sadness
})
ems.push({
name: "surprised",
singleDesc: "BOO! <heshe> is surpised! 😲",
value: emotion.surprise
})
ems = ems.sort((a, b) => b.value - a.value)
sortedEmotions.push(ems)
console.log("sorted", ems)
})
return sortedEmotions;
}