Skip to content

Instantly share code, notes, and snippets.

@timatron
Last active September 6, 2023 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timatron/12302d05cbac96e6674b57ba61e1af44 to your computer and use it in GitHub Desktop.
Save timatron/12302d05cbac96e6674b57ba61e1af44 to your computer and use it in GitHub Desktop.
Filter the results from IBM Watson in the Classifai AI WP Plugin
<?php
/**
* Functions for integrating classafai AI plugin
*
* @package Daily_Upside
*/
/**
* Filter the classified data and check for tags. If a tag exists for the entity, concept, keyword, or category, return it. Also, if there is a disambiguation name, update the name to match the tag.
*
* @param array $classified_data Classified data from watson.
* @return array
*/
function tdu_ibm_watson_tags( $classified_data = array() ) {
$filtered_classified_data = array();
$filtered_classified_data['usage'] = ( isset( $classified_data['usage'] ) ) ? $classified_data['usage'] : array();
$filtered_classified_data['language'] = ( isset( $classified_data['language'] ) ) ? $classified_data['language'] : array();
$watson_data_types = array( 'categories', 'concepts', 'entities', 'keywords' );
// Loop through all of the data types and then the items within them checking to see if any of them are tags. If they are, add them to the filtered array.
foreach ( $watson_data_types as $watson_data_type ) {
if ( array_key_exists( $watson_data_type, $classified_data ) && is_array( $classified_data[ $watson_data_type ] ) ) {
$filtered_classified_data[ $watson_data_type ] = array();
foreach ( $classified_data[ $watson_data_type ] as $key => $item ) {
$item_text = ( isset( $item['text'] ) ) ? $item['text'] : '';
$item_name = ( isset( $item['disambiguation']['name'] ) ) ? $item['disambiguation']['name'] : '';
if ( is_string( $item_text ) ) {
$item_text_tag_object = get_term_by( 'name', $item_text, 'post_tag' );
}
if ( is_string( $item_name ) ) {
$item_name_tag_object = get_term_by( 'name', $item_name, 'post_tag' );
}
// Sometimes watson provides two names for an idea, lets find the one we already have as a tag.
if ( $item_text_tag_object instanceof WP_Term ) {
if ( isset( $classified_data[ $watson_data_type ][ $key ]['disambiguation']['name'] ) && is_string( $classified_data[ $watson_data_type ][ $key ]['disambiguation']['name'] ) ) {
$classified_data[ $watson_data_type ][ $key ]['disambiguation']['name'] = $item_text;
}
$filtered_classified_data[ $watson_data_type ][] = $classified_data[ $watson_data_type ][ $key ];
} elseif ( $item_name_tag_object instanceof WP_Term ) {
$classified_data[ $watson_data_type ][ $key ]['text'] = $item_name;
$filtered_classified_data[ $watson_data_type ][] = $classified_data[ $watson_data_type ][ $key ];
}
}
}
}
return $filtered_classified_data;
}
add_filter( 'classifai_classified_data', 'tdu_ibm_watson_tags', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment