File size: 4,336 Bytes
3045238
 
99b3ef5
3045238
 
a676448
3045238
e4ee4df
3045238
75d4ca3
3045238
e8bbf2e
420ea26
e8bbf2e
420ea26
 
 
3045238
 
 
75d4ca3
 
 
 
 
 
 
 
 
 
 
 
 
7a778e3
75d4ca3
e8bbf2e
75d4ca3
7a778e3
 
75d4ca3
e8bbf2e
75d4ca3
 
 
 
 
 
 
 
 
 
8992d21
75d4ca3
 
8992d21
75d4ca3
e8bbf2e
401d60b
420ea26
 
 
e4ee4df
420ea26
e8bbf2e
7a778e3
 
401d60b
420ea26
401d60b
e4ee4df
420ea26
 
e4ee4df
420ea26
 
 
 
3045238
420ea26
3045238
 
 
420ea26
e8bbf2e
e4ee4df
 
 
 
 
420ea26
e4ee4df
 
 
e8bbf2e
3045238
e4ee4df
e8bbf2e
 
e4ee4df
e8bbf2e
 
3045238
e8bbf2e
7a778e3
e8bbf2e
401d60b
1b969ef
 
420ea26
3045238
e8bbf2e
420ea26
 
 
1b969ef
 
3045238
 
 
a676448
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)

import gradio as gr
from gradio_calendar import Calendar
from gematria import calculate_gematria, strip_diacritics
from datetime import datetime, timedelta
import json
import inflect

# --- Helper Functions ---
def calculate_gematria_sum(text):
    """Calculates the gematria sum of a given text."""
    if text:
        text_gematria = calculate_gematria(strip_diacritics(text))
        return text_gematria
    else:
        return None

# Custom function to convert number to ordinal words
def number_to_ordinal_word(number):
    ordinal_dict = {
        1: "first", 2: "second", 3: "third", 4: "fourth", 5: "fifth",
        6: "sixth", 7: "seventh", 8: "eighth", 9: "ninth", 10: "tenth",
        11: "eleventh", 12: "twelfth", 13: "thirteenth", 14: "fourteenth",
        15: "fifteenth", 16: "sixteenth", 17: "seventeenth", 18: "eighteenth",
        19: "nineteenth", 20: "twentieth", 21: "twentyfirst", 22: "twentysecond",
        23: "twentythird", 24: "twentyfourth", 25: "twentyfifth",
        26: "twentysixth", 27: "twentyseventh", 28: "twentyeighth",
        29: "twentyninth", 30: "thirtieth", 31: "thirtyfirst"
    }
    return ordinal_dict.get(number, "")

def date_to_words(date_string):
    """Converts a date in YYYY-MM-DD format to English words."""
    inf_engine = inflect.engine()
    date_obj = datetime.strptime(date_string, "%Y-%m-%d")

    year = date_obj.year
    if 1100 <= year <= 1999:
        year_words = f"{inf_engine.number_to_words(year // 100, andword='') } hundred"
        if year % 100 != 0:
            year_words += f" {inf_engine.number_to_words(year % 100, andword='')}"
    else:
        year_words = inf_engine.number_to_words(year, andword='')
    year_formatted = year_words.replace(',', '')

    month = date_obj.strftime("%B")
    day = date_obj.day
    day_ordinal = number_to_ordinal_word(day)

    output_text = f"{day_ordinal} {month} {year_formatted}"
    return output_text

def perform_gematria_calculation_for_date_range(start_date, end_date):
    """Performs gematria calculation for a date range and groups by sum."""
    logger.debug(f"Start Date: {start_date}, End Date: {end_date}")
    results = {}
    delta = timedelta(days=1)
    current_date = start_date

    while current_date <= end_date:
        # Convert date to string
        date_string = current_date.strftime("%Y-%m-%d")
        date_words = date_to_words(date_string)
        logger.debug(f"Date: {current_date}, Day Name: {date_words}")
        gematria_sum = calculate_gematria_sum(date_words)
        logger.debug(f"Gematria Sum: {gematria_sum}")

        if gematria_sum not in results:
            results[gematria_sum] = []

        results[gematria_sum].append({
            "date": current_date.strftime('%Y-%m-%d'),
            "date_words": date_words
        })

        current_date += delta

    return results

def generate_json_output(results, start_date, end_date):
    """Generates the JSON output with the date range and results."""
    result = {
        "DateRange": {
            "StartDate": start_date.strftime("%Y-%m-%d"),
            "EndDate": end_date.strftime("%Y-%m-%d")
        },
        "Results": results
    }
    return json.dumps(result, indent=4, ensure_ascii=False)

# --- Main Gradio App ---
with gr.Blocks() as app:
    with gr.Row():
        start_date = Calendar(type="datetime", label="Start Date")
        end_date = Calendar(type="datetime", label="End Date")

    calculate_btn = gr.Button("Calculate Gematria for Date Range")
    json_output = gr.Textbox(label="JSON Output")

    # --- Event Handlers ---
    def perform_calculation(start_date, end_date):
        """Performs the calculation and generates the JSON output."""
        logger.debug(f"perform_calculation called with: start_date={start_date}, end_date={end_date}")
        results = perform_gematria_calculation_for_date_range(start_date, end_date)
        json_result = generate_json_output(results, start_date, end_date)
        return json_result

    # --- Event Triggers ---
    calculate_btn.click(
        perform_calculation,
        inputs=[start_date, end_date],
        outputs=[json_output],
        api_name="calculate_gematria"
    )

if __name__ == "__main__":
    app.launch(share=False)