eletrotupi / tcc/ api/src/utils/emotions.ts3889890d76a27e3bf87944b2dd355476c36bb4cf
2.5 KB | 102 lines Raw
1
// Emotion types enum
2
export const Emotions = {
3
  JOY: 'joy',
4
  TRUST: 'trust',
5
  FEAR: 'fear',
6
  SURPRISE: 'surprise',
7
  SAD: 'sad',
8
  DISGUST: 'disgust',
9
  ANGRY: 'angry',
10
  ANXIETY: 'anxiety'
11
};
12

13
// Valid emotion values for validation
14
export const VALID_EMOTIONS = Object.values(Emotions);
15

16
// Validate emotion type
17
export function isValidEmotion(emotion) {
18
  return VALID_EMOTIONS.includes(emotion?.toLowerCase());
19
}
20

21
// Validate intensity value
22
export function isValidIntensity(intensity) {
23
  const num = Number(intensity);
24
  return !isNaN(num) && num >= 1 && num <= 10;
25
}
26

27
// Validate rating value
28
export function isValidRating(rating) {
29
  const num = Number(rating);
30
  return !isNaN(num) && num >= 1 && num <= 10;
31
}
32

33
// Validate level values (stress, anxiety, energy)
34
export function isValidLevel(level) {
35
  const num = Number(level);
36
  return !isNaN(num) && num >= 1 && num <= 10;
37
}
38

39
// Validate mood components array
40
export function validateMoodComponents(components) {
41
  if (!Array.isArray(components)) {
42
    return { valid: false, error: 'Components must be an array' };
43
  }
44

45
  const emotions = new Set();
46

47
  for (const component of components) {
48
    if (!Object.hasOwn(component, 'emotion') || !Object.hasOwn(component, 'intensity')) {
49
      return { valid: false, error: 'Each component must have emotion and intensity' };
50
    }
51

52
    const emotionLower = component.emotion.toLowerCase();
53

54
    if (!isValidEmotion(emotionLower)) {
55
      return { valid: false, error: `Invalid emotion: ${component.emotion}` };
56
    }
57

58
    if (!isValidIntensity(component.intensity)) {
59
      return { valid: false, error: `Invalid intensity for ${component.emotion}: must be 1-10` };
60
    }
61

62
    if (emotions.has(emotionLower)) {
63
      return { valid: false, error: `Duplicate emotion: ${component.emotion}` };
64
    }
65

66
    emotions.add(emotionLower);
67
  }
68

69
  return { valid: true };
70
}
71

72
// Get emotion statistics
73
export function calculateEmotionStats(moodComponents) {
74
  const stats = {
75
    dominant: null,
76
    average: 0,
77
    breakdown: {}
78
  };
79

80
  if (!moodComponents || moodComponents.length === 0) {
81
    return stats;
82
  }
83

84
  let maxIntensity = 0;
85
  let totalIntensity = 0;
86

87
  moodComponents.forEach(component => {
88
    const intensity = Number(component.intensity);
89
    totalIntensity += intensity;
90

91
    stats.breakdown[component.emotion] = intensity;
92

93
    if (intensity > maxIntensity) {
94
      maxIntensity = intensity;
95
      stats.dominant = component.emotion;
96
    }
97
  });
98

99
  stats.average = totalIntensity / moodComponents.length;
100

101
  return stats;
102
}