materio_ai: corpus builder for materials and themes (V4)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
This commit is contained in:
@@ -4,3 +4,8 @@ description: 'Recherche par le sens : vectorisation des fiches avec un modèle d
|
||||
core_version_requirement: ^10.3 || ^11
|
||||
package: 'Materio'
|
||||
configure: materio_ai.settings
|
||||
dependencies:
|
||||
- address:address
|
||||
- drupal:node
|
||||
- drupal:taxonomy
|
||||
- workflow:workflow
|
||||
|
||||
@@ -12,3 +12,8 @@ services:
|
||||
class: Drupal\materio_ai\Storage\EmbeddingStorage
|
||||
arguments: ['@database', '@datetime.time']
|
||||
Drupal\materio_ai\Storage\EmbeddingStorage: '@materio_ai.embedding_storage'
|
||||
|
||||
materio_ai.corpus_builder:
|
||||
class: Drupal\materio_ai\Corpus\CorpusBuilder
|
||||
arguments: ['@address.country_repository']
|
||||
Drupal\materio_ai\Corpus\CorpusBuilder: '@materio_ai.corpus_builder'
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\materio_ai\Corpus;
|
||||
|
||||
use CommerceGuys\Addressing\Country\CountryRepositoryInterface;
|
||||
use Drupal\Component\Utility\Html;
|
||||
use Drupal\Core\Entity\ContentEntityInterface;
|
||||
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
|
||||
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.
|
||||
*/
|
||||
final class CorpusBuilder {
|
||||
|
||||
/**
|
||||
* Node types that are vectorized.
|
||||
*/
|
||||
public const BUNDLES = ['materiau', 'thematique'];
|
||||
|
||||
/**
|
||||
* Workflow states excluded from the corpus.
|
||||
*/
|
||||
public const EXCLUDED_STATES = ['workflow_hidden', 'workflow_draft'];
|
||||
|
||||
/**
|
||||
* Languages that are vectorized.
|
||||
*/
|
||||
public const LANGCODES = ['fr', 'en'];
|
||||
|
||||
/**
|
||||
* Material families: field_famille has no translated labels.
|
||||
*/
|
||||
private const FAMILIES = [
|
||||
'fr' => ['W' => 'Bois', 'T' => 'Textile', 'R' => 'Plastique', 'P' => 'Papier', 'C' => 'Composite', 'M' => 'Métal', 'F' => 'Céramique', 'G' => 'Verre', 'S' => 'Béton, pierre', 'O' => 'Autre'],
|
||||
'en' => ['W' => 'Wood', 'T' => 'Textile', 'R' => 'Plastic', 'P' => 'Paper', 'C' => 'Composite', 'M' => 'Metal', 'F' => 'Ceramic', 'G' => 'Glass', 'S' => 'Concrete, stone', 'O' => 'Other'],
|
||||
];
|
||||
|
||||
/**
|
||||
* 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'],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly CountryRepositoryInterface $countryRepository,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the texts of all vectorized translations of a node.
|
||||
*
|
||||
* @return array<string, string>
|
||||
* Texts keyed by langcode; empty when the node is not eligible.
|
||||
*/
|
||||
public function buildAll(NodeInterface $node): array {
|
||||
if (!$this->isEligible($node)) {
|
||||
return [];
|
||||
}
|
||||
$texts = [];
|
||||
foreach (self::LANGCODES as $langcode) {
|
||||
if ($node->hasTranslation($langcode)) {
|
||||
$translation = $node->getTranslation($langcode);
|
||||
if ($translation->isPublished()) {
|
||||
$texts[$langcode] = $this->build($translation, $langcode);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $texts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the text of one translation.
|
||||
*/
|
||||
public function build(NodeInterface $node, string $langcode): string {
|
||||
$labels = self::LABELS[$langcode] ?? self::LABELS['en'];
|
||||
$parts = [];
|
||||
|
||||
$parts[] = self::clean($node->label() ?? '');
|
||||
if ($node->hasField('field_short_description')) {
|
||||
$parts[] = self::clean((string) $node->get('field_short_description')->value);
|
||||
}
|
||||
if ($node->hasField('body')) {
|
||||
$parts[] = self::clean((string) $node->get('body')->value);
|
||||
}
|
||||
|
||||
if ($node->hasField('field_famille') && ($code = $node->get('field_famille')->value)) {
|
||||
$family = self::FAMILIES[$langcode][$code] ?? self::FAMILIES['en'][$code] ?? NULL;
|
||||
if ($family) {
|
||||
$parts[] = $labels['family'] . ' : ' . $family . '.';
|
||||
}
|
||||
}
|
||||
foreach (['tags' => 'field_tags', 'thesaurus' => 'field_thesaurus'] as $key => $field) {
|
||||
if ($node->hasField($field) && ($names = $this->termNames($node->get($field), $langcode))) {
|
||||
$parts[] = $labels[$key] . ' : ' . implode(', ', $names) . '.';
|
||||
}
|
||||
}
|
||||
if ($node->hasField('field_manufacturer') && ($manufacturer = $this->manufacturer($node, $langcode))) {
|
||||
$parts[] = $labels['manufacturer'] . ' : ' . $manufacturer . '.';
|
||||
}
|
||||
|
||||
return implode("\n", array_filter($parts, fn(string $part): bool => $part !== ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the names of referenced terms in the given language.
|
||||
*
|
||||
* @return string[]
|
||||
* Unique names, 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;
|
||||
}
|
||||
}
|
||||
return array_values($names);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns "Name (Country)" for the manufacturer, or its name alone.
|
||||
*/
|
||||
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() . ')';
|
||||
}
|
||||
catch (\Exception) {
|
||||
// Unknown country code: keep the name alone.
|
||||
}
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the translation of an entity, or the entity itself.
|
||||
*/
|
||||
private static function translated(ContentEntityInterface $entity, string $langcode): ContentEntityInterface {
|
||||
return $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips HTML and collapses white space.
|
||||
*/
|
||||
private static function clean(string $text): string {
|
||||
$text = Html::decodeEntities(strip_tags(str_replace(['<br', '</p>', '</li>'], ["\n<br", "</p>\n", "</li>\n"], $text)));
|
||||
$text = preg_replace('/[ \t\x{00A0}]+/u', ' ', $text);
|
||||
$text = preg_replace('/\s*\n\s*/u', "\n", $text);
|
||||
return trim((string) $text);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user