Changeset 432610
- Timestamp:
- 09/02/2011 11:07:20 PM (15 years ago)
- Location:
- post-miner/trunk/PostMiner
- Files:
-
- 2 added
- 5 edited
-
Hooks.php (modified) (6 diffs)
-
Indexer.php (modified) (3 diffs)
-
Installer.php (modified) (4 diffs)
-
Profile.php (added)
-
TermMapper.php (modified) (2 diffs)
-
TermVector.php (modified) (3 diffs)
-
VisitsMapper.php (added)
Legend:
- Unmodified
- Added
- Removed
-
post-miner/trunk/PostMiner/Hooks.php
r429550 r432610 13 13 protected $plugin; 14 14 15 /** 16 * User's profile 17 * @var PostMiner_Profile 18 */ 19 private $profile; 20 15 21 public function __construct(PostMiner_Plugin $plugin) 16 22 { … … 76 82 77 83 require_once POST_MINER__PATH . 'TermMapper.php'; 84 require_once POST_MINER__PATH . 'VisitsMapper.php'; 78 85 79 86 global $post; … … 93 100 } 94 101 95 $ids = $termMapper->getSimilarPostIds( $postVector, $postId ); 96 102 if( ! PostMiner_VisitsMapper::isVisited( $postId, $this->profile->getId() ) ) 103 { 104 $this->profile->addInterest( $postVector ); 105 106 PostMiner_VisitsMapper::setVisited( $postId, $this->profile->getId() ); 107 } 108 109 $this->profile->getInterest()->normalize(); 110 111 $ids = $termMapper->getSimilarPostIdsByProfile( $this->profile, $postId ); 112 97 113 if( empty( $ids ) ) 98 114 { … … 129 145 } 130 146 147 /** 148 * Adding "Post Miner" option under settings menu on admin page 149 */ 131 150 public function actionAdminMenu() 132 151 { … … 138 157 } 139 158 140 159 /** 160 * Renders settings page 161 * 162 * @return PostMiner_View 163 */ 141 164 public function pageSettings() 142 165 { … … 161 184 } 162 185 186 187 /** 188 * Reads user interest 189 * 190 */ 191 public function actionPluginsLoaded() 192 { 193 require_once( POST_MINER__PATH . 'Profile.php' ); 194 195 $uid = isset($_COOKIE[ PostMiner_Profile::COOKIE_NAME ]) ? 196 $_COOKIE[ PostMiner_Profile::COOKIE_NAME ] : false; 197 198 if( $uid ) 199 { 200 $this->profile = PostMiner_Profile::initByUid( $uid ); 201 } 202 203 if( ! $uid || ! $this->profile ) 204 { 205 $this->profile = PostMiner_Profile::init(); 206 } 207 208 } 209 163 210 } -
post-miner/trunk/PostMiner/Indexer.php
r429451 r432610 105 105 * get term vector from the post title 106 106 */ 107 $postVector = $postMiner->createTermVector( $data['title'], 1 .06);107 $postVector = $postMiner->createTermVector( $data['title'], 1 ); 108 108 109 109 /** … … 113 113 { 114 114 $categories = implode( ' ', $data['categoty'] ); 115 $categoryVector = $postMiner->createTermVector( $categories, 1 .04);115 $categoryVector = $postMiner->createTermVector( $categories, 1 ); 116 116 $postVector = $postVector->sum( $categoryVector ); 117 117 } … … 123 123 { 124 124 $tags = implode( ' ', $data['post_tag'] ); 125 $tagsVector = $postMiner->createTermVector( $tags, 1 .05);125 $tagsVector = $postMiner->createTermVector( $tags, 1 ); 126 126 $postVector = $postVector->sum( $tagsVector ); 127 127 } -
post-miner/trunk/PostMiner/Installer.php
r429552 r432610 35 35 36 36 /** 37 * SQL statement for creating profiles table 38 * @var string 39 */ 40 static private $SQL_PROFILE = "CREATE TABLE IF NOT EXISTS `%s_profiles` ( 41 `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 42 `uid` varchar(32) NOT NULL, 43 `created` datetime NOT NULL, 44 `updated` datetime NOT NULL, 45 PRIMARY KEY (`id`), 46 KEY `uid` (`uid`) 47 ) ENGINE=MyISAM ;"; 48 49 /** 50 * SQL statement for creating profile interest table 51 * @var string 52 */ 53 static private $SQL_PROFILE_INTEREST = "CREATE TABLE IF NOT EXISTS `%s_profile_interest` ( 54 `profile_id` bigint(20) unsigned NOT NULL, 55 `postminer_term_id` int(10) unsigned NOT NULL, 56 `weight` double NOT NULL, 57 KEY `object_id` (`profile_id`,`postminer_term_id`) 58 ) ENGINE=MyISAM ;"; 59 60 /** 61 * SQL statement for creating visits table 62 * @var string 63 */ 64 static private $SQL_VISITED = "CREATE TABLE IF NOT EXISTS `%s_visits` ( 65 `object_id` bigint(20) unsigned NOT NULL, 66 `postminer_profile_id` int(10) unsigned NOT NULL, 67 KEY `postminer_profile_id` (`postminer_profile_id`) 68 ) ENGINE=MyISAM"; 69 70 /** 37 71 * SQL statement for dropping terms table 38 72 * @var string … … 45 79 */ 46 80 static private $SQL_DROP_TERMS_RELATION = "DROP TABLE `%s_term_relationships`"; 81 82 /** 83 * SQL statement for dropping profile table 84 * @var string 85 */ 86 static private $SQL_DROP_PROFILE = "DROP TABLE `%s_profiles`"; 87 88 /** 89 * SQL statement for dropping profile interest table 90 * @var string 91 */ 92 static private $SQL_DROP_PROFILE_INTEREST = "DROP TABLE `%s_profile_interest`"; 93 94 /** 95 * SQL statement for dropping visits table 96 * @var string 97 */ 98 static private $SQL_DROP_VISITED = "DROP TABLE `%s_visits`"; 99 47 100 48 101 /** … … 62 115 $wpdb->get_results( sprintf( self::$SQL_TERMS_RELATION, $prefix ) ); 63 116 117 $wpdb->get_results( sprintf( self::$SQL_PROFILE, $prefix ) ); 118 $wpdb->get_results( sprintf( self::$SQL_PROFILE_INTEREST, $prefix ) ); 119 120 $wpdb->get_results( sprintf( self::$SQL_VISITED, $prefix ) ); 121 64 122 add_option("post-miner_db_version", '1.0.0'); 123 124 update_option("post-miner_db_version", '1.0.1'); 65 125 66 126 add_option("post-miner_recommendations_header", "You might be also interested in:"); … … 86 146 $wpdb->get_results( sprintf( self::$SQL_DROP_TERMS_RELATION, $prefix ) ); 87 147 148 $wpdb->get_results( sprintf( self::$SQL_DROP_PROFILE, $prefix ) ); 149 $wpdb->get_results( sprintf( self::$SQL_DROP_PROFILE_INTEREST, $prefix ) ); 150 151 $wpdb->get_results( sprintf( self::$SQL_DROP_VISITED, $prefix ) ); 152 88 153 delete_option("post-miner_db_version"); 89 154 delete_option("post-miner_recommendations_header"); -
post-miner/trunk/PostMiner/TermMapper.php
r429538 r432610 41 41 return new PostMiner_TermVector( $vector ); 42 42 } 43 /** 44 45 */ 43 46 44 public function getSimilarPostIds( PostMiner_TermVector $vector, $ignorePostId = false ) 47 public function getSimilarPostIdsByProfile( PostMiner_Profile $profile, $ignorePostId ) 48 { 49 $sql = ' SELECT u.object_id, if( pv.object_id is null, sum(u.weight), sum(u.weight) -1000) coorelation, pv.object_id visited_id 50 FROM 51 ( 52 SELECT ptr.object_id, (ptr.weight * pi.weight) weight 53 FROM `%spostminer_term_relationships` ptr 54 JOIN `%spostminer_terms` pt ON pt.id = ptr.postminer_term_id 55 JOIN `%spostminer_profile_interest` pi ON (pi.postminer_term_id = ptr.postminer_term_id) 56 WHERE profile_id = %d 57 ) as u 58 LEFT JOIN `%spostminer_visits` pv ON (pv.object_id = u.object_id) 59 WHERE u.object_id != "1738" group by u.object_id order by coorelation desc limit 10'; 60 61 62 $posts = array(); 63 64 foreach( $this->wpdb->get_results( sprintf( $sql, $this->wpdb->prefix, 65 $this->wpdb->prefix, 66 $this->wpdb->prefix, 67 $profile->getId(), 68 $this->wpdb->prefix ) ) as $row ) 69 { 70 $posts[] = $row->object_id; 71 } 72 73 return $posts; 74 75 } 76 77 public function getSimilarPostIds( PostMiner_TermVector $vector, $profileId, $ignorePostId = false ) 45 78 { 46 79 $_sql = 'SELECT object_id, (weight * %s) weight … … 49 82 50 83 $subSelect = array(); 84 $ids = array(); 51 85 foreach( $vector->getValues() as $termId => $weight ) 52 86 { 87 $ids[] = $termId; 53 88 $subSelect[] = sprintf( $_sql, $weight, $this->wpdb->prefix, $termId ); 54 89 } 90 91 $sql = 'SELECT u.object_id, if( pv.object_id is null, sum(u.weight), sum(u.weight) -1000) coorelation, pv.object_id visited_id FROM( '; 92 //$sql.= implode( ' UNION ', $subSelect ); 93 $sql.= ') as u '; 94 95 $sql.= 'LEFT JOIN `%spostminer_visits` pv ON (pv.object_id = u.object_id)'; 96 97 if( $ignorePostId ) 98 { 99 $sql.= sprintf(' WHERE u.object_id != "%d" ', $ignorePostId ); 100 } 101 else 102 { 103 $sql.= ' WHERE 1=1'; 104 } 105 106 $sql.= 'group by u.object_id order by coorelation desc limit 10'; 55 107 56 108 57 $sql = 'SELECT object_id, sum(weight) coorelation FROM( ';58 $sql.= implode( ' UNION ', $subSelect );59 $sql.= ') as u ';60 61 if( $ignorePostId )62 {63 $sql.= sprintf(' WHERE object_id != "%d" ', $ignorePostId );64 }65 66 $sql.= 'group by object_id order by coorelation desc limit 10';67 68 109 $posts = array(); 69 foreach( $this->wpdb->get_results( $sql) as $row )110 foreach( $this->wpdb->get_results( sprintf( $sql, $this->wpdb->prefix, $this->wpdb->prefix, $profileId ) ) as $row ) 70 111 { 71 112 $posts[] = $row->object_id; -
post-miner/trunk/PostMiner/TermVector.php
r429538 r432610 51 51 52 52 /** 53 * Returns new terms vector which is aum ofvectors53 * Add vectors 54 54 * 55 55 * @param PostMiner_TermVector $vector vector to be added … … 62 62 63 63 $allDimensions = array_merge( array_keys( $v1 ), array_keys( $v2 ) ); 64 $allDimensions = array_unique( $allDimensions ); 65 64 66 $newVector = array(); 65 67 … … 68 70 $val1 = isset( $v1[ $name ] ) ? $v1[ $name ] : 0; 69 71 $val2 = isset( $v2[ $name ] ) ? $v2[ $name ] : 0; 70 $newVector[ $name ] = $val1 + $val2; 72 73 $this->values[ $name ] = $val1 + $val2; 71 74 } 72 73 return new PostMiner_TermVector( $newVector ); 75 76 return $this; 77 } 78 79 /** 80 * Multiply vector by a number 81 * 82 * @param float $number 83 * @return PostMiner_TermVector 84 */ 85 public function mul( $number ) 86 { 87 foreach( $this->values as $name => $value ) 88 { 89 $this->values[ $name ] *= $value; 90 } 91 92 return $this; 74 93 } 75 94
Note: See TracChangeset
for help on using the changeset viewer.