Create Filtered definitions and full version of It

Redux: parsedDictionary has 2 arrays. compounded and filtered.
	Filtered will use for first look at fields
This commit is contained in:
horhik 2020-03-28 03:28:54 -03:00
parent d737741029
commit 999b23f436
11 changed files with 67 additions and 87 deletions

View file

@ -5,7 +5,7 @@ App which provide translating and adding words to anki
![screenshot](./readme-files/screenshot1.png) ![screenshot](./readme-files/screenshot1.png)
## Build and start ## Build and start
```shell ```bash
git clone https://github.com/Horhik/ankilan git clone https://github.com/Horhik/ankilan
yarn install yarn install

View file

@ -36,21 +36,25 @@ const setDictioanry = dictioanry => ({
export const wordInfo = word => async dispatch => { export const wordInfo = word => async dispatch => {
try { try {
const api1 = await getResFromWordsAPI(word); const api1 = await getResFromWordsAPI(word);
//TODO add Urban Dictionary //TODO add Urban Dictionary
/* ********Working with Urban Dictionary*******
// const api2 = await getResFromUrbanDictionary(word); const api2 = await getResFromUrbanDictionary(word);
// const availableApi = getAvailableApi([api1, api2]); const availableApi = getAvailableApi([api1, api2]);
// if (availableApi === false) { if (availableApi === false) {
// throw new Error('word not found'); 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
//TODO available dictionary instead api1 //TODO available dictionary instead api1
await dispatch(setAvailableApi(api1)); await dispatch(setAvailableApi(api1));
const wordDictionary = await createDictionary(api1); const wordDictionary = await createDictionary(api1);
dispatch(setDictioanry(wordDictionary)); dispatch(setDictioanry(wordDictionary));
sendWord(setFields(wordDictionary)); /* TODO: move sendWord to submit function */
// sendWord(setFields(wordDictionary));
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
}; };
export const drawFields = fields => {};

View file

@ -1,27 +0,0 @@
// The object
// the string version of a function with /Function(
// in front and )/ at the end.
export const fJsonStingify = obj => {
return JSON.stringify(obj, function(key, value) {
if (typeof value === 'function') {
return '/Function(' + value.toString() + ')/';
}
return value;
});
};
// Convert to an object using a reviver function that
// recognizes the /Function(...)/ value and converts it
// into a function via -shudder- `eval`.
export const fJsonParse = json => {
return JSON.parse(json, function(key, value) {
if (
typeof value === 'string' &&
value.startsWith('/Function(') &&
value.endsWith(')/')
) {
value = value.substring(10, value.length - 2);
return eval('(' + value + ')');
}
return value;
});
};

View file

@ -1,13 +1,13 @@
import {search} from "urban-dictionary-client"; import {search} from 'urban-dictionary-client';
import {URBAN_DICTIONARY_API} from "../../constants/api-constants"; import {URBAN_DICTIONARY_API} from '../../constants/api-constants';
export async function getResFromUrbanDictionary(word) { export async function getResFromUrbanDictionary(word) {
const res = await search(word); const res = await search(word);
if (res.list.length === 0) { if (res.list.length === 0) {
// throw new Error('nothing was found'); // throw new Error('nothing was found');
return false; return false;
} }
return Promise.resolve({...res, word, source: URBAN_DICTIONARY_API}); return Promise.resolve({...res, word, source: URBAN_DICTIONARY_API});
} }
export const parseUrbanDictionaryApi = api => {} export const parseUrbanDictionaryApi = api => {};

View file

@ -17,31 +17,18 @@ export async function getResFromWordsAPI(word) {
return Promise.resolve(res); return Promise.resolve(res);
} }
const parsePOS = wordArray => {
let posSet = new Set();
let posArray = [];
wordArray.forEach((result, id) => {
const pos = result.partOfSpeech;
if (!posSet.has(pos)) {
posSet.add(pos);
posArray.push({pos, id});
}
});
return posArray;
};
//get all what anki template need for template //get all what anki template need for template
const getDefinitionList = wordsArray => { const getDefinitionList = wordsArray => {
const partOfSpeeches = parsePOS(wordsArray);
let definitionList = []; let definitionList = [];
partOfSpeeches.forEach(pos => { /*partOfSpeeches*/
const currentWord = wordsArray[pos.id]; wordsArray.forEach(currentWord => {
definitionList.push({ definitionList.push({
definition: currentWord.definition, definition: currentWord.definition,
example: currentWord.examples ? currentWord.examples[0] : undefined, example: currentWord.examples ? currentWord.examples[0] : undefined,
id: pos.id, pos: currentWord.partOfSpeech,
pos: pos.pos,
}); });
}); });
console.log(definitionList);
return definitionList; return definitionList;
}; };

View file

@ -41,8 +41,9 @@ export const createAnkiLanModel = model => async dispatch => {
//creator is an object what have to store in locale storage. //creator is an object what have to store in locale storage.
export const addNote = words => { export const addNote = words => {
const template = store.getState().anki.noteTemplate; const template = store.getState().anki.noteTemplate;
const deckId = store.getState().anki.selectedDeck.id;
const settings = { const settings = {
deckId: '1', deckId,
modelId: '1585139654585', modelId: '1585139654585',
}; };
const creator = new AnkiDroid(settings); const creator = new AnkiDroid(settings);

View file

@ -6,6 +6,8 @@ export const createDictionary = async apiRes => {
const word = apiRes.word; const word = apiRes.word;
const parsedDictionary = parseDictionary(apiRes); const parsedDictionary = parseDictionary(apiRes);
const audio = await getAudio(word); const audio = await getAudio(word);
const compounded = await compoundWithYDictionary(parsedDictionary, word); const compounded = await compoundWithYDictionary(parsedDictionary, word);
//TODO add shrinkToOneExample(compounded)
return {...compounded, sound: audio}; return {...compounded, sound: audio};
}; };

View file

@ -1,4 +1,19 @@
import yDictionary from '../api/yandex-dictionary'; import yDictionary from '../api/yandex-dictionary';
const selectByPos = wordArray => {
let posSet = new Set();
let selectedArray = [];
wordArray.forEach((result, id) => {
const pos = result.pos;
if (!posSet.has(pos)) {
posSet.add(pos);
selectedArray.push(result);
}
});
console.log(selectedArray);
return selectedArray;
};
export const compoundWithYDictionary = async (definitionList, word) => { export const compoundWithYDictionary = async (definitionList, word) => {
try { try {
const translations = await yDictionary(word); const translations = await yDictionary(word);
@ -16,28 +31,16 @@ export const compoundWithYDictionary = async (definitionList, word) => {
compounded.push(translate); compounded.push(translate);
} }
}); });
console.log(compounded);
const selected = selectByPos(compounded);
// console.log(`RESPONSE FOR: ${word}`, {word, compounded}); // console.log(`RESPONSE FOR: ${word}`, {word, compounded});
return { return {
word, word,
pronunciation: `/${definitionList.pronunciation}/`, pronunciation: `/${definitionList.pronunciation}/`,
compounded, compounded,
filtered: selected,
}; };
} catch (e) { } catch (e) {
console.log('erris HERE', e); console.log('error is HERE', e);
} }
}; };
/*
import yDictionary from '../api/yandex-dictionary';
let smallStore = {};
export const compoundWithYDictionary = async (definitionList, word) => {
let properDefinitions = [];
const translations = (() => {
yDictionary(word).then(res => {
smallStore.res = res;
});
})();
const yDictionaryRes = smallStore.res.payload;
console.log(yDictionaryRes, definitionList);
};*/

