Skip to content

Commit

Permalink
Merge pull request #15 from kartikvirendrar/master
Browse files Browse the repository at this point in the history
  • Loading branch information
ishvindersethi22 authored Jul 20, 2024
2 parents 1b7ae21 + fa548b3 commit c370fce
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ai4bharat/indic-transliterate",
"version": "1.3.3",
"version": "1.3.4",
"description": "Transliterate component for React",
"author": "AI4Bharat",
"license": "MIT",
Expand Down
8 changes: 4 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ export const IndicTransliterate = ({
// fetch suggestion from api
// const url = `https://www.google.com/inputtools/request?ime=transliteration_en_${lang}&num=5&cp=0&cs=0&ie=utf-8&oe=utf-8&app=jsapi&text=${lastWord}`;

const numOptions = showCurrentWordAsLastSuggestion
? maxOptions - 1
: maxOptions;
// const numOptions = showCurrentWordAsLastSuggestion
// ? maxOptions - 1
// : maxOptions;

const data = await getTransliterateSuggestions(lastWord, customApiURL, {
numOptions,
// numOptions,
showCurrentWordAsLastSuggestion,
lang,
});
Expand Down
88 changes: 79 additions & 9 deletions src/util/suggestions-util.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,70 @@
import { Language } from "../types/Language";

type Config = {
numOptions?: number;
// numOptions?: number;
showCurrentWordAsLastSuggestion?: boolean;
lang?: Language;
};

type CacheEntry = {
suggestions: string[];
frequency: number;
};

const MAX_CACHE_SIZE = 1000;
const SAVE_THRESHOLD = 20;
const CACHE_KEY = 'transliterateCache';

const cache: Record<string, Record<string, CacheEntry>> = loadCacheFromLocalStorage();
let newEntriesCount = 0;

function loadCacheFromLocalStorage(): Record<string, Record<string, CacheEntry>> {
const cachedData = localStorage.getItem(CACHE_KEY);
return cachedData ? JSON.parse(cachedData) : {};
}

function saveCacheToLocalStorage() {
localStorage.setItem(CACHE_KEY, JSON.stringify(cache));
}

const getWordWithLowestFrequency = (dictionary: Record<string, CacheEntry>): string | null => {
let lowestFreqWord: string | null = null;
let lowestFreq = Infinity;

for (const word in dictionary) {
if (dictionary[word].frequency < lowestFreq) {
lowestFreq = dictionary[word].frequency;
lowestFreqWord = word;
}
}

return lowestFreqWord;
};

export const getTransliterateSuggestions = async (
word: string,
customApiURL: string,
config?: Config,
): Promise<string[] | undefined> => {
const { showCurrentWordAsLastSuggestion, lang } = config || {
numOptions: 5,
showCurrentWordAsLastSuggestion: true,
lang: "hi",
};
const {
// numOptions = 5,
showCurrentWordAsLastSuggestion = true,
lang = "hi",
} = config || {};
// fetch suggestion from api
// const url = `https://www.google.com/inputtools/request?ime=transliteration_en_${lang}&num=5&cp=0&cs=0&ie=utf-8&oe=utf-8&app=jsapi&text=${word}`;
// let myHeaders = new Headers();
// myHeaders.append("Content-Type", "application/json");

if (!cache[lang]) {
cache[lang] = {};
}

if (cache[lang][word]) {
cache[lang][word].frequency += 1;
return cache[lang][word].suggestions;
}

const requestOptions = {
method: "GET",
};
Expand All @@ -37,17 +81,41 @@ export const getTransliterateSuggestions = async (
);
let data = await res.json();
console.log("library data", data);
if(!customApiURL.includes("xlit-api")){
if (!customApiURL.includes("xlit-api")) {
data.result = data.output[0].target;
}
if (data && data.result.length > 0) {
const found = showCurrentWordAsLastSuggestion
? [...data.result, word]
: data.result;

if (Object.keys(cache[lang]).length >= MAX_CACHE_SIZE) {
const lowestFreqWord = getWordWithLowestFrequency(cache[lang]);
if (lowestFreqWord) {
delete cache[lang][lowestFreqWord];
}
}

cache[lang][word] = {
suggestions: found,
frequency: 1,
};

newEntriesCount += 1;
if (newEntriesCount >= SAVE_THRESHOLD) {
saveCacheToLocalStorage();
newEntriesCount = 0;
}

return found;
} else {
if (showCurrentWordAsLastSuggestion) {
return [word];
const fallback = [word];
cache[lang][word] = {
suggestions: fallback,
frequency: 1,
};
return fallback;
}
return [];
}
Expand All @@ -56,4 +124,6 @@ export const getTransliterateSuggestions = async (
console.error("There was an error with transliteration", e);
return [];
}
};
};

window.addEventListener('beforeunload', saveCacheToLocalStorage);

0 comments on commit c370fce

Please sign in to comment.