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 12-24-2014, 05:16 PM   #1
weberr
New old guy
weberr began at the beginning.
 
Posts: 69
Karma: 10
Join Date: Feb 2012
Device: kindle fire
regex-function convert roman numerals

Has anyone created a function to convert roman numerals to Arabic, and vice versus?
Or help me to learn how to create such a function?
weberr is offline   Reply With Quote
Old 12-24-2014, 05:25 PM   #2
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 73,957
Karma: 128903250
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
There is no way to convert I to 1 without also converting the word I. So that's out for sure. You would have to do I by doing a manual search/replace.
JSWolf is offline   Reply With Quote
Advert
Old 12-24-2014, 06:19 PM   #3
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 20,567
Karma: 26954694
Join Date: Mar 2012
Location: Sydney Australia
Device: none
Quote:
Originally Posted by weberr View Post
Has anyone created a function to convert roman numerals to Arabic, and vice versus?
Or help me to learn how to create such a function?
@weberr - Here's the python code ==>> To and From Roman and Integer

Maybe it could be wrangled onto a regex function;

BR
BetterRed is offline   Reply With Quote
Old 12-24-2014, 09:09 PM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 43,851
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
It should be fairly trivial, I outlined a solution in this thread: https://www.mobileread.com/forums/sho...d.php?t=244255

for uppercasing roman numerals. Converting them to arabic is similar, a bit of googling will find you a python function to do the actual conversion.

The only major problem would be dealing with small numbers, the dictionary based approach will probably not work well for numbers of less than three digits, so you would need to special case them.
kovidgoyal is offline   Reply With Quote
Old 12-25-2014, 04:29 AM   #5
jbacelar
Interested in the matter
jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.
 
jbacelar's Avatar
 
Posts: 421
Karma: 426094
Join Date: Dec 2011
Location: Spain, south coast
Device: Pocketbook InkPad 3
What I use.

Arabic to Roman
Search (eg):
(>Chapter )(\d+)(<)

