In this task, your job will be to write a program that can decide whether two words are synonyms or not. You will get a synonym dictionary describing pairs of synonymous words. Afterwards, you will answer several queries asking whether given two words are synonyms or not. Both dictionary of synonym pairs and queries for your program will be included in the input file.
Use the following rules to decide:
Input :
Input starts with a number of test cases T (0 < T < 100). Each test case begins with a line containing a single number N (0 N s 100) — the length of a synonym dictionary. On each of the following N lines, there is exactly one pair of synonyms separated by a single space. Next line contains a single number Q (0 Q 100) — number of queries. Each of the following lines contains a pair of query words separated by a single space. Each word consists only of English alphabet letters ( [a- zA-Z] ) and is at most 20 characters long.
Output :
For each pair of query words output either string synonyms or different .
Sample input
2
4
big large
large huge
small little
apple banana
6
same same
big huge
huge big
apple peach
big tall
peach PEACH
5
wood FORest
meadoW PrAirIe
WOOD Lumber
lumber forest
lumber forest
2
wood LUMBER
mEADOw fire
Sample output
synonyms
synonyms
synonyms
different
different
synonyms
synonyms
different
Explanation of the sample problem
In the first test-case there are 6 queries:
Harvard University and Google in 2010 estimated a total of 1,022,000 words and that the number would grow by several thousand each year.
So, logically your code should know about all of them in order to work properly. Such database of words, their linkage and access methods are provided by external APIs. Your applicatin should search for synonyms using one of these APIs.
Here are some of them-
https://openbase.com/categories/js/best-nodejs-dictionary-api-libraries
There's no built in function in Node to test whether some word is a synonym or not. But (fortunately) there are some libraries to do that.
Here's an example:
First, install the synonyms package from NPM:
npm i synonyms
Then, in your JS file:
const synonyms = require("synonyms"); // Import the "synonyms" library
const fs = require("fs");
fs.readFile("path/to/file", "utf-8", (err, file) => {
// Read the file
const lines = file.split("\n"); // Each line is split into an array
let result = lines.map(i => {
return i.split(" ");
}); // "result" holds an array of arrays. Index 0 is word 1 and index 1 is word 2
result.forEach(item => {
// Loop through our words
const s = synonyms(item[0].toLowerCase()); // Call the function
let allSynonyms = [];
for (let q in s) {
s[q].forEach(i => {
if(typeof i === "object")
i.forEach(allSynonyms.push);
else allSynonyms.push(i);
});
}
if (allSynonyms.includes(item[1].toLowerCase())) {
console.log(`Yes, "${item[0]}" is a synonym with "${item[1]}"!`); // Yes
} else {
// Nope!
console.log(`Nope, "${item[0]}" is not a synonym with "${item[1]}".`);
}
});
});
Just an example :) You can check the result here. Just run npm start.