Author Topic: Remove flat keywords from PM using Find/Replace  (Read 1256 times)

Offline gand

  • Newcomer
  • *
  • Posts: 26
    • View Profile
Remove flat keywords from PM using Find/Replace
« on: December 29, 2021, 10:47:58 PM »
I'm new with PM and have installed the trial version.

However, I do have a problem with keywords that I hope someone can help with.

I use PM with Lightroom Classic.

If I create a structured keyword with PM, it works fine with LRC. If I create a hierarchical keyword in LRC, PM will also create flat keywords as well, so now I have both flat keywords and structured keywords in PM. The next time I update metadata in LRC, I will have both flat keywords and structured keywords in LRC.

I know that I can remove the structured keywords in PM with Find-Replace with this expression ([\|] {1})| (\[(.*?) \])

Now the question, is there a similar expression I can use in Find-Replace, I can use to remove flat keywords and retain the structured keywords.

So having this structure in PM:
Alma,  Family, Persons, Persons | Family | Alma
I would like this:
Persons | Family | Alma

Is this possible?

Rgds
Gert

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 24118
    • View Profile
    • Camera Bits, Inc.
Re: Remove flat keywords from PM using Find/Replace
« Reply #1 on: December 30, 2021, 05:20:31 PM »
Gert,

Now the question, is there a similar expression I can use in Find-Replace, I can use to remove flat keywords and retain the structured keywords.

So having this structure in PM:
Alma,  Family, Persons, Persons | Family | Alma
I would like this:
Persons | Family | Alma

Is this possible?

No, there is no Regex that will remove only the flat keywords.  PM is following the IPTC specification for keywords, making hierarchical keywords flattened and represented in the Keywords field.

The topic has been discussed many times on the forum.

-Kirk

Offline gand

  • Newcomer
  • *
  • Posts: 26
    • View Profile
Re: Remove flat keywords from PM using Find/Replace
« Reply #2 on: December 30, 2021, 09:47:06 PM »
Hi Kirk

Thanks a lot for your prompt reply.

My question was based on this article:

https://www.carlseibert.com/clean-up-lightroom-keywords-in-photo-mechanic/

and I just thought that someone might have a solution to delete flat keywords with a regex. I know PM follows the IPTC specification.

Gert

Offline gand

  • Newcomer
  • *
  • Posts: 26
    • View Profile
Re: Remove flat keywords from PM using Find/Replace
« Reply #3 on: December 30, 2021, 10:24:40 PM »
Hi

In the article he used this expression in Find/Replace:
([\|]{1})|(\[(.*?)\])

Do you know where to find a find information about the syntax?

Sorry, I'm 1 week into the trial so my questions might be dumb, but he could remove the hierarchical keywords, so I just thougt there might be a regex to remove the flat keywords.

Gert

Offline Kirk Baker

  • Senior Software Engineer
  • Camera Bits Staff
  • Superhero Member
  • *****
  • Posts: 24118
    • View Profile
    • Camera Bits, Inc.
Re: Remove flat keywords from PM using Find/Replace
« Reply #4 on: December 31, 2021, 03:13:28 PM »
Look up "regular expressions documentation" in your favorite search engine.

I'm no regex expert, but I think it would be quite difficult to do the inverse of removing the hierarchical keywords.

Can I ask why you need to remove them?

-Kirk

Offline ejhutch

  • Newcomer
  • *
  • Posts: 45
    • View Profile
Re: Remove flat keywords from PM using Find/Replace
« Reply #5 on: January 01, 2022, 04:19:29 AM »
Look up "regular expressions documentation" in your favorite search engine.

I'm no regex expert, but I think it would be quite difficult to do the inverse of removing the hierarchical keywords.

Likely it is possible, however it is also likely difficult to form the regular expression with the proper syntax that does what you want.

You'll probably want to investigate lookaheads and lookbehinds.

Try these sites if you haven't run across them already:

https://www.regular-expressions.info

https://regex101.com

The first gives a ton of info on regexes, and the second gives you a way to test your regex as you build it.

Offline gand

  • Newcomer
  • *
  • Posts: 26
    • View Profile
Re: Remove flat keywords from PM using Find/Replace
« Reply #6 on: January 01, 2022, 02:22:17 PM »
Hi

@Kirk
I have many keywords, more than 2,000 thousands. In PM I now have 2 sets, both the structured and the flat keywords. Know they do not harm, but it is a little bit unstructured in my opinion. I'm also aware that it's the interaction with Lightroom that causes this. It would be ideal if there was an option, when using structured keywords, to turn off display/creation of flat keywords.

@ejhutch
Thank you for your suggestions, I will take a closer look at the pages you suggest and see if it can take me further.

Rgds
Gert

Offline Graham1

  • Full Member
  • ***
  • Posts: 147
    • View Profile
Re: Remove flat keywords from PM using Find/Replace
« Reply #7 on: February 06, 2022, 10:55:22 PM »
There is a way of removing just the flat keywords, leaving only the hierarchal keywords for Lightroom, but it is very laborious and you can only do it one file at a time.  It will probably not be worthwhile doing it for a large batch of files with different keywords.  But if you have a batch with the same keywords on a bulk ingest, you could remove the flat keywords in one image, then copy the resulting hierarchical keywords to a Metadata Template, then apply that template to the entire batch before importing into Lightroom.

To do this, you will need to use the excellent (and free) Notepad++.  Make sure that the Compare plugin is installed (do this from Plugins ¦ Plugins Admin if not already installed).

Set up Notepad++ with two tabbed blank text windows, say for example New1.txt and New2.txt.  It is easier if you look at them side by side: if you just see two tabs but only one window, right click on one tab header and select Move to Other View, then you will see two windows.

These are the steps:

1. In the PM Metadata (IPTC) Info panel, select all the keywords (ctrl-A), copy them, then past them (ctrl-V) into the New1.txt window in Notepad++.

2. In PM, return to the thumbnail and run Find and Replace (ctrl-shift-F). Run the ([\|]{1})|(\[(.*?)\]) grep string in Find and Replace to remove the structured keywords (if you look at Carl Seibert's article, you will see that this grep string was my suggestion).

3. Copy the resulting flat keywords from the PM Metadata (IPTC) Info panel and paste them into the New2.txt window in Notepad++.

4. Run the Compare plugin in Notepad ++.  You will see the differing text highlighted in darker yellow (at least on my system) in the New1.txt window.  That darker colouration represents the hierarchical keywords. 

5. Select the block of hierarchical keywords, copy, then paste this over the flat keywords in the PM Metadata (IPTC) Info panel resulting from step 3. above (or delete them first and then paste into the blank keywords panel if you prefer).

You will now have only the hierarchical keywords.  If you now save the metadata and import the images into Lightroom, the keywords will fit into your existing hierarchy without at the same time adding a load of unwanted flat keywords at the bottom of the Lightroom Keyword List.

It's all a bit long winded, but it should work (it does for me), if you really want to delete your flat keywords before importing into Lightroom.

I have not yet found a way of doing this via grep in PM alone, but if I do, I will post it on this forum.  The process in Notepad++ could possibly be shortened via grep and/or a macro but it is simple enough anyway.

Graham





Offline gand

  • Newcomer
  • *
  • Posts: 26
    • View Profile
Re: Remove flat keywords from PM using Find/Replace
« Reply #8 on: February 07, 2022, 07:55:54 PM »
Hi Graham

Thank you very much for your suggestion. I'll try that.

I hope PM at some point will open up the opportunity to choose whether to show flat keywords or not. It's a bit annoying to have all keywords double.

Gert