neuralworm commited on
Commit
57bd653
1 Parent(s): 84b4b28

add month search

Browse files
Files changed (1) hide show
  1. app.py +69 -22
app.py CHANGED
@@ -15,10 +15,8 @@ def calculate_gematria_sum(text):
15
  if text:
16
  text_gematria = calculate_gematria(strip_diacritics(text))
17
  return text_gematria
18
- else:
19
- return None
20
 
21
- # Custom function to convert number to ordinal words
22
  def number_to_ordinal_word(number):
23
  ordinal_dict = {
24
  1: "first", 2: "second", 3: "third", 4: "fourth", 5: "fifth",
@@ -36,7 +34,16 @@ def date_to_words(date_string):
36
  """Converts a date in YYYY-MM-DD format to English words."""
37
  inf_engine = inflect.engine()
38
  date_obj = datetime.strptime(date_string, "%Y-%m-%d")
 
 
 
 
 
 
 
 
39
 
 
40
  year = date_obj.year
41
  if 1100 <= year <= 1999:
42
  year_words = f"{inf_engine.number_to_words(year // 100, andword='') } hundred"
@@ -47,11 +54,15 @@ def date_to_words(date_string):
47
  year_formatted = year_words.replace(',', '')
48
 
49
  month = date_obj.strftime("%B")
50
- day = date_obj.day
51
- day_ordinal = number_to_ordinal_word(day)
 
 
 
 
 
 
52
 
53
- output_text = f"{day_ordinal} {month} {year_formatted}"
54
- return output_text
55
 
56
  def perform_gematria_calculation_for_date_range(start_date, end_date):
57
  """Performs gematria calculation for a date range and groups by sum."""
@@ -61,27 +72,37 @@ def perform_gematria_calculation_for_date_range(start_date, end_date):
61
  current_date = start_date
62
 
63
  while current_date <= end_date:
64
- # Convert date to string
65
  date_string = current_date.strftime("%Y-%m-%d")
66
  date_words = date_to_words(date_string)
67
- logger.debug(f"Date: {current_date}, Day Name: {date_words}")
68
  gematria_sum = calculate_gematria_sum(date_words)
69
- logger.debug(f"Gematria Sum: {gematria_sum}")
70
 
71
  if gematria_sum not in results:
72
  results[gematria_sum] = []
 
 
 
 
 
 
 
73
 
74
- results[gematria_sum].append({
75
- "date": current_date.strftime('%Y-%m-%d'),
76
- "date_words": date_words
77
- })
 
 
78
 
79
  current_date += delta
80
 
81
  return results
82
 
 
 
 
83
  def generate_json_output(results, start_date, end_date):
84
  """Generates the JSON output with the date range and results."""
 
85
  result = {
86
  "DateRange": {
87
  "StartDate": start_date.strftime("%Y-%m-%d"),
@@ -94,40 +115,66 @@ def generate_json_output(results, start_date, end_date):
94
  def download_json(json_data, start_date, end_date):
95
  """Downloads the JSON data to a file."""
96
  filename = f"gematria_results_{start_date.strftime('%Y%m%d')}_{end_date.strftime('%Y%m%d')}.json"
97
- return (filename, json_data) # Rückgabe von Dateiname und JSON-Daten
98
 
99
  # --- Main Gradio App ---
100
  with gr.Blocks() as app:
101
  with gr.Row():
102
  start_date = Calendar(type="datetime", label="Start Date")
103
  end_date = Calendar(type="datetime", label="End Date")
 
 
104
 
105
  calculate_btn = gr.Button("Calculate Gematria for Date Range")
106
  json_output = gr.Textbox(label="JSON Output")
107
  download_btn = gr.Button("Download JSON")
108
- json_file = gr.File(label="Downloaded JSON") # Entferne visible=False
109
 
110
  # --- Event Handlers ---
111
- def perform_calculation(start_date, end_date):
112
  """Performs the calculation and generates the JSON output."""
113
- logger.debug(f"perform_calculation called with: start_date={start_date}, end_date={end_date}")
114
  results = perform_gematria_calculation_for_date_range(start_date, end_date)
115
- json_result = generate_json_output(results, start_date, end_date)
116
  return json_result
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  # --- Event Triggers ---
119
  calculate_btn.click(
120
  perform_calculation,
121
- inputs=[start_date, end_date],
122
  outputs=[json_output],
123
  api_name="calculate_gematria"
124
  )
125
 
126
  download_btn.click(
127
- download_json,
128
  inputs=[json_output, start_date, end_date],
129
  outputs=[json_file],
130
  )
131
 
132
  if __name__ == "__main__":
133
- app.launch(share=False)
 
15
  if text:
16
  text_gematria = calculate_gematria(strip_diacritics(text))
17
  return text_gematria
18
+ return None
 
19
 
 
20
  def number_to_ordinal_word(number):
21
  ordinal_dict = {
22
  1: "first", 2: "second", 3: "third", 4: "fourth", 5: "fifth",
 
34
  """Converts a date in YYYY-MM-DD format to English words."""
35
  inf_engine = inflect.engine()
36
  date_obj = datetime.strptime(date_string, "%Y-%m-%d")
37
+ return format_date(date_obj, inf_engine)
38
+
39
+
40
+ def month_year_to_words(date_string):
41
+ """Converts a month and year in YYYY-MM format to English words."""
42
+ inf_engine = inflect.engine()
43
+ date_obj = datetime.strptime(date_string, "%Y-%m")
44
+ return format_date(date_obj, inf_engine)
45
 
46
+ def format_date(date_obj, inf_engine):
47
  year = date_obj.year
48
  if 1100 <= year <= 1999:
49
  year_words = f"{inf_engine.number_to_words(year // 100, andword='') } hundred"
 
54
  year_formatted = year_words.replace(',', '')
55
 
56
  month = date_obj.strftime("%B")
57
+
58
+ try: # Try getting the day, if available
59
+ day = date_obj.day
60
+ day_ordinal = number_to_ordinal_word(day)
61
+ return f"{day_ordinal} {month} {year_formatted}" # Return with day
62
+ except:
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."""
 
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)
 
78
 
79
  if gematria_sum not in results:
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"),
 
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")
129
  json_output = gr.Textbox(label="JSON Output")
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,
168
+ inputs=[start_date, end_date, include_date_words],
169
  outputs=[json_output],
170
  api_name="calculate_gematria"
171
  )
172
 
173
  download_btn.click(
174
+ lambda json_data, start_date, end_date: download_json(json_data, start_date, end_date),
175
  inputs=[json_output, start_date, end_date],
176
  outputs=[json_file],
177
  )
178
 
179
  if __name__ == "__main__":
180
+ app.launch(share=False)