File size: 2,503 Bytes
7cfa475
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
iimport gradio as gr

def convert_to_french_letters(number):
    # Define lists of French words for numbers
    units = ['', 'un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept', 'huit', 'neuf']
    tens = ['', 'dix', 'vingt', 'trente', 'quarante', 'cinquante', 'soixante', 'soixante-dix', 'quatre-vingt', 'quatre-vingt-dix']
    # Define special cases for 70, 80, and 90
    special_tens = {70: 'soixante-dix', 80: 'quatre-vingt', 90: 'quatre-vingt-dix'}

    # Handle special cases
    if number in special_tens:
        return special_tens[number]

    # Extract digits from number
    billions = number // 1000000000
    millions = (number // 1000000) % 1000
    thousands = (number // 1000) % 1000
    hundreds = (number // 100) % 10
    tens_and_units = number % 100

    # Convert digits to words
    words = []
    if billions > 0:
        if billions == 1:
            words.append('un milliard')
        else:
            words.append(convert_to_french_letters(billions) + ' milliards')
    if millions > 0:
        if millions == 1:
            words.append('un million')
        else:
            words.append(convert_to_french_letters(millions) + ' millions')
    if thousands > 0:
        if thousands == 1:
            words.append('mille')
        else:
            words.append(convert_to_french_letters(thousands) + ' mille')
    if hundreds > 0:
        if hundreds == 1:
            words.append('cent')
        else:
            words.append(units[hundreds] + ' cent')
    if tens_and_units > 0:
        if tens_and_units < 10:
            words.append(units[tens_and_units])
        elif tens_and_units < 20:
            words.append('dix-' + units[tens_and_units-10])
        else:
            tens_digit = tens[tens_and_units // 10]
            units_digit = units[tens_and_units % 10]
            if tens_digit == 'vingt' and units_digit == 'un':
                tens_digit = 'vingt-et-un'
                units_digit = ''
            words.append(tens_digit + '-' + units_digit)

    # Join words and return result
    return ' '.join(words)

inputs = gr.inputs.Number(label="Number to convert")
outputs = gr.outputs.Textbox(label="Result")

title = "Number to French Letters Converter"
description = "A program that converts numbers to their French letter equivalents"
examples = [[25230], [1000000000], [1234567890]]

iface = gr.Interface(fn=convert_to_french_letters, inputs=inputs, outputs=outputs, title=title, description=description, examples=examples)

iface.launch()