Skip to content

Commit

Permalink
parse on php and replace pattern as json
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya-dev committed Dec 14, 2023
1 parent fc9fb50 commit 30bee15
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 38 deletions.
Empty file removed includes/SiteGen/Parser.php
Empty file.
82 changes: 52 additions & 30 deletions includes/SiteGen/SiteGen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace NewfoldLabs\WP\Module\AI\SiteGen;

use NewfoldLabs\WP\Module\AI\Utils\PatternParser;
use NewfoldLabs\WP\Module\Data\HiiveConnection;
use NewfoldLabs\WP\Module\Data\SiteCapabilities;

Expand Down Expand Up @@ -199,38 +200,59 @@ private static function generate_pattern_content(
}

$category_pattern_map[ $category ] = array();
$category_content = array();
$category_patterns = array();
$category_base_patterns = array();
foreach ( $random_selected_patterns as $pattern_slug ) {
$pattern = $patterns_for_category[ $pattern_slug ];
$response = wp_remote_post(
NFD_AI_BASE . 'generateSiteMeta',
array(
'headers' => array(
'Content-Type' => 'application/json',
),
'timeout' => 60,
'body' => wp_json_encode(
array(
'hiivetoken' => HiiveConnection::get_auth_token(),
'prompt' => array(
'pattern' => $pattern['content'],
'prompt' => self::get_prompt_from_info(
array(
'site_description' => $site_description,
'keywords' => wp_json_encode( $keywords ),
'content_style' => wp_json_encode( $content_style ),
'target_audience' => wp_json_encode( $target_audience ),
)
),
),
'identifier' => 'contentRegenerate',
)
),
)
);
$pattern = $patterns_for_category[ $pattern_slug ];
$pattern_parser = new PatternParser( $pattern['content'] );
$pattern_representation = $pattern_parser->get_pattern_representation();
array_push( $category_content, $pattern_representation );
array_push( $category_patterns, $pattern_parser );
array_push( $category_base_patterns, $pattern['content'] );
}
$generated_content = wp_remote_post(
NFD_AI_BASE . 'generateSiteMeta',
array(
'headers' => array(
'Content-Type' => 'application/json',
),
'timeout' => 60,
'body' => wp_json_encode(
array(
'hiivetoken' => HiiveConnection::get_auth_token(),
'prompt' => array(
'site_description' => $site_description,
'keywords' => wp_json_encode( $keywords ),
'content_style' => wp_json_encode( $content_style ),
'target_audience' => wp_json_encode( $target_audience ),
'content' => $category_content,
),
'identifier' => 'generateContent',
)
),
)
);

$generated_content = json_decode( wp_remote_retrieve_body( $generated_content ), true );

$parsed_response = json_decode( wp_remote_retrieve_body( $response ), true );
$generated_pattern = $parsed_response['content'];
array_push( $category_pattern_map[ $category ], $generated_pattern );
if ( array_key_exists( 'content', $generated_content ) ) {
$generated_content = $generated_content['content'];
} else {
$generated_content = array();
}

$category_content_length = count( $category_content );
for ( $i = 0; $i < $category_content_length; $i++ ) {
if ( empty( $generated_content ) ) {
array_push( $category_pattern_map[ $category ], $category_base_patterns[ $i ] );
}
if ( array_key_exists( $i, $generated_content ) ) {
$generated_pattern = $category_patterns[ $i ]->get_replaced_pattern( $generated_content[ $i ] );
array_push( $category_pattern_map[ $category ], $generated_pattern );
} else {
array_push( $category_pattern_map[ $category ], $category_base_patterns[ $i ] );
}
}
}

