I've created a method that combine some "tiles" to find all sequences to compose a word.
<?php
/* convert word to tiles sequences */
function word2sequences($word, $tiles, &$founds = array()) {
foreach ($tiles as $tile_id => $tile) {
if (!preg_match("/^(|[;0-9]{1,})(".$tile.")(.*)$/i", $word, $tok)) continue;
$found = $tok[1].";".$tile_id.";".$tok[3];
if (!array_key_exists($word, $founds)) $founds[$tile] = array();
$founds[$tile] = word2sequences($found, $tiles);
}
return $founds;
}
/* example */
$tiles = [1 => "A", 21 => "B", 34 => "AH", 40 => "R", 51 => "S", 83 => "SA", 14 => "T", 21 => "TA"];
$word = "stars";
$sequences = word2sequences($word, $tiles);
echo "<pre>";
var_dump($sequences);
?>
the output is
array(1) {
["S"]=>
array(2) {
["TA"]=>
array(1) {
["R"]=>
array(1) {
["S"]=>
array(0) {
}
}
}
["T"]=>
array(1) {
["A"]=>
array(1) {
["R"]=>
array(1) {
["S"]=>
array(0) {
}
}
}
}
}
}
so I know that to compose the word "stars" I can use the tiles "S,TA,R,S" or "S,T,A,R,S".
there is a way to walk the $requences array to extract all the sequences in a simplified format like:
$sequences = array(
0 => array("S", "TA", "R", "S"),
1 => array("S", "T", "A", "R", "S")
)
Tnx in advance.
source https://stackoverflow.com/questions/71237759/php-rebuild-words-using-keys-of-a-multidimensional-array
No comments:
Post a Comment