|
|
| ''' |
| Created on ٢٨/٠٦/٢٠١٠ |
| |
| @Created by: Muhammad Altabba |
| ''' |
| from ...Models.Lexicon.LettersConstants import DiacriticsConstants |
|
|
| class ArabicStringUtility(object): |
| """ |
| # PyUML: Do not remove this line! # XMI_ID:_qysDPI35Ed-gg8GOK1TmhA |
| """ |
| ''' |
| Arabic String Utility |
| ''' |
|
|
| def __init__(self, string): |
| ''' |
| Constructor |
| ''' |
| pass |
|
|
|
|
| def IsCompatible(self, string, voweledForm): |
| stringIndexer = 0; |
| voweledPatternsIndexer = 0; |
| while stringIndexer < len(string): |
| while stringIndexer < len(string) and voweledPatternsIndexer < len(voweledForm)\ |
| and (voweledForm[voweledPatternsIndexer] == string[stringIndexer]\ |
| or (string[stringIndexer] not in DiacriticsConstants.AllDiacritics and voweledForm[voweledPatternsIndexer] in ['ل','ع','ف'])): |
| stringIndexer += 1; |
| voweledPatternsIndexer += 1; |
| if voweledPatternsIndexer == len(voweledForm): |
| return True; |
| while (stringIndexer < len(string)\ |
| and voweledForm[voweledPatternsIndexer] not in DiacriticsConstants.AllDiacritics\ |
| and string[stringIndexer] in DiacriticsConstants.AllDiacritics): |
| stringIndexer += 1; |
| if stringIndexer == len(string): |
| return True; |
| |
| if (string[stringIndexer] in DiacriticsConstants.AllDiacritics\ |
| and voweledForm[voweledPatternsIndexer] in DiacriticsConstants.AllDiacritics): |
| |
| return False; |
| while (voweledForm[voweledPatternsIndexer] in DiacriticsConstants.AllDiacritics): |
| voweledPatternsIndexer += 1; |
| return True; |
| pass |
|
|
| def ClipString(self, string, lettersCountFromStart, lettersCountFromEnd): |
| |
| if(lettersCountFromStart+lettersCountFromEnd >= len(string) or len(string) == 0): |
| return ''; |
| |
| startIndex = 0; |
| lettersCounter = 0; |
| try: |
| while (lettersCounter < lettersCountFromStart): |
| |
| |
| |
| |
| |
| startIndex += 1; |
| lettersCounter += 1; |
| |
| |
| if(startIndex == len(string)): |
| |
| |
| |
| |
| |
| startIndex -= 1; |
| lettersCounter += 1; |
| break; |
| raise Exception('len(string) = startIndex = '+str(startIndex)\ |
| +', lettersCounter = '+str(lettersCounter)\ |
| +', string = ' +string+ ', len = '+str(len(string))\ |
| + ', lettersCountFromStart = ' +str(lettersCountFromStart)\ |
| + ', lettersCountFromEnd = ' +str(lettersCountFromEnd)); |
| while (string[startIndex] in DiacriticsConstants.AllDiacritics): |
| |
| |
| |
| |
| |
| startIndex += 1; |
| if(startIndex == len(string)): |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| startIndex -= 1; |
| break; |
| except: |
| print('e len(string) = startIndex = '+str(startIndex)\ |
| +', lettersCounter = '+str(lettersCounter)\ |
| +', string = ' +string+ ', len = '+str(len(string))\ |
| + ', lettersCountFromStart = ' +str(lettersCountFromStart)\ |
| + ', lettersCountFromEnd = ' +str(lettersCountFromEnd)); |
| raise Exception('len(string) = startIndex = '+str(startIndex)\ |
| +', lettersCounter = '+str(lettersCounter)\ |
| +', string = ' +string+ ', len = '+str(len(string))\ |
| + ', lettersCountFromStart = ' +str(lettersCountFromStart)\ |
| + ', lettersCountFromEnd = ' +str(lettersCountFromEnd)); |
| |
| |
| endIndex = 0; |
| lettersCounter = 0; |
| while (lettersCounter < lettersCountFromEnd): |
| if (string[len(string)-1 -endIndex] not in DiacriticsConstants.AllDiacritics): |
| lettersCounter += 1; |
| endIndex += 1; |
| |
| |
| return string[startIndex:len(string)-endIndex]; |
| pass |
| |
| def GetDiacratic(self, string, char, startIndex, searchFromRight = False): |
| |
| |
| |
| |
| |
| if(searchFromRight == True): |
| if(string.rfind(char, startIndex) == len(string)-1): |
| return None; |
| letter = string[string.rfind(char, startIndex)+1]; |
| if(letter in DiacriticsConstants.AllDiacritics): |
| return letter; |
| else: |
| return None; |
| |
| |
| else: |
| if(string.find(char, startIndex) == len(string)-1): |
| return None; |
| letter = string[string.find(char, startIndex)+1]; |
| if(letter in DiacriticsConstants.AllDiacritics): |
| return letter; |
| else: |
| return None; |
| pass |
|
|
| def AddDiacratics(self, string, diacratics): |
| |
| |
| if(len(string)!=len(diacratics)): |
| raise Exception('string ['+string+'] with length ['+str(len(string))+']'+\ |
| 'is not compatible with diacratics ['+diacratics+'] with length ['+str(len(diacratics))+']'); |
| voweledForm = ''; |
| for i in range(len(string)): |
| voweledForm = ''.join([voweledForm, string[i],diacratics[i]]); |
| return voweledForm; |
| pass |
|
|
| def ColorizeDiacraticInHtml(self, text, cssClassName = "diacritic"): |
| resSeq = []; |
| for char in text[:]: |
| if(char in DiacriticsConstants.AllDiacritics): |
| resSeq.append(''.join(['<span class=',cssClassName,'>', char, '</span>'])); |
| else: |
| resSeq.append(char); |
| return ''.join(resSeq); |
| pass |
|
|