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)
## Build and start
```shell
```bash
git clone https://github.com/Horhik/ankilan
yarn install

View File

@ -36,21 +36,25 @@ const setDictioanry = dictioanry => ({
export const wordInfo = word => async dispatch => {
try {
const api1 = await getResFromWordsAPI(word);
//TODO add Urban Dictionary
// const api2 = await getResFromUrbanDictionary(word);
// const availableApi = getAvailableApi([api1, api2]);
// if (availableApi === false) {
// throw new Error('word not found');
// }
/* ********Working with Urban Dictionary*******
const api2 = await getResFromUrbanDictionary(word);
const availableApi = getAvailableApi([api1, api2]);
if (availableApi === false) {
throw new Error('word not found');
}
*/
//function which return universal template for more simple interaction with api
//TODO available dictionary instead api1
await dispatch(setAvailableApi(api1));
const wordDictionary = await createDictionary(api1);
dispatch(setDictioanry(wordDictionary));
sendWord(setFields(wordDictionary));
/* TODO: move sendWord to submit function */
// sendWord(setFields(wordDictionary));
} catch (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 {URBAN_DICTIONARY_API} from "../../constants/api-constants";
import {search} from 'urban-dictionary-client';
import {URBAN_DICTIONARY_API} from '../../constants/api-constants';
export async function getResFromUrbanDictionary(word) {
const res = await search(word);
if (res.list.length === 0) {
// throw new Error('nothing was found');
return false;
}
return Promise.resolve({...res, word, source: URBAN_DICTIONARY_API});
const res = await search(word);
if (res.list.length === 0) {
// throw new Error('nothing was found');
return false;
}
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);
}
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
const getDefinitionList = wordsArray => {
const partOfSpeeches = parsePOS(wordsArray);
let definitionList = [];
partOfSpeeches.forEach(pos => {
const currentWord = wordsArray[pos.id];
/*partOfSpeeches*/
wordsArray.forEach(currentWord => {
definitionList.push({
definition: currentWord.definition,
example: currentWord.examples ? currentWord.examples[0] : undefined,
id: pos.id,
pos: pos.pos,
pos: currentWord.partOfSpeech,
});
});
console.log(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.
export const addNote = words => {
const template = store.getState().anki.noteTemplate;
const deckId = store.getState().anki.selectedDeck.id;
const settings = {
deckId: '1',
deckId,
modelId: '1585139654585',
};
const creator = new AnkiDroid(settings);

View File

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

View File

@ -1,4 +1,19 @@
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) => {
try {
const translations = await yDictionary(word);
@ -16,28 +31,16 @@ export const compoundWithYDictionary = async (definitionList, word) => {
compounded.push(translate);
}
});
console.log(compounded);
const selected = selectByPos(compounded);
// console.log(`RESPONSE FOR: ${word}`, {word, compounded});
return {
word,
pronunciation: `/${definitionList.pronunciation}/`,
compounded,
filtered: selected,
};
} 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 sendWord = async fields => {
// const creator = await getCreator();
// const template = JSON.parse(await getTemplate()).payload;
// console.log(creator);
// console.log(template);
console.log(fields);
addNote(fields.payload);
};

View File

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