Package: dcl
Diacritics LibraryThis library is used for adding, and removing diacritics from strings.Getting startedStart by importing the module:importdclDCL currently supports a multitude of diacritics:acutebrevecaroncedillagraveinterpunctmacronogonekringring_and_acuteslashstrokestroke_and_acutetildetittleumlaut/diaresisumlaut_and_macronEach accent has their own attribute which is directly accessible from the dcl module.dcl.acute('a')>>>'á'These attributes return a Character object, which is essentially just a handy "wrapper"
around our diacritic, which we can use to access various attributes to retrieve further
information about the diacritic we're focusing on.char=dcl.ogonek('a')repr(char)>>>"<ogonek 'ą'>"char.character# the same as str(char)>>>'ą'char.diacritic# some return <unprintable>>>>'˛'char.diacritic_name>>>'ogonek'char.raw# returns the raw representation of our character>>>'\U00000105'char.raw_diacritic>>>'\U000002db'Some functions can't take certain letters. For example, the letterhcannot take
a cedilla diacritic. In this case, an exception is raised namedDiacriticError.
You can access this exception viadcl.errors.DiacriticError.fromdcl.errorsimportDiacriticErrortry:char=dcl.cedilla('h')exceptDiacriticErrorase:print(e)else:print(repr(char))>>>'Character h cannot take a cedilla diacritic'If you want to, you may also use theDiacriticApplicantobject fromdcl.objects. The functions you see above use this object too, and it's virtually
the same principle, except from the fact that we use properties to get the
diacritic, and the class simply holds the string and it's properties. Alas with the
functions above, this object also returns the sameCharacterobject through it's properties.fromdcl.objectsimportDiacriticApplicantda=DiacriticApplicant('a')repr(da.ogonek)>>>"<ogonek 'ą'>"There is also theclean_diacriticsfunction, accessible straight from the dcl module.
This function allows us to completely clean a string from any diacritics.dcl.clean_diacritics("Krëûšàdå")>>>'Kreusada'dcl.clean_diacritics("Café")>>>'Cafe'Along with this function, there's alsocount_diacritics,get_diacriticsandhas_diacritics.Thehas_diacriticsfunction simply checks if the string contains a character
with a diacritic.dcl.has_diacritics("Café")>>>Truedcl.has_diacritics("dcl")>>>FalseTheget_diacriticsfunction is used to get all the diacritics in a string.
It returns a dictionary. For each diacritic in the string, the key will show
the diacritic's index in the string, and the value will show theCharacterrepresentation.dcl.get_diacritics("Café")>>>{3:<acute'é'>}dcl.get_diacritics("Krëûšàdå")>>>{2:<umlaut'ë'>,3:<circumflex'û'>,4:<caron'š'>,5:<grave'à'>,7:<ring'å'>}Thecount_diacriticsfunction counts the number of diacritics in a string. The actual
implementation of this simply returns the dictionary length fromget_diacritics.dcl.count_diacritics("Café")>>>1Creating an end user programCreating a program would be pretty simple for this, and I'd love to be able to help
you out with a base idea. Have a look at this for example:importdclimportstringfromdcl.errorsimportDiacriticErrorchar=str(input("Enter a character: "))ifnotcharinstring.ascii_letters:print("Please enter a letter from a-Z.")else:accent=str(input("Enter an accent, you can choose from the following: "+", ".join(dcl.diacritic_list)))ifnotdcl.isdiacritictype(accent):print("That was not a valid accent.")else:try:function=getattr(dcl,accent)# or dcl.objects.DiacriticApplicantoutput=function(char)exceptDiacriticErrorase:print(e)else:print(str(output))It's worth checking if the provided accent is a diacritic type. If it is, then you can usegetattr.
Without checking, the user could provide a default global such as__file__.You can also create a program which can remove diacritics from a string. It's made easy!importdclstring=str(input("Enter the string which you want to be cleared from diacritics: "))print("Here is your cleaned string: "+dcl.clean_diacritics(string))Or perhaps your program wants to count the number of diacritics contained
within your string.importdclstring=str(input("This program will count the number of diacritics contained in your input. Enter a string: "))count=dcl.count_diacritics(string)ifcount==1:grammar="is"else:grammar="are"print(f"There{grammar}{count}diacritics/accent in your string.")