1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| <meta charset="UTF-8"/><pre><?php
ini_set('diplay_errors', 'On');
mb_internal_encoding("UTF-8");
define('FILENAME1', './text_files/xaa');
define('FILENAME2', './text_files/xab');
// chargement des fichiers
$txt1 = file_get_contents(FILENAME1);
$txt2 = file_get_contents(FILENAME2);
// on fait une copie en lowercase
$txt1_lc = mb_strtolower($txt1);
$txt2_lc = mb_strtolower($txt2);
// construction des dictionaires
function build_dict(&$dict, $str) {
$dict = array();
$arr = preg_split('~\b~u', $str);
$offset = strlen(array_shift($arr));
$m_size = count($arr);
$length = array_map('strlen', $arr);
$arr = preg_replace('~\s+~u', '\s+', $arr);
for ($i=0; $i<$m_size ; ++$i) {
$key = $arr[$i] . $arr[$i+1] . $arr[$i+2];
$dict[$key]['offsets'][] = $offset;
$dict[$key][$arr[$i+3] . $arr[$i+4]][] = $offset;
$offset += $length[$i] + $length[++$i];
}
}
build_dict($dict1, $txt1_lc);
build_dict($dict2, $txt2_lc);
// intersections
$dict1 = array_intersect_key($dict1, $dict2);
$dict2 = array_intersect_key($dict2, $dict1);
// traitement
$results = array();
$n = 1; // nombre de mots
while($dict1) {
$n++;
foreach ($dict1 as $k=>$v) {
$dict1[$k] = array_intersect_key($dict1[$k], $dict2[$k]);
// on vérifie qu'il y a des mots suivants en commun (aprés intersection)
if (count($dict1[$k])>1) {
$dict2[$k] = array_intersect_key($dict2[$k], $dict1[$k]); // dans ce cas on réduit également $dict2[$k]
// on récupère et on enlève les anciennes listes d'offsets
$offsets1 = array_shift($dict1[$k]);
$offsets2 = array_shift($dict2[$k]);
foreach ($dict1[$k] as $next_word=>$offsets) {
// on créé une nouvelle clef avec l'ancienne et le mot suivant
$new_key = $k . $next_word;
// on créé la pattern de recherche du mot suivant
$pattern = '~' . $new_key . '\K\W++\w+~u';
// on enlève les offsets qui ont des correspondances plus grandes
$offsets1 = array_diff($offsets1, $offsets);
$offsets2 = array_diff($offsets2, $dict2[$k][$next_word]);
// on créé les listes d'offsets pour les nouvelles clefs
$dict1[$new_key]['offsets'] = $offsets;
$dict2[$new_key]['offsets'] = $dict2[$k][$next_word];
// on cherche le mot suivant pour chaque offset
// s'il existe on créé une nouvelle entrée
foreach ($offsets as $offset) {
if ( preg_match($pattern, $txt1_lc, $m, 0, $offset) )
$dict1[$new_key][preg_replace('~\s+~u', '\s+', $m[0])][] = $offset;
}
foreach ($dict2[$new_key]['offsets'] as $offset) {
if ( preg_match($pattern, $txt2_lc, $m, 0, $offset) )
$dict2[$new_key][preg_replace('~\s+~u', '\s+', $m[0])][] = $offset;
}
}
if ($offsets1 && $offset2)
$results[$n][$k] = array('1' => $offsets1, '2' => $offsets2);
} else // il n'y a pas de correspondances de n+1 mots, on stocke les résultats pour n mots.
$results[$n][$k] = array('1' => $dict1[$k]['offsets'], '2' => $dict2[$k]['offsets']);
unset($dict1[$k], $dict2[$k]);
}
}
unset($dict1, $dict2, $txt1_lc, $txt2_lc, $offsets1, $offsets2, $offset, $next_word, $offsets, $new_key, $pattern, $k, $v, $n);
echo "\n\n<strong>results</strong>:\n" . print_r($results, true); |
Partager