Please login or register.

Login with username, password and session length
Advanced search  

News:

You need/want an older version of sNews ? Download an older/unsupported version here.

Author Topic: [MOD] Automatically generate Meta Keywords (sNews 1.5)  (Read 4178 times)

Fotrane

  • Newbie
  • *
  • Karma: 1
  • Posts: 28
[MOD] Automatically generate Meta Keywords (sNews 1.5)
« on: November 07, 2006, 03:52:07 PM »

Here is a quick and dirty function to automatically generate a list of keywords based on the article text. Feel free to improvise, but as shown, it will automatically generate meta keywoirds if the field itself is left blank. Anything entered manually will(should) override this.

First: the function (I keep all addon fuinctionality in a file called "mods.php"):
Code: [Select]
// Automatically Generates Keywords based on article text
// SPF 11/7/2006

function kwgen($text){
//  Define Common Words
$commonwords = <<the a if i you still to when of if can while was and it in that with my so at for up on by this from be as me some her she time again were down back would his brother both all one needed not had after there out lot quite many know no but like who your will we is are or our have an more what us which its being into - later these following \\ such over ensure months
EOF;
$text2parse[] = $text;
$parsestring = "z ".strtolower(join($text2parse," "))." y";
$parsestring = str_replace (",", "", $parsestring);
$parsestring = str_replace ("\n", "", $parsestring);
$parsestring = str_replace (")", "", $parsestring);
$parsestring = str_replace ("(", "", $parsestring);
$parsestring = str_replace (".", "", $parsestring);
$parsestring = str_replace ("'", "", $parsestring);
$parsestring = str_replace ('"', "", $parsestring);

$commonarray = split(" ",$commonwords);

for ($i=0; $i   $parsestring = str_replace (" ".$commonarray[$i]." ", " ", $parsestring);
}

$parsestring = str_replace ("  ", " ", $parsestring);
$parsestring = str_replace ("  ", " ", $parsestring);
$parsestring = str_replace ("  ", " ", $parsestring);

$wordsarray = split(" ",$parsestring);

for ($i=0; $i   $word = $wordsarray[$i];
   if ($freqarray[$word]) {
       $freqarray[$word] += 1;
   } else {
       $freqarray[$word]=1;
   }
}


arsort($freqarray);

$i=0;
while (list($key, $val) = each($freqarray)) {    
   $i++;
   $freqall[$key] = $val;
   if ($i==15) {
      break;
   }
}

for ($i=0; $i   $j = $i+1;
   $word2 = $wordsarray[$i]." ".$wordsarray[$j];
   if ($freqarray2[$word2]) {
       $freqarray2[$word2] += 1;
   } else {
       $freqarray2[$word2]=1;
   }
}

arsort($freqarray2);

$i=0;
while (list($key, $val) = each($freqarray2)) {    
   $i++;
   $freqall[$key] = $val;
   if ($i==4) {
      break;
   }
}

for ($i=0; $i   $j = $i+1;
   $word3 = $wordsarray[$i]." ".$wordsarray[$j]." ".$wordsarray[$j+1];
   if ($freqarray3[$word3]) {
       $freqarray3[$word3] += 1;
   } else {
       $freqarray3[$word3]=1;
   }
}

arsort($freqarray3);

$i=0;
while (list($key, $val) = each($freqarray3)) {    
   $i++;
   $freqall[$key] = $val;
   if ($i==1) {
      break;
   }
}

arsort($freqall);

while (list($key, $val) = each($freqall)) {    
   $pagecontents .= "$key => $val
";
   $keys .= $key.", ";
}

// karate time

chop($keys);
return $keys;
}
Now, simply tell snews.php to auto-generate these words if necessary.
Search for:  case(check_if_unique('article_seftitle', $seftitle, $id)): notification(l('err_SEFExists'),l('errNote'),''); form_articles(''); unset($_SESSION['temp']); break;
Add the following below it:
Code: [Select]

case(empty($keywords_meta)): $keywords_meta = kwgen($text);
It's not perfect, but I feel at leasst auto-generated keywords are better than nonew at all.

Enjoy.
Logged

hyele

  • Newbie
  • *
  • Karma: 0
  • Posts: 44
[MOD] Automatically generate Meta Keywords (sNews 1.5)
« Reply #1 on: January 18, 2007, 10:02:24 AM »

fine! with any doubt
:)
Domenico
Logged

bobcat

  • Jr. Member
  • **
  • Karma: 1
  • Posts: 67
    • ESL Jobs by Country
Re: [MOD] Automatically generate Meta Keywords (sNews 1.5)
« Reply #2 on: May 27, 2008, 07:25:59 AM »

Anyone got this working?

I have pasted exactly as in this thread but am getting:

Parse error: syntax error, unexpected T_VARIABLE, expecting ';' in /home/qqqqqq/public_html/news/snews.php on line 2383

line 2383 is:
   $parsestring = str_replace (" ".$commonarray[$i]." ", " ", $parsestring);