From d31bb7e808dead8ed54f7ea4dd9351ca3020934e Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 22 Oct 2024 10:14:49 -0500 Subject: [PATCH 1/2] synonym search --- src/engine.ts | 30 +++++++++++++++++++++++++++++ src/lib/symbolSets.ts | 45 ++++++++++++++++++++++++++++--------------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/engine.ts b/src/engine.ts index 3535c04..054b6b2 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -207,3 +207,33 @@ async function isContentSafe(textPrompt: string): Promise { throw new Error("Error checking content safety: " + error); } } + +export async function getSynonym( + word: string, + language: string +): Promise { + const languageName = getLanguageName(language); + const prompt = `Act as a speech pathologist providing synonym for the word "${word}" in ${languageName} to support a non-verbal person. + Follow these mandatory instructions when generating the synonym: + -Provide only one synonym for each word. + -Ensure the synonym is simple and commonly used in everyday speech. + -If no synonym exists, return the original word. + -Do not include any additional text, symbols, or characters beyond the words requested. + `; + const completionRequestParams = { + model: "gpt-3.5-turbo-instruct", + prompt: prompt, + temperature: 0, + max_tokens: 10, + }; + + const response = await globalConfiguration.openAIInstance.createCompletion( + completionRequestParams + ); + const synonymData = response.data?.choices[0]?.text; + if (synonymData) { + const synonym = synonymData.trim(); + if (synonym) return synonym; + } + throw new Error("ERROR: Synonym not found"); +} diff --git a/src/lib/symbolSets.ts b/src/lib/symbolSets.ts index 1be3f80..416752b 100644 --- a/src/lib/symbolSets.ts +++ b/src/lib/symbolSets.ts @@ -4,6 +4,7 @@ import { LabelsSearchApiResponse } from "../types/global-symbols"; import { BestSearchApiResponse } from "../types/arasaac"; import { Suggestion } from "../engine"; import { ARASAAC } from "../constants"; +import { getSynonym } from "../engine"; export type SymbolSet = "arasaac" | "global-symbols"; @@ -45,27 +46,39 @@ export async function getArasaacPictogramSuggestions({ ); } -async function fetchArasaacData(URL: string, word: string, language: string) { +async function fetchArasaacData(URL: string, word: string, language: string): Promise { const cleanedWord = removeDiacritics(word); - const bestSearchUrl = `${URL}/${language}/bestsearch/${encodeURIComponent( - cleanedWord - )}`; - const searchUrl = `${URL}/${language}/search/${encodeURIComponent( - cleanedWord - )}`; + const bestSearchUrl = `${URL}/${language}/bestsearch/${encodeURIComponent(cleanedWord)}`; + const searchUrl = `${URL}/${language}/search/${encodeURIComponent(cleanedWord)}`; - try { - const { data } = await axios.get(bestSearchUrl); + let data: BestSearchApiResponse | [] = []; + + const bestSearchResponse = await axios.get(bestSearchUrl).catch(() => null); + if (bestSearchResponse && bestSearchResponse.data.length) { + return bestSearchResponse.data; + } + + const searchResponse = await axios.get(searchUrl).catch(() => null); + if (searchResponse && searchResponse.data.length) { + data = searchResponse.data.length > 5 ? searchResponse.data.slice(0, 5) : searchResponse.data; return data; - } catch { - try { - let { data } = await axios.get(searchUrl); - if (data.length > 5) data = data.slice(0, 5); + } + + for (let attempt = 0; attempt < 5; attempt++) { + const synonym = await getSynonym(word, language).catch(() => null); + if (!synonym) continue; + + const cleanedSynonym = removeDiacritics(synonym); + const synonymSearchUrl = `${URL}/${language}/search/${encodeURIComponent(cleanedSynonym)}`; + + const synonymResponse = await axios.get(synonymSearchUrl).catch(() => null); + if (synonymResponse && synonymResponse.data) { + data = synonymResponse.data.length > 5 ? synonymResponse.data.slice(0, 5) : synonymResponse.data; return data; - } catch { - return []; } } + + return data; } function mapArasaacResponse( @@ -116,7 +129,7 @@ async function fetchGlobalSymbolsData( word: string, language: string, symbolSet: string | null -) { +): Promise { const cleanedWord = removeDiacritics(word); const config: AxiosRequestConfig = { params: { From ffe4f7da5e88de6bd72f873b99762bd937db1b72 Mon Sep 17 00:00:00 2001 From: Ali Date: Tue, 22 Oct 2024 10:50:28 -0500 Subject: [PATCH 2/2] get pictogram using synonym --- src/engine.ts | 11 +++++----- src/lib/symbolSets.ts | 48 +++++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/engine.ts b/src/engine.ts index 054b6b2..e7664e6 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -213,12 +213,11 @@ export async function getSynonym( language: string ): Promise { const languageName = getLanguageName(language); - const prompt = `Act as a speech pathologist providing synonym for the word "${word}" in ${languageName} to support a non-verbal person. - Follow these mandatory instructions when generating the synonym: - -Provide only one synonym for each word. - -Ensure the synonym is simple and commonly used in everyday speech. - -If no synonym exists, return the original word. - -Do not include any additional text, symbols, or characters beyond the words requested. + const prompt = `Provide a simple, commonly used synonym for the word "${word}" in ${languageName} to support a non-verbal person. Follow these mandatory instructions: + - Return only one synonym. + - Ensure the synonym is simple, familiar, and frequently used in everyday language. + - If no suitable synonym exists, return the original word unchanged. + - Do not include any additional words, explanations, symbols, or characters; only the synonym itself. `; const completionRequestParams = { model: "gpt-3.5-turbo-instruct", diff --git a/src/lib/symbolSets.ts b/src/lib/symbolSets.ts index 416752b..8f878f0 100644 --- a/src/lib/symbolSets.ts +++ b/src/lib/symbolSets.ts @@ -46,34 +46,56 @@ export async function getArasaacPictogramSuggestions({ ); } -async function fetchArasaacData(URL: string, word: string, language: string): Promise { +async function fetchArasaacData( + URL: string, + word: string, + language: string +): Promise { const cleanedWord = removeDiacritics(word); - const bestSearchUrl = `${URL}/${language}/bestsearch/${encodeURIComponent(cleanedWord)}`; - const searchUrl = `${URL}/${language}/search/${encodeURIComponent(cleanedWord)}`; + const bestSearchUrl = `${URL}/${language}/bestsearch/${encodeURIComponent( + cleanedWord + )}`; + const searchUrl = `${URL}/${language}/search/${encodeURIComponent( + cleanedWord + )}`; let data: BestSearchApiResponse | [] = []; - const bestSearchResponse = await axios.get(bestSearchUrl).catch(() => null); + const bestSearchResponse = await axios + .get(bestSearchUrl) + .catch(() => null); if (bestSearchResponse && bestSearchResponse.data.length) { return bestSearchResponse.data; } - const searchResponse = await axios.get(searchUrl).catch(() => null); + const searchResponse = await axios + .get(searchUrl) + .catch(() => null); if (searchResponse && searchResponse.data.length) { - data = searchResponse.data.length > 5 ? searchResponse.data.slice(0, 5) : searchResponse.data; + data = + searchResponse.data.length > 5 + ? searchResponse.data.slice(0, 5) + : searchResponse.data; return data; } for (let attempt = 0; attempt < 5; attempt++) { - const synonym = await getSynonym(word, language).catch(() => null); + const synonym = await getSynonym(word, language).catch(() => ""); if (!synonym) continue; - + word = synonym; const cleanedSynonym = removeDiacritics(synonym); - const synonymSearchUrl = `${URL}/${language}/search/${encodeURIComponent(cleanedSynonym)}`; - - const synonymResponse = await axios.get(synonymSearchUrl).catch(() => null); - if (synonymResponse && synonymResponse.data) { - data = synonymResponse.data.length > 5 ? synonymResponse.data.slice(0, 5) : synonymResponse.data; + const synonymSearchUrl = `${URL}/${language}/search/${encodeURIComponent( + cleanedSynonym + )}`; + + const synonymResponse = await axios + .get(synonymSearchUrl) + .catch(() => null); + if (synonymResponse && synonymResponse.data.length) { + data = + synonymResponse.data.length > 5 + ? synonymResponse.data.slice(0, 5) + : synonymResponse.data; return data; } }