In the article “Generating a AI news page with ChatGPT and GDELT“, I have already shown how it is possible in principle to generate a simple topic-specific news website.
Today, I want to implement a small optimization for the news page with the help of ChatGPT. Currently, similar news or even news with exactly the same title are displayed.
Here’s the chat log:
The following PHP code needs to be adjusted so that if titles are duplicated, only the first data set is used. In addition, only a maximum of 5 data sets should be output.
function display_ai_news_data() { $url = 'https://api.gdeltproject.org/api/v2/doc/doc?format=html×pan=24H&query=Artificial%20Intelligence%20sourcelang:eng&mode=artlist&maxrecords=5&format=json&sort=hybridrel'; $response = wp_remote_get( $url ); if ( is_array( $response ) ) { $headers = $response['headers']; $body = json_decode( $response['body'], true ); if ( ! empty( $body ) && isset( $body['articles'] ) ) { $articles = $body['articles']; foreach ( $articles as $article ) { $title = $article['title']; $url = $article['url']; $image = $article['socialimage']; echo '<div>'; echo '<h2>' . $title . '</h2>'; echo '<img src="' . $image . '" />'; echo '<p><a href="' . $url . '">Read more</a></p>'; echo '</div>'; } } } }
Here is an updated version of the code that only uses the first data set for each duplicate title and outputs a maximum of 5 data sets:
function display_ai_news_data() { $url = 'https://api.gdeltproject.org/api/v2/doc/doc?format=html×pan=24H&query=Artificial%20Intelligence%20sourcelang:eng&mode=artlist&maxrecords=5&format=json&sort=hybridrel'; $response = wp_remote_get( $url ); if ( is_array( $response ) ) { $headers = $response['headers']; $body = json_decode( $response['body'], true ); if ( ! empty( $body ) && isset( $body['articles'] ) ) { $articles = $body['articles']; $used_titles = array(); foreach ( $articles as $article ) { $title = $article['title']; if (in_array($title, $used_titles)) { continue; // Skip articles with already used title } $used_titles[] = $title; // Mark this title as used $url = $article['url']; $image = $article['socialimage']; echo '<div>'; echo '<h2>' . $title . '</h2>'; echo '<img src="' . $image . '" />'; echo '<p><a href="' . $url . '">Read more</a></p>'; echo '</div>'; if (count($used_titles) >= 5) { break; // Limit reached, stop the loop } } } } }
Can you rephrase this to select 5 random data sets to display from articles?
Yes, I can do that! Here is an updated version of the code that selects 5 data sets at random from articles:
function display_ai_news_data() { $url = 'https://api.gdeltproject.org/api/v2/doc/doc?format=html×pan=24H&query=Artificial%20Intelligence%20sourcelang:eng&mode=artlist&maxrecords=50&format=json&sort=hybridrel'; $response = wp_remote_get( $url ); if ( is_array( $response ) ) { $headers = $response['headers']; $body = json_decode( $response['body'], true ); if ( ! empty( $body ) && isset( $body['articles'] ) ) { $articles = $body['articles']; shuffle($articles); // randomly shuffle the array $used_titles = array(); $count = 0; foreach ( $articles as $article ) { if ($count >= 5) { break; // limit reached } $title = $article['title']; if (in_array($title, $used_titles)) { continue; // Skip articles with already used title } $used_titles[] = $title; // Mark this title as used $url = $article['url']; $image = $article['socialimage']; echo '<div>'; echo '<h2>' . $title . '</h2>'; echo '<img src="' . $image . '" />'; echo '<p><a href="' . $url . '">Read more</a></p>'; echo '</div>'; $count++; } } } }
The code uses the shuffle() function to randomly shuffle the articles array. Then a foreach loop is used to iterate through each article in the random order. The code also uses the $used_titles array to check if the title has already been used. If the title has already been used, the data set is skipped. Otherwise, the data set is output and the title is marked as used. When the maximum of 5 data sets is reached, the loop stops.
I hope that helps!
I was able to use this code directly as well.
ChatGPT even explained the code changes to me and increased the value of maxrecords in the API call from 5 to 50 based on the context, even though I didn’t explicitly ask for it.
In summary, ChatGPT provides great support for code optimizations. Thanks to its abilities in machine learning and artificial intelligence, ChatGPT can quickly and accurately respond to questions and requests related to programming and offer customized solutions.
Leave a Reply