Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pictogram search #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,32 @@ async function isContentSafe(textPrompt: string): Promise<boolean> {
throw new Error("Error checking content safety: " + error);
}
}

export async function getSynonym(
word: string,
language: string
): Promise<string> {
const languageName = getLanguageName(language);
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",
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");
}
55 changes: 45 additions & 10 deletions src/lib/symbolSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -45,7 +46,11 @@ export async function getArasaacPictogramSuggestions({
);
}

async function fetchArasaacData(URL: string, word: string, language: string) {
async function fetchArasaacData(
URL: string,
word: string,
language: string
): Promise<BestSearchApiResponse | []> {
const cleanedWord = removeDiacritics(word);
const bestSearchUrl = `${URL}/${language}/bestsearch/${encodeURIComponent(
cleanedWord
Expand All @@ -54,18 +59,48 @@ async function fetchArasaacData(URL: string, word: string, language: string) {
cleanedWord
)}`;

try {
const { data } = await axios.get<BestSearchApiResponse>(bestSearchUrl);
let data: BestSearchApiResponse | [] = [];

const bestSearchResponse = await axios
.get<BestSearchApiResponse>(bestSearchUrl)
.catch(() => null);
if (bestSearchResponse && bestSearchResponse.data.length) {
return bestSearchResponse.data;
}

const searchResponse = await axios
.get<BestSearchApiResponse>(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<BestSearchApiResponse>(searchUrl);
if (data.length > 5) data = data.slice(0, 5);
}

for (let attempt = 0; attempt < 5; attempt++) {
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<BestSearchApiResponse>(synonymSearchUrl)
.catch(() => null);
if (synonymResponse && synonymResponse.data.length) {
data =
synonymResponse.data.length > 5
? synonymResponse.data.slice(0, 5)
: synonymResponse.data;
return data;
} catch {
return [];
}
}

return data;
}

function mapArasaacResponse(
Expand Down Expand Up @@ -116,7 +151,7 @@ async function fetchGlobalSymbolsData(
word: string,
language: string,
symbolSet: string | null
) {
): Promise<LabelsSearchApiResponse | []> {
const cleanedWord = removeDiacritics(word);
const config: AxiosRequestConfig = {
params: {
Expand Down