Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

388
Views
whether given two words are synonyms or not

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:

  1. If the pair of words is declared synonymous in the input, then they are synonyms.
  2. Being synonyms doesn't depend on order, e.g. if big is a synonym for large then large is a synonym for big.
  3. We can derive the synonymous relationship indirectly: if big is a synonym for large and large is a synonym for huge then big is a synonym for huge.
  4. If two words differ only by case, they are synonyms, e.g. same is a synonym for both SAmE, SAME and also same (itself).
  5. If none of the above rules can be used to decide whether two words are synonyms, then they are not.

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:

  1. Words are the same.
  2. Words are derived synonyms.
  3. Symmetric to 2nd query.
  4. No rule can be used to derive the synonym pair.
  5. No rule can be used to derive the synonym pair, even though they are synonyms in English.
  6. Words differ only in case. 2 nd test case:
  7. Defined as synonyms by 3rd rule. The case does not matter.
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!