From da4e9e45d8301411582198a8804514c7a721e7d1 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Wed, 4 Dec 2019 12:12:10 +0100 Subject: [PATCH] fix: [decaying:basescoreComputation] Correctly support 2-tag and 3-tag --- app/Model/DecayingModelsFormulas/Base.php | 48 +++++++++++++++++------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/app/Model/DecayingModelsFormulas/Base.php b/app/Model/DecayingModelsFormulas/Base.php index 73f491037..c048bb315 100644 --- a/app/Model/DecayingModelsFormulas/Base.php +++ b/app/Model/DecayingModelsFormulas/Base.php @@ -6,8 +6,30 @@ abstract class DecayingModelBase return 'BONFIRE LIT'; } + + protected function __extractTagBasename($tagName) { + $pieces = array(); + if (preg_match('/^[^:="]+:[^:="]+="[^:="]+"$/i', $tagName)) { + $temp = explode(':', $tagName); + $pieces = array_merge(array($temp[0]), explode('=', $temp[1])); + $pieces['complete'] = $tagName; + $pieces['namespace'] = $pieces[0]; + $pieces['predicate'] = $pieces[1]; + $pieces['2tag'] = sprintf('%s:%s', $pieces[0], $pieces[1]); + $pieces['base'] = sprintf('%s:%s', $pieces[0], $pieces[1]); + } elseif (preg_match('/^[^:="]+:[^:="]+$/i', $tagName)) { + $pieces = explode(':', $tagName); + $pieces['complete'] = $tagName; + $pieces['namespace'] = $pieces[0]; + $pieces['predicate'] = $pieces[1]; + $pieces['2tag'] = sprintf('%s:%s', $pieces[0], $pieces[1]); + $pieces['base'] = $pieces[0]; + } + return $pieces; + } + // Get effective taxonomy ratio based on taxonomies attached to the attribute - // Basically, it adapts the ratio defined in the model to fit the actual attached tags + // Basically, it adapts the ratio defined in the model to fit the actual attached tags protected function __getRatioScore($model, $tags) { $ratioScore = array(); @@ -17,21 +39,21 @@ abstract class DecayingModelBase } $total_score = 0.0; foreach ($tags as $tag) { - $namespace_predicate = explode('=', $tag['Tag']['name'])[0]; - if (isset($taxonomy_base_ratio[$namespace_predicate]) && is_numeric($tag['Tag']['numerical_value'])) { - $total_score += floatval($taxonomy_base_ratio[$namespace_predicate]); + $tagBaseName = $this->__extractTagBasename($tag['Tag']['name'])['base']; + if (isset($taxonomy_base_ratio[$tagBaseName]) && is_numeric($tag['Tag']['numerical_value'])) { + $total_score += floatval($taxonomy_base_ratio[$tagBaseName]); } } foreach ($tags as $i => $tag) { - $namespace_predicate = explode('=', $tag['Tag']['name'])[0]; - if (isset($taxonomy_base_ratio[$namespace_predicate]) && is_numeric($tag['Tag']['numerical_value'])) { - $ratioScore[$namespace_predicate] = floatval($taxonomy_base_ratio[$namespace_predicate]) / $total_score; + $tagBaseName = $this->__extractTagBasename($tag['Tag']['name'])['base']; + if (isset($taxonomy_base_ratio[$tagBaseName]) && is_numeric($tag['Tag']['numerical_value'])) { + $ratioScore[$tagBaseName] = floatval($taxonomy_base_ratio[$tagBaseName]) / $total_score; } } return $ratioScore; } - // return attribute tag with event tag matching the namespace+predicate overridden + // return attribute tag with event tag matching the tag basename overridden protected function __getPrioritisedTag($attribute) { $tags = array(); @@ -40,15 +62,15 @@ abstract class DecayingModelBase if (isset($attribute['EventTag'])) { foreach ($attribute['EventTag'] as $i => $tag) { $tags[] = $tag; - $namespace_predicate = explode('=', $tag['Tag']['name'])[0]; - $temp_mapping[$namespace_predicate][] = $i; + $tagBaseName = $this->__extractTagBasename($tag['Tag']['name'])['base']; + $temp_mapping[$tagBaseName][] = $i; } } if (isset($attribute['AttributeTag'])) { foreach ($attribute['AttributeTag'] as $tag) { - $namespace_predicate = explode('=', $tag['Tag']['name'])[0]; - if (!empty($temp_mapping[$namespace_predicate])) { // need to override event tag - foreach ($temp_mapping[$namespace_predicate] as $i => $eventtag_index) { + $tagBaseName = $this->__extractTagBasename($tag['Tag']['name'])['base']; + if (!empty($temp_mapping[$tagBaseName])) { // need to override event tag + foreach ($temp_mapping[$tagBaseName] as $i => $eventtag_index) { $overridden_tags[] = array( 'EventTag' => $tags[$eventtag_index], 'AttributeTag' => $tag