View file

@ -3,10 +3,6 @@ import {addNote} from './createAnkiLanModel';
export const submit = () => ({}); export const submit = () => ({});
export const sendWord = async fields => { export const sendWord = async fields => {
// const creator = await getCreator();
// const template = JSON.parse(await getTemplate()).payload;
// console.log(creator);
// console.log(template);
console.log(fields); console.log(fields);
addNote(fields.payload); addNote(fields.payload);
}; };

View file

@ -1,8 +1,8 @@
// export const ct = 'ct' // export const ct = 'ct'
export const SET_YANDEX_DICTIONARY_RESPONSE = 'SET_YANDEX_DICTIONARY_RESPONSE'; export const SET_YANDEX_DICTIONARY_RESPONSE = 'SET_YANDEX_DICTIONARY_RESPONSE';
export const SET_AVAILABLE_API = 'SET_AVAILABLE_API'; export const SET_AVAILABLE_API = 'SET_AVAILABLE_API';
export const WORDS_API = 'WORDS_API' export const WORDS_API = 'WORDS_API';
export const URBAN_DICTIONARY_API = 'URBAN_DICTIONARY_API' export const URBAN_DICTIONARY_API = 'URBAN_DICTIONARY_API';
export const SET_ALL_FIELDS = 'SET_ALL_FIELDS';
export const SET_PARSED_DICTIONARY = 'SET_PARSED_DICTIONARY';
export const SET_PARSED_DICTIONARY = 'SET_PARSED_DICTIONARY'

View file

@ -1,8 +1,10 @@
import { import {
SET_ALL_FIELDS,
SET_AVAILABLE_API, SET_AVAILABLE_API,
SET_PARSED_DICTIONARY, SET_PARSED_DICTIONARY,
SET_YANDEX_DICTIONARY_RESPONSE, SET_YANDEX_DICTIONARY_RESPONSE,
} from '../constants/api-constants'; } from '../constants/api-constants';
import {SET_FIELDS} from '../constants/anki-constants';
const initialState = { const initialState = {
word: '', word: '',
@ -12,6 +14,8 @@ const initialState = {
availableApiName: '', availableApiName: '',
yandexDictionaryInfo: [], yandexDictionaryInfo: [],
parsedDictionary: {}, parsedDictionary: {},
availableFields: [],
allFields: [],
}; };
const apiReducer = (state = initialState, action) => { const apiReducer = (state = initialState, action) => {
@ -32,6 +36,16 @@ const apiReducer = (state = initialState, action) => {
...state, ...state,
parsedDictionary: action.payload, parsedDictionary: action.payload,
}; };
case SET_FIELDS:
return {
...state,
availableFields: action.payload,
};
case SET_ALL_FIELDS:
return {
...state,
allFields: action.payload,
};
default: default:
return state; return state;
} }