materio_ai: corpus adds distributor and synonyms, includes every published node (V4)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
This commit is contained in:
@@ -13,8 +13,12 @@ use Drupal\node\NodeInterface;
|
|||||||
/**
|
/**
|
||||||
* Builds the text vectorized for each translation of a material or theme.
|
* Builds the text vectorized for each translation of a material or theme.
|
||||||
*
|
*
|
||||||
* Only public information goes into the text: field_memo (internal notes) is
|
* Fields follow the Search API "database" index: title, descriptions, tags and
|
||||||
* never read. Hidden and draft nodes are not vectorized.
|
* thesaurus with their synonyms, manufacturer and distributor with their
|
||||||
|
* country, plus the family. field_memo (internal notes) is never read.
|
||||||
|
*
|
||||||
|
* Every published node is vectorized, whatever its workflow state: like the
|
||||||
|
* Solr index, access is filtered at query time with the node grants (I4).
|
||||||
*/
|
*/
|
||||||
final class CorpusBuilder {
|
final class CorpusBuilder {
|
||||||
|
|
||||||
@@ -23,11 +27,6 @@ final class CorpusBuilder {
|
|||||||
*/
|
*/
|
||||||
public const BUNDLES = ['materiau', 'thematique'];
|
public const BUNDLES = ['materiau', 'thematique'];
|
||||||
|
|
||||||
/**
|
|
||||||
* Workflow states excluded from the corpus.
|
|
||||||
*/
|
|
||||||
public const EXCLUDED_STATES = ['workflow_hidden', 'workflow_draft'];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Languages that are vectorized.
|
* Languages that are vectorized.
|
||||||
*/
|
*/
|
||||||
@@ -45,8 +44,8 @@ final class CorpusBuilder {
|
|||||||
* Labels of the text sections, per language.
|
* Labels of the text sections, per language.
|
||||||
*/
|
*/
|
||||||
private const LABELS = [
|
private const LABELS = [
|
||||||
'fr' => ['family' => 'Famille', 'tags' => 'Mots-clés', 'thesaurus' => 'Thésaurus', 'manufacturer' => 'Fabricant'],
|
'fr' => ['family' => 'Famille', 'tags' => 'Mots-clés', 'thesaurus' => 'Thésaurus', 'manufacturer' => 'Fabricant', 'distributor' => 'Distributeur'],
|
||||||
'en' => ['family' => 'Family', 'tags' => 'Keywords', 'thesaurus' => 'Thesaurus', 'manufacturer' => 'Manufacturer'],
|
'en' => ['family' => 'Family', 'tags' => 'Keywords', 'thesaurus' => 'Thesaurus', 'manufacturer' => 'Manufacturer', 'distributor' => 'Distributor'],
|
||||||
];
|
];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
@@ -57,16 +56,7 @@ final class CorpusBuilder {
|
|||||||
* Tells whether a node belongs to the corpus.
|
* Tells whether a node belongs to the corpus.
|
||||||
*/
|
*/
|
||||||
public function isEligible(NodeInterface $node): bool {
|
public function isEligible(NodeInterface $node): bool {
|
||||||
if (!in_array($node->bundle(), self::BUNDLES, TRUE) || !$node->isPublished()) {
|
return in_array($node->bundle(), self::BUNDLES, TRUE) && $node->isPublished();
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
if ($node->hasField('field_workflow')) {
|
|
||||||
$state = $node->get('field_workflow')->value;
|
|
||||||
if (in_array($state, self::EXCLUDED_STATES, TRUE)) {
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return TRUE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,42 +107,61 @@ final class CorpusBuilder {
|
|||||||
$parts[] = $labels[$key] . ' : ' . implode(', ', $names) . '.';
|
$parts[] = $labels[$key] . ' : ' . implode(', ', $names) . '.';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($node->hasField('field_manufacturer') && ($manufacturer = $this->manufacturer($node, $langcode))) {
|
foreach (['manufacturer' => 'field_manufacturer', 'distributor' => 'field_distributor'] as $key => $field) {
|
||||||
$parts[] = $labels['manufacturer'] . ' : ' . $manufacturer . '.';
|
if ($node->hasField($field) && ($companies = $this->companies($node->get($field), $langcode))) {
|
||||||
|
$parts[] = $labels[$key] . ' : ' . implode(', ', $companies) . '.';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode("\n", array_filter($parts, fn(string $part): bool => $part !== ''));
|
return implode("\n", array_filter($parts, fn(string $part): bool => $part !== ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the names of referenced terms in the given language.
|
* Returns the names of referenced terms, with their synonyms, in a language.
|
||||||
|
*
|
||||||
|
* Terms keep the field order: editors put the most important ones first.
|
||||||
*
|
*
|
||||||
* @return string[]
|
* @return string[]
|
||||||
* Unique names, in field order.
|
* Unique entries such as "name (synonym, synonym)", in field order.
|
||||||
*/
|
*/
|
||||||
private function termNames(EntityReferenceFieldItemListInterface $items, string $langcode): array {
|
private function termNames(EntityReferenceFieldItemListInterface $items, string $langcode): array {
|
||||||
$names = [];
|
$names = [];
|
||||||
foreach ($items->referencedEntities() as $term) {
|
foreach ($items->referencedEntities() as $term) {
|
||||||
// Thesaurus names use underscores instead of spaces (isolation_acoustique).
|
// Thesaurus names use underscores instead of spaces (isolation_acoustique).
|
||||||
$name = self::clean(str_replace('_', ' ', self::translated($term, $langcode)->label() ?? ''));
|
$name = self::clean(str_replace('_', ' ', self::translated($term, $langcode)->label() ?? ''));
|
||||||
if ($name !== '') {
|
if ($name === '') {
|
||||||
$names[$name] = $name;
|
continue;
|
||||||
}
|
}
|
||||||
|
$translated = self::translated($term, $langcode);
|
||||||
|
$synonyms = [];
|
||||||
|
if ($translated->hasField('field_synonyms')) {
|
||||||
|
foreach ($translated->get('field_synonyms') as $item) {
|
||||||
|
$synonym = self::clean(str_replace('_', ' ', (string) $item->value));
|
||||||
|
if ($synonym !== '' && mb_strtolower($synonym) !== mb_strtolower($name)) {
|
||||||
|
$synonyms[$synonym] = $synonym;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$names[$name] = $synonyms ? $name . ' (' . implode(', ', $synonyms) . ')' : $name;
|
||||||
}
|
}
|
||||||
return array_values($names);
|
return array_values($names);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns "Name (Country)" for the manufacturer, or its name alone.
|
* Returns "Name (Country)" for each referenced company, or the name alone.
|
||||||
|
*
|
||||||
|
* @return string[]
|
||||||
|
* Unique entries, in field order.
|
||||||
*/
|
*/
|
||||||
private function manufacturer(NodeInterface $node, string $langcode): ?string {
|
private function companies(EntityReferenceFieldItemListInterface $items, string $langcode): array {
|
||||||
$company = $node->get('field_manufacturer')->entity;
|
$companies = [];
|
||||||
|
foreach ($items->referencedEntities() as $company) {
|
||||||
if (!$company instanceof ContentEntityInterface) {
|
if (!$company instanceof ContentEntityInterface) {
|
||||||
return NULL;
|
continue;
|
||||||
}
|
}
|
||||||
$name = self::clean(self::translated($company, $langcode)->label() ?? '');
|
$name = self::clean(self::translated($company, $langcode)->label() ?? '');
|
||||||
if ($name === '') {
|
if ($name === '') {
|
||||||
return NULL;
|
continue;
|
||||||
}
|
}
|
||||||
$address = $company->hasField('field_public_address') ? $company->get('field_public_address')->first() : NULL;
|
$address = $company->hasField('field_public_address') ? $company->get('field_public_address')->first() : NULL;
|
||||||
$code = $address?->getCountryCode();
|
$code = $address?->getCountryCode();
|
||||||
@@ -164,7 +173,9 @@ final class CorpusBuilder {
|
|||||||
// Unknown country code: keep the name alone.
|
// Unknown country code: keep the name alone.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $name;
|
$companies[$name] = $name;
|
||||||
|
}
|
||||||
|
return array_values($companies);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user