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.
|
||||
*
|
||||
* Only public information goes into the text: field_memo (internal notes) is
|
||||
* never read. Hidden and draft nodes are not vectorized.
|
||||
* Fields follow the Search API "database" index: title, descriptions, tags and
|
||||
* 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 {
|
||||
|
||||
@@ -23,11 +27,6 @@ final class CorpusBuilder {
|
||||
*/
|
||||
public const BUNDLES = ['materiau', 'thematique'];
|
||||
|
||||
/**
|
||||
* Workflow states excluded from the corpus.
|
||||
*/
|
||||
public const EXCLUDED_STATES = ['workflow_hidden', 'workflow_draft'];
|
||||
|
||||
/**
|
||||
* Languages that are vectorized.
|
||||
*/
|
||||
@@ -45,8 +44,8 @@ final class CorpusBuilder {
|
||||
* Labels of the text sections, per language.
|
||||
*/
|
||||
private const LABELS = [
|
||||
'fr' => ['family' => 'Famille', 'tags' => 'Mots-clés', 'thesaurus' => 'Thésaurus', 'manufacturer' => 'Fabricant'],
|
||||
'en' => ['family' => 'Family', 'tags' => 'Keywords', 'thesaurus' => 'Thesaurus', 'manufacturer' => 'Manufacturer'],
|
||||
'fr' => ['family' => 'Famille', 'tags' => 'Mots-clés', 'thesaurus' => 'Thésaurus', 'manufacturer' => 'Fabricant', 'distributor' => 'Distributeur'],
|
||||
'en' => ['family' => 'Family', 'tags' => 'Keywords', 'thesaurus' => 'Thesaurus', 'manufacturer' => 'Manufacturer', 'distributor' => 'Distributor'],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
@@ -57,16 +56,7 @@ final class CorpusBuilder {
|
||||
* Tells whether a node belongs to the corpus.
|
||||
*/
|
||||
public function isEligible(NodeInterface $node): bool {
|
||||
if (!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;
|
||||
return in_array($node->bundle(), self::BUNDLES, TRUE) && $node->isPublished();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,54 +107,75 @@ final class CorpusBuilder {
|
||||
$parts[] = $labels[$key] . ' : ' . implode(', ', $names) . '.';
|
||||
}
|
||||
}
|
||||
if ($node->hasField('field_manufacturer') && ($manufacturer = $this->manufacturer($node, $langcode))) {
|
||||
$parts[] = $labels['manufacturer'] . ' : ' . $manufacturer . '.';
|
||||
foreach (['manufacturer' => 'field_manufacturer', 'distributor' => 'field_distributor'] as $key => $field) {
|
||||
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 !== ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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[]
|
||||
* Unique names, in field order.
|
||||
* Unique entries such as "name (synonym, synonym)", in field order.
|
||||
*/
|
||||
private function termNames(EntityReferenceFieldItemListInterface $items, string $langcode): array {
|
||||
$names = [];
|
||||
foreach ($items->referencedEntities() as $term) {
|
||||
// Thesaurus names use underscores instead of spaces (isolation_acoustique).
|
||||
$name = self::clean(str_replace('_', ' ', self::translated($term, $langcode)->label() ?? ''));
|
||||
if ($name !== '') {
|
||||
$names[$name] = $name;
|
||||
if ($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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
$company = $node->get('field_manufacturer')->entity;
|
||||
if (!$company instanceof ContentEntityInterface) {
|
||||
return NULL;
|
||||
}
|
||||
$name = self::clean(self::translated($company, $langcode)->label() ?? '');
|
||||
if ($name === '') {
|
||||
return NULL;
|
||||
}
|
||||
$address = $company->hasField('field_public_address') ? $company->get('field_public_address')->first() : NULL;
|
||||
$code = $address?->getCountryCode();
|
||||
if ($code) {
|
||||
try {
|
||||
$name .= ' (' . $this->countryRepository->get($code, $langcode)->getName() . ')';
|
||||
private function companies(EntityReferenceFieldItemListInterface $items, string $langcode): array {
|
||||
$companies = [];
|
||||
foreach ($items->referencedEntities() as $company) {
|
||||
if (!$company instanceof ContentEntityInterface) {
|
||||
continue;
|
||||
}
|
||||
catch (\Exception) {
|
||||
// Unknown country code: keep the name alone.
|
||||
$name = self::clean(self::translated($company, $langcode)->label() ?? '');
|
||||
if ($name === '') {
|
||||
continue;
|
||||
}
|
||||
$address = $company->hasField('field_public_address') ? $company->get('field_public_address')->first() : NULL;
|
||||
$code = $address?->getCountryCode();
|
||||
if ($code) {
|
||||
try {
|
||||
$name .= ' (' . $this->countryRepository->get($code, $langcode)->getName() . ')';
|
||||
}
|
||||
catch (\Exception) {
|
||||
// Unknown country code: keep the name alone.
|
||||
}
|
||||
}
|
||||
$companies[$name] = $name;
|
||||
}
|
||||
return $name;
|
||||
return array_values($companies);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user