Skip to content

Instantly share code, notes, and snippets.

@Konard
Created October 18, 2023 11:25
Show Gist options
  • Save Konard/eceeeef1aaa4b9879658362bed14eef9 to your computer and use it in GitHub Desktop.
Save Konard/eceeeef1aaa4b9879658362bed14eef9 to your computer and use it in GitHub Desktop.
async ({ data: { newLink, oldLink }, deep, require }) => {
if (!newLink?.value?.value) return;
const currentId = newLink?.id || oldLing?.id;
const Az = require('az');
const Word = Number(await deep.id('@deep-foundation/post-to-cloud', 'Word'));
const Count = Number(await deep.id('@deep-foundation/post-to-cloud', 'Count'));
const Post = Number(await deep.id('@deep-foundation/rss-to-deep', 'Post'));
const Then = Number(await deep.id('@deep-foundation/core', 'Then'));
const Resolved = Number(await deep.id('@deep-foundation/core', 'Resolved'));
const Rejected = Number(await deep.id('@deep-foundation/core', 'Rejected'));
const delay = (time) => new Promise(res => setTimeout(() => res(null), time));
while (true) {
const {data: inProgres} = await deep.select({
from: { type_id: {_eq: Post}, id: {_lt: currentId} },
type_id: { _eq: Then },
to: {
_not: {
out: {
type_id: { _in: [Resolved, Rejected] }
}
}
}
});
if (!inProgres.length) break;
await delay(1000);
}
const text = newLink?.value?.value || "";
const textToWords = new Promise((resolve, reject) => {
const hash = {};
Az.Morph.init('node_modules/az/dicts', function() {
Az.Tokens(text).done().filter(t => {
const word = t.source.slice(t.st, t.st + t.length);
return t.length > 1 && t.type == 'WORD' && Az.Morph(word).some(variant => {
const tag = variant.tag.toString();
return tag.startsWith('NOUN') || tag.startsWith('VERB') || tag.startsWith('LATN') && !variant.tag.stat.includes('Abbr');
})
}).forEach(t => {
const word = t.source.slice(t.st, t.st + t.length);
const result = Az.Morph(word).find(variant => {
const tag = variant.tag.toString();
return tag.startsWith('NOUN') || tag.startsWith('VERB');
});
const normalized = result?.normalize ? result.normalize().word : word;
hash[normalized] = hash[normalized] ? ++hash[normalized] : 1
});
resolve(hash);
});
});
const hash = await textToWords;
const words = Object.keys(hash);
try {
const { data: foundedWordLinksWithCounts } = await deep.select({
type_id: Word,
string: {
value: {
_in: words
}
}
}, { returning: `id value in { id from_id }` });
const notFoundedWords = words.filter(word => !foundedWordLinksWithCounts.find(founded => founded?.value?.value === word));
const foundedWordsWithoutCounts = foundedWordLinksWithCounts.filter(wordLink => !(wordLink?.['in']?.find && wordLink?.['in']?.find(count => count?.from_id === newLink.id))).map(wordLink => ({ id: wordLink?.id, value: wordLink?.value }));
const insertObjectsNotFoundedWords = notFoundedWords.map(word => { return { type_id: Word, string: word } });
const { data: insertedWords } = await deep.insert(insertObjectsNotFoundedWords, { returning: `id value` });
const insertObjectsCounts = insertedWords.concat(foundedWordsWithoutCounts).map(word => ({ type_id: Count, number: hash[word?.value?.value], from_id: newLink.id, to_id: word.id }));
const { data: insertedCounts } = await deep.insert(insertObjectsCounts, { returning: `id value` });
const selectedCountsWithWords = (await deep.select({ id: { _nin: insertedCounts.map(count => count.id)}, type_id: Count, to_id: { _in: foundedWordLinksWithCounts.map(word => word.id) }, from_id: newLink.id }, { returning: `id value to { id value }` }))?.data;
const countsToDelete = (await deep.select({ type_id: Count, to: { string: { value: { _nin: words } } }, from_id: newLink.id }, { returning: `id` }))?.data?.map(count => count.id);
const deletedCounts = (await deep.delete(countsToDelete))?.data;
const updatedCounts = await Promise.all(selectedCountsWithWords.map(async (selectedCount) => {
let updated;
if (hash[selectedCount?.to?.value?.value] != selectedCount?.value?.value){
if (selectedCount?.value === null) {
updated = { operation: 'insert', selectedCount, result: await deep.insert({ link_id: selectedCount?.id, value: hash[selectedCount?.to?.value?.value] }, { table: 'numbers' } ) };
} else {
updated = { operation: 'update', selectedCount, result: await deep.update({ link_id: selectedCount?.id }, { value: hash[selectedCount?.to?.value?.value] }, { table: 'numbers' } ) };
}
}
return updated?.result?.data?.length ? { mutationReuslt: updated?.result, data: { link_id: selectedCount?.id, oldValue: selectedCount?.value, newValue: hash[selectedCount?.to?.value?.value] } } : { selectedCount: updated?.selectedCount, operation: updated?.operation, notUpdatedData: { link_id: selectedCount?.id, oldValue: selectedCount?.value, newValue: hash[selectedCount?.to?.value?.value] }};
}));
return { hash, foundedWordLinksWithCounts, foundedWordsWithoutCounts, insertedWords, insertObjectsNotFoundedWords, insertedCounts, selectedCountsWithWords, updatedCounts, deletedCounts, insertObjectsCounts };
} catch (error) {
throw new Error(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment