From 3659b9e2a1a1a9b1de9027c38e0efa98ff18a1ed Mon Sep 17 00:00:00 2001 From: bach Date: Mon, 16 Nov 2026 20:34:06 +0100 Subject: [PATCH] materio_ai: corpus builder for materials and themes (V4) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM --- .../custom/materio_ai/materio_ai.info.yml | 5 + .../custom/materio_ai/materio_ai.services.yml | 5 + .../materio_ai/src/Corpus/CorpusBuilder.php | 187 ++++++++++++++++++ 3 files changed, 197 insertions(+) create mode 100644 web/modules/custom/materio_ai/src/Corpus/CorpusBuilder.php diff --git a/web/modules/custom/materio_ai/materio_ai.info.yml b/web/modules/custom/materio_ai/materio_ai.info.yml index 7324ac35..c826da5f 100644 --- a/web/modules/custom/materio_ai/materio_ai.info.yml +++ b/web/modules/custom/materio_ai/materio_ai.info.yml @@ -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 diff --git a/web/modules/custom/materio_ai/materio_ai.services.yml b/web/modules/custom/materio_ai/materio_ai.services.yml index 5a713b42..972df21c 100644 --- a/web/modules/custom/materio_ai/materio_ai.services.yml +++ b/web/modules/custom/materio_ai/materio_ai.services.yml @@ -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' diff --git a/web/modules/custom/materio_ai/src/Corpus/CorpusBuilder.php b/web/modules/custom/materio_ai/src/Corpus/CorpusBuilder.php new file mode 100644 index 00000000..18da23c7 --- /dev/null +++ b/web/modules/custom/materio_ai/src/Corpus/CorpusBuilder.php @@ -0,0 +1,187 @@ + ['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 + * 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(['', ''], ["\n\n", "\n"], $text))); + $text = preg_replace('/[ \t\x{00A0}]+/u', ' ', $text); + $text = preg_replace('/\s*\n\s*/u', "\n", $text); + return trim((string) $text); + } + +}