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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
public function titleFeed()
{
/*
* Assemblage des titres
*/
$results = $this->getTitles();
$titres = '';
foreach ($results as $article){
$titres.= $article->titre . ' ';
}
// Scan des occurences
$scan = $this->getOccurences($titres);
$occurencies = $scan['wordcount'];
/*
* Insertion de chaque mot dans la base
*/
$table_titre_key = new Model_TitreKeys();
foreach ($occurencies as $word => $count){
if ($count < 10)
continue;
$data = array (
'word' => $word,
'count' => $count
);
$table_titre_key->insert($data);
echo $word . " inséré \n";
}
/**
* Scan les occurrences
*/
public function getOccurences($in)
{
/**
* Mots à effacer des titres
*/
$in = $this->_filtreWords($in);
$tok = strtok($in, " \n");
$toks = Array();
while($tok !== false){
if (trim($tok)){
$toks[] = trim($tok);
}
$tok = strtok(" \n");
}
$totalcount = count($toks);
//wordcount - 1 word
$wordcount = Array();
foreach($toks AS $word){
$word = strtolower($word);
if (!isset($wordcount[$word])){
$wordcount[$word]=1;
} else {
$wordcount[$word]++;
}
}
//wordcount = 2, 3 & 4 words
$toks2 = Array(); // Contient les groupes de 2 mots
$toks3 = Array(); // de 3 mots
$toks4 = Array(); // de 4 mots
for($i = 0; $i < count($toks); $i++){
if (isset($toks[$i]) && isset($toks[$i+1]) && trim($toks[$i]) && trim($toks[$i+1])){
$toks2[] = strtolower(trim($toks[$i])." ".trim($toks[$i+1]));
}
if (isset($toks[$i]) && isset($toks[$i+1]) && isset($toks[$i+2]) && trim($toks[$i]) && trim($toks[$i+1]) && trim($toks[$i+2])){
$toks3[] = strtolower(trim($toks[$i])." ".trim($toks[$i+1])." ".trim($toks[$i+2]));
}
if (isset($toks[$i]) && isset($toks[$i+1]) && isset($toks[$i+2]) && isset($toks[$i+3]) && trim($toks[$i]) && trim($toks[$i+1]) && trim($toks[$i+2]) && trim($toks[$i+3])){
$toks4[] = strtolower(trim($toks[$i])." ".trim($toks[$i+1])." ".trim($toks[$i+2])." ".trim($toks[$i+3]));
}
}
foreach($toks2 AS $phrase){
if (!isset($wordcount[$phrase])){
$wordcount[$phrase]=1;
}
else {
$wordcount[$phrase]++;
}
}
foreach($toks3 AS $phrase){
if (!isset($wordcount[$phrase])){
$wordcount[$phrase]=1;
}
else {
$wordcount[$phrase]++;
}
}
foreach($toks4 AS $phrase){
if (!isset($wordcount[$phrase])){
$wordcount[$phrase]=1;
}
else {
$wordcount[$phrase]++;
}
}
$densities = Array();
foreach($wordcount AS $word => $num){
//calculate densities
$phrasecount = count(explode(" ", $word));
$density = number_format( ($num / $totalcount) * $phrasecount * 100, 2);
$densities[$word] = $density;
}
// On trie le tableau des occurences par ordre décroissant
arsort($wordcount);
// Tri par ordre décroissant des densités
arsort($densities);
return array(
'wordcount' => $wordcount,
'densities' => $densities
);
} |
Partager