Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Editor

Notices

Reply
 
Thread Tools Search this Thread
Old 07-29-2022, 01:36 AM   #16
zhliu0124
Member
zhliu0124 began at the beginning.
 
Posts: 12
Karma: 10
Join Date: Aug 2017
Device: Kindle Paperwhite 3
It works like a charm! Thank you so much for the code. It's neat and very efficient!

Quote:
Originally Posted by lomkiri View Post
You can do that with a regex-function.

Get the regex at https://regex101.com/r/4fWfX1/1 , it selects every word of 2 letters or more (you can increase this limit). Thanks to EbookMakers, he made the regex.

Get the function below, and "replace all".
You'll have to change the file name, and put another tag if you wish
At the end of the job, there is a "debug" msg with the number of tagged words.

BEWARE:
The search in the function is case sensitive. If you want it insensitive, change the line 40.
If you do that (case insensitive), remember that all words in the list must be lowered.

BEWARE: The file used here is not a CVS one
The file is build in this form : a single word by line (one line = one word to mark)
If you want to use a CVS file, show me the format you used, and I'll adjust the function load_file() for you, but only tomorrow night.

Code:
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    
    # ============= change this if needed ===============
    fname = '/data/temp/words.txt'    # This is for linux, find by yourself how to give the path in windows
    tag_begin = '<mark>'    # or <span class="marked"> for exemple
    tag_end = '</mark>'
    # See also the test at line 40 if you want a search with case insensitive
    # =================
    
    def load_list(fname):
        data['error'] = False
        data['nb_tagged'] = 0
        try:
            fd = open(fname, 'r')
        except FileNotFoundError:
            print(f"File {fname} not found, or error in opening it")
            # raise		# if we raise the error, the msg above will not be printed
            data['error'] = True
            return []

        # put the file in the dict
        list_word = [line.strip() for line in fd]
        fd.close()
        if not list_word:
            print(f"File {fname} is empty")
            # raise		# if we raise the error, the msg above will not be printed
            data['error'] = True
        return list_word
    
    if not match:	# last passage
        print(f"Number of words tagged: {data['nb_tagged']}")
        return

    if number == 1:	# first passage
        replace.call_after_last_match = True	# ask for last passage after all occ.
        data['words'] = load_list(fname)

    # put this instead if you want insensitive search:
    # if not data['error'] and match[1].lower() in data['words']:
    if not data['error'] and match[1] in data['words']:
        
        data['nb_tagged'] += 1
        return tag_begin + match[1] + tag_end
    return match[1]

Last edited by zhliu0124; 07-29-2022 at 01:40 AM.
zhliu0124 is offline   Reply With Quote
Old 07-29-2022, 01:37 AM   #17
zhliu0124
Member
zhliu0124 began at the beginning.
 
Posts: 12
Karma: 10
Join Date: Aug 2017
Device: Kindle Paperwhite 3
Thanks! It could be a good alternative!

Quote:
Originally Posted by BetterRed View Post
FWIW - if you have Word (2007 on) you could convert the book to DOCX and use the Mark feature of Toxaris' eBook Tools MS Word add-in and then convert the DOCX back to EPUB.

The Mark tool provides for multiple lists of 'words' that you could highlight in different colours (e.g. yellow for nouns, pink for verbs, green for adjectives etc). The word lists are stored in XML files, there's a tool to maintain them, or you can edit them in a text editor.

BR
zhliu0124 is offline   Reply With Quote
Advert
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I send the viewer's annotation/highlight to an epub file? jeju2 Library Management 3 12-19-2021 09:10 PM
How to highlight words/sentences in PDF files? KarlVoit Onyx Boox 10 11-30-2020 08:24 AM
How to make Balabolka highlight words instead of sentences? MRUser Audiobook Hardware & Software 0 01-28-2018 02:17 AM
Cant highlight/ lookup words Kindle touch alanHd Amazon Kindle 10 08-26-2013 06:32 PM
Kindle 3: Highlight and lookup the definitions for words not in dictionary? ivanatpr Amazon Kindle 13 10-28-2010 09:54 AM


All times are GMT -4. The time now is 08:28 PM.


MobileRead.com is a privately owned, operated and funded community.