Created
February 19, 2020 08:24
-
-
Save abdalla-alothman/8ebef3cf05258d5b6e7e2a0ebcd7ec15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!python | |
| # A quick utility to fetch the Jummal value of an Arabic string or a series of strings. | |
| # -- Abdullah S. A. Alothman. February 19, 2020. | |
| import re | |
| class Jummal: | |
| def __init__(self): | |
| self.valueMap = dict() # A dictionary of letters and their values. | |
| self.rDict = dict() # A returned dictionary that contains strings and their values | |
| # Regular expression to trap a single string entry | |
| self.singleWord = re.compile(r"^[ذضصثقفغإعهخحجدطكمنتاألبيسشئءؤرلاىةوآزظ]{1,15}$", re.UNICODE) | |
| # Regex to handle an entry with more than one string | |
| self.multipleWords = re.compile(r"\b([ذضصثقفغإعهخحجدطكمنتاألبيسشئءؤرلاىةوآزظ]{1,30})\b\s", re.UNICODE) | |
| self.loadJMap() # load the valueMap | |
| def processEntry(self, userEntry=str("")): | |
| try: | |
| if re.match(self.singleWord, userEntry): | |
| self.singleEntry(userEntry) | |
| return self.rDict | |
| elif re.match(self.multipleWords, userEntry): | |
| self.UEList = userEntry.split() | |
| self.multipleEntry(self.UEList) | |
| return self.rDict | |
| else: | |
| raise Exception("Value not in Jummal") | |
| except Exception: | |
| print("Received Unknown Input. Exiting.") | |
| raise | |
| def changeStandard(self): # change value standard if desired. | |
| self.valueMap["ء"] = 1 # some consider the "ء" as 0, others as 1. | |
| self.valueMap["آ"] = 2 # I assume the آ is a new addition the Arabic character set> | |
| def loadJMap(self): | |
| self.valueMap = {"ء":0, "ا":1, "أ":1, "إ":1, "آ":1, | |
| "ب":2, "ج":3, "د":4, "ه":5, "ة":5, "و":6, "ؤ":6, "ز":7, | |
| "ح":8, "ط":9, "ي":10, "ئ":10, "ى":10, "ك":20, "ل":30, "م":40, | |
| "ن":50, "س":60, "ع":70, "ف":80, "ص":90, "ق":100, "ر":200, "ش":300, | |
| "ت":400, "ث":500, "خ":600, "ذ":700, "ض":800, "ظ":900, "غ":100} | |
| return self.valueMap | |
| def singleEntry(self, userString): | |
| print("received single entry") | |
| SEList = [c for c in userString] | |
| jummalList = [self.valueMap[c] for c in SEList] | |
| self.rDict[userString] = sum(jummalList) | |
| def multipleEntry(self, wordList=list()): | |
| print("received multiple entry") | |
| wordChars = list() | |
| charList = list() | |
| for words in wordList: | |
| wordChars = [c for c in words] | |
| charList.append(wordChars) | |
| for cl in charList: | |
| jummalList = [self.valueMap[c] for c in cl] | |
| self.rDict[words] = sum(jummalList) | |
| def jFactors(self, n): # Find the factors of the Jummal sum if necessary> | |
| from functools import reduce | |
| return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))) | |
| if __name__ == "__main__": # Testing the class... | |
| j = Jummal() | |
| switchStandard = input("Switch ء to 1 and آ to 2?> ") | |
| if re.match(r"([yYEeSs]{3})", switchStandard): | |
| print("Switching standards.") | |
| j.changeStandard() | |
| elif re.match(r"([NnOo]{2})", switchStandard): | |
| print("Preserving standard") | |
| else: | |
| print("Unknown option. Preserving valueMap...") | |
| letter = input("Enter string:> ") | |
| result = j.processEntry(letter) | |
| for uEntry, jValue in result.items(): | |
| print(f"{uEntry} has jummal value of: {jValue} {j.jFactors(jValue)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment