The warnings you are encountering (Warning: Character 'ٰ' not found in Abjad values. and Warning: Character 'ۤ' not found in Abjad values.) stem from the presence of additional characters in the input string that are not defined in the abjad_values dictionary. These characters are typically diacritical marks or special Arabic symbols.
To fix this issue, we can modify the calculate_abjad_value function to filter out any characters that are not present in the abjad_values dictionary before processing. This ensures that no warnings are printed for characters that are irrelevant to the Abjad calculation.
Updated Code: Abjad Calculator without Warnings for Unrecognized Characters
Here’s the revised version of the code with the necessary adjustments to avoid warnings for unrecognized characters:
import re
# ANSI escape code for red text
RED = "\033[91m"
RESET = "\033[0m"
# Define a dictionary to hold letter values and their categories
abjad_values = {
'ا': (1, 'Fire'), 'أ': (1, 'Fire'), 'إ': (1, 'Fire'), 'آ': (1, 'Fire'),
'ٱ': (1, 'Fire'), 'ء': (1, 'Fire'), 'ئ': (1, 'Fire'),
'ب': (2, 'Earth'), 'پ': (2, 'Earth'),
'ج': (3, 'Air'), 'چ': (3, 'Air'),
'د': (4, 'Water'), 'ڈ': (4, 'Water'),
'ه': (5, 'Fire'), 'ہ': (5, 'Fire'), 'ه': (5, 'Fire'), 'ھ': (5, 'Fire'), 'ة': (5, 'Fire'),
'و': (6, 'Earth'), 'ؤ': (6, 'Earth'),
'ز': (7, 'Air'), 'ژ': (7, 'Air'),
'ح': (8, 'Water'),
'ط': (9, 'Fire'),
'ى': (10, 'Earth'), 'ي': (10, 'Earth'), 'ی': (10, 'Earth'), 'ے': (10, 'Earth'),
'ک': (20, 'Air'), 'ك': (20, 'Air'), 'گ': (20, 'Air'),
'ل': (30, 'Water'),
'م': (40, 'Fire'),
'ن': (50, 'Earth'), 'ں': (50, 'Earth'),
'س': (60, 'Air'),
'ع': (70, 'Water'),
'ف': (80, 'Fire'),
'ص': (90, 'Earth'),
'ق': (100, 'Air'),
'ر': (200, 'Water'), 'ڑ': (200, 'Water'),
'ش': (300, 'Fire'),
'ت': (400, 'Earth'), 'ٹ': (400, 'Earth'),
'ث': (500, 'Air'),
'خ': (600, 'Water'),
'ذ': (700, 'Fire'),
'ض': (800, 'Earth'),
'ظ': (900, 'Air'),
'غ': (1000, 'Water')
}
def remove_diacritics_and_spaces(phrase):
"""Removes Arabic diacritics and spaces from the phrase."""
diacritics_pattern = r'[\u064B-\u0652]' # Arabic diacritics
phrase = re.sub(diacritics_pattern, '', phrase) # Remove diacritics
phrase = phrase.replace(' ', '') # Remove spaces
return phrase
def calculate_abjad_value(phrase):
"""Calculates the Abjad value of a given Arabic phrase and provides a breakdown."""
total_value = 0
category_totals = {'Fire': 0, 'Earth': 0, 'Air': 0, 'Water': 0}
breakdown = {}
letter_count = {}
# Clean the phrase
cleaned_phrase = remove_diacritics_and_spaces(phrase)
for character in cleaned_phrase:
# Only process characters that are in the abjad_values dictionary
if character not in abjad_values:
continue # Skip characters not found in abjad_values
value, category = abjad_values[character]
# Count letters
letter_count[character] = letter_count.get(character, 0) + 1
total_value += value
category_totals[category] += value
breakdown[character] = value
return total_value, category_totals, breakdown, letter_count, cleaned_phrase
def display_missing_letters(cleaned_phrase):
"""Displays the Abjad letters not found in the input phrase."""
found_letters = set(cleaned_phrase)
all_letters = set(abjad_values.keys())
missing_letters = all_letters - found_letters
if missing_letters:
print("\nMissing Letters:")
for letter in sorted(missing_letters):
print(f" {RED}{letter}{RESET}")
def display_results(phrase, total_value, category_totals, breakdown, letter_count, cleaned_phrase):
"""Displays the calculated results in a nice format."""
print(f"\nResults for the phrase: \"{phrase}\" (after cleaning)")
print(f"Total Abjad Value: {total_value}")
print("Category Breakdown:")
for category, total in category_totals.items():
print(f" {category}: {total}")
print("\nDetailed Breakdown with Contributions:")
for character in sorted(breakdown.keys()):
value = breakdown[character]
count = letter_count[character]
contribution = value * count # Contribution = Value * Count
print(f" {character} = {value} x {count} = {contribution}")
print("\nLetter Count Summary:")
for character in sorted(letter_count.keys()):
count = letter_count[character]
print(f" {character} = {count} occurrence(s)")
# Call the function to display missing letters
display_missing_letters(cleaned_phrase)
def main():
print("Welcome to the Enhanced Abjad Calculator")
while True:
phrase = input("\nEnter an Arabic phrase (or 'exit' to quit): ")
if phrase.lower() == 'exit':
break
total_value, category_totals, breakdown, letter_count, cleaned_phrase = calculate_abjad_value(phrase)
display_results(phrase, total_value, category_totals, breakdown, letter_count, cleaned_phrase)
if __name__ == "__main__":
main()
Key Changes
- Skip Unrecognized Characters:
- In the
calculate_abjad_value function, before processing each character, we check if the character is in the abjad_values dictionary.
- If a character is not found, it is simply skipped (using
continue), thus preventing any warnings from being printed.
Testing
- You can run the modified code and test it with various input phrases.
- The program will now only process valid Abjad characters and will not print warnings for invalid characters.
This change should address your concerns about unwanted warnings while ensuring the functionality remains intact. If you have any further requests or modifications, feel free to ask!