3

I have a few possibilities for an input term:

  1. "black and blue toy" (black and blue are colors) ==> "toy"
  2. "silver spoon" (silver is a color) ==> "spoon"
  3. "black and decker drill system" (notice, black is not a color -- it is part of a brand name - black and decker) ==> "black and decker drill system"
  4. "red car" (red is a color) ==> "car"

What i want to do

  • replace colors with "" -- but only if it is a color. So for #3 above i do not want black replaced since it is part of a brand. After "and " in 3 it is not a color so that i can infer that is part of a name instead.
  • Now the question is how do i do it? :)
$common_terms_to_replace_for_tag_search = array("green","blue","grey","gray","black","red","silver","maroon","violet","white");
$term = str_replace($common_terms_to_replace_for_tag_search," ",$term);

However, above code only works for examples cases 1, 2, 4 - and not for 3. How do i update code update to work for cases such as 3 above? Thanks much!

6
  • If the brands are known, an oft-used hack is to tokenize them to something else, do the general replace, and the replace the token with the original string. Apr 10, 2015 at 14:47
  • You can't. You would need a very extensive list of company names with colours in them and even then there would be situations where the colour can both be part of the name or a colour.
    – jeroen
    Apr 10, 2015 at 14:47
  • That's not something that you can do with regex's, what you can do, though, is ensure you're not replacing part of a word (ie: redirect -> preg_replace('/\bred\b/i', '', 'redirect'); will not replace "red"). Tell us why you'd need to to this in the first place, perhaps the problem is best fixed elsewhere (X-Y problem...) Apr 10, 2015 at 14:48
  • I think the basic thing is to: replace <color> alone, or <color> and <color>.. where color is part of the array to search/replace. In the case of black and decker (its <color> and decker) and should not replace. The feedback i am hearing is that maybe regex wont do what i am looking for here.. Apr 10, 2015 at 14:57
  • you cant't do this deterministically. how would you distinguish a brand name that can be part of a legal sentence?
    – 1010
    Apr 10, 2015 at 15:35

1 Answer 1

1

I'd suggest turning those colors to a list of regexes, and then run preg_replace like this:

<?php
   $string = 'black toshiba';
   $all_color_alternation = "\b(?:green|blue|gr[ea]y|black|red|silver|maroon|violet|white)\b";
   $common_terms_to_replace_for_tag_search = array(
      "/$all_color_alternation\\s+and\\s+$all_color_alternation/i",
      "/\\bblack(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i",
      "/\\bblue(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i",
      "/\\bgr[ea]y(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i",
      "/\\bred(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i",
      "/\\bsilver(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i",
      "/\\bmaroon(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i",
      "/\\bviolet(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i",
      "/\\bwhite(?=(?: and $all_color_alternation|\s+(?!and)))(?: and $all_color_alternation)?\\b/i");
   $replacement = array('');
   echo preg_replace($common_terms_to_replace_for_tag_search, $replacement, $string);
?>

I added i ignore case option to also remove color names in both upper and lower cases.

The first regex will first remove all colors within COLOR+and+COLOR context.

See sample program.

Output:

black and white apple         ==>  apple
black and decker drill system ==> black and decker drill system
black apple                   ==>  apple
gray and black toshiba        ==>  toshiba
7
  • This will change "reduction" into "uction", use word-boundaries Apr 10, 2015 at 14:49
  • @EliasVanOotegem: Not now, I added \b word boundaries :) However, it is a limited work-around, I agree. Regexes are not THAT smart. Apr 10, 2015 at 14:51
  • Sure - but like you said it is only for known context. The general context is to: replace <color> alone, or <color> and <color>.. where color is part of the array to search/replace. In the case of black and decker (its <color> and decker) and should not replace. Anyway above can be modified to make this happen? Apr 10, 2015 at 15:00
  • So, you want to set the condition like "if there is and COLOR after COLOR, remove all", and another condition: "if there is no and after COLOR, remove the COLOR". Correct? Apr 10, 2015 at 15:06
  • great looks like that did the trick. Works for "Black and Decker".. will cricle back if there any issues i run into for other similar patterns of company names. thanks again! Apr 10, 2015 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.