php - Calculate pair occurrences -


let's have following string:

foo,bar,baz bar,foo quux,baz,foo 

i'd generate list of pairs occur more 1 you'll following array:

[['foo', 'bar'], ['foo', 'baz']], 

maybe sounds silly, i've been banging head time on how this. problem set couple of mb's large and, if possible code needs efficient.

can push me in right direction? maybe kind of algorithm efficiency or sample code?

a lot of in advance!!!

divide , conquer.

prepare list of pairs of 1 line , concatenate lines list of pairs , find out repeating.

$string = <<<string foo,bar,baz bar,foo quux,baz,foo string;  $lines = array_map(function ($line) {     // split lines words     $words = explode(',', $line);      // filter repeats     $words = array_unique($words);      // sort words     sort($words);      return $words; }, preg_split('/\r/', $string));  function pairs($words) {     $length = count($words);      if ($length < 2) {         throw new exception('no pairs if length < 2');     }      $pairs = [];      // iterate start 1 before last word     ($i = 0; $i < $length - 1; $i++) {         // iterate next word end         ($j = $i + 1; $j < $length; $j++) {             $pairs[] = [$words[$i], $words[$j]];         }     }      return $pairs; }  $allpairs = []; $nonuniquepairs = []; foreach ($lines $words) {     $pairs = pairs($words);     foreach ($pairs $pair) {         // check if pair added , not in $nonuniquepairs array         if (in_array($pair, $allpairs, true) && !in_array($pair, $nonuniquepairs, true)) {             $nonuniquepairs[] = $pair;         }     }      $allpairs = array_unique(array_merge($allpairs, $pairs), sort_regular); } 

and results:

'allpairs' =>      array (size=5)       0 =>          array (size=2)           0 => string 'bar' (length=3)           1 => string 'baz' (length=3)       1 =>          array (size=2)           0 => string 'bar' (length=3)           1 => string 'foo' (length=3)       2 =>          array (size=2)           0 => string 'baz' (length=3)           1 => string 'foo' (length=3)       4 =>          array (size=2)           0 => string 'baz' (length=3)           1 => string 'quux' (length=4)       5 =>          array (size=2)           0 => string 'foo' (length=3)           1 => string 'quux' (length=4) 'nonuniquepairs' =>      array (size=2)       0 =>          array (size=2)           0 => string 'bar' (length=3)           1 => string 'foo' (length=3)       1 =>          array (size=2)           0 => string 'baz' (length=3)           1 => string 'foo' (length=3) 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -