You are here:  » Category Mapping by Phrase Match

Active Forum Topics


Category Mapping by Phrase Match

Submitted by bat on Tue, 2022-02-08 13:47 in

Hi David,
Please delete this if I've already sent through - I can't recall if I pressed submit on the topic or not.

On category mapping, is there any way to use phrase match? I know there's the exact match and broad match but I have categories that kind of overlap each other so would be good if phrase match is possible.

e.g. "t shirts" would only map category names that include the words "t" + "shirts" together, in that order.

As it stands, 't shirts' entered on its own ends up mapping 'shirts' and 'sweatshirts' into my custom category which I don't want.

Thanks!

Submitted by support on Wed, 2022-02-09 08:56

Hi,

Sure - to add a RegExp match option to Category Mapping, edit includes/admin.php and look for the following code at line 390:

        if (substr($k,0,1) !== "=")

...and REPLACE with:

        if (substr($k,0,1) == "~")
        {
          if (preg_match(substr($k,1),$importRecord["category"]))
          {
            $importRecord["category"] = $v;
            break;
          }
        }
        elseif (substr($k,0,1) !== "=")

Then to add a RegExp mapping to a Category Mapping to match a phrase, prefix the expression with ~, delimit with "/" and enter the expression as required, for example to match only "t shirt" exactly use:

~/\bt shirt\b/

"\b" is "Word Boundary" in a regular expression (which includes beginning / end of line), so this will match only "t shirt" exactly.

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Thu, 2022-02-10 16:29

Thanks David.
I realised I needed it on Category Hierarchy, not the normal Category Mapping, so replaced the code in the Hierarchy section instead. Is it still the same or should it be different? As I've tried using the 't shirt' code you provided in the particular category hierarchy panel, clicked import and it's not mapping them.

Here's the code in my includes/admin.php

 /* apply category hierarchy mappings */
if (isset($admin_importCategoryHierarchyMappings["=".$importRecord["name"]]))
    {
      $importRecord["categoryid"] = $admin_importCategoryHierarchyMappings["=".$importRecord["name"]];
    }
    elseif (isset($admin_importCategoryHierarchyMappings["=".$importRecord["category"]]))
    {
      $importRecord["categoryid"] = $admin_importCategoryHierarchyMappings["=".$importRecord["category"]];
    }
    else
    {
      foreach($admin_importCategoryHierarchyMappings as $k => $v)
      {
        if (substr($k,0,1) == "~")
        {
          if (preg_match(substr($k,1),$importRecord["category"]))
          {
            $importRecord["category"] = $v;
            break;
          }
        }
        elseif (substr($k,0,1) !== "=")
        {
          $found = 0;
          $words = explode(" ",$k);
          foreach($words as $word)
          {
            if ($word)
            {
                if (stripos($importRecord["category"].$importRecord["name"],$word) !== FALSE) $found++;
            }
          }
          if ($found == count($words))
          {
            $importRecord["categoryid"] = $v;
            break;
          }
        }
      }
    }

Submitted by support on Fri, 2022-02-11 08:31

Nearly - it's the categoryid field that needs to be set in this case; so in the new code instead of:

             $importRecord["category"] = $v;

...use:

             $importRecord["categoryid"] = $v;

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Fri, 2022-02-11 14:16

Brilliant! Thanks David! It's working a treat now.

Submitted by bat on Wed, 2022-03-02 18:14

Hi David,
I'm new to Regex so I've been doing some research into Regex and what I'd need to write to map a category that includes one word but doesn't include another. I got the below (I've tried with and without the tilde at the front - do I need that all the time?) but I can't get it to work and I'm not sure why.

/^(?!.*Scarf).*Hat.*$/

Example: I want any category mapped that contains the word Hat but not when it also includes Scarf

Please can you advise. It's the same installation as mentioned above.

Submitted by support on Thu, 2022-03-03 08:22

Hi,

I just tested that regexp in standalone code and it seems to work as expected - when including it in the Alternatives box on the configuration page for your mapping don't forget to include "~" prefix e.g.;

~/^(?!.*Scarf).*Hat.*$/

If already in place and still not working as expected let me know and I'll check it out further with you...

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Fri, 2022-03-04 11:39

Cheers David. Yes, it's working now. Not sure why it wasn't at first but all good now :)