skip to Main Content

I want to generate two word combination from the word with the same category. Example I have this table:

+--------------+----------+
|     Word     | Category |
+--------------+----------+
| human        | social   |
| politic      | social   |
| law          | social   |
| artificial   | science  |
| intelligence | science  |
+--------------+----------+

and I want the output like this:

+-------------------------+----------+
|          Word           | Category |
+-------------------------+----------+
| human-law               | social   |
| human-politic           | social   |
| politic-law             | social   |
| artificial-intelligence | science  |
+-------------------------+----------+

But I have no idea how to do that. This is my code, and this code just combine all word without category filter.

Model Class (md_classification):

function getWords(){
    $stat = $this->db->query("SELECT DISTINCT(word), category from tb_words group by word, category");
    return $stat->result_array();
}

function getAllCombinations($array){
    $combinations = array();

    foreach ($array as $x)
        foreach ($array as $y) {
            if ($x != $y && !in_array($y . '-' . $x, $combinations))
                array_push($combinations, $x . '-' . $y);
        }
    return $combinations;
}

Controller:

$getWord = $this->md_classification->getWords();

foreach ($getWord as $row) {
    $word[] = $row['word'];
    $category[] = $row['category'];
}
$result = $this->md_classification->getAllCombinations($word);

2

Answers


  1. your current getAllCombinations function will give you reverse duplcates since you are iterating on all indexes inside a nested loop.
    I think the answer here is using a basic for loop as follows

    function getAllCombinations($array){
        $combinations = array();
    
        for ($i = 0; $i < sizeof($array); $i++)
            for ($j = $i+1; $j < sizeof($array); $j++) 
                if ($array[$i]['category'] == $array[$j]['category'])
                    array_push($combinations, $array[$i]['word'] . '-' . $array[$j]['word']);
            
        return $combinations;
    }
    

    notice that the function should receive the array of results the same way it came from the DB. meaning:

    $result = $this->md_classification->getAllCombinations($getWord);
    

    with this method, you won’t get any duplicates since you won’t iterate on the same rows twice. hope it helped.

    Login or Signup to reply.
  2. I can advice the MySQL solution for this problem:

    select concat(w.Word, ' - ', w1.Word) Words, w.Category
    from words w
    join words w1 on w.Category = w1.Category
    where w.Word < w1.Word
    ;
    

    result:

    +===========================+==========+
    | Words                     | Category |
    +===========================+==========+
    | human - politic           | social   |
    +---------------------------+----------+
    | law - politic             | social   |
    +---------------------------+----------+
    | human - law               | social   |
    +---------------------------+----------+
    | artificial - intelligence | science  |
    +---------------------------+----------+
    

    test SQL code here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search