関連ニュースPHP、phpmorphy、MySQLの使用

「類似」レコードを決定する方法を共有したいと思います。 ブログやニュースリソースに役立つと思います。

この投稿の目的は原則を示すことです。著者は教授ではないので、実装は完全に一般的ではないかもしれません。 プログラマーですが、アマチュアです。





だから挑戦


ニュースはMySQLテーブルタイプに保存されます。



同じ表から最も類似したものを判別するには、各ニュース項目をページに表示する必要があります。

ここでは、タイトル、リード、ボディフィールドの内容に興味があります。 簡単にするために、すべてをゼロから作成していると仮定し、既存のレコードを処理する必要性は考慮しません。



タグフィールド


タグフィールドを追加します(実際、これらは擬似タグですが、サイト上のどこにも表示されません-このフィールドはテキストの比較にのみ必要です)。 フィールドタイプをVARCHAR(512)として示し、フルテキストタイプのインデックス(FULLTEXT(タグ))を追加します。



擬似タグ生成


ニュースをデータベースに書き込む前(INSERTステートメントの直前)に、タイトル、リード、ボディフィールドから疑似タグを生成します。 これを行うには、こちらから phpmorphyと辞書ダウンロードしてください



重要でない単語(ストップワード)を除外するには、$ストップワードの配列を作成し、ストップワード用のテキストファイルを使用します( たとえば 、stopwords.txtとして保存します)。



$stopwords=explode("\n", file_get_contents("stopwords.txt"));
      
      







次に、phpmorphyとその辞書を接続し、タイトル、リード、本文を組み合わせて、すべての単語をphpmorphyで実行します。

擬似タグ生成
 $lowercaseLetters = array("''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''"); $uppercaseLetters = array("''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''", "''"); function cyrUpper($str) { global $lowercaseLetters; global $uppercaseLetters; return str_replace("'", "", preg_replace($lowercaseLetters, $uppercaseLetters, $str)); } function cyrLower($str) { global $lowercaseLetters; global $uppercaseLetters; return str_replace("'", "", preg_replace( $uppercaseLetters,$lowercaseLetters, $str)); } function cleanUP ($new_string) { //$new_string=nl2br($new_string); $new_string= str_replace("-"," ",$new_string); $new_string= str_replace("\r\n"," ",$new_string); $new_string= str_replace("\r"," ",$new_string); $new_string= str_replace("\n"," ",$new_string); $new_string= str_replace("."," ",$new_string); $new_string = ereg_replace("[^0-9 ]", "",$new_string ); return $new_string; } require_once( 'morphy/src/common.php'); $text=cleanUP($_REQUEST[title]." ".$_REQUEST[lead]." ".$_REQUEST[body]." "); $aText = explode(' ',$text); $aPort = array(); $aMorph = array(); foreach ($aText as $word) $aMorph[] = cyrUpper($word);//  1251    // set some options $opts = array( 'storage' => PHPMORPHY_STORAGE_FILE, // Extend graminfo for getAllFormsWithGramInfo method call 'with_gramtab' => false, // Enable prediction by suffix 'predict_by_suffix' => true, // Enable prediction by prefix 'predict_by_db' => true ); $dir = 'morphy/dicts'; $lang = 'ru_RU'; // Create descriptor for dictionary located in $dir directory with russian language $dict_bundle = new phpMorphy_FilesBundle($dir, 'rus'); // Create phpMorphy instance try { $morphy = new phpMorphy($dict_bundle, $opts); } catch(phpMorphy_Exception $e) { throw new Exception('Error occured while creating stemmer instance: ' . $e->getMessage()); } try { if($getroot==22) $pseudo_root = $morphy->getPseudoRoot($aMorph);//     else $pseudo_root = $morphy->getBaseForm($aMorph);//   //   $getroot=TRUE } catch(phpMorphy_Exception $e) { throw new Exception('Error occured while text processing: ' . $e->getMessage()); } foreach ($pseudo_root as $roots){ $slovo=cyrLower($roots[0]); if (strlen( $slovo)>3 && !in_array($slovo,$stopwords) && count($roots)==1 ) { $tags.=$slovo." "; } } }
      
      







変数$ tagsのタグの結果リストは、accに書き込まれます。 テーブルフィールド。 その結果、このフィールドの各ニュースには、比較に使用する単語のリストがあります。





ソーステキスト

サムスンは、3次元メモリV-NANDを使用したソリッドステートハードドライブの生産を開始しました。 この技術により、ドライブのボリュームを増やすことができ、情報転送の2倍の速度を提供し、デバイスの信頼性を最大10倍に向上します。 480および960 GBのボリュームで現在作成されているSSDディスクは、企業サーバー専用です。 家庭用コンピュータに関しては、特定のリリース日はありませんでした。


生成された単語リスト:

デバイスは、テクノロジー企業のソリッドステート時間を増加させるだけで、サーバーの生産速度を向上させ、転送メモリの量を増やすことができます。




SQLクエリ


最も興味深いのは、このSQLクエリを使用して同様のレコードを決定することです。



  SELECT * FROM news WHERE MATCH (tags) AGAINST ('[    ]' ) > [ ]
      
      







ここでは、関連性の値はテキストの「類似性」です-実験(1つから開始)



All Articles