Last active
June 26, 2023 09:51
-
-
Save DavidNgugi/a10b9666b5fc3fd89709f7658909f4a3 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
const fs = require('fs'); | |
const wordnet = require('wordnet'); | |
const natural = require('natural'); | |
const tokenizePhrase = (phrase) => { | |
// Tokenize the phrase into individual words | |
const tokenizer = new natural.WordTokenizer(); | |
return tokenizer.tokenize(phrase.toLowerCase()); | |
}; | |
const loadMemory = () => { | |
try { | |
const memoryData = fs.readFileSync('memory.json'); | |
return JSON.parse(memoryData); | |
} catch (error) { | |
console.log('Error loading memory:', error); | |
return {}; | |
} | |
}; | |
const saveMemory = (memory) => { | |
try { | |
const memoryData = JSON.stringify(memory); | |
fs.writeFileSync('memory.json', memoryData); | |
} catch (error) { | |
console.log('Error saving memory:', error); | |
} | |
}; | |
const findSynonymsRecursive = async (word, visited, memory, depth) => { | |
try { | |
if (visited.has(word) || depth === 0) { | |
return []; | |
} | |
visited.add(word); | |
let synonyms = []; | |
const results = await wordnet.lookup(word); | |
if (results.length === 0) { | |
return []; | |
} | |
const wordMap = results | |
.filter((result) => result.meta && result.meta.pointers) | |
.map((result) => | |
result.meta.pointers | |
.filter((pointer) => pointer.data && pointer.data.meta) | |
.map((pointer) => pointer.data.meta.words) | |
) | |
.flat(); | |
for (const result of wordMap) { | |
for (const item of result) { | |
const synonym = item.word.toLowerCase(); | |
if (synonym !== word) { | |
if (!memory.hasOwnProperty(synonym)) { | |
memory[synonym] = []; | |
} | |
if (!memory[synonym].includes(word)) { | |
memory[synonym].push(word); // Append the current word as a synonym | |
} | |
if (!memory.hasOwnProperty(word)) { | |
memory[word] = []; | |
} | |
if (!memory[word].includes(synonym)) { | |
memory[word].push(synonym); // Append the synonym to the current word's synonyms | |
} | |
synonyms.push(...memory[word], synonym); // Add the synonym to the list of synonyms | |
const deeperSynonyms = await findSynonymsRecursive( | |
synonym, | |
visited, | |
memory, | |
depth - 1 | |
); | |
synonyms.push(...deeperSynonyms); | |
} | |
} | |
} | |
// Save the updated memory to a JSON file | |
saveMemory(memory); | |
return Array.from(new Set(synonyms)); // Return unique synonyms | |
} catch (e) { | |
console.log(e); | |
return []; | |
} | |
}; | |
const matchPhraseWithSynonyms = async (phrase, maxDepth) => { | |
const words = tokenizePhrase(phrase); | |
await wordnet.init(); | |
const visited = new Set(); | |
const memory = loadMemory(); | |
const matchedSynonyms = []; | |
for (const word of words) { | |
const synonyms = await findSynonymsRecursive(word, visited, memory, maxDepth); | |
matchedSynonyms.push(...synonyms); | |
} | |
return matchedSynonyms; | |
}; | |
(async () => { | |
const phrase = 'cardiac'; | |
const maxDepth = 2; | |
const matchedSynonyms = await matchPhraseWithSynonyms(phrase, maxDepth); | |
console.log(`------------------Synonyms for '${phrase}'------------------`); | |
console.log(matchedSynonyms); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment