# RANKING ALGORITHM

25 Мая (ред)

I don't know if we will ever implement some ranking algorithm here but here are some few things I learns about algorithm and would like to share it with @evg maybe you can look into it and give me your feedback

So I designed this simple algorithm in PHP

``````<?php

// Function to calculate the ranking score for an article
function calculateRankingScore(\$article, \$userPreferences)
{
// Assign weights to different factors
\$likesWeight = 0.4;
\$bookmarksWeight = 0.2;
\$sharesWeight = 0.1;

// Get current timestamp
\$currentTimestamp = time();

// Calculate the age of the article in seconds
\$ageInSeconds = \$currentTimestamp - strtotime(\$article['timestamp']);

// Calculate the time decay factor based on the age of the article
\$timeDecayFactor = 1 / log10(max(\$ageInSeconds, 1) / 3600 + 2);

// Calculate the user interest factor based on their reading preferences
\$userInterestFactor = getUserInterestFactor(\$article['topic'], \$userPreferences);

// Calculate the final ranking score
\$rankingScore = (\$likesWeight * \$article['likes']) +
(\$bookmarksWeight * \$article['bookmarks']) +
(\$sharesWeight * \$article['shares']);

// Apply time decay and user interest factors
\$rankingScore *= \$timeDecayFactor * \$userInterestFactor;

return \$rankingScore;
}

// Function to get the user's interest factor for a specific topic
function getUserInterestFactor(\$topic, \$userPreferences)
{
// Check if the user has a preference for the topic
if (isset(\$userPreferences[\$topic])) {
// Get the user's interest factor for the topic
\$interestFactor = \$userPreferences[\$topic];
} else {
// Default interest factor if no preference is found
\$interestFactor = 0.5;
}

return \$interestFactor;
}

// Example articles array
\$articles = [
[
'id' => 1,
'likes' => 100,
'bookmarks' => 30,
'shares' => 20,
'timestamp' => '2023-05-24 10:00:00',
'topic' => 'technology'
],
[
'id' => 2,
'likes' => 50,
'bookmarks' => 10,
'shares' => 5,
'timestamp' => '2023-05-25 08:00:00',
'topic' => 'science'
],
];

// User preferences (example)
\$userPreferences = [
'technology' => 0.8,
'science' => 0.6,
'health' => 0.4
];

// Calculate ranking scores for all articles
foreach (\$articles as &\$article) {
\$article['rankingScore'] = calculateRankingScore(\$article, \$userPreferences);
}

// Sort the articles based on their ranking scores (in descending order)
usort(\$articles, function (\$a, \$b) {
return \$b['rankingScore'] - \$a['rankingScore'];
});

// Print the sorted articles
foreach (\$articles as \$article) {
echo 'Article ID: ' . \$article['id'] . ' - Ranking Score: ' . \$article['rankingScore'] . "\n";
}``````

Here is how it works

Weighted Factors: The algorithm assigns weights to different user interactions, such as likes, comments, bookmarks, and shares. These weights reflect the relative importance of each interaction in determining the ranking score. Adjust the weights according to your preferences to prioritize certain interactions over others.

Time Decay: The algorithm calculates the age of each article in seconds by comparing the current timestamp with the article's timestamp. It then applies a time decay factor to account for the freshness of the article. Older articles receive a lower time decay factor, which reduces their impact on the ranking score. The specific formula used here applies a logarithmic decay based on the age of the article.

User Interest Factor: The algorithm considers the user's reading preferences for different topics. It retrieves the user's interest factor for a specific topic from their preferences. If no preference is found, a default interest factor is used. The interest factor reflects how interested the user is in articles related to a particular topic. You can assign interest factors to topics based on user behavior or explicitly captured preferences.

Ranking Score Calculation: The algorithm calculates the ranking score for each article based on the weighted factors, time decay, and user interest factor. The ranking score is the sum of the weighted user interactions, multiplied by the time decay factor and the user interest factor.

Sorting: After calculating the ranking scores for all articles, the algorithm sorts the articles in descending order based on their ranking scores. This ensures that articles with higher scores appear at the top of the list.

Output: The algorithm then prints the sorted articles, displaying their ID and corresponding ranking score.

4
83
Cipherchunk + 97
Опубликовано в Chunk

## 4 Ответа

1. Evg 25 Мая (ред.)

Very interesting. Thank you.

I think the rating and ranking of articles will have to be done sooner or later anyway. Without it, nowhere. I will study and think about your decision. Everything seems to be logical. But we need to see how it will really work.

Очень интересно. Спасибо.

Думаю, рейтинг и ранжирование статей рано или поздно всё равно придется делать. Без него никуда. Я поизучаю и подумаю над вашим решением. Вроде всё логично. Но надо смотреть, как это будет реально работать.

1. Please when are you releasing the next update but I really when to start a communication with this your script.
The algorithm above is just a very minimal example but can be improved on when I tested it, it gives articles scores and aggregated them in descending order which make article with more relevant score outrank the others. But my thoughts on implementing this is it should be in a way that after the general feed you can have a way of sorting articles base on trending, conversational and hot where each of this sorting has its individual algorithm anyways let's see how it turns out

1. Evg 25 Мая

Yes, there are options here.

Regarding the new release, if I understand correctly, I don't have a deadline yet. I think within 2 weeks. It is necessary to check all the errors and make a couple of minor changes.

1. Okay