Skip to content

Commit

Permalink
Merge pull request #77 from nkc-ug/feater/firestore
Browse files Browse the repository at this point in the history
Feater/firestore
  • Loading branch information
Mount-Book authored Aug 27, 2023
2 parents 8a439e5 + fdd7632 commit 88fcad3
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions src/components/getEmotionApi.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,69 @@
import axios from 'axios';
import { EmotionDataType } from '../types/EmotionDataType';
import { addWordEmotions } from '../api/addWordEmotions';
import { getWordEmotions } from '../api/getWordEmotions';

type emotionDataType = EmotionDataType;
type addWordObjectType = {
text: string;
emotionData: EmotionDataType;
};

export const getEmotionApi = async (text: string, emotionData: emotionDataType) => {
let ans = {} as emotionDataType;
let updateEmotionData = {} as emotionDataType;
let newEmotionData = Object.assign({},emotionData);
let maxEmotion = '';
let maxScore = -Infinity;
updateEmotionData = emotionData;
let maxScore = 0;

const fetchAPI = async () => {
const res = await axios.get(`https://callgpt-f6bkalktuq-uc.a.run.app?text=${text}`);
const fetchEmotionData = res.data as emotionDataType;
ans = {

newEmotionData = {
Joy: Number(fetchEmotionData.Joy),
Anger: Number(fetchEmotionData.Anger),
Sorrow: Number(fetchEmotionData.Sorrow),
Enjoyable: Number(fetchEmotionData.Enjoyable),
emoId: 0,
};
};
await fetchAPI();

for (const emotion in ans) {
if (ans[emotion as keyof emotionDataType] > maxScore) {
maxEmotion = emotion;
maxScore = ans[emotion as keyof emotionDataType];
newEmotionData = await getWordEmotions(text);
if(newEmotionData.emoId === undefined){
await fetchAPI();
for (const emotion in newEmotionData) {
if (newEmotionData[emotion as keyof emotionDataType] > maxScore) {
maxEmotion = emotion;
maxScore = newEmotionData[emotion as keyof emotionDataType];
}
}
}
switch (maxEmotion) {
case 'Joy':
updateEmotionData.emoId = 1;
break;
case 'Anger':
updateEmotionData.emoId = 2;
break;
case 'Sorrow':
updateEmotionData.emoId = 3;
break;
case 'Enjoyable':
updateEmotionData.emoId = 4;
break;
switch (maxEmotion) {
case 'Joy':
newEmotionData.emoId = 1;
break;
case 'Anger':
newEmotionData.emoId = 2;
break;
case 'Sorrow':
newEmotionData.emoId = 3;
break;
case 'Enjoyable':
newEmotionData.emoId = 4;
break;
}
const addWord: addWordObjectType = {
text: text,
emotionData: newEmotionData
};
addWordEmotions(addWord);
}

updateEmotionData.Joy += ans.Joy;
updateEmotionData.Anger += ans.Anger;
updateEmotionData.Sorrow += ans.Sorrow;
updateEmotionData.Enjoyable += ans.Enjoyable;
const updateEmotionData = {
Joy: emotionData.Joy += newEmotionData.Joy,
Anger: emotionData.Anger += newEmotionData.Anger,
Sorrow: emotionData.Sorrow += newEmotionData.Sorrow,
Enjoyable: emotionData.Enjoyable += newEmotionData.Enjoyable,
emoId: newEmotionData.emoId,
}

return updateEmotionData;
};

0 comments on commit 88fcad3

Please sign in to comment.