Need help with the problem "Compare two string with sub arrays"

Revision en2, by mugy_mb, 2022-12-13 14:36:49

hey

I am trying to solve this problem for the last 2 days but I am not able to solve it ,can any of you please solve it in JavaScript?

###

Exercise

As part of a Natural Language Processing application, we want to build a simple module that can detect if two sentences are similar. The first version of this module will have a simple, naive implementation.

Function and Input

Create a function called isSimilar(sentence1, sentence2, similarWords)

Both sentence1 and sentence2 are plain strings (without most punctuation)

E.g. We are doing fine

similarWords is an array of arrays that contains all the words that are similar to each other

E.g. [ ['fine', 'great'], ['happy', 'glad', 'cheery'] ]

Algorithm

The function should do the following to determine if the two sentences are similar, and if so return true or false:

Check both sentences have an equal number of words

Ignore casing

Check words not in similarWords appear as-is in the second sentence

Check words in simlarWords have the same or an alternate word in the same position in the second sentence

Example

Sentence 1: "The Greatest Trick"
Sentence 2: "The supreme con"
Similar Words: [ ["supreme", "greatest"], ["trick", "hoax", "con"] ]
Output: true


Example 2

Sentence 1: "I'm very glad"
Sentence 2: "I am super happy"
Similar Words: [ ["happy", "glad", "cheery"], ["very", "super", "extremely"] ]
Output: false

Tags js, c++, compare function

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English mugy_mb 2022-12-13 14:36:49 280
en1 English mugy_mb 2022-12-13 14:35:23 1817 Initial revision (published)