![]() |
#1 | |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 106
Karma: 4486
Join Date: Mar 2020
Location: Netherlands
Device: i-pad
|
Renaming items on a list
Quote:
Also I'm not sure I understand how to use the template scope properly to search and then get the "template output is calibre search' using listed items. I sort of got it to work, but I'm not sure I use this correctly: Code:
#tags_show:"=tram" or #tags_show:"=bus" Code:
search(#tags_show, ('tram, bus') |
|
![]() |
![]() |
![]() |
#2 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 106
Karma: 4486
Join Date: Mar 2020
Location: Netherlands
Device: i-pad
|
So I've been fiddling with it some more, and figured out I can use the same principle as this
Code:
program: a = list_intersection($ao3tags, 'tram, train, bus, ferry', ','); list_union($#genre, a, ',') Code:
program: a = list_intersection($#show, 'Ferry, Bus', ','); re($#show, a , 'Vehicles, Public Transport') tags: Bus, Car, Ferry, Ferry Terminal, the result will be: tags: Vehicles, Public Transport, Vehicles, Public Transport Terminal, Red, Car The tag 'Ferry Terminal' will result in 'Public Transport Terminal'. I figured the reason is because it is a match but not an exact match. I've tried several options trying to get an exact match using ^ and $ like with regex but I keep getting errors ![]() Can anyone point me in the right direction, because the Calibre Manual ![]() |
![]() |
![]() |
Advert | |
|
![]() |
#3 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Lets say you are trying to replace 'Ferry' or 'Starship' with 'Vehicle', and 'Bus' with 'Public Transport'. This does it. Code:
program: sample_input = 'Bus, Car, Starship, Ferry, Ferry Terminal, Alien Spacecraft'; output = list_re_group( sample_input, ',', '.', '(^bus$)|(^ferry$|^starship$)', 'Public Transport', 'Vehicle') Code:
Public Transport, Car, Vehicle, Ferry Terminal, Alien Spacecraft One thing to know: re() is nearly useless when dealing with lists. It doesn't deal with list order, instead treating the list as text. In certain situations such as my example above you can use list_re_group() so that the regular expression is applied to each list item, not the list itself. If this discussion will go on for a while then it should get moved to its own thread. I can do that. |
|
![]() |
![]() |
![]() |
#4 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Moderator Notice
Discussion moved to its own thread |
![]() |
![]() |
![]() |
#5 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
This is a way to do the same example using a for loop. It might be easier to read and understand.
Code:
program: sample_input = 'Bus, Car, Starship, Ferry, Ferry Terminal, Alien Spacecraft'; output = ''; for item in sample_input: if item == 'bus' then ni = 'Public Transport' elif item == 'ferry' then ni = 'Vehicle' elif item == 'starship' then ni = 'Vehicle' else ni = item fi; output = output & ',' & ni rof; output = list_remove_duplicates(output, ',') Code:
Public Transport, Car, Vehicle, Ferry Terminal, Alien Spacecraft Code:
output = list_sort(list_remove_duplicates(output, ','), 0, ',') Code:
Alien Spacecraft, Car, Ferry Terminal, Public Transport, Vehicle |
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 106
Karma: 4486
Join Date: Mar 2020
Location: Netherlands
Device: i-pad
|
Template Functionality
@Chaley,
Thanks for the quick reply, I'm trying to learn my may around with template functions and the best way for me is to learn by trial and error and I try to learn from previous posts or the Calibre manual but I'm missing some basic examples to make me understand sometimes and some of the posts are too advanced for me to understand for now. I even started some free online phyton code writing course to improve my understanding of the examples on this forum. So more questions are likely tbh, if this is not the place but a separate thread is, then by all means move it, I wouldn't know how to (if allowed at all). To answer your question, yes this was a continuation of my post on the 14th. I thought I had figured it out, but guess not ![]() I do not care about the order of the tags, but good to know it exists. Obviously the data is bogus, I was trying to change 2 out of the 4 tags by matching the whole string while keeping the rest in tact. Perhaps my example was not so clear since I also had two tags as the outcome. But for clarity another example people use ship names in characters for example 'Clexa' and 'Princess Commander' the character names are Clarke and Lexa, so I'm trying to convert these lists of ship names into separate characters. Since AO3 keeps inventing new tags I occasionally dump the new / left over data into excel and figure out which I want to keep and change and then auto convert them into strings that I copy back adding into an new/or existing line in the action chains. There are built in options, but the Search and Replace box was becoming to small and unreadable and single field edit annoying because you can not amend you list but in stead have to select it all over again. Which is why I was looking into these template functions to begin with. |
![]() |
![]() |
![]() |
#7 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
@ackomb: If I came across as critical or negative then I am sorry. I really like it when people try and I am glad to help. We both end up learning something.
![]() Also, the posts are already moved to a new thread. ![]() Given your Clexa etc example I suspect that the for loop version will be easiest for you to maintain over time. It is more clear what is being changed to what. It can be slightly improved to better handle several variants going to the same result, which might be useful as the list of conditions gets longer. Example: Code:
program: sample_input = 'Bus, Car, Starship, Ferry, Ferry Terminal, Alien Spacecraft'; output = ''; for item in sample_input: if '^bus$' in item then ni = 'Public Transport' elif '^ferry|starship$'in item then ni = 'Vehicle' else ni = item fi; output = output & ',' & ni rof; output = list_remove_duplicates(output, ',') On the other hand I can imagine using excel to generate the template replacement expressions, which could make the '==' form better. I have in the past considered writing a 'list_map() function that would take a series of pairs: (list_of_terms_to_change, result_value). Example: Code:
map(src_list, separator, 'ferry, boat, hydroplane', 'watercraft', 'car, auto, truck', 'vehicle', ...) EDIT: Another option is to supply a template interface to the existing tag mapper (Preferences / Adding books / Adding actions / Rules to filter tags). The function would take a series of lists that each define a mapper rule, then apply those rules. The benefit here is that the code exists and is maintained. The downside is that rules are a bit more complicated to express. Last edited by chaley; 07-16-2022 at 02:27 PM. |
![]() |
![]() |
![]() |
#8 | |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 106
Karma: 4486
Join Date: Mar 2020
Location: Netherlands
Device: i-pad
|
Quote:
So I guess my code will go from here adding elif's when needed Code:
program: output = ''; for item in $#cumstom_column: if item == 'bus' then ni = 'Public Transport' elif item == 'car' then ni = 'Public Transport' elif item == 'ferry' then ni = 'Public Transport' elif item == 'red' then ni = 'Public Transport' else ni = item fi; output = output & ',' & ni rof; output = list_sort(list_remove_duplicates(output, ','), 0, ',') Input: Code:
Bus, Car, Ferry, Ferry Terminal, Red Code:
Ferry Terminal, Public Transport Code:
program: a = list_intersection($#cumstom_column_a, 'tram, train, bus, ferry', ','); list_union($#cumstom_column_b, a, ',') Code:
program: list = 'bus, car, ferry, red' output = ''; for item in $#cumstom_column: if item == in list then ni = 'Public Transport' else ni = item fi; output = output & ',' & ni rof; output = list_sort(list_remove_duplicates(output, ','), 0, ',') |
|
![]() |
![]() |
![]() |
#9 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
program: sample_input = 'bus, carpool, ferry, redbus'; list = 'bus, car, ferry, red'; output = ''; # Change $#custom_column to sample_input so the value exists, avoiding an error. for item in sample_input: if ('^' & item & '$') inlist list then ni = 'Public Transport' else ni = item fi; output = output & ',' & ni rof; output = list_sort(list_remove_duplicates(output, ','), 0, ',') Quote:
|
||
![]() |
![]() |
![]() |
#10 | |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 106
Karma: 4486
Join Date: Mar 2020
Location: Netherlands
Device: i-pad
|
Quote:
![]() Thank you for your help, right now I'm to tired to think straight, so I will try to implement it tomorrow and see how far I will get. I'm beginning to understand more and more. ![]() |
|
![]() |
![]() |
![]() |
#11 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 13,511
Karma: 78910202
Join Date: Nov 2007
Location: Toronto
Device: Libra H2O, Libra Colour
|
Could you also use a switch statement, rather than lots of elif statements?
Sent from my Pixel 4a using Tapatalk |
![]() |
![]() |
![]() |
#12 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
One difference is that the chain of elifs stops on a match while a switch evaluates all the pairs then picks the first that matches. The switch is slower if there are matches. In most contexts that probably doesn't matter. |
|
![]() |
![]() |
![]() |
#13 | |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 106
Karma: 4486
Join Date: Mar 2020
Location: Netherlands
Device: i-pad
|
Quote:
Let say that my custom_column named #ao3show contains the following items: Code:
Bus, Car, Ferry, Ferry Terminal, Red Code:
program: sample_input = 'Bus, Car, Ferry, Ferry Terminal'; list = 'bus, ferry'; output = ''; # Change $#custom_column to sample_input so the value exists, avoiding an error. for item in sample_input: if ('^' & item & '$') inlist list then ni = 'Public Transport' else ni = item fi; output = output & ',' & ni rof; output = list_sort(list_remove_duplicates(output, ','), 0, ',') Code:
Car, Ferry Terminal, Public Transport Whereas if I replace the list sample_input with the actual column and add a 2nd list with tags I want to remove like this: Code:
program: list = 'bus, ferry'; list2 = 'Car'; output = ''; # Change $#custom_column to sample_input so the value exists, avoiding an error. for item in $#ao3show: if ('^' & item & '$') inlist list then ni = 'Public Transport' elif ('^' & item & '$') inlist list2 then ni = ' ' else ni = item fi; output = output & ',' & ni rof; output = list_sort(list_remove_duplicates(output, ','), 0, ',') Code:
Ferry Terminal, Public Transport, Red Last edited by ackomb; 07-17-2022 at 06:30 AM. Reason: I attached the wrong screenshot, Car should not be in there |
|
![]() |
![]() |
![]() |
#14 | ||||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
Quote:
FWIW: The comment Change ... is now meaningless because you are using live data that I don't have. Quote:
|
||||
![]() |
![]() |
![]() |
#15 |
Zealot
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 106
Karma: 4486
Join Date: Mar 2020
Location: Netherlands
Device: i-pad
|
So far so good, I've been tinkering with all this for a while but I've run into something that has me stumped.
Withing FFF there is this option 'sort_ships' which alphabetically sorts the characters within a ship. Now I want to sort them after I changed them, how would I program such a thing? |
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Jumping to items in book list without activating keyboard shortcuts | ownedbycats | Library Management | 7 | 01-26-2021 03:22 PM |
Commas Following List Items | TheArtfulDodger | Library Management | 3 | 07-15-2018 06:33 PM |
All my documents from "My Items" list on Kindle Touch disappeared | kinkle | Kindle Developer's Corner | 33 | 03-02-2016 02:42 PM |
Recipe for Safari "Reading List" unread items | anoved | Recipes | 6 | 03-19-2013 01:21 PM |
Cant remove items from "Im Reading" list | fictionaddiction | Kobo Reader | 1 | 06-21-2011 12:11 PM |