skip to Main Content

I have two strings and I need to compare the words of the each sentence.
The strings are this two:

$correctSentence = "The year stations are summer, winter, spring and fall.";

$textSentence = "The year stations are four summer, winter and spring and summer.";

So, I make arrays of each sentence and I have the next arrays to compare the words who are no similar between sentences.

$correctArray = ["The", "year", "stations", "are", "summer", "winter", "spring", "and", "fall"];

$textArray = ["The", "year", "stations", "are", "four", "summer", "winter", "and", "spring", "and", "summer"];

I’m comparing the arrays in this form:
With a for loop, I compare every word of the $textArray with the words of $correctArray.

And if the word of $textArray[$i] is not similar to any word of $correctArray, that word is stored in a new array called $finalArray.

$finalArray = [];
for($i=0; $i<count($textArray); $i++){
     if(!in_array($textArray[$i], $correctArray)){
           array_push($finalArray, $textArray[$i]);
     }
}

The results of $finalArray are this:

$finalArray = ["four"];

But I need to store in $finalArray also the words that are duplicated in $textArray like this:

$finalArray = ["four", "and", "summer"];

Because "and" and "summer" are duplicated in the $textSentence and $textArray, so that words don’t are similar to the words of $correctSentence and $correctArray

I’m thinking the solution is delete the similar word of $correctArray when the if condition is true in this section:

$finalArray = [];
for($i=0; $i<count($textArray); $i++){
     if(!in_array($textArray[$i], $correctArray)){
           array_push($finalArray, $textArray[$i]);

           //In this section, delete the word of $correctArray 
           //which is similar of $textArray[$i]. 
           //For example: After comparing "summer" of $textArray[$i] with "summer" of $correctArray. 
           //The  record "summer" of $correctArray should be deleted.
           //And this way the following record "summer" of $textArray (which is duplicated) 
          //should be stored in $finalArray
     }
}

2

Answers


  1. You can count the looped string/word appear inside $textArray using array_count_values() and check if the counted value is more than 1 (means duplicate) than insert into $finalArray, also maybe before insert to $finalArray you need to check if the string/word already exists inside $finalArray or not.

    $finalArray = [];
        
    for($i=0; $i<count($textArray); $i++){
        if(!in_array($textArray[$i], $correctArray)){
            array_push($finalArray, $textArray[$i]);
        }
    
        // count how many times the string appear in the array
        $total_string_in_array = array_count_values($textArray)[$textArray[$i]];
        if ($total_string_in_array > 1) {
            // check if the duplicate string already exists inside $finalArray
            if ( ! in_array($textArray[$i], $finalArray)) {
                array_push($finalArray, $textArray[$i]);
            }
        }
    }
    

    Reference:

    Login or Signup to reply.
  2. PHP has native functions for everything that you need for this task.

    1. Extract words from the sentence without punctuation with str_word_count().
    2. Find differences between two flat arrays with array_diff().
    3. Count the occurrences of values in an array with array_count_values().
    4. Remove elements that have a value (count) less than 2 with array_filter().
    5. Get array keys with array_keys().
    6. Merge two arrays with array_merge().

    Code: (Demo)

    $correctSentence = "The year stations are summer, winter, spring and fall.";
    
    $textSentence = "The year stations are four summer, winter and spring and summer.";
    
    $correctArray = str_word_count($correctSentence, 1);
    $textArray = str_word_count($textSentence, 1);
    
    var_export(
        array_merge(
            array_diff($textArray, $correctArray),
            array_keys(array_filter(array_count_values($textArray), fn($v) => $v > 1))
        )
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search