neuralworm commited on
Commit
4428eed
1 Parent(s): 57bd653

fix year-words

Browse files
Files changed (1) hide show
  1. app.py +58 -46
app.py CHANGED
@@ -63,6 +63,16 @@ def format_date(date_obj, inf_engine):
63
  return f"{month} {year_formatted}" # Return without day
64
 
65
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  def perform_gematria_calculation_for_date_range(start_date, end_date):
68
  """Performs gematria calculation for a date range and groups by sum."""
@@ -71,7 +81,11 @@ def perform_gematria_calculation_for_date_range(start_date, end_date):
71
  delta = timedelta(days=1)
72
  current_date = start_date
73
 
 
 
 
74
  while current_date <= end_date:
 
75
  date_string = current_date.strftime("%Y-%m-%d")
76
  date_words = date_to_words(date_string)
77
  gematria_sum = calculate_gematria_sum(date_words)
@@ -80,49 +94,79 @@ def perform_gematria_calculation_for_date_range(start_date, end_date):
80
  results[gematria_sum] = []
81
  results[gematria_sum].append({"date": date_string, "date_words": date_words})
82
 
83
- # Handle Month-Year separately, without the day
84
- if current_date.day == 1: # Prevent multiple entries for the same month-year
85
- month_year_string = current_date.strftime("%Y-%m")
86
- month_year_words = f"{current_date.strftime('%B')} {current_date.year}"
 
87
  month_year_gematria_sum = calculate_gematria_sum(month_year_words)
88
 
89
  if month_year_gematria_sum not in results:
90
  results[month_year_gematria_sum] = []
91
  results[month_year_gematria_sum].append({
92
- "date": month_year_string,
93
  "date_words": month_year_words
94
  })
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  current_date += delta
97
 
98
  return results
99
 
100
 
 
 
 
 
 
 
 
101
 
102
 
103
- def generate_json_output(results, start_date, end_date):
104
  """Generates the JSON output with the date range and results."""
