Created
October 14, 2023 16:18
-
-
Save Konard/f5c9e50a5546b9968ae0a1012d351438 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async ({ data: { newLink, oldLink }, deep, require }) => { | |
if (!newLink?.value?.value) return; | |
const nlp = require('ru-compromise'); | |
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 textToWords = (text) => { | |
const hash = {}; | |
const doc = nlp(text) | |
doc.match('#Pronoun').remove(); | |
doc.match('#Conjunction').remove(); | |
doc.match('#Preposition').remove(); | |
doc.match('#Cardinal').remove(); | |
doc.match('#Ordinal').remove(); | |
doc.toLowerCase(); | |
const words = nlp(text).terms().json().map(term => term.terms[0].normal); | |
words.forEach(word => hash[word] = hash[word] ? ++hash[word] : 1) | |
return hash; | |
} | |
const hash = textToWords(newLink?.value?.value || ""); | |
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