From 171fef62c5310d8585dcaa2d806245a9f91ee201 Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Thu, 21 Aug 2014 13:24:55 +0400 Subject: [PATCH] add aging for hashtags --- src/twister.cpp | 52 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index 24a19f21..9e1cfd39 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -70,6 +70,13 @@ static std::map m_users; static CCriticalSection cs_seenHashtags; static std::map m_seenHashtags; +const double hashtagHalfLife = 8*60*60; // Halve votes within 8 hours (sec) +const double hashtagExpiration = 7*24*60*60; // Remove a hashtag from the list after ~ hashtagExpiration*count (sec) +const int hashtagTimerInterval = 60; // Timer interval (sec) + +const double hashtagAgingFactor = pow(0.5, hashtagTimerInterval/hashtagHalfLife); +const double hashtagCriticalValue = pow(0.5, hashtagExpiration/hashtagHalfLife); + class SimpleThreadCounter { public: SimpleThreadCounter(CCriticalSection *lock, int *counter, const char *name) : @@ -848,6 +855,34 @@ void ThreadSessionAlerts() } } +void ThreadHashtagsAging() +{ + SimpleThreadCounter threadCounter(&cs_twister, &m_threadsToJoin, "hashtags-aging"); + + while(!m_ses && !m_shuttingDownSession) { + MilliSleep(200); + } + + while (m_ses && !m_shuttingDownSession) { + + { + LOCK(cs_seenHashtags); + for( std::map::iterator iter = m_seenHashtags.begin(); iter != m_seenHashtags.end(); ) { + iter->second *= hashtagAgingFactor; + if( iter->second < hashtagCriticalValue ) { + m_seenHashtags.erase(iter++); + } else { + ++iter; + } + } + } + + for(int i=0; i curTime ) msgTime = curTime; - - double vote = 1.0; - if( msgTime + (2*3600) < curTime ) { - double timeDiff = (curTime - msgTime); - timeDiff /= (2*3600); - vote /= timeDiff; - } - // split and look for hashtags vector tokens; set hashtags; @@ -1403,6 +1429,12 @@ void updateSeenHashtags(std::string &message, int64_t msgTime) } if( hashtags.size() ) { + boost::int64_t curTime = GetAdjustedTime(); + if( msgTime > curTime ) msgTime = curTime; + + double timeDiff = (curTime - msgTime); + double vote = pow(0.5, timeDiff/hashtagHalfLife); + LOCK(cs_seenHashtags); BOOST_FOREACH(string const& word, hashtags) { if( m_seenHashtags.count(word) ) {