<? global $wpdb; define('INTERESTABLE', "{$wpdb->prefix}interes"); define('BATCHSIZE',…
- User
- Assistant
Certainly! Below is the adjusted code incorporating the new parameters $identifier and $similar_to. The adjustments allow the algorithm to:
calcularFeedPersonalizado FunctionFirst, we adjust the calcularFeedPersonalizado function to pass the new parameters to calcularPuntosPost:
function calcularFeedPersonalizado($userId, $identifier = '', $similar_to = null)
{
$datos = obtenerDatosFeedConCache($userId);
if (empty($datos)) {
return [];
}
$usuario = get_userdata($userId);
$esAdmin = in_array('administrator', (array) $usuario->roles);
$vistas_posts_processed = obtenerYProcesarVistasPosts($userId);
$posts_personalizados = [];
$resumenPuntos = [];
foreach ($datos['author_results'] as $post_id => $post_data) {
$puntosFinal = calcularPuntosPost(
$post_id,
$post_data,
$datos,
$esAdmin,
$vistas_posts_processed,
$identifier,
$similar_to
);
$posts_personalizados[$post_id] = $puntosFinal;
$resumenPuntos[$post_id] = round($puntosFinal, 2);
}
arsort($posts_personalizados);
arsort($resumenPuntos);
logResumenDePuntos($userId, $resumenPuntos);
return $posts_personalizados;
}
calcularPuntosPost FunctionNext, we update calcularPuntosPost to accept and process the new parameters:
function calcularPuntosPost($post_id, $post_data, $datos, $esAdmin, $vistas_posts_processed, $identifier = '', $similar_to = null)
{
$autor_id = $post_data->post_author;
$post_date = $post_data->post_date;
$puntosUsuario = in_array($autor_id, $datos['siguiendo']) ? 20 : 0;
$puntosIntereses = calcularPuntosIntereses($post_id, $datos);
// Calculate points based on identifier matches
$puntosIdentifier = 0;
if (!empty($identifier)) {
$puntosIdentifier = calcularPuntosIdentifier($post_id, $identifier);
}
// Calculate points based on similarity to the specified post
$puntosSimilarTo = 0;
if (!empty($similar_to)) {
$puntosSimilarTo = calcularPuntosSimilarTo($post_id, $similar_to);
}
$likes = isset($datos['likes_by_post'][$post_id]) ? $datos['likes_by_post'][$post_id] : 0;
$puntosLikes = 30 + $likes;
$diasDesdePublicacion = (current_time('timestamp') - strtotime($post_date)) / (3600 * 24);
$factorTiempo = pow(0.99, $diasDesdePublicacion);
$metaVerificado = isset($datos['verificado_results'][$post_id]->meta_value) && $datos['verificado_results'][$post_id]->meta_value == '1';
$metaPostAut = isset($datos['postAut_results'][$post_id]->meta_value) && $datos['postAut_results'][$post_id]->meta_value == '1';
$puntosFinal = calcularPuntosFinales(
$puntosUsuario,
$puntosIntereses + $puntosIdentifier + $puntosSimilarTo,
$puntosLikes,
$metaVerificado,
$metaPostAut,
$esAdmin
);
if (isset($vistas_posts_processed[$post_id])) {
$vistas = $vistas_posts_processed[$post_id]['count'];
$reduccion_por_vista = 0.50;
$factorReduccion = pow(1 - $reduccion_por_vista, $vistas);
$puntosFinal *= $factorReduccion;
}
$aleatoriedad = mt_rand(0, 20);
$puntosFinal = $puntosFinal * (1 + ($aleatoriedad / 100));
$ajusteExtra = mt_rand(-50, 50);
$puntosFinal = $puntosFinal * $factorTiempo;
$puntosFinal += $ajusteExtra;
return max($puntosFinal, 0);
}
calcularPuntosIdentifierWe create a new function to calculate points based on the frequency of the identifier(s) in the post:
function calcularPuntosIdentifier($post_id, $identifier)
{
// Get post content and title
$post_content = get_post_field('post_content', $post_id);
$post_title = get_post_field('post_title', $post_id);
// Combine title and content
$full_text = $post_title . ' ' . $post_content;
// Normalize text: lowercase and remove special characters
$full_text = strtolower($full_text);
$full_text = preg_replace('/[^\w\s]/u', '', $full_text);
// Split into words
$words_in_post = preg_split('/\s+/', $full_text, -1, PREG_SPLIT_NO_EMPTY);
// Process identifier(s)
if (is_array($identifier)) {
$identifiers = $identifier;
} else {
$identifiers = preg_split('/\s+/', strtolower($identifier), -1, PREG_SPLIT_NO_EMPTY);
}
$count = 0;
foreach ($identifiers as $id_word) {
$id_word = trim($id_word);
if (empty($id_word)) {
continue;
}
$occurrences = array_count_values($words_in_post);
if (isset($occurrences[$id_word])) {
$count += $occurrences[$id_word];
}
}
// Assign points based on occurrences
$puntosIdentifier = $count * 10; // For example, 10 points per occurrence
return $puntosIdentifier;
}
Explanation:
calcularPuntosSimilarToWe also add a function to calculate points based on the similarity to another post:
function calcularPuntosSimilarTo($post_id, $similar_to)
{
// Get content and title of the current post
$post_content_1 = get_post_field('post_content', $post_id);
$post_title_1 = get_post_field('post_title', $post_id);
$full_text_1 = $post_title_1 . ' ' . $post_content_1;
$full_text_1 = strtolower($full_text_1);
$full_text_1 = preg_replace('/[^\w\s]/u', '', $full_text_1);
$words_in_post_1 = preg_split('/\s+/', $full_text_1, -1, PREG_SPLIT_NO_EMPTY);
// Get content and title of the post to compare with
$post_content_2 = get_post_field('post_content', $similar_to);
$post_title_2 = get_post_field('post_title', $similar_to);
$full_text_2 = $post_title_2 . ' ' . $post_content_2;
$full_text_2 = strtolower($full_text_2);
$full_text_2 = preg_replace('/[^\w\s]/u', '', $full_text_2);
$words_in_post_2 = preg_split('/\s+/', $full_text_2, -1, PREG_SPLIT_NO_EMPTY);
if (empty($words_in_post_1) || empty($words_in_post_2)) {
return 0;
}
// Compute similarity as the Jaccard index
$set1 = array_unique($words_in_post_1);
$set2 = array_unique($words_in_post_2);
$intersection = array_intersect($set1, $set2);
$union = array_unique(array_merge($set1, $set2));
$similarity = count($intersection) / count($union);
// Assign points based on similarity
$puntosSimilarTo = $similarity * 100; // Scale to 0-100 points
return $puntosSimilarTo;
}
Explanation:
calcularPuntosIdentifier and calcularPuntosSimilarTo are added to the overall interest points ($puntosIntereses).Here are the adjusted parts of your code incorporating the new functionality:
// Adjusted calcularFeedPersonalizado function
function calcularFeedPersonalizado($userId, $identifier = '', $similar_to = null)
{
// ... existing code ...
foreach ($datos['author_results'] as $post_id => $post_data) {
$puntosFinal = calcularPuntosPost(
$post_id,
$post_data,
$datos,
$esAdmin,
$vistas_posts_processed,
$identifier, // Pass new parameter
$similar_to // Pass new parameter
);
// ... existing code ...
}
// ... existing code ...
}
// Adjusted calcularPuntosPost function
function calcularPuntosPost($post_id, $post_data, $datos, $esAdmin, $vistas_posts_processed, $identifier = '', $similar_to = null)
{
// ... existing code ...
// Calculate points based on identifier matches
$puntosIdentifier = 0;
if (!empty($identifier)) {
$puntosIdentifier = calcularPuntosIdentifier($post_id, $identifier);
}
// Calculate points based on similarity to the specified post
$puntosSimilarTo = 0;
if (!empty($similar_to)) {
$puntosSimilarTo = calcularPuntosSimilarTo($post_id, $similar_to);
}
// ... existing code ...
$puntosFinal = calcularPuntosFinales(
$puntosUsuario,
$puntosIntereses + $puntosIdentifier + $puntosSimilarTo, // Include new points
$puntosLikes,
$metaVerificado,
$metaPostAut,
$esAdmin
);
// ... existing code ...
}
// New function calcularPuntosIdentifier
function calcularPuntosIdentifier($post_id, $identifier)
{
// ... function code as above ...
}
// New function calcularPuntosSimilarTo
function calcularPuntosSimilarTo($post_id, $similar_to)
{
// ... function code as above ...
}
All other functions remain unchanged and continue to operate as before.
Feel free to integrate these adjustments into your codebase. Let me know if you need any further assistance or explanations regarding the implementation.
Not yet included in a published release.