import Amplify, { Predictions } from 'aws-amplify'; import { AmazonAIPredictionsProvider } from '@aws-amplify/predictions'; import awsconfig from './aws-exports'; import React, { useState } from 'react'; import { TextField, Button, Tooltip } from '@material-ui/core'; import Autocomplete from './components/select'; import AccountCircleIcon from '@material-ui/icons/AccountCircleTwoTone'; import Negative from '@material-ui/icons/SentimentDissatisfiedTwoTone'; import Positive from '@material-ui/icons/SentimentSatisfiedAltTwoTone'; import Neutral from '@material-ui/icons/FaceTwoTone'; import PartsOfSpeech from './assets/json/parts-of-speech.json'; import Languages from './assets/json/languages.json'; Amplify.configure(awsconfig); Amplify.addPluggable(new AmazonAIPredictionsProvider()); let initialLanguage = navigator.languages ? navigator.languages : (navigator.language || (navigator as any).userLanguage); if (!Array.isArray(initialLanguage)) initialLanguage = [initialLanguage]; initialLanguage = initialLanguage.find((lang: any) => Languages.languages.hasOwnProperty(lang)); if (!initialLanguage) initialLanguage = ''; function App() { const [words, getWords] = useState([]); const [text,getText] = useState(''); const [mood,getMood] = useState(''); const [targetInput,getTargetInput] = useState(''); const [targetValue, getTargetValue] = useState(''); const [sourceInput,getSourceInput] = useState(''); const [sourceValue, getSourceValue] = useState(initialLanguage); function parseText() { return words.map((word: {text:string, syntax?:keyof typeof PartsOfSpeech}, ind:number) => { if (!word.syntax) { return (
{word.text}
) } else { const pos: string = PartsOfSpeech[word.syntax]; return (
{word.text}
) } }); } function parseInterpretation(interpretation:any) { if (typeof interpretation !== 'object' || !interpretation) return; const {sentiment, language, syntax} = interpretation; if (!targetValue) { getTargetValue(language); } const newMood = sentiment.predominant; getMood(newMood); getWords(syntax); } function translate(inputText: string, cb = (_:any)=>_) { if (!targetValue) return; Predictions.convert({ translateText: { source: { text: inputText, language: sourceValue }, targetLanguage: targetValue } }).then(result => { cb(result.text); console.log(result.text); }) .catch(err => { throw err }) } function interpret(inputText: string, cb = (_:any)=>_) { const txt: any = { text: { source: { text: inputText, }, type: 'ALL' } } Predictions.interpret(txt).then(result => { const { textInterpretation } = result; cb(textInterpretation); console.log(textInterpretation); }) .catch(err => { throw err }) } function reset() { getWords([]); getMood('none'); getTargetValue(''); getTargetInput(''); } function displayMood() { let res; switch(mood.toUpperCase()) { case 'POSITIVE': res = ; break; case 'NEGATIVE': res = ; break; case 'MIXED': case 'NEUTRAL': res = ; break; default: res = ; break; } return res; } function setText(e: any) { getText(e.target.value); } function onChange(e: any, val: any) { getTargetValue(val); } function onInputChange(e: any, val: any) { getTargetInput(val); } function sourceChange(e: any, val: any) { getSourceValue(val); } function sourceInputChange(e: any, val: any) { getSourceInput(val); } function onSubmit() { if (targetValue) { translate(text, (res:string) => { const syn = Object.keys(Languages.syntax); if ((syn.includes(targetValue))) { interpret(res, parseInterpretation); } else { const word = [{ text: res }] getWords((word as any)); } }); } else { interpret(text, parseInterpretation) } } function checkForEnter(e: any) { if (!text.length) reset(); if (e.key === 'Enter' && text.length) { onSubmit(); } } return (
{displayMood()}
{parseText()}
); } export default App;