create stable working dictionary api. Yeahvi
This commit is contained in:
parent
3f2abc4954
commit
2a05cd5cb3
|
@ -29,15 +29,18 @@ const setAvailableApi = api => ({
|
||||||
export const wordInfo = word => async dispatch => {
|
export const wordInfo = word => async dispatch => {
|
||||||
try {
|
try {
|
||||||
const api1 = await getResFromWordsAPI(word);
|
const api1 = await getResFromWordsAPI(word);
|
||||||
const api2 = await getResFromUrbanDictionary(word);
|
//TODO add Urban Dictionary
|
||||||
const availableApi = getAvailableApi([api1, api2]);
|
|
||||||
if (availableApi === false) {
|
// const api2 = await getResFromUrbanDictionary(word);
|
||||||
throw new Error('word not found');
|
// const availableApi = getAvailableApi([api1, api2]);
|
||||||
}
|
// if (availableApi === false) {
|
||||||
console.log(availableApi);
|
// throw new Error('word not found');
|
||||||
|
// }
|
||||||
|
|
||||||
//function which return universal template for more simple interaction with api
|
//function which return universal template for more simple interaction with api
|
||||||
await dispatch(setAvailableApi(availableApi));
|
//TODO available dictionary instead api1
|
||||||
createDictionary(availableApi);
|
await dispatch(setAvailableApi(api1));
|
||||||
|
await createDictionary(api1);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const getAudio = async url => {
|
const getAudio = async word => {
|
||||||
const site = await fetch(url, {
|
const site = await fetch(word, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {'Content-Type': 'text/html'},
|
headers: {'Content-Type': 'text/html'},
|
||||||
});
|
});
|
||||||
|
|
|
@ -37,8 +37,9 @@ const getDefinitionList = wordsArray => {
|
||||||
const currentWord = wordsArray[pos.id];
|
const currentWord = wordsArray[pos.id];
|
||||||
definitionList.push({
|
definitionList.push({
|
||||||
definition: currentWord.definition,
|
definition: currentWord.definition,
|
||||||
example: currentWord.examples[0],
|
example: currentWord.examples ? currentWord.examples[0] : undefined,
|
||||||
id: pos.id,
|
id: pos.id,
|
||||||
|
pos: pos.pos,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return definitionList;
|
return definitionList;
|
||||||
|
@ -46,5 +47,5 @@ const getDefinitionList = wordsArray => {
|
||||||
|
|
||||||
export const parseWordsApi = api => ({
|
export const parseWordsApi = api => ({
|
||||||
pronunciation: api.pronunciation.all,
|
pronunciation: api.pronunciation.all,
|
||||||
words: getDefinitionList(api.results),
|
words: api.results ? getDefinitionList(api.results) : [],
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,8 +13,8 @@ const yDictionary = async (
|
||||||
{method: 'GET'},
|
{method: 'GET'},
|
||||||
);
|
);
|
||||||
const json = await res.json();
|
const json = await res.json();
|
||||||
|
// console.log(word, json.def);
|
||||||
return findPartofSpeech(json.def);
|
return findPartofSpeech(json.def);
|
||||||
return json.def;
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('err in yandex-dictionary.js: ', e);
|
console.log('err in yandex-dictionary.js: ', e);
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,15 @@ const translateTemplate = (pos, tr) => ({
|
||||||
});
|
});
|
||||||
|
|
||||||
export const findPartofSpeech = dictionary => {
|
export const findPartofSpeech = dictionary => {
|
||||||
return {
|
if (dictionary.length > 1) {
|
||||||
type: SET_YANDEX_DICTIONARY_RESPONSE,
|
return [
|
||||||
payload: [
|
|
||||||
// TODO create flexible field selector, by poses count
|
// TODO create flexible field selector, by poses count
|
||||||
translateTemplate(dictionary[0].pos, dictionary[0].tr[0].text),
|
translateTemplate(dictionary[0].pos, dictionary[0].tr[0].text),
|
||||||
translateTemplate(dictionary[1].pos, dictionary[1].tr[0].text),
|
translateTemplate(dictionary[1].pos, dictionary[1].tr[0].text),
|
||||||
],
|
];
|
||||||
};
|
} else {
|
||||||
|
return [translateTemplate(dictionary[0].pos, dictionary[0].tr[0].text)];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export default yDictionary;
|
export default yDictionary;
|
||||||
|
|
|
@ -2,9 +2,9 @@ import {parseDictionary} from './parsing-dictionary';
|
||||||
import getAudio from '../api/word-sound';
|
import getAudio from '../api/word-sound';
|
||||||
import {compoundWithYDictionary} from './get-translate';
|
import {compoundWithYDictionary} from './get-translate';
|
||||||
|
|
||||||
export const createDictionary = apiRes => {
|
export const createDictionary = async apiRes => {
|
||||||
const word = apiRes.word;
|
const word = apiRes.word;
|
||||||
const parsedDictionary = parseDictionary(apiRes);
|
const parsedDictionary = parseDictionary(apiRes);
|
||||||
const audio = getAudio(word);
|
// const audio = await getAudio(word);
|
||||||
compoundWithYDictionary(parsedDictionary, word);
|
await compoundWithYDictionary(parsedDictionary, word);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,10 +1,26 @@
|
||||||
import yDictionary from '../api/yandex-dictionary';
|
import yDictionary from '../api/yandex-dictionary';
|
||||||
export const compoundWithYDictionary = (definitionList, word) => {
|
export const compoundWithYDictionary = async (definitionList, word) => {
|
||||||
const translations = (async () => {
|
try {
|
||||||
const res = await yDictionary(word);
|
const translations = await yDictionary(word);
|
||||||
return await res;
|
let compounded = [];
|
||||||
})();
|
// console.log('YANDEX ', translations);
|
||||||
console.log(translations);
|
// console.log('WORDS', definitionList);
|
||||||
|
|
||||||
|
translations.forEach(translate => {
|
||||||
|
definitionList.words.forEach(definition => {
|
||||||
|
if (definition.pos === translate.pos) {
|
||||||
|
const compound = {...definition, ...translate};
|
||||||
|
compounded.push(compound);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (definitionList.words.length === 0) {
|
||||||
|
compounded.push(translate);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
console.log(`RESPONSE FOR: ${word}`, compounded);
|
||||||
|
} catch (e) {
|
||||||
|
console.log('erris HERE', e);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -9,7 +9,14 @@ import {wordInfo} from "../actions/api/dictionary";
|
||||||
|
|
||||||
const AnkiForm = props => {
|
const AnkiForm = props => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
props.wordInfo('like')
|
props.wordInfo('Urge');
|
||||||
|
props.wordInfo('Maze');
|
||||||
|
props.wordInfo('Ramification');
|
||||||
|
props.wordInfo('Dare');
|
||||||
|
props.wordInfo('Entrepreneurship');
|
||||||
|
props.wordInfo('meagre');
|
||||||
|
props.wordInfo('meager');
|
||||||
|
|
||||||
}, []);
|
}, []);
|
||||||
return (
|
return (
|
||||||
<Container style={{padding: 20}}>
|
<Container style={{padding: 20}}>
|
||||||
|
|
Loading…
Reference in a new issue