skip to Main Content

I’m working on a speech to text module and, often enough, we have the following scenario:

var str1 = "It was great, I finally had some time to"
var str2 = "had some time to relax and catch up on my reading."

I need a function that will go through str1 backwards and detect if it has a duplicated sentence from str2 to combine without repeating it, into a concise final phrase like:

var result = "It was great, I finally had some time to relax and catch up on my reading."

This is my attempt:

function formPhrase(str1, str2) {
  const words1 = str1.split(' ');
  const words2 = str2.split(' ');

  const uniqueWords = [];

  for (const word of words1) {
    if (!uniqueWords.includes(word)) {
      uniqueWords.push(word);
    }
  }

  for (const word of words2) {
    if (!uniqueWords.includes(word)) {
      uniqueWords.push(word);
    }
  }

  const result = uniqueWords.join(' ');

  return result;
}

var str1 = "It was great, I finally had some time to"
var str2 = "had some time to relax and catch up on my reading."

console.log(formPhrase(str1, str2))

It does work for str1 and str2, but if the concatenated area has words that is also included on other parts of str1 and str2, it will fail, example: this code transforms this and transforms this string.

3

Answers


  1. To achieve this, you can try using the following function:

    function removeCommonSubstring(str1, str2) {
        // Initialize the combinedString with str1
        var combinedString = str1;
    
        // Loop through str2 from the end and check if it's a suffix of 
        combinedString
        for (var i = str2.length; i >= 0; i--) {
            if (combinedString.endsWith(str2.substring(0, i))) {
            // Remove the common suffix from combinedString and concatenate str2
            combinedString = combinedString.slice(0, combinedString.length - i) + str2;
              break; // Stop after the first match to avoid duplicates
            }
        }
    
      return combinedString;
    }
    
    Login or Signup to reply.
  2. Find the longest phrase from the beginning of the second sentence that the first sentence ends with. We slice phrases by spaces for speed:

    var str1 = "It was great, I finally had some time to"
    var str2 = "had some time to relax and catch up on my reading."
    
    let pos = -1, foundPos = -1;
    while((pos = str2.indexOf(' ', pos + 1)) > -1){
      if(str1.endsWith(str2.slice(0, pos))){
        foundPos = pos;
        break;
      }
    }
    
    console.log(foundPos > -1  ? str1 + str2.slice(foundPos) : 'not found!');
    Cycles: 1000000 / Chrome/117
    --------------------------------------------------
    Alexander    83/min  1.0x   88   87   83   88   90
    Arjun       241/min  2.9x  244  280  259  241  274
    --------------------------------------------------
    https://github.com/silentmantra/benchmark
    
    <script benchmark="1000000">
    
    
    var str1 = "It was great, I finally had some time to"
    var str2 = "had some time to relax and catch up on my reading."
    
    // @benchmark Arjun
    function removeCommonSubstring(str1, str2) {
        // Initialize the combinedString with str1
        var combinedString = str1;
    
        // Loop through str2 from the end and check if it's a suffix of 
        combinedString
        for (var i = str2.length; i >= 0; i--) {
            if (combinedString.endsWith(str2.substring(0, i))) {
            // Remove the common suffix from combinedString and concatenate str2
            combinedString = combinedString.slice(0, combinedString.length - i) + str2;
              break; // Stop after the first match to avoid duplicates
            }
        }
    
      return combinedString;
    }
    
    removeCommonSubstring(str1, str2)
    
    // @benchmark Alexander
    
    let pos = -1, foundPos = -1;
    while((pos = str2.indexOf(' ', pos + 1)) > -1){
      if(str1.endsWith(str2.slice(0, pos))){
        foundPos = pos;
        break;
      }
    }
    str1 + str2.slice(foundPos);
    
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
  3. function combineStrings(str1, str2) {
      // Split both strings into arrays of words
      const words1 = str1.split(" ");
      const words2 = str2.split(" ");
    
     // Initialize the result with str2
     let result = str2;
    
     // Iterate through words in str1 in reverse order
     for (let i = words1.length - 1; i >= 0; i--) {
         const word = words1[i];
    
         // Check if the word is not already in the result
         if (result.indexOf(word) === -1) {
             // Prepend the word to the result
             result = word + " " + result;
         }
       }  
      return result;
    }
    
    const str1 = "It was great, I finally had some time to";
    const str2 = "had some time to relax and catch up on my reading.";
    
    const result = combineStrings(str1, str2);
    console.log(result);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search