View Single Post
Old 01-18-2022, 08:33 AM   #934
Wiggo
Leftutti
Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.Wiggo ought to be getting tired of karma fortunes by now.
 
Wiggo's Avatar
 
Posts: 549
Karma: 1717097
Join Date: Feb 2019
Location: Bavaria
Device: iPad Pro, Kobo Libra 2
Quote:
Originally Posted by capink View Post


Example No. 4 How to match by alias instead of author.

Spoiler:

This is one of the most interesting examples on how to use templates to find duplicates. It assumes that you are already using user categories in Calibre to set up author aliases as this post by @BetterRed suggests (which is further illustrated here in this attachment).

After creating your aliases as explained above, we will use the template below, which will substitute the author name with the alias if it finds one in the user categories, otherwise it returns the author name unchanged. We can use this template directly as in previous examples, but this time it makes sense to use the template to create a composite column containing aliases that can used for other things, beside duplicate finding, like searching in Calibre by alias.
  • Add the code below to Calibre templates (we have already how to do this explained in example No. 3).

    Code:
    def evaluate(self, formatter, kwargs, mi, locals, val, col_name, user_cat_prefix):
        new_val = ''
        if hasattr(mi, '_proxy_metadata'):
            all_cats = mi._proxy_metadata.user_categories
            cats = {k:v for k,v in all_cats.items() if k.startswith(user_cat_prefix)}
            SEP = mi.metadata_for_field(col_name)['is_multiple'].get('list_to_ui', '')
            new_val = set()
            if SEP:
                val_ = val.split(SEP)
            else:
                val_ = [val]  
            for user_cat, v in cats.items():
                repl = user_cat.lstrip(user_cat_prefix)
                for user_cat_item, src_cat in v:
                    if src_cat == col_name:
                        for item in val_[:]:
                            if item == user_cat_item:
                                new_val.add(repl)
                                val_.remove(item)
                            else:
                                new_val.add(item)
            if new_val:
                return SEP.join(list(new_val))
        return val
    The rest of the the template setting should look like this:
    Code:
    Function: replace_with_category
    Argument count: 3
    Now, press create button to add the code to Calibre's templates.
  • Create a new composite column with these exact settings (see attachment 8):

    Code:
    Lookup name: alias
    Column heading: alias
    Column type: Column built from other columns, behaves like tags
    Template: {authors:replace_with_category(authors,Authors.Alias.)}
    You will have to restart Calibre for the new column to be effective.

    Note: If you don't choose the "column built from other columns, behave like tags" the duplicate search will not work.

    Note: if you decide to use different structure for your user category, you have to replace Authors.Alias. with whatever the user category hierarchy you are using

    Note: In simple template mode, spaces are significant. Don't add any space after the comma.
  • Now, to start matching by alias choose the title field and whatever algorithm you want (I will choose similar match for the title).
  • Instead of using the authors column we will use the newly built alias column, you will notice as the soon as you choose this column, a new "contains names" checkbox appears (see attachment 9), you have to check it for this duplicate search to work.

    This setting tells the plugin to split contents of the alias column using "&" as it does with the authors.

    Note that we don't need this option when dealing with the authors column — on any non-composite custom column — as the plugin can know from their metadata that they contain names, and acts accordingly. Unfortunately, Calibre does not give us an option when we create a composite column to indicate that it contains names, so we have to use this option to tell the plugin what to do.
  • Now choose an algorithm you want for the alias column (you can choose identical here) and click OK to start the find duplicate search.

Note: The above template can be used for purposes other than aliases. Any user categories you apply for series or authors (like nationality) can be added to a composite column appearing in books.


Hi capink,

it is possible to separate the aliases like in the normal authors (black instead of blue ampersand)?

I have a group-search "pen-names" with authors and #alias and do my similar authors search by this pen-names. If I search for "Robert Lynn Asprin" all the books with Robert Asprin & Co-Author will not be found, because the alias is e.g. "Robert Lynn Asprin & Linda Evans" and not "Robert Lynn Asprin" & "Linda Evans".

I hope I expressed it clearly
Attached Thumbnails
Click image for larger version

Name:	calibre_8wYgVuBfTG.jpg
Views:	223
Size:	12.2 KB
ID:	191671  
Wiggo is offline   Reply With Quote