105
-
106
  result = {
107
  "DateRange": {
108
  "StartDate": start_date.strftime("%Y-%m-%d"),
109
  "EndDate": end_date.strftime("%Y-%m-%d")
110
  },
111
- "Results": results
112
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
113
  return json.dumps(result, indent=4, ensure_ascii=False)
114
 
115
- def download_json(json_data, start_date, end_date):
116
- """Downloads the JSON data to a file."""
117
- filename = f"gematria_results_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}.json"
118
- return (filename, json_data)
119
 
120
  # --- Main Gradio App ---
121
  with gr.Blocks() as app:
122
  with gr.Row():
123
  start_date = Calendar(type="datetime", label="Start Date")
124
  end_date = Calendar(type="datetime", label="End Date")
125
-
126
  include_date_words = gr.Checkbox(value=True, label="Include Date-Words in JSON")
127
 
128
  calculate_btn = gr.Button("Calculate Gematria for Date Range")
@@ -130,38 +174,6 @@ with gr.Blocks() as app:
130
  download_btn = gr.Button("Download JSON")
131
  json_file = gr.File(label="Downloaded JSON")
132
 
133
- # --- Event Handlers ---
134
- def perform_calculation(start_date, end_date, include_words):
135
- """Performs the calculation and generates the JSON output."""
136
- logger.debug(f"perform_calculation called with: start_date={start_date}, end_date={end_date}, include_words={include_words}")
137
- results = perform_gematria_calculation_for_date_range(start_date, end_date)
138
- json_result = generate_json_output(results, start_date, end_date, include_words)
139
- return json_result
140
-
141
- def generate_json_output(results, start_date, end_date, include_words):
142
- """Generates the JSON output with the date range and results."""
143
- result = {
144
- "DateRange": {
145
- "StartDate": start_date.strftime("%Y-%m-%d"),
146
- "EndDate": end_date.strftime("%Y-%m-%d")
147
- },
148
- "Results": []
149
- }
150
- for gematria_sum, entries in results.items():
151
- group = {
152
- "GematriaSum": gematria_sum,
153
- "Entries": []
154
- }
155
- for entry in entries:
156
- entry_data = {"date": entry["date"]}
157
- if include_words: # Conditionally include `date_words`
158
- entry_data["date_words"] = entry["date_words"]
159
- group["Entries"].append(entry_data)
160
- result["Results"].append(group)
161
-
162
- # Ensure JSON validity
163
- return json.dumps(result, indent=4, ensure_ascii=False)
164
-
165
  # --- Event Triggers ---
166
  calculate_btn.click(
167
  perform_calculation,
@@ -177,4 +189,4 @@ with gr.Blocks() as app:
177
  )
178
 
179
  if __name__ == "__main__":
180
- app.launch(share=False)
 
63
  return f"{month} {year_formatted}" # Return without day
64
 
65
 
66
+ def format_year_to_words(year):
67
+ """Formats a year as English words."""
68
+ inf_engine = inflect.engine()
69
+ if 1100 <= year <= 1999:
70
+ year_words = f"{inf_engine.number_to_words(year // 100, andword='')} hundred"
71
+ if year % 100 != 0:
72
+ year_words += f" {inf_engine.number_to_words(year % 100, andword='')}"
73
+ else:
74
+ year_words = inf_engine.number_to_words(year, andword='')
75
+ return year_words.replace(',', '')
76
 
77
  def perform_gematria_calculation_for_date_range(start_date, end_date):
78
  """Performs gematria calculation for a date range and groups by sum."""
 
81
  delta = timedelta(days=1)
82
  current_date = start_date
83
 
84
+ processed_months = set() # To track processed month-year combinations
85
+ processed_years = set() # To track processed years
86
+
87
  while current_date <= end_date:
88
+ # Full date calculation
89
  date_string = current_date.strftime("%Y-%m-%d")
90
  date_words = date_to_words(date_string)
91
  gematria_sum = calculate_gematria_sum(date_words)
 
94
  results[gematria_sum] = []
95
  results[gematria_sum].append({"date": date_string, "date_words": date_words})
96
 
97
+ # Month+Year calculation (processed once per month)
98
+ month_year_key = current_date.strftime("%Y-%m")
99
+ if month_year_key not in processed_months:
100
+ processed_months.add(month_year_key)
101
+ month_year_words = f"{current_date.strftime('%B')} {format_year_to_words(current_date.year)}"
102
  month_year_gematria_sum = calculate_gematria_sum(month_year_words)
103
 
104
  if month_year_gematria_sum not in results:
105
  results[month_year_gematria_sum] = []
106
  results[month_year_gematria_sum].append({
107
+ "date": month_year_key,
108
  "date_words": month_year_words
109
  })
110
 
111
+ # Year-only calculation (processed once per year)
112
+ year_key = str(current_date.year)
113
+ if year_key not in processed_years:
114
+ processed_years.add(year_key)
115
+ year_words = format_year_to_words(current_date.year)
116
+ year_gematria_sum = calculate_gematria_sum(year_words)
117
+
118
+ if year_gematria_sum not in results:
119
+ results[year_gematria_sum] = []
120
+ results[year_gematria_sum].append({
121
+ "date": year_key,
122
+ "date_words": year_words
123
+ })
124
+
125
  current_date += delta
126
 
127
  return results
128
 
129
 
130
+ # --- Event Handlers ---
131
+ def perform_calculation(start_date, end_date, include_words):
132
+ """Performs the calculation and generates the JSON output."""
133
+ logger.debug(f"perform_calculation called with: start_date={start_date}, end_date={end_date}, include_words={include_words}")
134
+ results = perform_gematria_calculation_for_date_range(start_date, end_date)
135
+ json_result = generate_json_output(results, start_date, end_date, include_words)
136
+ return json_result
137
 
138
 
139
+ def generate_json_output(results, start_date, end_date, include_words):
140
  """Generates the JSON output with the date range and results."""
 
141
  result = {
142
  "DateRange": {
143
  "StartDate": start_date.strftime("%Y-%m-%d"),
144
  "EndDate": end_date.strftime("%Y-%m-%d")
145
  },
146
+ "Results": []
147
  }
148
+ for gematria_sum, entries in results.items():
149
+ group = {
150
+ "GematriaSum": gematria_sum,
151
+ "Entries": []
152
+ }
153
+ for entry in entries:
154
+ entry_data = {"date": entry["date"]}
155
+ if include_words: # Conditionally include `date_words`
156
+ entry_data["date_words"] = entry["date_words"]
157
+ group["Entries"].append(entry_data)
158
+ result["Results"].append(group)
159
+
160
+ # Ensure JSON validity
161
  return json.dumps(result, indent=4, ensure_ascii=False)
162
 
 
 
 
 
163
 
164
  # --- Main Gradio App ---
165
  with gr.Blocks() as app:
166
  with gr.Row():
167
  start_date = Calendar(type="datetime", label="Start Date")
168
  end_date = Calendar(type="datetime", label="End Date")
169
+
170
  include_date_words = gr.Checkbox(value=True, label="Include Date-Words in JSON")
171
 
172
  calculate_btn = gr.Button("Calculate Gematria for Date Range")
 
174
  download_btn = gr.Button("Download JSON")
175
  json_file = gr.File(label="Downloaded JSON")
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  # --- Event Triggers ---
178
  calculate_btn.click(
179
  perform_calculation,
 
189
  )
190
 
191
  if __name__ == "__main__":
192
+ app.launch(share=False)