Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"npm-asset/bootstrap-datepicker": "1.10.1",
"npm-asset/easepick--bundle": "1.2.1",
"npm-asset/slick-carousel": "1.6.0",
"php-science/textrank": "1.2.3",
"renanbr/bibtex-parser": "2.2.0",
"seboettg/citeproc-php": "2.7.1"
},
Expand Down
53 changes: 53 additions & 0 deletions modules/custom/az_core/az_core.module
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use Drupal\node\NodeInterface;
use Drupal\pathauto\PathautoPatternInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use PhpScience\TextRank\TextRankFacade;
use PhpScience\TextRank\Tool\StopWords\English;

/**
* Implements hook_help().
Expand Down Expand Up @@ -207,6 +209,10 @@ function az_core_token_info() {
'name' => t("AZ Canonical Summary Link Title"),
'description' => t("Returns the link title value of field_az_link if present on the node or nothing if not."),
],
'az-automatic-summary' => [
'name' => t("AZ Automatic Summary"),
'description' => t("Returns the node summary field if not empty, or a generated summary."),
],
],
],
];
Expand All @@ -232,13 +238,60 @@ function az_core_tokens($type, $tokens, array $data, array $options, BubbleableM
case 'az-canonical-summary-link-title':
$replacements[$original] = az_core_canonical_summary_link_title($data);
break;

case 'az-automatic-summary':
$replacements[$original] = az_core_automatic_summary($data);
break;
}
}
}

return $replacements;
}

/**
* Token replacement callback for automatic summary.
*
* Computes an automatic summary via TextRank.
*
* @param array $data
* Node data from hook_tokens().
*
* @return string
* The node's generated summary.
*/
function az_core_automatic_summary(array $data) {
$summary = '';
/** @var \Drupal\node\NodeInterface $node */
$node = $data['node'] ?? NULL;

if ($node) {
// @todo check for actual summary fields and use those in preference to automatic summary.
// Build and render the node.
$builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$build = $builder->view($node, 'full');
$output = \Drupal::service('renderer')->renderRoot($build);
$text = strip_tags($output->__toString());
$text = html_entity_decode($text);
// @todo make end of tag text considered to be the end of a sentence.
// Fetch programmatic summary.
$rank = new TextRankFacade();
// @todo langcode handling
$stops = new English();
$rank->setStopWords($stops);
$result = $rank->summarizeTextBasic($text);

// Get most important sentence.
// @todo refuse to consider a sentence with low score. (e.g. no clear summary)
$best = array_shift($result);
if (!empty($best)) {
$summary = $best;
}
}

return $summary;
}

/**
* Token replacement callback providing the canonical link title.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ id: node
label: Content
tags:
canonical_url: '[node:field_az_link:uri]'
description: '[node:field_az_summary]'
og_description: '[node:field_az_summary]'
description: '[node:az-automatic-summary]'
og_description: '[node:az-automatic-summary]'
og_image: '[node:field_az_media_image:entity:field_media_az_image]'
og_title: '[node:title]'
og_url: '[node:az-canonical-absolute-url]'
Expand Down
Loading