Regex-function:
Code:
def num_roman(input):
    ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
    nums = ('M',  'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
    result = ""
    input=int(input)
    for i in range(len(ints)):
        count = input//ints[i]
        result += nums[i] * count
        input -= ints[i] * count
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(num_roman(match.group(2)))+match.group(3)
Roman to Arabic
Search (eg):
(>Chapter )(.+?)(<)

Regex-function:

Code:
numeral_map = zip(
    (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
    ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
)
	   
def arabic_num(n):
    n = unicode(n).upper()
    i = result = 0
    for integer, numeral in numeral_map:
        while n[i:i + len(numeral)] == numeral:
            result += integer
            i += len(numeral)
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(arabic_num(match.group(2)))+match.group(3)
jbacelar is offline   Reply With Quote
Advert
Old 12-25-2014, 08:16 AM   #6
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 73,957
Karma: 128903250
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
Quote:
Originally Posted by jbacelar View Post
What I use.

Arabic to Roman
Search (eg):
(>Chapter )(\d+)(<)

Regex-function:
Code:
def num_roman(input):
    ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
    nums = ('M',  'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
    result = ""
    input=int(input)
    for i in range(len(ints)):
        count = input//ints[i]
        result += nums[i] * count
        input -= ints[i] * count
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(num_roman(match.group(2)))+match.group(3)
Roman to Arabic
Search (eg):
(>Chapter )(.+?)(<)

Regex-function:

Code:
numeral_map = zip(
    (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
    ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
)
	   
def arabic_num(n):
    n = unicode(n).upper()
    i = result = 0
    for integer, numeral in numeral_map:
        while n[i:i + len(numeral)] == numeral:
            result += integer
            i += len(numeral)
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(arabic_num(match.group(2)))+match.group(3)
And do you then go back and fix every occurrence of the WORD I changed to a 1?
JSWolf is offline   Reply With Quote
Old 12-25-2014, 08:40 AM   #7
jbacelar
Interested in the matter
jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.jbacelar ought to be getting tired of karma fortunes by now.
 
jbacelar's Avatar
 
Posts: 421
Karma: 426094
Join Date: Dec 2011
Location: Spain, south coast
Device: Pocketbook InkPad 3
Quote:
Originally Posted by JSWolf View Post
And do you then go back and fix every occurrence of the WORD I changed to a 1?
I use it to change things as these:
Chapter 22, which changes to Chapter XXII.
O Chapter XXII, which changes to Chapter 22.
Also for the notes, as [22] changes to [XXII] and vice versa.
As you can see, I will not go to fix anything later.
It is a matter of imagination and cleverness to search.

Last edited by jbacelar; 12-25-2014 at 03:20 PM. Reason: My bad English
jbacelar is offline   Reply With Quote
Old 12-25-2014, 09:32 AM   #8
JSWolf
Resident Curmudgeon
JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.JSWolf ought to be getting tired of karma fortunes by now.
 
JSWolf's Avatar
 
Posts: 73,957
Karma: 128903250
Join Date: Nov 2006
Location: Roslindale, Massachusetts
Device: Kobo Libra 2, Kobo Aura H2O, PRS-650, PRS-T1, nook STR, PW3
Quote:
Originally Posted by jbacelar View Post
I use it to change things how are you:
Chapter 22, which changes to Chapter XXII.
O Chapter XXII, which changes to Chapter 22.
Also for the notes, as [22] changes to [XXII] and vice versa.
As you can see, I will not go to fix anything later.
It is a matter of imagination and cleverness to search.
Yes, if you can find text or code in front of the I that will differentiate an I (1) from an I, then yes, it will work. But if you don't have anything you can use to differentiate then it will be a problem.
JSWolf is offline   Reply With Quote
Old 12-25-2014, 10:27 AM   #9
theducks
Well trained by Cats
theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.theducks ought to be getting tired of karma fortunes by now.
 
theducks's Avatar
 
Posts: 29,799
Karma: 54830978
Join Date: Aug 2009
Location: The Central Coast of California
Device: Kobo Libra2,Kobo Aura2v1, K4NT(Fixed: New Bat.), Galaxy Tab A
Quote:
Originally Posted by JSWolf View Post
Yes, if you can find text or code in front of the I that will differentiate an I (1) from an I, then yes, it will work. But if you don't have anything you can use to differentiate then it will be a problem.
The example S&R will not touch the body of the work. It looks for "Chapter\s followed by a Roman Number

If you want to be sloppy, I guess you would have to worry about your
"I worry that this will become..."
"1 worry that this became..."
theducks is offline   Reply With Quote
Old 12-25-2014, 11:24 AM   #10
BetterRed
null operator (he/him)
BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.BetterRed ought to be getting tired of karma fortunes by now.
 
Posts: 20,567
Karma: 26954694
Join Date: Mar 2012
Location: Sydney Australia
Device: none
Quote:
Originally Posted by JSWolf View Post
Yes, if you can find text or code in front of the I that will differentiate an I (1) from an I, then yes, it will work. But if you don't have anything you can use to differentiate then it will be a problem.
If you had your wits about you then you wouldn't use the function indiscriminately

The editor provides a checkpointing facility for use by witful users who want to dodge the bullets they aim at their feet, but in the final washup there is nothing one can do to protect the witless from their witlessness.

Quote:
Originally Posted by theducks View Post
If you want to be sloppy, I guess you would have to worry about your
"I worry that this will become..."
"1 worry that this became..."
If you worry about whatever became of 'will become' then your troubles haven't even begun ==>> English verb become conjugated in all tenses.

BR

Last edited by BetterRed; 12-25-2014 at 11:40 AM.
BetterRed is offline   Reply With Quote
Old 12-25-2014, 10:31 PM   #11
weberr
New old guy
weberr began at the beginning.
 
Posts: 69
Karma: 10
Join Date: Feb 2012
Device: kindle fire
Thanks. Works for me -- as long as you 'replace all'
Nice piece of work
weberr is offline   Reply With Quote
Old 09-22-2021, 05:15 PM   #12
Ted Friesen
Nameless Being
 
Quote:
Originally Posted by jbacelar View Post
What I use.

Arabic to Roman
Search (eg):
(>Chapter )(\d+)(<)

Regex-function:
Code:
def num_roman(input):
    ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
    nums = ('M',  'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')
    result = ""
    input=int(input)
    for i in range(len(ints)):
        count = input//ints[i]
        result += nums[i] * count
        input -= ints[i] * count
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(num_roman(match.group(2)))+match.group(3)
Roman to Arabic
Search (eg):
(>Chapter )(.+?)(<)

Regex-function:

Code:
numeral_map = zip(
    (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
    ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
)
	   
def arabic_num(n):
    n = unicode(n).upper()
    i = result = 0
    for integer, numeral in numeral_map:
        while n[i:i + len(numeral)] == numeral:
            result += integer
            i += len(numeral)
    return result
def replace(match, number, file_name, metadata, dictionaries, data, functions, *args, **kwargs):
    return match.group(1)+str(arabic_num(match.group(2)))+match.group(3)
I know it's been a while, but I just found this and want to thank you for posting. All I had to do to get it to work was to change unicode to str.
  Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Book detail area: series number given in Roman numerals BeccaPrice Calibre 19 09-08-2015 11:42 PM
Roman Numerals for series and on book jacket Arbait Library Management 31 05-17-2015 01:16 AM
Disabling roman numerals in series display? MelBr Calibre 2 09-19-2013 10:49 PM
Convert Roman numerals to Arabic? Peter W Sigil 2 04-09-2012 11:55 AM
regex search for roman numerals Blurr Calibre 2 12-16-2009 05:55 PM


All times are GMT -4. The time now is 11:29 PM.


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