PHP 中的关键字提取

Keyword extraction in PHP

我需要使用 php 从文本文件中提取关键字及其频率计数。我发现了一个只输出关键字的代码,例如。一些,文本,机器,自动售货机。我还需要频率计数以及这些关键字,例如。 some 3, text 2, machines 1, vending 1. 你能建议必要的修改吗?

function extractCommonWords($string)
{
      $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');

      $string = preg_replace('/ss+/i', '', $string);
      $string = trim($string); // trim the string
      $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
      $string = strtolower($string); // make it lowercase

      preg_match_all('/\b.*?\b/i', $string, $matchWords);
      $matchWords = $matchWords[0];

      $totalWords = count($matchWords[0]);

      foreach ( $matchWords as $key=>$item ) 
      {
          if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) 
          {
              unset($matchWords[$key]);
          }
      }
      $wordCountArr = array();
      if ( is_array($matchWords) ) 
      {
          foreach ( $matchWords as $key => $val ) 
          {
              $val = strtolower($val);
              if ( !isset($wordCountArr[$val])) 
              {
                  $wordCountArr[$val] = array();
              }
              if ( isset($wordCountArr[$val]['count']) ) 
              {
                  $wordCountArr[$val]['count']++;
              } 
              else 
              {
                  $wordCountArr[$val]['count'] = 1;
              }
          }
          arsort($wordCountArr);

          $wordCountArr = array_slice($wordCountArr, 0, 10);     
          foreach ($wordCountArr as $key => $val) 
          {
              $val['bytotal'] = $val['count'] / $totalWords;
          } 
      }       
      return $wordCountArr;
}
$text = "This is some text. This is some text. Vending Machines are great.";
$words = extractCommonWords($text);
echo implode(',', array_keys($words));
function extractCommonWords($string)
{
    $stopWords = array('i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www');

    $string = preg_replace('/ss+/i', '', $string);
    $string = trim($string);
    $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
    $string = strtolower($string); // make it lowercase
    echo $string."<br>";

    preg_match_all('/\b.*?\b/i', $string, $matchWords);
    $matchWords = $matchWords[0];
    $totalWords = count($matchWords[0]);

    foreach ( $matchWords as $key=>$item ){
        if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
            unset($matchWords[$key]);
        }
    }

    $wordCountArr = array();
    if ( is_array($matchWords) ) {
        foreach ( $matchWords as $key => $val ) {
            $val = strtolower($val);
            if (isset($wordCountArr[$val])){
                $wordCountArr[$val] += 1;
            } else {
                $wordCountArr[$val] = 1;
            }
        }
        arsort($wordCountArr);
    }
}

$text = "This is some text. This is some text. Vending Machines are great.";
$words = extractCommonWords($text);
foreach ($words as $word => $count){
    print ($word . " was found " . $count . " time(s)<br> ");
}