Expand Down
19 changes: 11 additions & 8 deletions includes/Utils/AISearchUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AISearchUtil {
/**
* The function to check capabilities for module AI
*/
private static function _check_capabilities( ) {
private static function check_capabilities() {
$capability = new SiteCapabilities();

$help_enabled = $capability->get( 'canAccessHelpCenter' );
Expand All @@ -25,10 +25,10 @@ private static function _check_capabilities( ) {
/**
* The function to check just Help capability
*/
private static function _check_help_capability() {
private static function check_help_capability() {
$capability = new SiteCapabilities();

$help_enabled = $capability->get('canAccessHelpCenter');
$help_enabled = $capability->get( 'canAccessHelpCenter' );

return $help_enabled;
}
Expand All @@ -43,10 +43,13 @@ private static function _check_help_capability() {
* @return array
*/
public static function get_search_results(
string $hiive_token, string $user_prompt, string $identifier, array $extra = null
string $hiive_token,
string $user_prompt,
string $identifier,
array $extra = null
) {

if ( !self::_check_capabilities() ) {
if ( ! self::check_capabilities() ) {
return array(
'error' => __( 'We are unable to process the request at this moment' ),
);
Expand Down Expand Up @@ -97,9 +100,9 @@ public static function get_search_results(
* @return array
*/
public static function get_default_search_results( $hiive_token ) {
if (!self::_check_help_capability()) {
if ( ! self::check_help_capability() ) {
return array(
'error' => __('We are unable to process the request at this moment'),
'error' => __( 'We are unable to process the request at this moment' ),
);
}

Expand All @@ -120,7 +123,7 @@ public static function get_default_search_results( $hiive_token ) {
);
if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
return array(
'error' => __('We are unable to process the request at this moment'),
'error' => __( 'We are unable to process the request at this moment' ),
);
}

Expand Down
193 changes: 193 additions & 0 deletions includes/Utils/PatternParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

namespace NewfoldLabs\WP\Module\AI\Utils;

use DOMDocument;

/**
* Helper class to get generated patterns and replace them
*/
class PatternParser {

/**
* The input pattern
*
* @var string
*/
public $pattern;

/**
* The escaped pattern, to preserve block grammar
*
* @var string
*/
public $pattern_escaped;

/**
* The pattern content
*
* @var array
*/
public $pattern_content = array();

/**
* DOM Document for the parsing
*
* @var mixed
*/
private $document;

/**
* The list of headings we are going to parse from the content
*
* @var array
*/
private $headings = array(
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
);

/**
* The tag names to be parsed
*
* @var array
*/
private $tag_names_to_parse = array( 'p', 'a' );

/**
* The pattern representation
*
* @var array
*/
public $pattern_representation;

/**
* The pattern representation with replaced content
*
* @var array
*/
public $replaced_pattern_representation;

/**
* Constructor to initialize values
*
* @param string $pattern The initial pattern
*/
public function __construct( $pattern ) {
$this->pattern_escaped = $pattern;
$this->pattern = trim( $pattern );
$this->document = new DOMDocument();
$this->document->loadHTML( $this->pattern );
}

/**
* Function to extract the headings out of a pattern
*/
public function get_headings() {
$headings = array();
// Iterate and get all h1 through h6
foreach ( $this->headings as $heading ) {
$elements = $this->document->getElementsByTagName( $heading );
$heading_texts = array();
foreach ( $elements as $element ) {
array_push( $heading_texts, $element->textContent );
}

if ( ! empty( $heading_texts ) ) {
$headings[ $heading ] = $heading_texts;
}
}

return $headings;
}

/**
* Function to parse the pre defined tags from the content
*
* @param string $tag_name The tag name to be parsed
*/
public function get_tag_text( $tag_name ) {
$paras = array();
$para_elements = $this->document->getElementsByTagName( $tag_name );
if ( ! empty( $para_elements ) ) {
foreach ( $para_elements as $element ) {
array_push( $paras, $element->textContent );
}
}

return $paras;
}

/**
* Get the pattern representation
*/
public function get_pattern_representation() {
$representation = $this->get_headings();

foreach ( $this->tag_names_to_parse as $tag_name ) {
$tag_name_content = $this->get_tag_text( $tag_name );
if ( ! empty( $tag_name_content ) ) {
$representation[ $tag_name ] = $tag_name_content;
}
}

$this->pattern_representation = $representation;
return $representation;
}

/**
* Function to generate the replaced pattern representation
*
* @param array $generated_content The generated content as replacement for the existing one.
*/
public function replace_content( $generated_content ) {
$this->replaced_pattern_representation = array();
// Match with the generated representation
foreach ( $this->pattern_representation as $tagname => $content ) {
if ( array_key_exists( $tagname, $generated_content ) ) {
$this->replaced_pattern_representation[ $tagname ] = array();
$existing_content_length = count( $this->pattern_representation[ $tagname ] );
for ( $i = 0; $i < $existing_content_length; $i++ ) {
if ( array_key_exists( $i, $generated_content[ $tagname ] ) ) {
array_push( $this->replaced_pattern_representation[ $tagname ], $generated_content[ $tagname ][ $i ] );
} else {
array_push(
$this->replaced_pattern_representation[ $tagname ],
$content[ $i ]
);
}
}
} else {
$this->replaced_pattern_representation[ $tagname ] = $content;
}
}
}

/**
* Function to replace the generated content in initial pattern and return
*
* @param array $generated_content the content generated for this pattern.
*
* @returns The replaced pattern string
*/
public function get_replaced_pattern( $generated_content ) {
$replaced_pattern = $this->pattern_escaped;
$this->replace_content( $generated_content );
foreach ( $this->replaced_pattern_representation as $replacement_tag => $replacement_contents ) {
$content_length = count( $replacement_contents );
// Do a string match and replace
for ( $i = 0; $i < $content_length; $i++ ) {
$replaced_pattern = str_replace(
$this->pattern_representation[ $replacement_tag ][ $i ],
$replacement_contents[ $i ],
$replaced_pattern
);
}
}
return $replaced_pattern;
}
}

0 comments on commit 30bee15

Please sign in to comment.