Plugin Directory

Changeset 432610


Ignore:
Timestamp:
09/02/2011 11:07:20 PM (15 years ago)
Author:
lukeuk
Message:

recording user interest

Location:
post-miner/trunk/PostMiner
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • post-miner/trunk/PostMiner/Hooks.php

    r429550 r432610  
    1313    protected $plugin;
    1414   
     15    /**
     16     * User's profile
     17     * @var PostMiner_Profile
     18     */
     19    private $profile;
     20   
    1521    public function __construct(PostMiner_Plugin $plugin)
    1622    {
     
    7682       
    7783        require_once POST_MINER__PATH . 'TermMapper.php';
     84        require_once POST_MINER__PATH . 'VisitsMapper.php';
    7885       
    7986        global $post;
     
    93100        }
    94101       
    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       
    97113        if( empty( $ids ) )
    98114        {
     
    129145    }
    130146   
     147    /**
     148     * Adding "Post Miner" option under settings menu on admin page
     149     */
    131150    public function actionAdminMenu()
    132151    {
     
    138157    }
    139158   
    140    
     159    /**
     160     * Renders settings page
     161     *
     162     * @return PostMiner_View
     163     */
    141164    public function pageSettings()
    142165    {
     
    161184    }
    162185   
     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   
    163210}
  • post-miner/trunk/PostMiner/Indexer.php

    r429451 r432610  
    105105         * get term vector from the post title
    106106         */
    107         $postVector = $postMiner->createTermVector( $data['title'], 1.06 );
     107        $postVector = $postMiner->createTermVector( $data['title'], 1 );
    108108           
    109109        /**
     
    113113        {
    114114            $categories = implode( ' ', $data['categoty'] );
    115             $categoryVector = $postMiner->createTermVector( $categories, 1.04 );
     115            $categoryVector = $postMiner->createTermVector( $categories, 1 );
    116116            $postVector = $postVector->sum( $categoryVector );
    117117        }
     
    123123        {
    124124            $tags = implode( ' ', $data['post_tag'] );
    125             $tagsVector = $postMiner->createTermVector( $tags, 1.05 );       
     125            $tagsVector = $postMiner->createTermVector( $tags, 1 );       
    126126            $postVector = $postVector->sum( $tagsVector );
    127127        }
  • post-miner/trunk/PostMiner/Installer.php

    r429552 r432610  
    3535   
    3636    /**
     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    /**
    3771     * SQL statement for dropping terms table
    3872     * @var string
     
    4579     */
    4680    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   
    47100   
    48101    /**
     
    62115        $wpdb->get_results( sprintf( self::$SQL_TERMS_RELATION, $prefix ) );
    63116       
     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       
    64122        add_option("post-miner_db_version", '1.0.0');
     123       
     124        update_option("post-miner_db_version", '1.0.1');
    65125       
    66126        add_option("post-miner_recommendations_header", "You might be also interested in:");
     
    86146        $wpdb->get_results( sprintf( self::$SQL_DROP_TERMS_RELATION, $prefix ) );
    87147       
     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       
    88153        delete_option("post-miner_db_version");
    89154        delete_option("post-miner_recommendations_header");
  • post-miner/trunk/PostMiner/TermMapper.php

    r429538 r432610  
    4141        return new PostMiner_TermVector( $vector );
    4242    }
     43    /**
     44
     45     */
    4346   
    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 )
    4578    {
    4679        $_sql = 'SELECT object_id, (weight * %s) weight
     
    4982
    5083        $subSelect = array();
     84        $ids = array();
    5185        foreach( $vector->getValues() as $termId => $weight )
    5286        {
     87            $ids[] = $termId;
    5388            $subSelect[] = sprintf( $_sql, $weight, $this->wpdb->prefix, $termId );
    5489        }
     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';
    55107       
    56108
    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        
    68109        $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 )
    70111        {
    71112            $posts[] = $row->object_id;
  • post-miner/trunk/PostMiner/TermVector.php

    r429538 r432610  
    5151   
    5252    /**
    53      * Returns new terms vector which is aum of vectors
     53     * Add vectors
    5454     *
    5555     * @param PostMiner_TermVector $vector vector to be added
     
    6262       
    6363        $allDimensions = array_merge( array_keys( $v1 ), array_keys( $v2 ) );
     64        $allDimensions = array_unique( $allDimensions );
     65       
    6466        $newVector = array();
    6567       
     
    6870            $val1 = isset( $v1[ $name ] ) ? $v1[ $name ] : 0;
    6971            $val2 = isset( $v2[ $name ] ) ? $v2[ $name ] : 0;
    70             $newVector[ $name ] = $val1 + $val2;
     72           
     73            $this->values[ $name ] = $val1 + $val2;
    7174        }
    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;
    7493    }
    7594   
Note: See TracChangeset for help on using the changeset viewer.